Skip to content

Commit

Permalink
[Search Sessions] Expire empty sessions after a minute (#105430)
Browse files Browse the repository at this point in the history
* Expire empty sessions after a minute

* Take value from config
  • Loading branch information
lizozom authored Jul 13, 2021
1 parent bd2e4e3 commit 95176b0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

describe('bulkUpdateSessions', () => {
let mockClient: any;
const mockConfig: any = {};
let savedObjectsClient: jest.Mocked<SavedObjectsClientContract>;
const mockLogger: any = {
debug: jest.fn(),
Expand Down Expand Up @@ -66,6 +67,7 @@ describe('bulkUpdateSessions', () => {
client: mockClient,
logger: mockLogger,
},
mockConfig,
so
);

Expand Down Expand Up @@ -105,6 +107,7 @@ describe('bulkUpdateSessions', () => {
client: mockClient,
logger: mockLogger,
},
mockConfig,
so
);

Expand Down Expand Up @@ -139,6 +142,7 @@ describe('bulkUpdateSessions', () => {
client: mockClient,
logger: mockLogger,
},
mockConfig,
so
);

Expand Down Expand Up @@ -176,6 +180,7 @@ describe('bulkUpdateSessions', () => {
client: mockClient,
logger: mockLogger,
},
mockConfig,
so
);

Expand Down Expand Up @@ -219,6 +224,7 @@ describe('bulkUpdateSessions', () => {
client: mockClient,
logger: mockLogger,
},
mockConfig,
so
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchSessionSavedObjectAttributes>
) {
let sessionUpdated = false;
Expand Down Expand Up @@ -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;
Expand All @@ -79,13 +85,14 @@ export async function updateSessionStatus(

export async function getAllSessionsStatusUpdates(
deps: CheckSearchSessionsDeps,
config: SearchSessionsConfig,
searchSessions: SearchSessionsResponse
) {
const updatedSessions = new Array<SavedObjectsFindResult<SearchSessionSavedObjectAttributes>>();

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);
Expand Down

0 comments on commit 95176b0

Please sign in to comment.