[Reporting] roll over the reporting data stream when the template has changed#234119
[Reporting] roll over the reporting data stream when the template has changed#234119pmuellr merged 9 commits intoelastic:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements automatic data stream rollover for Kibana reporting indices when the index template has changed. The solution addresses a bug where new fields added to index templates (like space_id) weren't being applied to existing indices, causing reports in non-default spaces to not appear properly in the reports list.
Key changes:
- Added startup logic to compare index template versions with backing index mapping versions
- Automatically rolls over the reporting data stream when template updates are detected
- Enhanced error handling and logging for the startup process
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| x-pack/platform/plugins/private/reporting/server/lib/store/store.ts | Implements rollover logic with version comparison and integrates it into the startup process |
| src/platform/packages/private/kbn-reporting/server/constants.ts | Adds constants for index template name and mapping meta field |
| // newer mappings than the template shouldn't happen | ||
| throw new Error(`${msgPrefix} has newer mappings than the template`); | ||
| } else { | ||
| // lastest mappings already applied |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
c1a75f7 to
309e269
Compare
9b1a6c7 to
ab475ee
Compare
… changed resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. Startup processing for reporting has changed to apply the current index template mappings to the kibana reporting index, if it exists. This fixes a problem where a new field added to the index template, however it was not applied, so the field could not reliably used. The field was `space_id`, which could cause reports in non-default spaces to not appear in the reports list.
ab6424e to
37cacbd
Compare
|
Pinging @elastic/response-ops (Team:ResponseOps) |
| // @ts-ignore ; I've seen either result.acknowledged or result.status === 404 | ||
| expect(result.acknowledged || result.status === 404).to.be(true); |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
It's a test, not adding that kinda stuff for a test! Not quite sure why the typing was off on that API, but I did see a status: 404 in the debugger at one point, so made sure we checked that ...
There was a problem hiding this comment.
Do you think Copilot will request changes to block you from merging? 😂
There was a problem hiding this comment.
hehe - actually, I'm pretty sure it explicitly says such comments will never block a merge.
| // 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'); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
I think a is fine here. I probably should have mentioned WHY I'm doing this. In case one of the numbers isn't a number (like undefined), we want to filter them out, but you'd think you could do
.filter((a: any) => typeof a === 'number')
The problem is the result of that is typed any[]. Adding the predicate : a is number ends up typing the result as number[].
ymao1
left a comment
There was a problem hiding this comment.
LGTM! Works as described.
Verified by doing the following:
- started on 8.18, stack template version 14, created a report
- upgraded to latest 8.19, stack template version 18, created a report and see the bad
space_idfield dynamically mapped - upgraded to this PR, stack template version 18, see startup messages rolling over the reporting index, created a report, see the rolled over index with correct mappings
- restarted Kibana, stack template version 18, see no rollover
- added new stack template version 19 locally and run ES from source, see startup messages rolling over the reporting index, created a report, see the rolled over index with new mapping.
|
/ci |
|
@elasticmachine merge upstream |
csr
left a comment
There was a problem hiding this comment.
x-pack/platform/test/tsconfig.json changes LGTM 👍
|
@elasticmachine merge upstream |
|
@elasticmachine merge upstream |
|
Starting backport for target branches: 8.18, 8.19, 9.0, 9.1 |
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Public APIs missing comments
History
|
💔 All backports failed
Manual backportTo create the backport manually run: Questions ?Please refer to the Backport tool documentation |
… changed (#234119) resolves: #231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> (cherry picked from commit c89ba82) # Conflicts: # .github/CODEOWNERS # x-pack/platform/test/reporting_api_integration/reporting_without_security.config.ts # x-pack/platform/test/tsconfig.json
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> (cherry picked from commit c89ba82) # Conflicts: # .github/CODEOWNERS # x-pack/test/reporting_api_integration/reporting_without_security.config.ts # x-pack/test/reporting_api_integration/reporting_without_security/index.ts # x-pack/test/reporting_api_integration/reporting_without_security/roll_datastream.ts # x-pack/test/tsconfig.json
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> (cherry picked from commit c89ba82) # Conflicts: # .github/CODEOWNERS # x-pack/platform/test/reporting_api_integration/reporting_without_security.config.ts # x-pack/platform/test/tsconfig.json
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> (cherry picked from commit c89ba82) # Conflicts: # .github/CODEOWNERS # x-pack/test/reporting_api_integration/reporting_without_security.config.ts # x-pack/test/reporting_api_integration/reporting_without_security/index.ts # x-pack/test/reporting_api_integration/reporting_without_security/roll_datastream.ts # x-pack/test/tsconfig.json
|
We only need backports to 8.19 and 9.1, based on the pre-req PR elastic/elasticsearch#133846 only being backported there. |
|
Starting backport for target branches: 8.19, 9.1 |
💔 All backports failed
Manual backportTo create the backport manually run: Questions ?Please refer to the Backport tool documentation |
…ate has changed (#234119) (#237115) # Backport This will backport the following commits from `main` to `8.19`: - [[Reporting] roll over the reporting data stream when the template has changed (#234119)](#234119) <!--- Backport version: 10.0.2 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Patrick Mueller","email":"patrick.mueller@elastic.co"},"sourceCommit":{"committedDate":"2025-09-29T23:06:34Z","message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","backport missing","backport:all-open","Feature:Reporting:Framework","v9.2.0"],"title":"[Reporting] roll over the reporting data stream when the template has changed","number":234119,"url":"https://github.com/elastic/kibana/pull/234119","mergeCommit":{"message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/234119","number":234119,"mergeCommit":{"message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04"}}]}] BACKPORT--> --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
…te has changed (#234119) (#237049) # Backport This will backport the following commits from `main` to `9.1`: - [[Reporting] roll over the reporting data stream when the template has changed (#234119)](#234119) <!--- Backport version: 10.0.2 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Patrick Mueller","email":"patrick.mueller@elastic.co"},"sourceCommit":{"committedDate":"2025-09-29T23:06:34Z","message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","backport missing","backport:all-open","Feature:Reporting:Framework","v9.2.0"],"title":"[Reporting] roll over the reporting data stream when the template has changed","number":234119,"url":"https://github.com/elastic/kibana/pull/234119","mergeCommit":{"message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/234119","number":234119,"mergeCommit":{"message":"[Reporting] roll over the reporting data stream when the template has changed (#234119)\n\nresolves: https://github.com/elastic/kibana/issues/231200\n\nThis PR adds code run at startup to check the version in the reporting\nindex template against the `_meta.template_version` value stamped into\neach backing index's mappings via\nhttps://github.com/elastic/elasticsearch/pull/133846\n\nIf it determines there are existing reporting indices that have not been\ncreated with the current index template version, it will roll over the\nreporting data stream, to ensure future indexing will use the latest\nmappings. This is done with the \"lazy\" option, which will perform the\nroll over on the next write to the datastream. This will prevent\nsituations where multiple Kibanas restarting at the same time would roll\nover multiple times.\n\n---------\n\nCo-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>","sha":"c89ba8264f5a43c01d69b42577d79925cb020c04"}}]}] BACKPORT--> --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
|
This PR is a bit noisy, with failed auto-merged back ports, so I thought I'd be specific about the ones that actually merged: |
… changed (elastic#234119) resolves: elastic#231200 This PR adds code run at startup to check the version in the reporting index template against the `_meta.template_version` value stamped into each backing index's mappings via elastic/elasticsearch#133846 If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
resolves: #231200
This PR adds code run at startup to check the version in the reporting index template against the
_meta.template_versionvalue stamped into each backing index's mappings via elastic/elasticsearch#133846If it determines there are existing reporting indices that have not been created with the current index template version, it will roll over the reporting data stream, to ensure future indexing will use the latest mappings. This is done with the "lazy" option, which will perform the roll over on the next write to the datastream. This will prevent situations where multiple Kibanas restarting at the same time would roll over multiple times.
Note that we added a function test here, but would have preferred a jest integration test, but we can't. Instead, we expose the stand-alone-ish
rollDataStreamIfRequired()function via an HTTP route added in the function tests. Then invoke it via that route in the function tests. This allows us to set up several scenarios with real data streams, templates, and mappings, to ensure the correct behavior.to validate
Start with an old Kibana, like 8.19, which we will "upgrade" to this PR running ES with
-E path.data=to re-use the "old" resources when upgrading.Generate a report. I always load sample data, go it's dashboard, and generate a PDF.
Check the index template, and you should see a
versionless than 18. http://localhost:5601/app/management/data/index_management/templates/.kibana-reportingKill that ES and Kibana, and start them on this PR.
The logs should show the message:
[INFO ][plugins.reporting.store] Data stream .kibana-reporting has no mapping versions so rolling over the data streamChecking the index template again, and you should see the version is now 18. You should also see in the mappings
"_meta": { "template_version": 18 }Release note
Startup processing for reporting has changed to roll over the reporting data stream, if it's template version is newer than the version in the data stream's mappings. This fixes a problem when a new field added to the index template mappings in a new stack version, but the reporting data stream already exists, and the new mappings are not automatically applied. By rolling over the data stream, if required, we will ensure the latest backing index of the data stream will match the template's mappings.