How a migration works #
This page explains what start does with a target model: how the plan is computed, which changes apply in place and which need a copy, what the copy keeps and drops, and what is left behind (failed documents, archives).
The plan #
start compares the collection's current mapping with the full target model (mappings: properties, and optionally dynamic and _meta), the renames and the settings, and lists every change. Without apply: true, it stops there: that is the dry run.
| Change | kind | How it applies |
|---|---|---|
| New field | fieldAdded | In place |
Root dynamic or _meta changed | rootParameterChanged | In place |
| A field's type changed | fieldTypeChanged | Copy |
Another change to a field's definition (index, analyzer, ignore_above, fields…) | fieldModified | Copy |
| A field left out of the target | fieldRemoved | Copy |
A field declared in renames | fieldRenamed | Copy |
Any index setting in settings | settingsChanged | Copy |
The comparison reads definitions the way Elasticsearch returns them: type: "object" is implicit, and scalars compare as strings (false and "false", 256 and "256" are the same value). _kuzzle_info is managed by Kuzzle and never part of the plan. The full description of a change is in the start reference.
Before anything is locked, Elasticsearch checks the whole new model: a model or setting it refuses (a type typo such as "lnog", number_of_shards: 0…) fails start with invalid_target (400) and Elasticsearch's reason, and the collection stays as it is.
Renames #
A field left out of the target is removed, values included. To keep a field's values under another name, declare it in renames (old dotted path → new dotted path). The old path must be a field of the collection and no longer in the target; the new path must be a new field of the target. When a renamed object's sub-fields change too, the plan lists them under the new path: a sub-field the target leaves out is removed, like any other field.
A document where a renamed path goes through an array (location is an array of objects) cannot be renamed unambiguously: it becomes a failed document.
Fingerprint #
Each plan has a fingerprint: a SHA-256 of the collection's current mapping, the request (mappings, renames, settings) and the changes. Sent back in the body with apply: true, it makes start refuse (plan_changed, 412) a plan that differs from the one reviewed. It covers the mapping, not the index settings: a setting changed meanwhile by someone else is kept by the copy, as any setting the request does not change.
In-place changes #
When every change is in place (new fields, root dynamic / _meta), start with apply: true applies the new model through Kuzzle's collection:update with reindexCollection: true (the application's pipes and hooks on it run). There is no copy and writes stay open. The new fields are live at once, but the existing documents only become searchable on them as an Elasticsearch background task reindexes them: follow it with status.
If a document already stores a value the new field refuses (a stored but unmapped battery: "abc", then battery added as an integer), the reindex stops at that document, and the documents after it cannot be searched on the new field. status reports it (reindex.state: "failed", with the refused documents); the mapping stays applied. Fix or remove the refused values, then reindex the collection (Kuzzle's collection:update with reindexCollection: true). Before adding a field that documents may already hold, check that their values fit its type.
The copy #
Any other change copies the collection:
- The collection is write-locked cluster-wide: every write (create, update, delete, bulk…) gets
services.storage.collection_locked(503). Reads keep working on the current data. - After a grace delay (
graceDelay, 5 s by default), so that writes accepted just before the lock land first, every document is copied by batches into a new indice with the new model, writes not refreshed yet included (anyrefresh_interval,-1too). The new indice has no replica and no refresh during the copy; both are restored at the end. - The collection is switched to the new indice in one atomic step, and writes reopen. If a write still reached the old indice during the copy (a long import still running on another node past the grace delay), the switch does not happen: the job ends
aborted, nothing changes, and it can be started again.
Only one migration runs at a time across the cluster: a second start gets job_running (412). There is no cancel: a migration runs to its end. If the node running it stops, another node takes it over from its last checkpoint (see Planning).
What the copy changes, and what it does not #
- Document content is not converted. The copy sends each document's
_sourceas it is: aftercodegoes fromkeywordtolong, a document that held"code": "7"still returns"code": "7"(a string) when read. Elasticsearch indexes it as the number 7, so searches, sorts and aggregations treat it as a number, but clients reading the document get the original JSON. Convert the values yourself if clients need the new type in the content. - Removed fields are dropped from the copied documents, values included.
- Renamed fields move to their new path in each document.
- Unmapped fields are kept. A value stored but not mapped (a collection with
dynamic: false) is not a field of the model, so leaving it out of the target does not remove it: it stays in the documents. If the target setsdynamic: true, Elasticsearch maps it in the new indice; withdynamic: "strict", the document is refused (a failed document). _id,_kuzzle_infoand the rest of the mapping that is not part of the model (dynamic_templates…) are kept.- Settings: the new indice takes the collection's current settings, merged with the ones the request changes. Any change to
settingsneeds a copy, even a dynamic setting Elasticsearch could change on a live indice (refresh_interval,number_of_replicas): to change only those, use Kuzzle'scollection:updaterather than this plugin.
Failed documents #
A document fails when the new model refuses one of its values ("abc" into a long), or a rename goes through an array.
A document Elasticsearch only rejects for the moment (a busy cluster: 429, 5xx) is not a failed document: it is sent again, up to 5 times with a growing delay (retryDelay). Still rejected after that, the batch stops with a transient error in lastError, and the job resumes from its last checkpoint.
- By default the migration aborts: the copy is dropped, the collection stays as it was, writes reopen (
aborted). Fix the data, then start again. - With
allowFailures: trueonstart, it switches anyway: the failed documents are not in the new collection; they only exist in the archive until it is purged.
failures lists them, with Elasticsearch's reason, until the job is purged; the failed documents of an aborted attempt stay listed after a new attempt. To recover one, see Troubleshooting.
Archives and purge #
After a switch, the old indice is kept, outside of any collection, as an archive. It holds the old values (removed fields, failed documents) for a manual recovery through Elasticsearch, and it takes the disk space of the whole collection before the migration. status lists every archive, oldest first, with its job's id, collection, indice, plan and counts (see the archives fields).
purge (jobId) deletes a job's archive and its failed documents for good, and returns { indice }, the deleted archive indice. It also works on an aborted job, which has no archive: it then drops its failed documents list and returns { indice: null }. A job still running cannot be purged (job_running). A new migration may start before earlier archives are purged.