From 9fa5abdab02759b21e3accaa0b8b39f899a8a82c Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Mon, 5 Jun 2023 11:24:28 +0200 Subject: [PATCH] Test for a failed clone during split migration (#158998) ## Summary Adds a test for #158733. This is based on the un-merged #158940, so see the last commit [#6eafe910424414b5670e5f325accc59d87dd6dc4](https://github.com/elastic/kibana/commit/6eafe910424414b5670e5f325accc59d87dd6dc4) for the actual changes proposed by this PR ### Checklist Delete any items that are not applicable to this PR. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: Gerard Soldevila (cherry picked from commit 75ec1ec7c3b78b3b9ff17874e2c3008079942abd) --- .../group3/split_failed_to_clone.test.ts | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/core/server/integration_tests/saved_objects/migrations/group3/split_failed_to_clone.test.ts diff --git a/src/core/server/integration_tests/saved_objects/migrations/group3/split_failed_to_clone.test.ts b/src/core/server/integration_tests/saved_objects/migrations/group3/split_failed_to_clone.test.ts new file mode 100644 index 00000000000000..da9657e1765d10 --- /dev/null +++ b/src/core/server/integration_tests/saved_objects/migrations/group3/split_failed_to_clone.test.ts @@ -0,0 +1,188 @@ +/* + * 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 Path from 'path'; +import type { TestElasticsearchUtils } from '@kbn/core-test-helpers-kbn-server'; +import { + type ISavedObjectTypeRegistry, + type SavedObjectsType, + MAIN_SAVED_OBJECT_INDEX, +} from '@kbn/core-saved-objects-server'; +import { DEFAULT_INDEX_TYPES_MAP } from '@kbn/core-saved-objects-base-server-internal'; +import { + clearLog, + startElasticsearch, + getKibanaMigratorTestKit, + getCurrentVersionTypeRegistry, + overrideTypeRegistry, + getAggregatedTypesCount, + type KibanaMigratorTestKit, +} from '../kibana_migrator_test_kit'; +import { delay } from '../test_utils'; +import '../jest_matchers'; +import { CloneIndexParams } from '@kbn/core-saved-objects-migration-server-internal/src/actions'; + +// mock clone_index from packages/core +jest.mock('@kbn/core-saved-objects-migration-server-internal/src/actions/clone_index', () => { + const realModule = jest.requireActual( + '@kbn/core-saved-objects-migration-server-internal/src/actions/clone_index' + ); + return { + ...realModule, + cloneIndex: (params: CloneIndexParams) => async () => { + // we need to slow down the clone operation for indices other than + // .kibana so that .kibana can completely finish the migration before we + // fail + if (params.target.includes('slow_clone')) + await new Promise((resolve) => setTimeout(resolve, 1000)); + return realModule.cloneIndex(params)(); + }, + }; +}); + +// define a type => index distribution +const RELOCATE_TYPES: Record = { + dashboard: '.kibana_slow_clone_1', + visualization: '.kibana_slow_clone_1', + 'canvas-workpad': '.kibana_slow_clone_1', + search: '.kibana_slow_clone_2', + task: '.kibana_task_manager', + 'epm-packages-assets': '.kibana_slow_clone_1', + // the remaining types will be forced to go to '.kibana', + // overriding `indexPattern: foo` defined in the registry +}; + +export const logFilePath = Path.join(__dirname, 'split_failed_to_clone.test.log'); + +describe('when splitting .kibana into multiple indices and one clone fails', () => { + let esServer: TestElasticsearchUtils['es']; + let typeRegistry: ISavedObjectTypeRegistry; + let migratorTestKitFactory: () => Promise; + + beforeAll(async () => { + typeRegistry = await getCurrentVersionTypeRegistry({ oss: false }); + await clearLog(logFilePath); + esServer = await startElasticsearch({ + dataArchive: Path.join(__dirname, '..', 'archives', '7.14.0_xpack_sample_saved_objects.zip'), + timeout: 60000, + }); + }); + + afterAll(async () => { + await esServer?.stop(); + await delay(2); + }); + + it('after resolving the problem and retrying the migration completes successfully', async () => { + const updatedTypeRegistry = overrideTypeRegistry( + typeRegistry, + (type: SavedObjectsType) => { + return { + ...type, + indexPattern: RELOCATE_TYPES[type.name] ?? MAIN_SAVED_OBJECT_INDEX, + }; + } + ); + + migratorTestKitFactory = () => + getKibanaMigratorTestKit({ + types: updatedTypeRegistry.getAllTypes(), + kibanaIndex: '.kibana', + logFilePath, + defaultIndexTypesMap: DEFAULT_INDEX_TYPES_MAP, + }); + + const { runMigrations: runMigrationsWhichFailsWhenCloning, client } = + await migratorTestKitFactory(); + + // count of types in the legacy index + expect(await getAggregatedTypesCount(client, '.kibana')).toEqual({ + 'apm-telemetry': 1, + application_usage_daily: 4, + 'canvas-workpad': 3, + 'canvas-workpad-template': 5, + config: 1, + 'core-usage-stats': 1, + dashboard: 19, + 'epm-packages': 3, + 'epm-packages-assets': 293, + event_loop_delays_daily: 1, + 'graph-workspace': 3, + 'index-pattern': 5, + 'ingest-agent-policies': 2, + 'ingest-outputs': 1, + 'ingest-package-policies': 2, + ingest_manager_settings: 1, + map: 3, + 'osquery-usage-metric': 1, + 'sample-data-telemetry': 3, + search: 14, + space: 1, + 'spaces-usage-stats': 1, + telemetry: 1, + 'ui-metric': 5, + 'usage-counters': 4, + visualization: 173, + }); + + // cause a failure when cloning .kibana_slow_clone_* indices + client.cluster.putSettings({ persistent: { 'cluster.max_shards_per_node': 15 } }); + + await expect(runMigrationsWhichFailsWhenCloning()).rejects.toThrowError( + /cluster_shard_limit_exceeded/ + ); + + // remove the failure + client.cluster.putSettings({ persistent: { 'cluster.max_shards_per_node': 20 } }); + + const { runMigrations: runMigrations2ndTime } = await migratorTestKitFactory(); + await runMigrations2ndTime(); + + expect(await getAggregatedTypesCount(client, '.kibana')).toMatchInlineSnapshot(` + Object { + "apm-telemetry": 1, + "application_usage_daily": 4, + "canvas-workpad-template": 5, + "config": 1, + "core-usage-stats": 1, + "epm-packages": 3, + "event_loop_delays_daily": 1, + "graph-workspace": 3, + "index-pattern": 5, + "ingest-agent-policies": 2, + "ingest-outputs": 1, + "ingest-package-policies": 2, + "ingest_manager_settings": 1, + "map": 3, + "sample-data-telemetry": 3, + "space": 1, + "spaces-usage-stats": 1, + "telemetry": 1, + "ui-metric": 5, + "usage-counters": 4, + } + `); + expect(await getAggregatedTypesCount(client, '.kibana_slow_clone_1')).toMatchInlineSnapshot(` + Object { + "canvas-workpad": 3, + "dashboard": 19, + "epm-packages-assets": 293, + "visualization": 173, + } + `); + expect(await getAggregatedTypesCount(client, '.kibana_slow_clone_2')).toMatchInlineSnapshot(` + Object { + "search": 14, + } + `); + + // If we run a third time, we should not get any errors + const { runMigrations: runMigrations3rdTime } = await migratorTestKitFactory(); + await runMigrations3rdTime(); + }); +});