[8.7] [SO PIT Finder] Ensure mandatory sort fields (#150574) (#150672)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[SO PIT Finder] Ensure mandatory sort fields
(#150574)](https://github.com/elastic/kibana/pull/150574)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Alejandro Fernández
Haro","email":"alejandro.haro@elastic.co"},"sourceCommit":{"committedDate":"2023-02-09T10:23:20Z","message":"[SO
PIT Finder] Ensure mandatory sort fields (#150574)\n\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>\r\nResolves
https://github.com/elastic/kibana/issues/150310","sha":"94085525a9233423d7b3b0164b56c6213078f20a","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","release_note:skip","backport:prev-minor","v8.8.0"],"number":150574,"url":"https://github.com/elastic/kibana/pull/150574","mergeCommit":{"message":"[SO
PIT Finder] Ensure mandatory sort fields (#150574)\n\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>\r\nResolves
https://github.com/elastic/kibana/issues/150310","sha":"94085525a9233423d7b3b0164b56c6213078f20a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/150574","number":150574,"mergeCommit":{"message":"[SO
PIT Finder] Ensure mandatory sort fields (#150574)\n\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>\r\nResolves
https://github.com/elastic/kibana/issues/150310","sha":"94085525a9233423d7b3b0164b56c6213078f20a"}}]}]
BACKPORT-->

Co-authored-by: Alejandro Fernández Haro <alejandro.haro@elastic.co>
This commit is contained in:
Kibana Machine 2023-02-09 13:48:53 -05:00 committed by GitHub
parent 4f4f92ff2c
commit e41a2a1f91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 3 deletions

View file

@ -235,6 +235,52 @@ describe('createPointInTimeFinder()', () => {
);
});
test('still applies the defaults in the mandatory fields even when `undefined` is explicitly provided', async () => {
repository.openPointInTimeForType.mockResolvedValueOnce({
id: 'abc123',
});
repository.find.mockResolvedValueOnce({
total: 2,
saved_objects: mockHits,
pit_id: 'abc123',
per_page: 2,
page: 0,
});
const findOptions: SavedObjectsCreatePointInTimeFinderOptions = {
type: ['visualization'],
search: 'foo*',
// Intentionally trying to remove the sort fields
sortField: undefined,
sortOrder: undefined,
};
const internalOptions = {};
const finder = new PointInTimeFinder(findOptions, {
logger,
client: repository,
internalOptions,
});
const hits: SavedObjectsFindResult[] = [];
for await (const result of finder.find()) {
hits.push(...result.saved_objects);
}
expect(hits.length).toBe(2);
expect(repository.openPointInTimeForType).toHaveBeenCalledTimes(1);
expect(repository.closePointInTime).toHaveBeenCalledTimes(1);
expect(repository.find).toHaveBeenCalledTimes(1);
expect(repository.find).toHaveBeenCalledWith(
expect.objectContaining({
pit: expect.objectContaining({ id: 'abc123', keepAlive: '2m' }),
sortField: 'updated_at',
sortOrder: 'desc',
type: ['visualization'],
}),
internalOptions
);
});
describe('#close', () => {
test('calls closePointInTime with correct ID', async () => {
repository.openPointInTimeForType.mockResolvedValueOnce({

View file

@ -147,14 +147,14 @@ export class PointInTimeFinder<T = unknown, A = unknown>
try {
return await this.#client.find<T, A>(
{
...findOptions,
// Sort fields are required to use searchAfter, so we set some defaults here
sortField: 'updated_at',
sortOrder: 'desc',
sortField: findOptions.sortField ?? 'updated_at',
sortOrder: findOptions.sortOrder ?? 'desc',
// Bump keep_alive by 2m on every new request to allow for the ES client
// to make multiple retries in the event of a network failure.
pit: id ? { id, keepAlive: '2m' } : undefined,
searchAfter,
...findOptions,
},
this.#internalOptions
);