-
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.
[7.x] Add ability for types to define an excludeOnUpgrade hook (#106534…
…) (#106930) * Add ability for types to define an excludeOnUpgrade hook (#106534) * Add ability for types to define an exclude from upgrade hook * Update test to use 7.13 fixture * Add 404 handling to oldest action task query * Update api docs * Update and add unit tests * Rename deleteOnUpgrade to excludeOnUpgrade * Disable reporting browser download for integration test * Update exports and TSDocs * Disable reporting plugin entirely * Typo in config key name * Update api docs * Fix eslint * Add timeouts for hooks * Make adjustments to getOldestIdleActionTask * Add type name to log messages for failed hooks * Update api docs * Update fixture for 7.x Co-authored-by: Josh Dover <[email protected]>
- Loading branch information
1 parent
92a4a4e
commit 395428e
Showing
28 changed files
with
805 additions
and
7 deletions.
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
...ment/core/server/kibana-plugin-core-server.savedobjectstype.excludeonupgrade.md
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,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsType](./kibana-plugin-core-server.savedobjectstype.md) > [excludeOnUpgrade](./kibana-plugin-core-server.savedobjectstype.excludeonupgrade.md) | ||
|
||
## SavedObjectsType.excludeOnUpgrade property | ||
|
||
If defined, allows a type to exclude unneeded documents from the migration process and effectively be deleted. See [SavedObjectTypeExcludeFromUpgradeFilterHook](./kibana-plugin-core-server.savedobjecttypeexcludefromupgradefilterhook.md) for more details. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
excludeOnUpgrade?: SavedObjectTypeExcludeFromUpgradeFilterHook; | ||
``` |
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
19 changes: 19 additions & 0 deletions
19
...server/kibana-plugin-core-server.savedobjecttypeexcludefromupgradefilterhook.md
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,19 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectTypeExcludeFromUpgradeFilterHook](./kibana-plugin-core-server.savedobjecttypeexcludefromupgradefilterhook.md) | ||
|
||
## SavedObjectTypeExcludeFromUpgradeFilterHook type | ||
|
||
If defined, allows a type to run a search query and return a query filter that may match any documents which may be excluded from the next migration upgrade process. Useful for cleaning up large numbers of old documents which are no longer needed and may slow the migration process. | ||
|
||
If this hook fails, the migration will proceed without these documents having been filtered out, so this should not be used as a guarantee that these documents have been deleted. | ||
|
||
Experimental and subject to change | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type SavedObjectTypeExcludeFromUpgradeFilterHook = (toolkit: { | ||
readonlyEsClient: Pick<ElasticsearchClient, 'search'>; | ||
}) => estypes.QueryDslQueryContainer | Promise<estypes.QueryDslQueryContainer>; | ||
``` |
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
113 changes: 113 additions & 0 deletions
113
src/core/server/saved_objects/migrationsv2/actions/calculate_exclude_filters.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,113 @@ | ||
/* | ||
* 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 * as Either from 'fp-ts/lib/Either'; | ||
import { errors as esErrors } from '@elastic/elasticsearch'; | ||
import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks'; | ||
import { calculateExcludeFilters } from './calculate_exclude_filters'; | ||
|
||
describe('calculateExcludeFilters', () => { | ||
const client = elasticsearchClientMock.createInternalClient(); | ||
|
||
it('calls each provided hook and returns combined filter', async () => { | ||
const hook1 = jest.fn().mockReturnValue({ bool: { must: { term: { fieldA: '123' } } } }); | ||
const hook2 = jest.fn().mockResolvedValue({ bool: { must: { term: { fieldB: 'abc' } } } }); | ||
|
||
const task = calculateExcludeFilters({ | ||
client, | ||
excludeFromUpgradeFilterHooks: { type1: hook1, type2: hook2 }, | ||
}); | ||
const result = await task(); | ||
|
||
expect(hook1).toHaveBeenCalledWith({ readonlyEsClient: { search: expect.any(Function) } }); | ||
expect(hook2).toHaveBeenCalledWith({ readonlyEsClient: { search: expect.any(Function) } }); | ||
expect(Either.isRight(result)).toBe(true); | ||
expect((result as Either.Right<any>).right).toEqual({ | ||
excludeFilter: { | ||
bool: { | ||
must_not: [ | ||
{ bool: { must: { term: { fieldA: '123' } } } }, | ||
{ bool: { must: { term: { fieldB: 'abc' } } } }, | ||
], | ||
}, | ||
}, | ||
errorsByType: {}, | ||
}); | ||
}); | ||
|
||
it('ignores hooks that return non-retryable errors', async () => { | ||
const error = new Error('blah!'); | ||
const hook1 = jest.fn().mockRejectedValue(error); | ||
const hook2 = jest.fn().mockResolvedValue({ bool: { must: { term: { fieldB: 'abc' } } } }); | ||
|
||
const task = calculateExcludeFilters({ | ||
client, | ||
excludeFromUpgradeFilterHooks: { type1: hook1, type2: hook2 }, | ||
}); | ||
const result = await task(); | ||
|
||
expect(Either.isRight(result)).toBe(true); | ||
expect((result as Either.Right<any>).right).toEqual({ | ||
excludeFilter: { | ||
bool: { | ||
must_not: [{ bool: { must: { term: { fieldB: 'abc' } } } }], | ||
}, | ||
}, | ||
errorsByType: { type1: error }, | ||
}); | ||
}); | ||
|
||
it('returns Either.Left if a hook returns a retryable error', async () => { | ||
const error = new esErrors.NoLivingConnectionsError( | ||
'reason', | ||
elasticsearchClientMock.createApiResponse() | ||
); | ||
const hook1 = jest.fn().mockRejectedValue(error); | ||
const hook2 = jest.fn().mockResolvedValue({ bool: { must: { term: { fieldB: 'abc' } } } }); | ||
|
||
const task = calculateExcludeFilters({ | ||
client, | ||
excludeFromUpgradeFilterHooks: { type1: hook1, type2: hook2 }, | ||
}); | ||
const result = await task(); | ||
|
||
expect(Either.isLeft(result)).toBe(true); | ||
expect((result as Either.Left<any>).left).toEqual({ | ||
type: 'retryable_es_client_error', | ||
message: 'reason', | ||
error, | ||
}); | ||
}); | ||
|
||
it('ignores and returns errors for hooks that take longer than timeout', async () => { | ||
const hook1 = jest.fn().mockReturnValue(new Promise((r) => setTimeout(r, 40_000))); | ||
const hook2 = jest.fn().mockResolvedValue({ bool: { must: { term: { fieldB: 'abc' } } } }); | ||
|
||
const task = calculateExcludeFilters({ | ||
client, | ||
excludeFromUpgradeFilterHooks: { type1: hook1, type2: hook2 }, | ||
hookTimeoutMs: 100, | ||
}); | ||
const resultPromise = task(); | ||
await new Promise((r) => setTimeout(r, 110)); | ||
const result = await resultPromise; | ||
|
||
expect(Either.isRight(result)).toBe(true); | ||
expect((result as Either.Right<any>).right).toEqual({ | ||
excludeFilter: { | ||
bool: { | ||
must_not: [{ bool: { must: { term: { fieldB: 'abc' } } } }], | ||
}, | ||
}, | ||
errorsByType: expect.any(Object), | ||
}); | ||
expect((result as Either.Right<any>).right.errorsByType.type1.toString()).toMatchInlineSnapshot( | ||
`"Error: excludeFromUpgrade hook timed out after 0.1 seconds."` | ||
); | ||
}); | ||
}); |
107 changes: 107 additions & 0 deletions
107
src/core/server/saved_objects/migrationsv2/actions/calculate_exclude_filters.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,107 @@ | ||
/* | ||
* 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 { estypes } from '@elastic/elasticsearch'; | ||
import { withTimeout } from '@kbn/std'; | ||
import * as Either from 'fp-ts/lib/Either'; | ||
import * as TaskEither from 'fp-ts/lib/TaskEither'; | ||
import { RetryableEsClientError } from '.'; | ||
import { ElasticsearchClient } from '../../../elasticsearch'; | ||
import { SavedObjectTypeExcludeFromUpgradeFilterHook } from '../../types'; | ||
import { catchRetryableEsClientErrors } from './catch_retryable_es_client_errors'; | ||
|
||
export interface CalculateExcludeFiltersParams { | ||
client: ElasticsearchClient; | ||
excludeFromUpgradeFilterHooks: Record<string, SavedObjectTypeExcludeFromUpgradeFilterHook>; | ||
hookTimeoutMs?: number; | ||
} | ||
|
||
export interface CalculatedExcludeFilter { | ||
/** Composite filter of all calculated filters */ | ||
excludeFilter: estypes.QueryDslQueryContainer; | ||
/** Any errors that were encountered during filter calculation, keyed by the type name */ | ||
errorsByType: Record<string, Error>; | ||
} | ||
|
||
export const calculateExcludeFilters = ({ | ||
client, | ||
excludeFromUpgradeFilterHooks, | ||
hookTimeoutMs = 30_000, // default to 30s, exposed for testing | ||
}: CalculateExcludeFiltersParams): TaskEither.TaskEither< | ||
RetryableEsClientError, | ||
CalculatedExcludeFilter | ||
> => () => { | ||
return Promise.all< | ||
| Either.Right<estypes.QueryDslQueryContainer> | ||
| Either.Left<{ soType: string; error: Error | RetryableEsClientError }> | ||
>( | ||
Object.entries(excludeFromUpgradeFilterHooks).map(([soType, hook]) => | ||
withTimeout({ | ||
promise: Promise.resolve( | ||
hook({ readonlyEsClient: { search: client.search.bind(client) } }) | ||
), | ||
timeoutMs: hookTimeoutMs, | ||
}) | ||
.then((result) => | ||
result.timedout | ||
? Either.left({ | ||
soType, | ||
error: new Error( | ||
`excludeFromUpgrade hook timed out after ${hookTimeoutMs / 1000} seconds.` | ||
), | ||
}) | ||
: Either.right(result.value) | ||
) | ||
.catch((error) => { | ||
const retryableError = catchRetryableEsClientErrors(error); | ||
if (Either.isLeft(retryableError)) { | ||
return Either.left({ soType, error: retryableError.left }); | ||
} else { | ||
// Really should never happen, only here to satisfy TypeScript | ||
return Either.left({ | ||
soType, | ||
error: new Error( | ||
`Unexpected return value from catchRetryableEsClientErrors: "${retryableError.toString()}"` | ||
), | ||
}); | ||
} | ||
}) | ||
.catch((error: Error) => Either.left({ soType, error })) | ||
) | ||
).then((results) => { | ||
const retryableError = results.find( | ||
(r) => | ||
Either.isLeft(r) && | ||
!(r.left.error instanceof Error) && | ||
r.left.error.type === 'retryable_es_client_error' | ||
) as Either.Left<{ soType: string; error: RetryableEsClientError }> | undefined; | ||
if (retryableError) { | ||
return Either.left(retryableError.left.error); | ||
} | ||
|
||
const errorsByType: Array<[string, Error]> = []; | ||
const filters: estypes.QueryDslQueryContainer[] = []; | ||
|
||
// Loop through all results and collect successes and errors | ||
results.forEach((r) => | ||
Either.isRight(r) | ||
? filters.push(r.right) | ||
: Either.isLeft(r) && errorsByType.push([r.left.soType, r.left.error as Error]) | ||
); | ||
|
||
// Composite filter from all calculated filters that successfully executed | ||
const excludeFilter: estypes.QueryDslQueryContainer = { | ||
bool: { must_not: filters }, | ||
}; | ||
|
||
return Either.right({ | ||
excludeFilter, | ||
errorsByType: Object.fromEntries(errorsByType), | ||
}); | ||
}); | ||
}; |
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
Binary file added
BIN
+2.21 MB
...r/saved_objects/migrationsv2/integration_tests/archives/7.13_1.5k_failed_action_tasks.zip
Binary file not shown.
Oops, something went wrong.