From 77809650f1b72f181c757635c03274413371aec2 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Thu, 12 Oct 2023 23:09:10 -0700 Subject: [PATCH] Add support for aborting in fetchTextBased (ESQL) (#168544) ## Summary Adds support for aborting previous ESQL calls when a new one is issued. Before: https://github.com/elastic/kibana/assets/1178348/77ef5492-e914-4af5-aebd-ecc6d8d960b6 After: https://github.com/elastic/kibana/assets/1178348/1af57552-8974-4f6f-93af-cb01283b03da ### Checklist Delete any items that are not applicable to this PR. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: Stratoula Kalafateli Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../application/main/utils/fetch_all.test.ts | 28 +++++++++++++++++++ .../application/main/utils/fetch_all.ts | 14 +++++++++- .../main/utils/fetch_text_based.ts | 5 +++- .../embeddable/saved_search_embeddable.tsx | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts index 6de9781b0b58b..48d867f4b81c4 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_all.test.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_all.test.ts @@ -382,5 +382,33 @@ describe('test fetchAll', () => { }, ]); }); + + test('should swallow abort errors', async () => { + const collect = subjectCollector(subjects.documents$); + mockfetchTextBased.mockRejectedValue({ msg: 'The query was aborted' }); + const query = { esql: 'from foo' }; + deps = { + abortController: new AbortController(), + inspectorAdapters: { requests: new RequestAdapter() }, + searchSessionId: '123', + initialFetchStatus: FetchStatus.UNINITIALIZED, + useNewFieldsApi: true, + savedSearch: savedSearchMock, + services: discoverServiceMock, + getAppState: () => ({ query }), + getInternalState: () => ({ + dataView: undefined, + savedDataViews: [], + adHocDataViews: [], + expandedDoc: undefined, + customFilters: [], + }), + }; + fetchAll(subjects, false, deps); + deps.abortController.abort(); + await waitForNextTick(); + + expect((await collect()).find(({ error }) => error)).toBeUndefined(); + }); }); }); diff --git a/src/plugins/discover/public/application/main/utils/fetch_all.ts b/src/plugins/discover/public/application/main/utils/fetch_all.ts index ff754b065a130..289ad9e336b04 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_all.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_all.ts @@ -61,6 +61,7 @@ export function fetchAll( services, inspectorAdapters, savedSearch, + abortController, } = fetchDeps; const { data } = services; const searchSource = savedSearch.searchSource.createChild(); @@ -93,7 +94,14 @@ export function fetchAll( // Start fetching all required requests const response = useTextbased && query - ? fetchTextBased(query, dataView, data, services.expressions, inspectorAdapters) + ? fetchTextBased( + query, + dataView, + data, + services.expressions, + inspectorAdapters, + abortController.signal + ) : fetchDocuments(searchSource, fetchDeps); const fetchType = useTextbased && query ? 'fetchTextBased' : 'fetchDocuments'; const startTime = window.performance.now(); @@ -141,6 +149,10 @@ export function fetchAll( checkHitCount(dataSubjects.main$, records.length); }) + // In the case that the request was aborted (e.g. a refresh), swallow the abort error + .catch((e) => { + if (!abortController.signal.aborted) throw e; + }) // Only the document query should send its errors to main$, to cause the full Discover app // to get into an error state. The other queries will not cause all of Discover to error out // but their errors will be shown in-place (e.g. of the chart). diff --git a/src/plugins/discover/public/application/main/utils/fetch_text_based.ts b/src/plugins/discover/public/application/main/utils/fetch_text_based.ts index 6a164bfd8a5f8..a1aa14e47d79b 100644 --- a/src/plugins/discover/public/application/main/utils/fetch_text_based.ts +++ b/src/plugins/discover/public/application/main/utils/fetch_text_based.ts @@ -30,6 +30,7 @@ export function fetchTextBased( data: DataPublicPluginStart, expressions: ExpressionsStart, inspectorAdapters: Adapters, + abortSignal?: AbortSignal, filters?: Filter[], inputQuery?: Query ): Promise { @@ -43,9 +44,11 @@ export function fetchTextBased( }) .then((ast) => { if (ast) { - const execution = expressions.run(ast, null, { + const contract = expressions.execute(ast, null, { inspectorAdapters, }); + abortSignal?.addEventListener('abort', contract.cancel); + const execution = contract.getData(); let finalData: DataTableRecord[] = []; let textBasedQueryColumns: Datatable['columns'] | undefined; let error: string | undefined; diff --git a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx index 7e7bf727fe230..34f6043936d92 100644 --- a/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx +++ b/src/plugins/discover/public/embeddable/saved_search_embeddable.tsx @@ -331,6 +331,7 @@ export class SavedSearchEmbeddable this.services.data, this.services.expressions, this.services.inspector, + this.abortController.signal, this.input.filters, this.input.query );