Skip to content

Commit

Permalink
[Search Sessions] Don’t try to delete errored searches (#105434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant authored Jul 15, 2021
1 parent 4b4a405 commit 5fcdb9d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5fcdb9d

Please sign in to comment.