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

Demonstrate saved object import stalling and Kibana server becoming unresponsive #118916

Closed
wants to merge 1 commit into from

Conversation

nreese
Copy link
Contributor

@nreese nreese commented Nov 17, 2021

While working on #118617, there is a failing functional test Copy Saved Objects to Space. @jportner stated that saved object copying is just using export and import saved object APIs and we were able to produce the same problem by just importing the saved objects from the test.

@TinaHeiligers @rudolf I have opened this PR to isolate the changes from #118617 to demonstrate saved object import failure. To view the problem, run this PR and import the saved object found below. Notice how the import never finishes and the Kibana server is now unresponsive. Not sure if this is related to #117879 but sounds similar.

ndjson export of "A Dashboard" used in failing test copy_saved_objects.ts
{"attributes":{"fields":"[{\"name\":\"@message\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"@message.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"@tags\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"@tags.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"@timestamp\",\"type\":\"date\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"_id\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"_score\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_source\",\"type\":\"_source\",\"count\":0,\"scripted\":false,\"searchable\":false,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"_type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":false},{\"name\":\"agent\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"agent.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"bytes\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"clientip\",\"type\":\"ip\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"extension\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"extension.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"geo.coordinates\",\"type\":\"geo_point\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"geo.dest\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"geo.src\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"geo.srcdest\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"headings\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"headings.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"host\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"host.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"id\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"index\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"index.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"ip\",\"type\":\"ip\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"links\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"links.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"machine.os\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"machine.os.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"machine.ram\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"memory\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"meta.char\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"meta.related\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"meta.user.firstname\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"meta.user.lastname\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"phpmemory\",\"type\":\"number\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"referer\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.article:modified_time\",\"type\":\"date\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.article:published_time\",\"type\":\"date\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.article:section\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.article:section.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.article:tag\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.article:tag.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:description\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:description.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:image\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:image.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:image:height\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:image:height.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:image:width\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:image:width.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:site_name\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:site_name.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:title.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:type.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.og:url\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.og:url.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.twitter:card\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.twitter:card.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.twitter:description\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.twitter:description.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.twitter:image\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.twitter:image.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.twitter:site\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.twitter:site.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.twitter:title\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.twitter:title.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"relatedContent.url\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"relatedContent.url.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"request\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"request.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"response\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"response.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"spaces\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"spaces.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"type\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"url\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"url.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"utc_time\",\"type\":\"date\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true},{\"name\":\"xss\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":false,\"readFromDocValues\":false},{\"name\":\"xss.raw\",\"type\":\"string\",\"count\":0,\"scripted\":false,\"searchable\":true,\"aggregatable\":true,\"readFromDocValues\":true}]","timeFieldName":"@timestamp","title":"logstash-*"},"coreMigrationVersion":"8.1.0","id":"logstash-*","migrationVersion":{"index-pattern":"8.0.0"},"references":[],"type":"index-pattern","updated_at":"2018-12-21T00:43:07.096Z","version":"WzksMl0="}
{"attributes":{"description":"","kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"filter\":[],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"},"title":"A Pie","uiStateJSON":"{}","version":1,"visState":"{\"title\":\"A Pie\",\"type\":\"pie\",\"params\":{\"type\":\"pie\",\"addTooltip\":true,\"addLegend\":true,\"legendPosition\":\"right\",\"isDonut\":true,\"labels\":{\"show\":false,\"values\":true,\"last_level\":true,\"truncate\":100},\"dimensions\":{\"metric\":{\"accessor\":0,\"format\":{\"id\":\"number\"},\"params\":{},\"aggType\":\"count\"}},\"palette\":{\"type\":\"palette\",\"name\":\"kibana_palette\"},\"distinctColors\":true},\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"schema\":\"metric\",\"params\":{}},{\"id\":\"2\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"geo.src\",\"size\":5,\"order\":\"desc\",\"orderBy\":\"1\",\"otherBucket\":false,\"otherBucketLabel\":\"Other\",\"missingBucket\":false,\"missingBucketLabel\":\"Missing\"}}]}"},"coreMigrationVersion":"8.1.0","id":"75c3e060-1e7c-11e9-8488-65449e65d0ed","migrationVersion":{"visualization":"8.0.0"},"references":[{"id":"logstash-*","name":"kibanaSavedObjectMeta.searchSourceJSON.index","type":"index-pattern"}],"type":"visualization","updated_at":"2019-01-22T19:32:31.206Z","version":"WzEyLDJd"}
{"attributes":{"description":"","hits":0,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"filter\":[]}"},"optionsJSON":"{\"darkTheme\":false,\"useMargins\":true,\"hidePanelTitles\":false}","panelsJSON":"[{\"version\":\"7.3.0\",\"type\":\"visualization\",\"gridData\":{\"w\":24,\"h\":15,\"x\":0,\"y\":0,\"i\":\"1\"},\"panelIndex\":\"1\",\"embeddableConfig\":{\"enhancements\":{}},\"panelRefName\":\"panel_1\"}]","timeRestore":false,"title":"A Dashboard","version":1},"coreMigrationVersion":"8.1.0","id":"my-dashboard","migrationVersion":{"dashboard":"8.1.0"},"references":[{"id":"75c3e060-1e7c-11e9-8488-65449e65d0ed","name":"1:panel_1","type":"visualization"}],"type":"dashboard","updated_at":"2019-01-22T19:32:47.232Z","version":"WzEzLDJd"}
{"excludedObjects":[],"excludedObjectsCount":0,"exportedCount":3,"missingRefCount":0,"missingReferences":[]}

@nreese nreese changed the title Demonstrate saved object import stalling Demonstrate saved object import stalling and Kibana server becoming unresponsive Nov 17, 2021
@kibana-ci
Copy link
Collaborator

kibana-ci commented Nov 17, 2021

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] Default CI Group #18 / apis Maps endpoints map migrations embeddable migrations should apply embeddable migrations
  • [job] [logs] Default CI Group #18 / apis Maps endpoints map migrations embeddable migrations should apply embeddable migrations
  • [job] [logs] Default CI Group #19 / dashboard drilldowns Dashboard to dashboard drilldown Copy to space "after all" hook for "Dashboards linked by a drilldown are both copied to a space"
  • [job] [logs] Default CI Group #19 / dashboard drilldowns Dashboard to dashboard drilldown Copy to space "after all" hook for "Dashboards linked by a drilldown are both copied to a space"
  • [job] [logs] Default CI Group #19 / dashboard drilldowns Dashboard to dashboard drilldown Copy to space Dashboards linked by a drilldown are both copied to a space
  • [job] [logs] Default CI Group #19 / dashboard drilldowns Dashboard to dashboard drilldown Copy to space Dashboards linked by a drilldown are both copied to a space
  • [job] [logs] Jest Tests #7 / saved object migrations and embeddable migrations should have same versions registered (>7.12)
  • [job] [logs] Default CI Group #4 / Spaces app Copy Saved Objects to Space "after all" hook for "allows a dashboard to be copied to the marketing space, with circular references"
  • [job] [logs] Default CI Group #4 / Spaces app Copy Saved Objects to Space "after all" hook for "allows a dashboard to be copied to the marketing space, with circular references"
  • [job] [logs] Default CI Group #4 / Spaces app Copy Saved Objects to Space allows a dashboard to be copied to the marketing space, with all references
  • [job] [logs] Default CI Group #4 / Spaces app Copy Saved Objects to Space allows a dashboard to be copied to the marketing space, with all references

Metrics [docs]

✅ unchanged

History

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

@rudolf
Copy link
Contributor

rudolf commented Nov 17, 2021

Seems like migrations go into an endless loop because the document migrator never sets a coreMigrationVersion

For the visualization and index-pattern we set coreMigrationVersion in:
https://github.com/elastic/kibana/blob/main/src/core/server/saved_objects/migrations/core/document_migrator.ts#L771

But for the dashboard it stays undefined so getHasPendingCoreMigrationVersionTransform always returns true
https://github.com/elastic/kibana/blob/main/src/core/server/saved_objects/migrations/core/document_migrator.ts#L708-L711
https://github.com/elastic/kibana/blob/main/src/core/server/saved_objects/migrations/core/document_migrator.ts#L737

and we never break out of the while loop:
https://github.com/elastic/kibana/blob/main/src/core/server/saved_objects/migrations/core/document_migrator.ts#L493

I've always felt like a while(true) loop is just testing fate 😝

I think what happens is if any object has a migrationVersion higher than the reference migration version (8.0.0) then we always break here
https://github.com/elastic/kibana/blob/main/src/core/server/saved_objects/migrations/core/document_migrator.ts#L759

so we never get to doc.coreMigrationVersion = version; which then causes an endless loop

@nreese
Copy link
Contributor Author

nreese commented Nov 17, 2021

@rudolf Thanks for investigating. What are the next steps to resolve this problem?

@pgayvallet
Copy link
Contributor

Seems like migrations go into an endless loop because the document migrator never sets a coreMigrationVersion

The thing is, the document in the import file does specify "coreMigrationVersion": "8.1.0"

When collecting the objects in importSavedObjectsFromStream (src/core/server/saved_objects/import/import_saved_objects.ts), this value is present:

*** importSavedObjectsFromStream [
  {
    migrationVersion: { dashboard: '8.1.0' },
    attributes: { /* */ },
    coreMigrationVersion: '8.1.0', // <---
    id: 'my-dashboard',
    references: [ [Object] ],
    type: 'dashboard',
    updated_at: '2019-01-22T19:32:47.232Z',
    version: 'WzEzLDJd'
  }
]

But when reaching applyMigrations in the document migrator, the value of doc.coreMigrationVersion is undefined.

I tracked down where we're loosing this value, and it's inside the call of SavedObjectRepository.bulkCreate, where we're not passing coreMigrationVersion during the raw serialization of the document:

rawMigratedDoc: this._serializer.savedObjectToRaw(
this._migrator.migrateDocument({
id: object.id,
type: object.type,
attributes: object.attributes,
migrationVersion: object.migrationVersion,
...(savedObjectNamespace && { namespace: savedObjectNamespace }),
...(savedObjectNamespaces && { namespaces: savedObjectNamespaces }),
updated_at: time,
references: object.references || [],
originId: object.originId,
}) as SavedObjectSanitizedDoc
),
};
bulkCreateParams.push(

(Note: coreMigrationVersion is also omitted in the serialization performed in SavedObjectRepository.create)

I think this needs to be fixed, but I'm not sure 100% this will be sufficient, as when importing documents from older versions, coreMigrationVersion will be undefined anyway. However these older versions will probably have migrations registered between the source's version and the version we added coreMigrationVersion, so maybe fixing the serialization in the SO repository is sufficient?

cc @jportner we'll need your insight here given you're the one who implemented this feature.

@jportner
Copy link
Contributor

jportner commented Dec 1, 2021

I believe I found the problem.

Inside the document migrator, it takes all applicable types of transforms ("reference", "convert", and "migrate") and loops over them, applying each one to the saved object.

The "reference" transforms are applied based on the object's coreMigrationVersion, while the other two are applied based on the object's migrationVersion.
The logic here is tangled up because we want to make sure that all types of transforms for a given version are applied in that order.

Anyway, we technically support consumer-defined migrations that increase the object's migrationVersion, effectively allowing you to define an earlier migration that leapfrogs a later one. There is some logic in the loop that breaks out of the loop if the object's current migrationVersion is greater than the version of the given transform function. Context: #80945 (comment)


Edit: the comment chain on that PR is too long, so this link doesn't actually show you the discussion -- here's a screenshot:
image
and here's the linked commit: e27e6be


That logic incorrectly broke out of the loop for "reference" transforms. It should only break out of the loop for the other two types of transforms. This meant that in a certain situation where a saved object's migrationVersion was greater than any other object type's registered convertToMultiNamespaceTypeVersion, and the saved object had coreMigrationVersion: undefined, we would detect that we have a "reference" transform to apply, and we would start a loop, but then we would never actually apply the "reference" transform.

I tested this solution locally and it seems to do the trick:

diff --git a/src/core/server/saved_objects/migrations/core/document_migrator.ts b/src/core/server/saved_objects/migrations/core/document_migrator.ts
index da16dbc5e69..b405497eeed 100644
--- a/src/core/server/saved_objects/migrations/core/document_migrator.ts
+++ b/src/core/server/saved_objects/migrations/core/document_migrator.ts
@@ -754,7 +754,7 @@ function migrateProp(
 
   for (const { version, transform, transformType } of applicableTransforms(migrations, doc, prop)) {
     const currentVersion = propVersion(doc, prop);
-    if (currentVersion && Semver.gt(currentVersion, version)) {
+    if (currentVersion && Semver.gt(currentVersion, version) && transformType !== 'reference') {
       // the previous transform function increased the object's migrationVersion; break out of the loop
       break;
     }

We intentionally omit coreMigrationVersion in create/bulkCreate because we don't need it there. When objects are created, the standard document transformation should ensure that the object has the latest coreMigrationVersion set before it is saved to Elasticsearch, and we only apply "reference" transformations that rely on this during Index Migrations.

@jportner
Copy link
Contributor

jportner commented Dec 1, 2021

I created an issue to track this bug: #120129

I'll submit a PR to fix it and link it to that issue.

@rudolf rudolf closed this Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants