-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search] Share async strategy utils (#128358)
- Loading branch information
Showing
5 changed files
with
188 additions
and
51 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/plugins/data/server/search/strategies/common/async_utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getCommonDefaultAsyncSubmitParams, getCommonDefaultAsyncGetParams } from './async_utils'; | ||
import moment from 'moment'; | ||
import { SearchSessionsConfigSchema } from '../../../../config'; | ||
|
||
const getMockSearchSessionsConfig = ({ | ||
enabled = true, | ||
defaultExpiration = moment.duration(7, 'd'), | ||
} = {}) => | ||
({ | ||
enabled, | ||
defaultExpiration, | ||
} as SearchSessionsConfigSchema); | ||
|
||
describe('request utils', () => { | ||
describe('getCommonDefaultAsyncSubmitParams', () => { | ||
test('Uses `keep_alive` from default params if no `sessionId` is provided', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
}); | ||
const params = getCommonDefaultAsyncSubmitParams(mockConfig, {}); | ||
expect(params).toHaveProperty('keep_alive', '1m'); | ||
}); | ||
|
||
test('Uses `keep_alive` from config if enabled', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
}); | ||
const params = getCommonDefaultAsyncSubmitParams(mockConfig, { | ||
sessionId: 'foo', | ||
}); | ||
expect(params).toHaveProperty('keep_alive', '259200000ms'); | ||
}); | ||
|
||
test('Uses `keepAlive` of `1m` if disabled', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: false, | ||
}); | ||
const params = getCommonDefaultAsyncSubmitParams(mockConfig, { | ||
sessionId: 'foo', | ||
}); | ||
expect(params).toHaveProperty('keep_alive', '1m'); | ||
}); | ||
|
||
test('Uses `keep_on_completion` if enabled', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({}); | ||
const params = getCommonDefaultAsyncSubmitParams(mockConfig, { | ||
sessionId: 'foo', | ||
}); | ||
expect(params).toHaveProperty('keep_on_completion', true); | ||
}); | ||
|
||
test('Does not use `keep_on_completion` if disabled', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: false, | ||
}); | ||
const params = getCommonDefaultAsyncSubmitParams(mockConfig, { | ||
sessionId: 'foo', | ||
}); | ||
expect(params).toHaveProperty('keep_on_completion', false); | ||
}); | ||
}); | ||
|
||
describe('getCommonDefaultAsyncGetParams', () => { | ||
test('Uses `wait_for_completion_timeout`', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: true, | ||
}); | ||
const params = getCommonDefaultAsyncGetParams(mockConfig, {}); | ||
expect(params).toHaveProperty('wait_for_completion_timeout'); | ||
}); | ||
|
||
test('Uses `keep_alive` if `sessionId` is not provided', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: true, | ||
}); | ||
const params = getCommonDefaultAsyncGetParams(mockConfig, {}); | ||
expect(params).toHaveProperty('keep_alive', '1m'); | ||
}); | ||
|
||
test('Has no `keep_alive` if `sessionId` is provided', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: true, | ||
}); | ||
const params = getCommonDefaultAsyncGetParams(mockConfig, { sessionId: 'foo' }); | ||
expect(params).not.toHaveProperty('keep_alive'); | ||
}); | ||
|
||
test('Uses `keep_alive` if `sessionId` is provided but sessions disabled', async () => { | ||
const mockConfig = getMockSearchSessionsConfig({ | ||
defaultExpiration: moment.duration(3, 'd'), | ||
enabled: false, | ||
}); | ||
const params = getCommonDefaultAsyncGetParams(mockConfig, { sessionId: 'foo' }); | ||
expect(params).toHaveProperty('keep_alive', '1m'); | ||
}); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
src/plugins/data/server/search/strategies/common/async_utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
AsyncSearchSubmitRequest, | ||
AsyncSearchGetRequest, | ||
} from '@elastic/elasticsearch/lib/api/types'; | ||
import { SearchSessionsConfigSchema } from '../../../../config'; | ||
import { ISearchOptions } from '../../../../common'; | ||
|
||
/** | ||
@internal | ||
*/ | ||
export function getCommonDefaultAsyncSubmitParams( | ||
searchSessionsConfig: SearchSessionsConfigSchema | null, | ||
options: ISearchOptions | ||
): Pick< | ||
AsyncSearchSubmitRequest, | ||
'keep_alive' | 'wait_for_completion_timeout' | 'keep_on_completion' | ||
> { | ||
const useSearchSessions = searchSessionsConfig?.enabled && !!options.sessionId; | ||
|
||
const keepAlive = useSearchSessions | ||
? `${searchSessionsConfig!.defaultExpiration.asMilliseconds()}ms` | ||
: '1m'; | ||
|
||
return { | ||
// Wait up to 100ms for the response to return | ||
wait_for_completion_timeout: '100ms', | ||
// If search sessions are used, store and get an async ID even for short running requests. | ||
keep_on_completion: useSearchSessions, | ||
// The initial keepalive is as defined in defaultExpiration if search sessions are used or 1m otherwise. | ||
keep_alive: keepAlive, | ||
}; | ||
} | ||
|
||
/** | ||
@internal | ||
*/ | ||
export function getCommonDefaultAsyncGetParams( | ||
searchSessionsConfig: SearchSessionsConfigSchema | null, | ||
options: ISearchOptions | ||
): Pick<AsyncSearchGetRequest, 'keep_alive' | 'wait_for_completion_timeout'> { | ||
const useSearchSessions = searchSessionsConfig?.enabled && !!options.sessionId; | ||
|
||
return { | ||
// Wait up to 100ms for the response to return | ||
wait_for_completion_timeout: '100ms', | ||
...(useSearchSessions | ||
? // Don't change the expiration of search requests that are tracked in a search session | ||
undefined | ||
: { | ||
// We still need to do polling for searches not within the context of a search session or when search session disabled | ||
keep_alive: '1m', | ||
}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters