diff --git a/x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.test.ts b/x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.test.ts similarity index 92% rename from x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.test.ts rename to x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.test.ts index 0a80f1c06998f..4d9ecb9221709 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.test.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { checkNonPersistedSessions as checkNonPersistedSessions$ } from './check_non_persiseted_sessions'; +import { checkNonPersistedSessions as checkNonPersistedSessions$ } from './check_non_persisted_sessions'; import { SearchSessionStatus, SearchSessionSavedObjectAttributes, @@ -322,6 +322,46 @@ describe('checkNonPersistedSessions', () => { const { id } = mockClient.asyncSearch.delete.mock.calls[0][0]; expect(id).toBe('async-id'); }); + + test("doesn't attempt to delete errored out async search", async () => { + mockClient.asyncSearch.delete = jest.fn(); + + savedObjectsClient.find.mockResolvedValue({ + saved_objects: [ + { + id: '123', + attributes: { + persisted: false, + status: SearchSessionStatus.ERROR, + expires: moment().add(moment.duration(3, 'm')), + created: moment().subtract(moment.duration(30, 'm')), + touched: moment().subtract(moment.duration(6, 'm')), + idMapping: { + 'map-key': { + strategy: ENHANCED_ES_SEARCH_STRATEGY, + id: 'async-id', + status: SearchStatus.ERROR, + }, + }, + }, + }, + ], + total: 1, + } as any); + + await checkNonPersistedSessions( + { + savedObjectsClient, + client: mockClient, + logger: mockLogger, + }, + config + ); + + expect(savedObjectsClient.bulkUpdate).not.toBeCalled(); + expect(savedObjectsClient.delete).toBeCalled(); + expect(mockClient.asyncSearch.delete).not.toBeCalled(); + }); }); describe('update', () => { diff --git a/x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.ts b/x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.ts similarity index 95% rename from x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.ts rename to x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.ts index 2115ce85eeb27..180f276620a6c 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/check_non_persiseted_sessions.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/check_non_persisted_sessions.ts @@ -18,7 +18,7 @@ import { KueryNode, } from '../../../../../../src/plugins/data/common'; import { checkSearchSessionsByPage, getSearchSessionsPage$ } from './get_search_session_page'; -import { SearchSessionsConfig, CheckSearchSessionsDeps } from './types'; +import { SearchSessionsConfig, CheckSearchSessionsDeps, SearchStatus } from './types'; import { bulkUpdateSessions, getAllSessionsStatusUpdates } from './update_session_status'; export const SEARCH_SESSIONS_CLEANUP_TASK_TYPE = 'search_sessions_cleanup'; @@ -87,6 +87,8 @@ function checkNonPersistedSessionsPage( // Send a delete request for each async search to ES Object.keys(session.attributes.idMapping).map(async (searchKey: string) => { const searchInfo = session.attributes.idMapping[searchKey]; + if (searchInfo.status === SearchStatus.ERROR) return; // skip attempting to delete async search in case we know it has errored out + if (searchInfo.strategy === ENHANCED_ES_SEARCH_STRATEGY) { try { await client.asyncSearch.delete({ id: searchInfo.id }); diff --git a/x-pack/plugins/data_enhanced/server/search/session/session_service.ts b/x-pack/plugins/data_enhanced/server/search/session/session_service.ts index 0998c1f42e183..af646ac3c5604 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/session_service.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/session_service.ts @@ -57,7 +57,7 @@ import { SEARCH_SESSIONS_CLEANUP_TASK_TYPE, checkNonPersistedSessions, SEARCH_SESSIONS_CLEANUP_TASK_ID, -} from './check_non_persiseted_sessions'; +} from './check_non_persisted_sessions'; import { SEARCH_SESSIONS_EXPIRE_TASK_TYPE, SEARCH_SESSIONS_EXPIRE_TASK_ID,