-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Reporting] roll over the reporting data stream when the template has changed #234119
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
pmuellr
merged 9 commits into
elastic:main
from
pmuellr:231200-reporting-roll-over-ds-2
Sep 29, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7ee92e9
[Reporting] roll over the reporting data stream when the template has…
pmuellr 37cacbd
fix jest test
pmuellr e2cedac
remove (hopefully) unneccessary retry in function test
pmuellr 6ec4ada
Merge branch 'main' into 231200-reporting-roll-over-ds-2
elasticmachine f07525c
Merge branch 'main' into 231200-reporting-roll-over-ds-2
elasticmachine 39725b7
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 612b2a4
Merge branch 'main' into 231200-reporting-roll-over-ds-2
elasticmachine 40a57f3
Merge branch 'main' into 231200-reporting-roll-over-ds-2
elasticmachine fc0482c
Merge branch 'main' into 231200-reporting-roll-over-ds-2
elasticmachine 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
174 changes: 174 additions & 0 deletions
174
x-pack/platform/plugins/private/reporting/server/lib/store/rollover.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,174 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { elasticsearchServiceMock } from '@kbn/core/server/mocks'; | ||
| import { loggingSystemMock } from '@kbn/core/server/mocks'; | ||
| import { | ||
| REPORTING_DATA_STREAM_ALIAS, | ||
| REPORTING_DATA_STREAM_INDEX_TEMPLATE, | ||
| REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD, | ||
| } from '@kbn/reporting-server'; | ||
| import type { | ||
| IndicesGetIndexTemplateIndexTemplateItem, | ||
| IndicesGetMappingResponse, | ||
| } from '@elastic/elasticsearch/lib/api/types'; | ||
|
|
||
| import { rollDataStreamIfRequired } from './rollover'; | ||
|
|
||
| describe('rollDataStreamIfRequired', () => { | ||
| const mockLogger = loggingSystemMock.createLogger(); | ||
| let mockEsClient: ReturnType<typeof elasticsearchServiceMock.createElasticsearchClient>; | ||
|
|
||
| beforeEach(async () => { | ||
| mockEsClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
| }); | ||
|
|
||
| const msgPrefix = `Data stream ${REPORTING_DATA_STREAM_ALIAS}`; | ||
| const skipMessage = 'does not need to be rolled over'; | ||
| const rollMessage = 'rolling over the data stream'; | ||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('does nothing if there is no data stream', async () => { | ||
| mockEsClient.indices.exists.mockResponse(false); | ||
| await rollDataStreamIfRequired(mockLogger, mockEsClient); | ||
|
|
||
| expect(mockEsClient.indices.exists).toHaveBeenCalledWith({ | ||
| index: REPORTING_DATA_STREAM_ALIAS, | ||
| expand_wildcards: 'all', | ||
| }); | ||
| expect(mockLogger.debug).toHaveBeenCalledWith(`${msgPrefix} does not exist so ${skipMessage}`); | ||
| expect(mockEsClient.indices.getIndexTemplate).not.toHaveBeenCalled(); | ||
| expect(mockEsClient.indices.getMapping).not.toHaveBeenCalled(); | ||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws an error if no index template is returned', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ index_templates: [] }); | ||
| const err = `${msgPrefix} index template ${REPORTING_DATA_STREAM_INDEX_TEMPLATE} not found`; | ||
| await expect(rollDataStreamIfRequired(mockLogger, mockEsClient)).rejects.toThrow(err); | ||
|
|
||
| expect(mockEsClient.indices.getIndexTemplate).toHaveBeenCalledWith({ | ||
| name: REPORTING_DATA_STREAM_INDEX_TEMPLATE, | ||
| }); | ||
| expect(mockEsClient.indices.getMapping).not.toHaveBeenCalled(); | ||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws an error if there is no index template with a version', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| const templateWithoutVersion = getBasicIndexTemplate(); | ||
| delete templateWithoutVersion.index_template.version; | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [templateWithoutVersion], | ||
| }); | ||
|
|
||
| const err = `${msgPrefix} index template ${REPORTING_DATA_STREAM_INDEX_TEMPLATE} does not have a version field`; | ||
| await expect(rollDataStreamIfRequired(mockLogger, mockEsClient)).rejects.toThrow(err); | ||
|
|
||
| expect(mockEsClient.indices.getMapping).not.toHaveBeenCalled(); | ||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does nothing if there are no mappings on the backing indices', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [getBasicIndexTemplate()], | ||
| }); | ||
| mockEsClient.indices.getMapping.mockResponse({}); | ||
| await rollDataStreamIfRequired(mockLogger, mockEsClient); | ||
|
|
||
| const msg = `${msgPrefix} has no backing indices so ${skipMessage}`; | ||
| expect(mockLogger.debug).toHaveBeenCalledWith(msg); | ||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rolls over the data stream if there are no versions in the backing index mappings', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [getBasicIndexTemplate()], | ||
| }); | ||
| const mappings: IndicesGetMappingResponse = { | ||
| indexName: { | ||
| mappings: { _meta: {} }, | ||
| }, | ||
| }; | ||
| mockEsClient.indices.getMapping.mockResponse(mappings); | ||
| await rollDataStreamIfRequired(mockLogger, mockEsClient); | ||
|
|
||
| const msg = `${msgPrefix} has no mapping versions so ${rollMessage}`; | ||
| expect(mockLogger.info).toHaveBeenCalledWith(msg); | ||
| expect(mockEsClient.indices.rollover).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rolls over the data stream if the index template version is newer than the backing index mappings versions', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [getBasicIndexTemplate()], | ||
| }); | ||
| const mappings: IndicesGetMappingResponse = { | ||
| indexName: { | ||
| mappings: { _meta: { [REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD]: 41 } }, | ||
| }, | ||
| }; | ||
| mockEsClient.indices.getMapping.mockResponse(mappings); | ||
| await rollDataStreamIfRequired(mockLogger, mockEsClient); | ||
|
|
||
| const msg = `${msgPrefix} has older mappings than the template so ${rollMessage}`; | ||
| expect(mockLogger.info).toHaveBeenCalledWith(msg); | ||
| expect(mockEsClient.indices.rollover).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws an error if the index template version is older than the backing index mappings versions', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [getBasicIndexTemplate()], | ||
| }); | ||
| const mappings: IndicesGetMappingResponse = { | ||
| indexName: { | ||
| mappings: { _meta: { [REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD]: 43 } }, | ||
| }, | ||
| }; | ||
| mockEsClient.indices.getMapping.mockResponse(mappings); | ||
| const err = `${msgPrefix} has newer mappings than the template`; | ||
| await expect(rollDataStreamIfRequired(mockLogger, mockEsClient)).rejects.toThrow(err); | ||
|
|
||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does nothing if the index template version is not newer than the backing index mapping versions', async () => { | ||
| mockEsClient.indices.exists.mockResponse(true); | ||
| mockEsClient.indices.getIndexTemplate.mockResponse({ | ||
| index_templates: [getBasicIndexTemplate()], | ||
| }); | ||
| const mappings: IndicesGetMappingResponse = { | ||
| indexName: { | ||
| mappings: { _meta: { [REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD]: 42 } }, | ||
| }, | ||
| }; | ||
| mockEsClient.indices.getMapping.mockResponse(mappings); | ||
| await rollDataStreamIfRequired(mockLogger, mockEsClient); | ||
|
|
||
| const msg = `${msgPrefix} has latest mappings applied so ${skipMessage}`; | ||
| expect(mockLogger.debug).toHaveBeenCalledWith(msg); | ||
| expect(mockEsClient.indices.rollover).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| function getBasicIndexTemplate(): IndicesGetIndexTemplateIndexTemplateItem { | ||
| return { | ||
| name: REPORTING_DATA_STREAM_INDEX_TEMPLATE, | ||
| index_template: { | ||
| index_patterns: ['ignored'], | ||
| composed_of: ['ignored'], | ||
| version: 42, | ||
| }, | ||
| }; | ||
| } |
105 changes: 105 additions & 0 deletions
105
x-pack/platform/plugins/private/reporting/server/lib/store/rollover.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,105 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { | ||
| REPORTING_DATA_STREAM_ALIAS, | ||
| REPORTING_DATA_STREAM_INDEX_TEMPLATE, | ||
| REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD, | ||
| } from '@kbn/reporting-server'; | ||
| import type { ElasticsearchClient, Logger } from '@kbn/core/server'; | ||
|
|
||
| export async function rollDataStreamIfRequired( | ||
| logger: Logger, | ||
| esClient: ElasticsearchClient | ||
| ): Promise<boolean> { | ||
| const msgPrefix = `Data stream ${REPORTING_DATA_STREAM_ALIAS}`; | ||
| const skipMessage = 'does not need to be rolled over'; | ||
| const rollMessage = 'rolling over the data stream'; | ||
| // easy way to change debug log level when debugging | ||
| const debug = (msg: string) => logger.debug(msg); | ||
|
|
||
| const exists = await esClient.indices.exists({ | ||
| index: REPORTING_DATA_STREAM_ALIAS, | ||
| expand_wildcards: 'all', | ||
| }); | ||
|
|
||
| if (!exists) { | ||
| debug(`${msgPrefix} does not exist so ${skipMessage}`); | ||
| return false; | ||
| } | ||
|
|
||
| const gotTemplate = await esClient.indices.getIndexTemplate({ | ||
| name: REPORTING_DATA_STREAM_INDEX_TEMPLATE, | ||
| }); | ||
| if (gotTemplate.index_templates.length === 0) { | ||
| throw new Error( | ||
| `${msgPrefix} index template ${REPORTING_DATA_STREAM_INDEX_TEMPLATE} not found` | ||
| ); | ||
| } | ||
|
|
||
| const templateVersions: number[] = []; | ||
| for (const template of gotTemplate.index_templates) { | ||
| const templateVersion = template.index_template.version; | ||
| if (templateVersion) templateVersions.push(templateVersion); | ||
| } | ||
|
|
||
| if (templateVersions.length === 0) { | ||
| throw new Error( | ||
| `${msgPrefix} index template ${REPORTING_DATA_STREAM_INDEX_TEMPLATE} does not have a version field` | ||
| ); | ||
| } | ||
|
|
||
| // assume the highest version is the one in use | ||
| const templateVersion = Math.max(...templateVersions); | ||
| debug(`${msgPrefix} template version: ${templateVersion}`); | ||
|
|
||
| const mappings = await esClient.indices.getMapping({ | ||
| index: REPORTING_DATA_STREAM_ALIAS, | ||
| allow_no_indices: true, | ||
| expand_wildcards: 'all', | ||
| }); | ||
|
|
||
| const mappingsArray = Object.values(mappings); | ||
| if (mappingsArray.length === 0) { | ||
| debug(`${msgPrefix} has no backing indices so ${skipMessage}`); | ||
| return false; | ||
| } | ||
|
|
||
| // get the value of _meta.template_version from each index's mappings | ||
| const mappingsVersions = mappingsArray | ||
| .map((m) => m.mappings._meta?.[REPORTING_INDEX_TEMPLATE_MAPPING_META_FIELD]) | ||
| .filter((a: any): a is number => typeof a === 'number'); | ||
|
|
||
| const mappingsVersion = mappingsVersions.length === 0 ? undefined : Math.max(...mappingsVersions); | ||
| debug(`${msgPrefix} mappings version: ${mappingsVersion ?? '<none>'}`); | ||
|
|
||
| if (mappingsVersion === undefined) { | ||
| // no mapping version found on any indices | ||
| logger.info(`${msgPrefix} has no mapping versions so ${rollMessage}`); | ||
| } else if (mappingsVersion < templateVersion) { | ||
| // all mappings are old | ||
| logger.info(`${msgPrefix} has older mappings than the template so ${rollMessage}`); | ||
| } else if (mappingsVersion > templateVersion) { | ||
| // newer mappings than the template shouldn't happen | ||
| throw new Error(`${msgPrefix} has newer mappings than the template`); | ||
| } else { | ||
| // latest mappings already applied | ||
| debug(`${msgPrefix} has latest mappings applied so ${skipMessage}`); | ||
| return false; | ||
| } | ||
|
|
||
| // Roll over the data stream to pick up the new mappings. | ||
| // The `lazy` option will cause the rollover to run on the next write. | ||
| // This limits potential race conditions of multiple Kibana's rolling over at once. | ||
| await esClient.indices.rollover({ | ||
| alias: REPORTING_DATA_STREAM_ALIAS, | ||
| lazy: true, | ||
| }); | ||
|
|
||
| logger.info(`${msgPrefix} rolled over to pick up index template version ${templateVersion}`); | ||
| return true; | ||
| } | ||
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
16 changes: 16 additions & 0 deletions
16
x-pack/platform/test/reporting_api_integration/plugins/reporting_test_routes/kibana.jsonc
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,16 @@ | ||
| { | ||
| "type": "plugin", | ||
| "id": "@kbn/reporting-test-routes", | ||
| "owner": "@elastic/response-ops", | ||
| "visibility": "private", | ||
| "plugin": { | ||
| "id": "reportingTestRoutes", | ||
| "server": true, | ||
| "browser": false, | ||
| "requiredPlugins": [ | ||
| "reporting", | ||
| ], | ||
| "optionalPlugins": [ | ||
| ] | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
x-pack/platform/test/reporting_api_integration/plugins/reporting_test_routes/package.json
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,14 @@ | ||
| { | ||
| "name": "@kbn/reporting-test-routes", | ||
| "version": "1.0.0", | ||
| "kibana": { | ||
| "version": "kibana", | ||
| "templateVersion": "1.0.0" | ||
| }, | ||
| "main": "target/test/reporting_api_integration/plugins/reporting_api_integration", | ||
| "scripts": { | ||
| "kbn": "node ../../../../../../scripts/kbn.js", | ||
| "build": "rm -rf './target' && ../../../../../../node_modules/.bin/tsc" | ||
| }, | ||
| "license": "Elastic License 2.0" | ||
| } |
11 changes: 11 additions & 0 deletions
11
x-pack/platform/test/reporting_api_integration/plugins/reporting_test_routes/server/index.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,11 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { PluginInitializerContext } from '@kbn/core/server'; | ||
| import { TestPlugin } from './plugin'; | ||
|
|
||
| export const plugin = async (initContext: PluginInitializerContext) => new TestPlugin(initContext); |
Oops, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think
ais fine here. I probably should have mentioned WHY I'm doing this. In case one of the numbers isn't a number (likeundefined), we want to filter them out, but you'd think you could doThe problem is the result of that is typed
any[]. Adding the predicate: a is numberends up typing the result asnumber[].