[Search Sessions] Don’t try to delete errored searches (#105434)

This commit is contained in:
Anton Dosov 2021-07-15 12:59:39 +02:00 committed by GitHub
parent 4b4a405043
commit 5fcdb9d137
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View file

@ -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', () => {

View file

@ -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 });

View file

@ -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,