forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Alerting] Adding rule executor service functions to support executio…
…n cancellation (elastic#120506) * Adding abortable es client factory * Starting to add tests * Tests * Fixing types * Fixing types * Fixing types * Adding functional tests * Reverting changes to rule type * Handling the correct error message * Cleanup * Fixing preview rules route * Updating README * Removing unneeded observable * Cleanup * Fixing types * Using abort signal Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
Showing
17 changed files
with
553 additions
and
8 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
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/alerting/server/lib/create_abortable_es_client_factory.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,104 @@ | ||
/* | ||
* 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 { elasticsearchServiceMock } from '../../../../../src/core/server/mocks'; | ||
import { createAbortableEsClientFactory } from './create_abortable_es_client_factory'; | ||
|
||
const esQuery = { | ||
body: { query: { bool: { filter: { range: { '@timestamp': { gte: 0 } } } } } }, | ||
}; | ||
|
||
describe('createAbortableEsClientFactory', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('searches with asInternalUser when specified', async () => { | ||
const abortController = new AbortController(); | ||
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); | ||
const abortableSearchClient = createAbortableEsClientFactory({ | ||
scopedClusterClient, | ||
abortController, | ||
}); | ||
|
||
await abortableSearchClient.asInternalUser.search(esQuery); | ||
expect(scopedClusterClient.asInternalUser.search).toHaveBeenCalledWith(esQuery, { | ||
signal: abortController.signal, | ||
}); | ||
expect(scopedClusterClient.asCurrentUser.search).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('searches with asCurrentUser when specified', async () => { | ||
const abortController = new AbortController(); | ||
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); | ||
const abortableSearchClient = createAbortableEsClientFactory({ | ||
scopedClusterClient, | ||
abortController, | ||
}); | ||
|
||
await abortableSearchClient.asCurrentUser.search(esQuery); | ||
expect(scopedClusterClient.asCurrentUser.search).toHaveBeenCalledWith(esQuery, { | ||
signal: abortController.signal, | ||
}); | ||
expect(scopedClusterClient.asInternalUser.search).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('uses search options when specified', async () => { | ||
const abortController = new AbortController(); | ||
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); | ||
const abortableSearchClient = createAbortableEsClientFactory({ | ||
scopedClusterClient, | ||
abortController, | ||
}); | ||
|
||
await abortableSearchClient.asInternalUser.search(esQuery, { ignore: [404] }); | ||
expect(scopedClusterClient.asInternalUser.search).toHaveBeenCalledWith(esQuery, { | ||
ignore: [404], | ||
signal: abortController.signal, | ||
}); | ||
expect(scopedClusterClient.asCurrentUser.search).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('re-throws error when search throws error', async () => { | ||
const abortController = new AbortController(); | ||
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); | ||
scopedClusterClient.asInternalUser.search.mockRejectedValueOnce( | ||
new Error('something went wrong!') | ||
); | ||
const abortableSearchClient = createAbortableEsClientFactory({ | ||
scopedClusterClient, | ||
abortController, | ||
}); | ||
|
||
await expect( | ||
abortableSearchClient.asInternalUser.search | ||
).rejects.toThrowErrorMatchingInlineSnapshot(`"something went wrong!"`); | ||
}); | ||
|
||
test('throws error when search throws abort error', async () => { | ||
const abortController = new AbortController(); | ||
abortController.abort(); | ||
const scopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); | ||
scopedClusterClient.asInternalUser.search.mockRejectedValueOnce( | ||
new Error('Request has been aborted by the user') | ||
); | ||
const abortableSearchClient = createAbortableEsClientFactory({ | ||
scopedClusterClient, | ||
abortController, | ||
}); | ||
|
||
await expect( | ||
abortableSearchClient.asInternalUser.search | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Search has been aborted due to cancelled execution"` | ||
); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/alerting/server/lib/create_abortable_es_client_factory.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,65 @@ | ||
/* | ||
* 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 { TransportRequestOptions, TransportResult } from '@elastic/elasticsearch'; | ||
import { SearchResponse } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { IScopedClusterClient } from 'src/core/server'; | ||
import type { ESSearchRequest } from 'src/core/types/elasticsearch'; | ||
|
||
export interface IAbortableEsClient { | ||
search: ( | ||
query: ESSearchRequest, | ||
options?: TransportRequestOptions | ||
) => Promise<TransportResult<SearchResponse<unknown>, unknown>>; | ||
} | ||
|
||
export interface IAbortableClusterClient { | ||
readonly asInternalUser: IAbortableEsClient; | ||
readonly asCurrentUser: IAbortableEsClient; | ||
} | ||
export interface CreateAbortableEsClientFactoryOpts { | ||
scopedClusterClient: IScopedClusterClient; | ||
abortController: AbortController; | ||
} | ||
|
||
export function createAbortableEsClientFactory(opts: CreateAbortableEsClientFactoryOpts) { | ||
const { scopedClusterClient, abortController } = opts; | ||
return { | ||
asInternalUser: { | ||
search: async (query: ESSearchRequest, options?: TransportRequestOptions) => { | ||
try { | ||
const searchOptions = options ?? {}; | ||
return await scopedClusterClient.asInternalUser.search(query, { | ||
...searchOptions, | ||
signal: abortController.signal, | ||
}); | ||
} catch (e) { | ||
if (abortController.signal.aborted) { | ||
throw new Error('Search has been aborted due to cancelled execution'); | ||
} | ||
throw e; | ||
} | ||
}, | ||
}, | ||
asCurrentUser: { | ||
search: async (query: ESSearchRequest, options?: TransportRequestOptions) => { | ||
try { | ||
const searchOptions = options ?? {}; | ||
return await scopedClusterClient.asCurrentUser.search(query, { | ||
...searchOptions, | ||
signal: abortController.signal, | ||
}); | ||
} catch (e) { | ||
if (abortController.signal.aborted) { | ||
throw new Error('Search has been aborted due to cancelled execution'); | ||
} | ||
throw e; | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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.