Skip to content

Commit

Permalink
Add support for aborting in fetchTextBased (ESQL) (elastic#168544)
Browse files Browse the repository at this point in the history
## 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 <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2023
1 parent a82256b commit 7780965
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
14 changes: 13 additions & 1 deletion src/plugins/discover/public/application/main/utils/fetch_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function fetchAll(
services,
inspectorAdapters,
savedSearch,
abortController,
} = fetchDeps;
const { data } = services;
const searchSource = savedSearch.searchSource.createChild();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function fetchTextBased(
data: DataPublicPluginStart,
expressions: ExpressionsStart,
inspectorAdapters: Adapters,
abortSignal?: AbortSignal,
filters?: Filter[],
inputQuery?: Query
): Promise<RecordsFetchResponse> {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down

0 comments on commit 7780965

Please sign in to comment.