Getting started #
The mapping migration plugin applies a new model (mapping and index settings) to an existing Kuzzle collection, field type changes and removals included, without taking the platform down. New fields are applied in place; any other change copies the collection into the new model, with its writes closed for the time of the copy, then switches to the copy in one atomic step.
This page takes you through a first migration in six steps. The other guides explain how a migration works, how to plan and follow one, and the API gives every argument.
Requirements #
| Kuzzle | ≥ 2.62.0 (the first release announcing the reindex task of a collection update) |
| Storage | Elasticsearch 8 only. On Elasticsearch 7, Kuzzle starts, but every action of the plugin is refused with unsupported_engine. |
| Node.js | ≥ 20.19.0, < 25 |
| Cluster | Redis, as for any Kuzzle cluster: the job state, its mutex and the collection's write lock live there. |
1. Install #
The package is published on GitHub Packages. Add an .npmrc next to your application's package.json, the token read from an environment variable rather than written in the file:
@kuzzleio:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NPM_TOKEN}NPM_TOKEN is a GitHub token allowed to read the kuzzleio packages (read:packages). Then install the plugin:
NPM_TOKEN=<token> npm install @kuzzleio/plugin-mapping-migrationRegister it in your application:
import { Backend } from "kuzzle";
import { MappingMigrationPlugin } from "@kuzzleio/plugin-mapping-migration";
const app = new Backend("my-app");
app.plugin.use(new MappingMigrationPlugin());
await app.start();The plugin adds the mapping-migration/migration controller. Its configuration is optional, see Configuration.
2. Rights #
Give the people who run migrations the mapping-migration-admin role, and those who follow them the mapping-migration-viewer role. Both are ready to paste in Rights and roles:
await sdk.security.createOrReplaceRole("mapping-migration-admin", {
controllers: {
"mapping-migration/migration": {
actions: { failures: true, purge: true, start: true, status: true },
},
collection: { actions: { getMapping: true, update: true } },
document: { actions: { search: true } },
},
});The plugin's actions are not enough on their own: the caller also needs, on the collection being migrated, the right the same change would take through Kuzzle's API (collection:update to apply a migration). Calls made by the application's own code (embedded SDK, no user) are not checked.
3. Dry run #
start takes the full target model of the collection: a field left out is a field removed. Without apply: true, start is a dry run: it returns the plan and changes nothing.
const mappings = {
properties: {
code: { type: "long" }, // was a keyword
name: { type: "keyword" },
place: { properties: { city: { type: "keyword" } } },
},
};
const renames = { location: "place" }; // keep the values of a renamed field
const { result: plan } = await sdk.query({
controller: "mapping-migration/migration",
action: "start",
index: "iot",
collection: "sensors",
body: { mappings, renames },
});
console.log(plan.copyRequired, plan.changes, plan.fingerprint);Read every change of plan.changes (see change kinds). With copyRequired: true, applying the plan closes the collection's writes for the time of a copy: about 30 seconds for a million documents on a laptop, see Planning.
4. Apply #
Send the same request with apply: true, and the dry run's fingerprint in the body: if the plan computed now is not the one you reviewed (the collection's mapping or the request changed since), start refuses with plan_changed and changes nothing.
const { result } = await sdk.query({
controller: "mapping-migration/migration",
action: "start",
index: "iot",
collection: "sensors",
apply: true,
body: { mappings, renames, fingerprint: plan.fingerprint },
});
const jobId = result.job?.jobId; // set when a copy started- New fields only (
copyRequired: false):applied: true, the new model is live, writes stay open.result.reindexis the background reindex of the existing documents. - A copy (
copyRequired: true):applied: false, andresult.jobis the job just started. The collection's writes are refused (services.storage.collection_locked, 503) until it ends; reads keep working. See Client applications to handle it.
5. Follow #
Poll status with the job id until its phase is done or aborted:
let job;
do {
await new Promise((resolve) => setTimeout(resolve, 3000));
({ result: { job } } = await sdk.query({
controller: "mapping-migration/migration",
action: "status",
jobId,
}));
console.log(job.phase, `${job.processed}/${job.total}`, job.failed);
} while (job.phase !== "done" && job.phase !== "aborted");done: the new model is live, writes are open, the old indice is kept as an archive.aborted: the collection is unchanged and writes are open. Readjob.lastError, and the failed documents with failures, fix the data or the model, then start again from step 3.
For an in-place change, follow the reindex with status and the same index and collection until reindex.state is done. Realtime progress, logs and the reindex are described in Following a migration.
6. Purge #
Once the new model is checked, delete the archive, which holds the old values (removed fields, failed documents) for good:
const { result } = await sdk.query({
controller: "mapping-migration/migration",
action: "purge",
jobId,
});
// result.indice: the deleted archive indiceUntil then, the archive takes as much disk space as the collection before the migration.