From 95176b0f545c4c45a8396696101e98fa47b53aaf Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Tue, 13 Jul 2021 18:03:31 +0100 Subject: [PATCH] [Search Sessions] Expire empty sessions after a minute (#105430) * Expire empty sessions after a minute * Take value from config --- .../session/check_non_persiseted_sessions.ts | 6 +++- .../session/check_persisted_sessions.ts | 6 +++- .../session/expire_persisted_sessions.ts | 2 +- .../search/session/get_session_status.test.ts | 33 ++++++++++++++++--- .../search/session/get_session_status.ts | 16 +++++++-- .../session/update_session_status.test.ts | 6 ++++ .../search/session/update_session_status.ts | 13 ++++++-- 7 files changed, 69 insertions(+), 13 deletions(-) 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_persiseted_sessions.ts index 8c75ce91cac6a..2115ce85eeb27 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_persiseted_sessions.ts @@ -59,7 +59,11 @@ function checkNonPersistedSessionsPage( `${SEARCH_SESSIONS_CLEANUP_TASK_TYPE} Found ${nonPersistedSearchSessions.total} sessions, processing ${nonPersistedSearchSessions.saved_objects.length}` ); - const updatedSessions = await getAllSessionsStatusUpdates(deps, nonPersistedSearchSessions); + const updatedSessions = await getAllSessionsStatusUpdates( + deps, + config, + nonPersistedSearchSessions + ); const deletedSessionIds: string[] = []; await Promise.all( diff --git a/x-pack/plugins/data_enhanced/server/search/session/check_persisted_sessions.ts b/x-pack/plugins/data_enhanced/server/search/session/check_persisted_sessions.ts index 0d51e97952275..3e89383c16d5e 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/check_persisted_sessions.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/check_persisted_sessions.ts @@ -36,7 +36,11 @@ function checkPersistedSessionsPage( `${SEARCH_SESSIONS_TASK_TYPE} Found ${persistedSearchSessions.total} sessions, processing ${persistedSearchSessions.saved_objects.length}` ); - const updatedSessions = await getAllSessionsStatusUpdates(deps, persistedSearchSessions); + const updatedSessions = await getAllSessionsStatusUpdates( + deps, + config, + persistedSearchSessions + ); await bulkUpdateSessions(deps, updatedSessions); return persistedSearchSessions; diff --git a/x-pack/plugins/data_enhanced/server/search/session/expire_persisted_sessions.ts b/x-pack/plugins/data_enhanced/server/search/session/expire_persisted_sessions.ts index e261c324f440f..61d1635dabe1b 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/expire_persisted_sessions.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/expire_persisted_sessions.ts @@ -36,7 +36,7 @@ function checkSessionExpirationPage( `${SEARCH_SESSIONS_EXPIRE_TASK_TYPE} Found ${searchSessions.total} sessions, processing ${searchSessions.saved_objects.length}` ); - const updatedSessions = await getAllSessionsStatusUpdates(deps, searchSessions); + const updatedSessions = await getAllSessionsStatusUpdates(deps, config, searchSessions); await bulkUpdateSessions(deps, updatedSessions); return searchSessions; diff --git a/x-pack/plugins/data_enhanced/server/search/session/get_session_status.test.ts b/x-pack/plugins/data_enhanced/server/search/session/get_session_status.test.ts index fc86e75297393..c3946e5af16fa 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/get_session_status.test.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/get_session_status.test.ts @@ -5,16 +5,21 @@ * 2.0. */ -import { SearchStatus } from './types'; +import { SearchSessionsConfig, SearchStatus } from './types'; import { getSessionStatus } from './get_session_status'; import { SearchSessionStatus } from '../../../../../../src/plugins/data/common'; +import moment from 'moment'; describe('getSessionStatus', () => { + const mockConfig = ({ + notTouchedInProgressTimeout: moment.duration(1, 'm'), + } as unknown) as SearchSessionsConfig; test("returns an in_progress status if there's nothing inside the session", () => { const session: any = { idMapping: {}, + touched: moment(), }; - expect(getSessionStatus(session)).toBe(SearchSessionStatus.IN_PROGRESS); + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.IN_PROGRESS); }); test("returns an error status if there's at least one error", () => { @@ -25,7 +30,25 @@ describe('getSessionStatus', () => { c: { status: SearchStatus.COMPLETE }, }, }; - expect(getSessionStatus(session)).toBe(SearchSessionStatus.ERROR); + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.ERROR); + }); + + test('expires a empty session after a minute', () => { + const session: any = { + idMapping: {}, + touched: moment().subtract(2, 'm'), + }; + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.EXPIRED); + }); + + test('doesnt expire a full session after a minute', () => { + const session: any = { + idMapping: { + a: { status: SearchStatus.IN_PROGRESS }, + }, + touched: moment().subtract(2, 'm'), + }; + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.IN_PROGRESS); }); test('returns a complete status if all are complete', () => { @@ -36,7 +59,7 @@ describe('getSessionStatus', () => { c: { status: SearchStatus.COMPLETE }, }, }; - expect(getSessionStatus(session)).toBe(SearchSessionStatus.COMPLETE); + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.COMPLETE); }); test('returns a running status if some are still running', () => { @@ -47,6 +70,6 @@ describe('getSessionStatus', () => { c: { status: SearchStatus.IN_PROGRESS }, }, }; - expect(getSessionStatus(session)).toBe(SearchSessionStatus.IN_PROGRESS); + expect(getSessionStatus(session, mockConfig)).toBe(SearchSessionStatus.IN_PROGRESS); }); }); diff --git a/x-pack/plugins/data_enhanced/server/search/session/get_session_status.ts b/x-pack/plugins/data_enhanced/server/search/session/get_session_status.ts index 23e02eedc0004..e7ae52b6c88ae 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/get_session_status.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/get_session_status.ts @@ -5,16 +5,28 @@ * 2.0. */ +import moment from 'moment'; import { SearchSessionSavedObjectAttributes, SearchSessionStatus, } from '../../../../../../src/plugins/data/common/'; -import { SearchStatus } from './types'; +import { SearchSessionsConfig, SearchStatus } from './types'; -export function getSessionStatus(session: SearchSessionSavedObjectAttributes): SearchSessionStatus { +export function getSessionStatus( + session: SearchSessionSavedObjectAttributes, + config: SearchSessionsConfig +): SearchSessionStatus { const searchStatuses = Object.values(session.idMapping); + const curTime = moment(); if (searchStatuses.some((item) => item.status === SearchStatus.ERROR)) { return SearchSessionStatus.ERROR; + } else if ( + searchStatuses.length === 0 && + curTime.diff(moment(session.touched), 'ms') > + moment.duration(config.notTouchedInProgressTimeout).asMilliseconds() + ) { + // Expire empty sessions that weren't touched for a minute + return SearchSessionStatus.EXPIRED; } else if ( searchStatuses.length > 0 && searchStatuses.every((item) => item.status === SearchStatus.COMPLETE) diff --git a/x-pack/plugins/data_enhanced/server/search/session/update_session_status.test.ts b/x-pack/plugins/data_enhanced/server/search/session/update_session_status.test.ts index 485a30fd54951..d9e3fa6f8cab3 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/update_session_status.test.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/update_session_status.test.ts @@ -21,6 +21,7 @@ import { describe('bulkUpdateSessions', () => { let mockClient: any; + const mockConfig: any = {}; let savedObjectsClient: jest.Mocked; const mockLogger: any = { debug: jest.fn(), @@ -66,6 +67,7 @@ describe('bulkUpdateSessions', () => { client: mockClient, logger: mockLogger, }, + mockConfig, so ); @@ -105,6 +107,7 @@ describe('bulkUpdateSessions', () => { client: mockClient, logger: mockLogger, }, + mockConfig, so ); @@ -139,6 +142,7 @@ describe('bulkUpdateSessions', () => { client: mockClient, logger: mockLogger, }, + mockConfig, so ); @@ -176,6 +180,7 @@ describe('bulkUpdateSessions', () => { client: mockClient, logger: mockLogger, }, + mockConfig, so ); @@ -219,6 +224,7 @@ describe('bulkUpdateSessions', () => { client: mockClient, logger: mockLogger, }, + mockConfig, so ); diff --git a/x-pack/plugins/data_enhanced/server/search/session/update_session_status.ts b/x-pack/plugins/data_enhanced/server/search/session/update_session_status.ts index 1c484467bef63..4758e7cb22684 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/update_session_status.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/update_session_status.ts @@ -13,11 +13,17 @@ import { } from '../../../../../../src/plugins/data/common'; import { getSearchStatus } from './get_search_status'; import { getSessionStatus } from './get_session_status'; -import { CheckSearchSessionsDeps, SearchSessionsResponse, SearchStatus } from './types'; +import { + CheckSearchSessionsDeps, + SearchSessionsConfig, + SearchSessionsResponse, + SearchStatus, +} from './types'; import { isSearchSessionExpired } from './utils'; export async function updateSessionStatus( { logger, client }: CheckSearchSessionsDeps, + config: SearchSessionsConfig, session: SavedObjectsFindResult ) { let sessionUpdated = false; @@ -61,7 +67,7 @@ export async function updateSessionStatus( // And only then derive the session's status const sessionStatus = isExpired ? SearchSessionStatus.EXPIRED - : getSessionStatus(session.attributes); + : getSessionStatus(session.attributes, config); if (sessionStatus !== session.attributes.status) { const now = new Date().toISOString(); session.attributes.status = sessionStatus; @@ -79,13 +85,14 @@ export async function updateSessionStatus( export async function getAllSessionsStatusUpdates( deps: CheckSearchSessionsDeps, + config: SearchSessionsConfig, searchSessions: SearchSessionsResponse ) { const updatedSessions = new Array>(); await Promise.all( searchSessions.saved_objects.map(async (session) => { - const updated = await updateSessionStatus(deps, session); + const updated = await updateSessionStatus(deps, config, session); if (updated) { updatedSessions.push(session);