Skip to content

Commit

Permalink
Consistent handling in search strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Oct 29, 2023
1 parent 04eccf7 commit 0a13dbe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import type { TransportResult } from '@elastic/elasticsearch';
import { tap } from 'rxjs/operators';
import type { IScopedClusterClient, Logger } from '@kbn/core/server';
import { getKbnServerError } from '@kbn/kibana-utils-plugin/server';
import { SearchConfigSchema } from '../../../../config';
import {
EqlSearchStrategyRequest,
Expand All @@ -27,15 +28,19 @@ export const eqlSearchStrategyProvider = (
searchConfig: SearchConfigSchema,
logger: Logger
): ISearchStrategy<EqlSearchStrategyRequest, EqlSearchStrategyResponse> => {
async function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
const client = esClient.asCurrentUser.eql;
await client.delete({ id });
return client.delete({ id });
}

return {
cancel: (id, options, { esClient }) => {
cancel: async (id, options, { esClient }) => {
logger.debug(`_eql/delete ${id}`);
return cancelAsyncSearch(id, esClient);
try {
await cancelAsyncSearch(id, esClient);
} catch (e) {
throw getKbnServerError(e);
}
},

search: ({ id, ...request }, options: IAsyncSearchOptions, { esClient, uiSettingsClient }) => {
Expand Down Expand Up @@ -80,7 +85,12 @@ export const eqlSearchStrategyProvider = (
return toEqlKibanaSearchResponse(response as TransportResult<EqlSearchResponse>);
};

const cancel = () => id && !options.isStored && cancelAsyncSearch(id, esClient);
const cancel = () => {
if (!id || options.isStored) return;
cancelAsyncSearch(id, esClient).catch(() => {
// Swallow errors from cancellation
});
};

return pollSearch(search, cancel, {
pollInterval: searchConfig.asyncSearch.pollInterval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const enhancedEsSearchStrategyProvider = (
): ISearchStrategy => {
function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;
return client.asyncSearch.delete({ id }).then(() => {});
return client.asyncSearch.delete({ id });
}

function asyncSearch(
Expand Down Expand Up @@ -79,7 +79,10 @@ export const enhancedEsSearchStrategyProvider = (
};

const cancel = () => {
if (id && !options.isStored) cancelAsyncSearch(id, esClient);
if (!id || options.isStored) return;
cancelAsyncSearch(id, esClient).catch(() => {
// Swallow errors from cancellation
});
};

return pollSearch(search, cancel, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const sqlSearchStrategyProvider = (
logger: Logger,
useInternalUser: boolean = false
): ISearchStrategy<SqlSearchStrategyRequest, SqlSearchStrategyResponse> => {
async function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;
await client.sql.deleteAsync({ id });
return client.sql.deleteAsync({ id });
}

function asyncSearch(
Expand Down Expand Up @@ -79,7 +79,12 @@ export const sqlSearchStrategyProvider = (
return toAsyncKibanaSearchResponse(body, startTime, headers?.warning);
};

const cancel = () => id && !options.isStored && cancelAsyncSearch(id, esClient);
const cancel = () => {
if (!id || options.isStored) return;
cancelAsyncSearch(id, esClient).catch(() => {
// Swallow errors from cancellation
});
};

return pollSearch(search, cancel, {
pollInterval: searchConfig.asyncSearch.pollInterval,
Expand Down Expand Up @@ -112,9 +117,13 @@ export const sqlSearchStrategyProvider = (
* @returns `Promise<void>`
* @throws `KbnServerError`
*/
cancel: (id, options, { esClient }) => {
cancel: async (id, options, { esClient }) => {
logger.debug(`sql search: cancel async_search_id=${id}`);
return cancelAsyncSearch(id, esClient);
try {
await cancelAsyncSearch(id, esClient);
} catch (e) {
throw getKbnServerError(e);
}
},
/**
*
Expand Down

0 comments on commit 0a13dbe

Please sign in to comment.