-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Index Management] Fix bug with long http requests coming from index …
…actions (#171735)
- Loading branch information
1 parent
6905a0f
commit 0e3351c
Showing
21 changed files
with
171 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/index_management/server/routes/api/indices/helpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { IScopedClusterClient } from '@kbn/core/server'; | ||
|
||
import { executeAsyncByChunks } from './helpers'; | ||
|
||
const generateIndices = (count: number) => { | ||
const indices = []; | ||
|
||
for (let i = 0; i < count; i++) { | ||
indices.push(`index-${i}`); | ||
} | ||
|
||
return indices; | ||
}; | ||
|
||
const mockClient = { | ||
asCurrentUser: { | ||
indices: { | ||
delete: jest.fn(), | ||
}, | ||
}, | ||
} as unknown as IScopedClusterClient; | ||
|
||
describe('executeAsyncByChunks', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should make just one request for one index', async () => { | ||
const params = { | ||
index: generateIndices(1), | ||
}; | ||
|
||
await executeAsyncByChunks(params, mockClient, 'delete'); | ||
|
||
expect(mockClient.asCurrentUser.indices.delete).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should make 2 requests for 32 indices', async () => { | ||
const params = { | ||
index: generateIndices(32), | ||
}; | ||
|
||
await executeAsyncByChunks(params, mockClient, 'delete'); | ||
|
||
expect(mockClient.asCurrentUser.indices.delete).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
x-pack/plugins/index_management/server/routes/api/indices/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { chunk } from 'lodash'; | ||
|
||
import type { IScopedClusterClient } from '@kbn/core/server'; | ||
import { MAX_INDICES_PER_REQUEST } from '../../../../common/constants'; | ||
|
||
// To avoid having to to match method signatures with the client | ||
// type, we use a generic CallableFn type. | ||
type CallableFn = (args: Record<any, any>) => Promise<any>; | ||
|
||
export async function executeAsyncByChunks<T>( | ||
// Since we are using a key to access the index method, we need | ||
// to use a generic type. | ||
params: { | ||
index: T[]; | ||
format?: string; | ||
expand_wildcards?: string; | ||
max_num_segments?: number; | ||
}, | ||
dataClient: IScopedClusterClient, | ||
methodName: keyof IScopedClusterClient['asCurrentUser']['indices'] | ||
) { | ||
const { index: indices, ...commonParams } = params; | ||
|
||
// When the number of indices is small, we can execute in a single request | ||
// | ||
// Otherwise we need to split the indices into chunks and execute them in multiple requests because | ||
// if we try to execute an action with too many indices that account for a long string in the request | ||
// ES will throw an error saying that the HTTP line is too large. | ||
if (indices.length <= MAX_INDICES_PER_REQUEST) { | ||
await (dataClient.asCurrentUser.indices[methodName] as CallableFn)({ | ||
...commonParams, | ||
index: indices, | ||
}); | ||
} else { | ||
const chunks = chunk(indices, MAX_INDICES_PER_REQUEST); | ||
|
||
await Promise.all( | ||
chunks.map((chunkOfIndices) => | ||
(dataClient.asCurrentUser.indices[methodName] as CallableFn)({ | ||
...commonParams, | ||
index: chunkOfIndices, | ||
}) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.