Skip to content
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

[Saved Objects] Fix document migrator to prevent losing namespaces on import #164848

Merged
merged 5 commits into from
Sep 19, 2023

Conversation

dokmic
Copy link
Contributor

@dokmic dokmic commented Aug 25, 2023

Summary

Resolves #164454.

Checklist

For maintainers

@dokmic dokmic added review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:Saved Objects release_note:skip Skip the PR/issue when compiling release notes Feature:Migrations v8.11.0 labels Aug 25, 2023
Copy link
Contributor

@rudolf rudolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on Pierre's comment that create works but import doesn't I didn't expect that the problem was in the internal transform. Although he tested create not bulk_create, it seemed to me like there must be some difference in the payload we pass to bulkCreate from /api/saved_objects/_import vs /api/saved_objects/_bulk_create

https://github.com/elastic/kibana/blob/main/packages/core/saved-objects/core-saved-objects-import-export-server-internal/src/import/lib/create_saved_objects.ts#L123-L127
https://github.com/elastic/kibana/blob/main/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_create.ts#L80-L83

I think it would also be useful to add an API integration test against the _import API for this case.

@dokmic
Copy link
Contributor Author

dokmic commented Aug 29, 2023

@rudolf The problem is that we set typeMigrationVersion in case it's undefined to '':

...(!obj.migrationVersion && !obj.typeMigrationVersion ? { typeMigrationVersion: '' } : {}),

Before typeMigrationVersion, we were using {}, which was for the same purpose — explicitly triggering all the migrations.

In other words, if we pass "typeMigrationVersion": "" in a (bulk) create payload, we get the same issue. But adding a test for the import endpoint makes total sense 👍

@dokmic
Copy link
Contributor Author

dokmic commented Sep 14, 2023

After some investigation of the nature of the conversion transforms, I found out that there is some inconsistency in conversion transforms. If we import objects with types that have convertToMultiNamespaceTypeVersion, and there is a type migration with a version higher than in convertToMultiNamespaceTypeVersion, then the conversion transforms will never be applied.

After discussing with @jeramysoucy and digging into the commit history, I found the reason for deferring the conversion transforms. The idea is that the newly created objects or imported should not have legacy-url-alias, but the ones that migrated from the previous version should have that. This is why migrateAndConvert is called during the re-indexing only. That should probably not be called on Kibana restart, but it's not critical as long as we handle namespace migrations correctly.

After a few attempts at fixing this by changing the flow to apply conversion transform on all the input documents (newly created or imported), that creates unnecessary legacy-url-alias objects. Ignoring these objects makes using those conversion transforms redundant, as we already handle namespace conversion upon object creation. Changing the latter requires more work and additional changes in the test suite.

The best approach would be not to fix this inconsistency at this point, as the convertToMultiNamespaceTypeVersion parameter is already deprecated, and we can safely remove the conversion transform along with the parameter.

With regard to an additional test covering the _import endpoint covering the difference with the bulk create, it cannot be tested with the already existing saved object types as the setup will be too brittle and fail whenever the type introduces another migration. Also, migrator tests are already covering this behavior, so it doesn't make much sense to add this now. However, I have added a test covering the resolution of this particular bug.

cc @pgayvallet @rudolf

@dokmic dokmic marked this pull request as ready for review September 14, 2023 15:43
@dokmic dokmic requested a review from a team as a code owner September 14, 2023 15:43
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

References to deprecated APIs

id before after diff
@kbn/core-saved-objects-migration-server-internal 27 28 +1

History

  • 💚 Build #154038 succeeded ad5ce9dc34d186c7327913ce143c1f242bad0730
  • 💔 Build #153562 failed 5cfa68244e35327a6897dab7eb43cdd6447b086f

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@gsoldevila gsoldevila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

I tested the scenario described in https://github.com/elastic/sdh-kibana/issues/4101.

Without the fix, the Sample Data Logs dataset gets corrupted upon restart (3 saved objects are moved to the default space).
With the fix, all SO preserve their namespace upon restart.

@dokmic dokmic merged commit ac73d1f into elastic:main Sep 19, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Sep 19, 2023
@dokmic dokmic deleted the bugfix/164454 branch September 19, 2023 10:38
@gsoldevila gsoldevila added backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) and removed backport:skip This commit does not require backporting labels Sep 27, 2023
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Sep 27, 2023
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.10

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Sep 27, 2023
…aces on import (#164848) (#167372)

# Backport

This will backport the following commits from `main` to `8.10`:
- [[Saved Objects] Fix document migrator to prevent losing namespaces on
import (#164848)](#164848)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Michael
Dokolin","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-09-19T09:18:45Z","message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["review","Team:Core","Feature:Saved
Objects","release_note:skip","Feature:Migrations","backport:prev-minor","v8.11.0"],"number":164848,"url":"https://github.com/elastic/kibana/pull/164848","mergeCommit":{"message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/164848","number":164848,"mergeCommit":{"message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4"}}]}]
BACKPORT-->

Co-authored-by: Michael Dokolin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Feature:Migrations Feature:Saved Objects release_note:skip Skip the PR/issue when compiling release notes review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.10.3 v8.11.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Namespaces can be lost when importing multi-NS objects
6 participants