-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Use optimistic concurrency in SO migrations #231406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
d16b98d
base test
jesuswr dc2a7ba
added some logic to have updates before the migrator transformations
jesuswr d942626
remove dynamic, not needed
jesuswr 1a1757f
improve tests
jesuswr 3a2801a
zdt test done
jesuswr b6114e7
remove describe block
jesuswr b77a50b
improve test so it covers zdt and v2
jesuswr 7c02519
add logs to zdt
jesuswr c6345ab
add logs to v2
jesuswr 3ed0005
add tests for new sumarizeErrorsWithSameType
jesuswr a372afa
update tests for the update action
jesuswr cd9ad4f
add test for v2 model
jesuswr 237fc8c
new test for stages
jesuswr 496b071
Merge branch 'main' into optimistic-conc-control-tests
jesuswr 22c7cd8
fix snapshots in a test
jesuswr 779de83
fix more snapshots
jesuswr 34fe33a
Merge branch 'main' into optimistic-conc-control-tests
jesuswr 0c59109
fix test description
jesuswr 314757d
update createBulkIn... so we can decide if we want optimisc.. or not
jesuswr 5857b7d
Merge branch 'main' into optimistic-conc-control-tests
jesuswr 3dd63ef
lint
jesuswr 8aa95f7
fix test
jesuswr 441a0de
fix tests
jesuswr cbb71ab
Merge branch 'main' into optimistic-conc-control-tests
jesuswr 9d12416
remove logs
jesuswr 7561ae1
clean logs
jesuswr 3f95f13
undo change
jesuswr 3d27850
clean
jesuswr 2dc877c
Merge branch 'main' into optimistic-conc-control-tests
jesuswr 5e63c3a
move id !== originId logic inside createBulkIndex...
jesuswr 7abb899
Merge branch 'main' into optimistic-conc-control-tests
jesuswr a930da2
Update src/core/packages/saved-objects/migration-server-internal/src/…
jesuswr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
171 changes: 171 additions & 0 deletions
171
...r/integration_tests/saved_objects/migrations/zdt_v2_compat/optimistic_concurrency.test.ts
This file contains hidden or 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,171 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import Path from 'path'; | ||
| import fs from 'fs/promises'; | ||
| import { range } from 'lodash'; | ||
| import { type TestElasticsearchUtils } from '@kbn/core-test-helpers-kbn-server'; | ||
| import type { SavedObjectsBulkCreateObject } from '@kbn/core-saved-objects-api-server'; | ||
| import '../jest_matchers'; | ||
| import { getKibanaMigratorTestKit, startElasticsearch } from '../kibana_migrator_test_kit'; | ||
| import { parseLogFile } from '../test_utils'; | ||
| import { getBaseMigratorParams, getSampleAType } from '../fixtures/zdt_base.fixtures'; | ||
| import type { | ||
| SavedObjectModelTransformationDoc, | ||
| SavedObjectModelUnsafeTransformFn, | ||
| } from '@kbn/core-saved-objects-server'; | ||
|
|
||
| export const logFilePath = Path.join(__dirname, 'optimistic_concurrency.test.log'); | ||
|
|
||
| interface TestSOType { | ||
| boolean: boolean; | ||
| keyword: string; | ||
| } | ||
|
|
||
| describe('ZDT & V2 upgrades - optimistic concurrency tests', () => { | ||
| let esServer: TestElasticsearchUtils['es']; | ||
|
|
||
| beforeAll(async () => { | ||
| esServer = await startElasticsearch(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await esServer?.stop(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await fs.unlink(logFilePath).catch(() => {}); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it.each(['v2', 'zdt'] as const)( | ||
| 'doesnt overwrite changes made while migrating (%s)', | ||
| async (migrationAlgorithm) => { | ||
| const { runMigrations, savedObjectsRepository, client } = await prepareScenario( | ||
| migrationAlgorithm | ||
| ); | ||
|
|
||
| const originalBulkImplementation = client.bulk; | ||
| const spy = jest.spyOn(client, 'bulk'); | ||
| spy.mockImplementation(function (this: typeof client, ...args) { | ||
| // let's run some updates before we run the bulk operations | ||
| return Promise.all( | ||
| ['a-0', 'a-3', 'a-4'].map((id) => | ||
| savedObjectsRepository.update('sample_a', id, { | ||
| keyword: 'concurrent update that shouldnt be overwritten', | ||
| }) | ||
| ) | ||
| ).then(() => { | ||
| return originalBulkImplementation.apply(this, args); | ||
| }); | ||
| }); | ||
|
|
||
| await runMigrations(); | ||
|
|
||
| const records = await parseLogFile(logFilePath); | ||
| expect(records).toContainLogEntry('-> DONE'); | ||
|
|
||
| const { saved_objects: sampleADocs } = await savedObjectsRepository.find<TestSOType>({ | ||
| type: 'sample_a', | ||
| }); | ||
|
|
||
| expect( | ||
| sampleADocs | ||
| .map((doc) => ({ | ||
| id: doc.id, | ||
| keyword: doc.attributes.keyword, | ||
| })) | ||
| .sort((a, b) => a.id.localeCompare(b.id)) | ||
| ).toMatchInlineSnapshot(` | ||
| Array [ | ||
| Object { | ||
| "id": "a-0", | ||
| "keyword": "concurrent update that shouldnt be overwritten", | ||
| }, | ||
| Object { | ||
| "id": "a-1", | ||
| "keyword": "updated by the migrator", | ||
| }, | ||
| Object { | ||
| "id": "a-2", | ||
| "keyword": "updated by the migrator", | ||
| }, | ||
| Object { | ||
| "id": "a-3", | ||
| "keyword": "concurrent update that shouldnt be overwritten", | ||
| }, | ||
| Object { | ||
| "id": "a-4", | ||
| "keyword": "concurrent update that shouldnt be overwritten", | ||
| }, | ||
| ] | ||
| `); | ||
| } | ||
| ); | ||
|
|
||
| const prepareScenario = async (migrationAlgorithm: 'zdt' | 'v2') => { | ||
| await createBaseline(); | ||
|
|
||
| const typeA = getSampleAType(); | ||
|
|
||
| const transformFunc: SavedObjectModelUnsafeTransformFn<TestSOType, TestSOType> = ( | ||
| doc: SavedObjectModelTransformationDoc<TestSOType> | ||
| ) => { | ||
| const attributes = { | ||
| ...doc.attributes, | ||
| keyword: 'updated by the migrator', | ||
| }; | ||
| return { document: { ...doc, attributes } }; | ||
| }; | ||
| typeA.modelVersions = { | ||
| ...typeA.modelVersions, | ||
| '2': { | ||
| changes: [ | ||
| { | ||
| type: 'unsafe_transform', | ||
| transformFn: (typeSafeGuard) => typeSafeGuard(transformFunc), | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| const { runMigrations, client, savedObjectsRepository } = await getKibanaMigratorTestKit({ | ||
| ...getBaseMigratorParams({ migrationAlgorithm }), | ||
| logFilePath, | ||
| types: [typeA], | ||
| }); | ||
|
|
||
| return { runMigrations, client, savedObjectsRepository }; | ||
| }; | ||
|
|
||
| const createBaseline = async () => { | ||
| const { runMigrations, savedObjectsRepository, client } = await getKibanaMigratorTestKit({ | ||
| ...getBaseMigratorParams(), | ||
| types: [getSampleAType()], | ||
| }); | ||
|
|
||
| try { | ||
| await client.indices.delete({ index: '.kibana_1' }); | ||
| } catch (e) { | ||
| /* index wasn't created, that's fine */ | ||
| } | ||
|
|
||
| await runMigrations(); | ||
|
|
||
| const sampleAObjs = range(5).map<SavedObjectsBulkCreateObject<TestSOType>>((number) => ({ | ||
| id: `a-${number}`, | ||
| type: 'sample_a', | ||
| attributes: { | ||
| keyword: `a_${number}`, | ||
| boolean: true, | ||
| }, | ||
| })); | ||
| await savedObjectsRepository.bulkCreate<TestSOType>(sampleAObjs); | ||
| }; | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clever way to introduce the updates at just the right time in the migration algorithm