This plugin is part of the Kuzzle Enterprise Plan. If you are interested, please contact us.

Following a migration #

A copy runs as a background job; an in-place change starts an Elasticsearch background reindex. This page shows how to follow both: by polling status, by subscribing to the job's progress, and through the Kuzzle logs.

Job phases #

PhaseMeaningWrites
waitingCollection write-locked, grace delay runningClosed
copyingDocuments being copied (processed / total)Closed
copiedCopy done, switch or abort nextClosed
doneNew model live, old indice archivedOpen
abortedFailed documents without allowFailures, or an error the job cannot recover from (lastError): collection unchangedOpen

Polling #

status returns the current (or last) job: phase, total, processed, failed, lastError, owner (id of the Kuzzle node running it), checkpointAt (its last checkpoint: a phase change or a batch copied), and its timestamps (createdAt, lockedAt, copiedAt, finishedAt, updatedAt). With a jobId, it returns that job only, with the plan it applied (changes), even after another job replaced it.

Every 2 to 5 seconds is enough: the job state changes at each batch (batchSize documents, about 1,000 per 25 ms on a laptop); faster polling only adds load.

A stalled copy. checkpointAt moves at every batch. A checkpointAt that stays still while the job is not finished means its node stopped, or is stuck on an error (lastError): another node takes it over within mutexTtl + resumeInterval (see Planning), and owner then changes.

lastError holds the last error. A transient one (Elasticsearch unreachable, too many requests…) is retried on its own and clears at the next checkpoint. One a retry cannot fix (Elasticsearch or Kuzzle refusing a request, the collection deleted…) ends the job aborted, with the error kept in lastError: fix the cause, then start again. Once the collection is switched to the new indice, the job cannot go back: an error there is retried until the job ends done, and writes stay closed meanwhile. A lastError that keeps coming back in that phase needs a look at Elasticsearch.

Realtime progress #

status with subscribe: true, over a WebSocket or MQTT connection, also subscribes the connection to the job's progress. Each message is the same job view as status: one at each phase change or error, and at most one every progressInterval (2 s by default) during the copy.

const { result } = await sdk.query({
  controller: "mapping-migration/migration",
  action: "status",
  subscribe: true,
});

sdk.protocol.on(result.subscription.channel, (notification) => {
  const job = notification.result._source;
  console.log(job.phase, `${job.processed}/${job.total}`);
});

// Later
await sdk.query({
  controller: "realtime",
  action: "unsubscribe",
  body: { roomId: result.subscription.roomId },
});

Over HTTP, subscribe: true is refused (subscribe_unsupported, 400): an HTTP request cannot receive the messages. Poll status instead. Subscribing to the progress room directly (realtime:subscribe on plugin-mapping-migration) is refused too (progress_subscription, 403): following a migration takes the same right as reading its state, mapping-migration/migration:status.

Logs #

The node running a job logs each step of its life through Kuzzle's logger, which prefixes the plugin's lines with [mapping-migration]; each names the job id and the node:

LevelWhenLine
infoJob startedjob <jobId> started on <index>/<collection> by node <node>: <n> change(s), writes closed
infoCopy startsjob <jobId> copying <total> documents (node <node>)
infoCopy donejob <jobId> copied: <processed> documents, <failed> failed (node <node>)
infoSwitchedjob <jobId> done on <index>/<collection>: <processed> documents copied, <failed> failed, writes closed for <s> s (node <node>)
errorAbortedjob <jobId> aborted on <index>/<collection>: <lastError, or "<n> failed documents without allowFailures">; collection unchanged, writes open (node <node>)
warnTaken overjob <jobId> on <index>/<collection> taken over by node <node> from node <previous> (phase <phase>, last checkpoint <ISO date or none>)
infoResumed after an errorjob <jobId> resumed by node <node> (phase <phase>) after: <lastError>
errorTransient error<message>; the job resumes from its last checkpoint at the next check (node <node>)
warnJob lost by a nodejob <jobId>: mutex lost, stopping (node <node>), job <jobId>: its state changed on another node, stopping (node <node>) or Lock lost for resource … (node <node>): this node stops, another one runs the job
warnA realtime message failedprogress not published: <message>: the job goes on, only that message is lost
infoReindex started (in-place change, or any collection:update reindexing a public collection)reindex task <taskId> started on <index>/<collection> (node <node>)
warnIn-place change without a reported taskin-place change applied on <index>/<collection>, but Kuzzle did not report its reindex task: it cannot be followed
warnReindex not recorded (Redis error)reindex task <taskId> of <index>/<collection> not recorded: <message>

Kuzzle itself also warns, without the prefix, when a node held the job mutex past its TTL without extending it (it was unresponsive, and another node may have taken the job over meanwhile): Lock "{mapping-migration}/job/mutex" lost: ….

Reindexes #

Changes applied in place start no job: status reports the reindex of the existing documents under reindex: the last one of the cluster, or, with index and collection (both), the last one of that collection; null if none. Kuzzle announces every background reindex of a public collection with its task id, so status also reports those that other code starts: an application's collection:update with reindexCollection: true, or one changing dynamic from false. It is read from the Elasticsearch task, from any Kuzzle node; Elasticsearch keeps a finished task's result (in its .tasks indice) until it is deleted.

reindex.stateMeaning
runningElasticsearch is still reindexing (updated / total)
doneEvery document reindexed
failedStopped at documents whose value the new mapping refuses (failures), or on an error (error): the documents after them cannot be searched on the new fields
unknownElasticsearch no longer knows the task: its node restarted while it ran, or its result was deleted
untrackedKuzzle did not report the task (a storage driver that does not tell it): the reindex runs, but cannot be followed
const { result } = await sdk.query({
  controller: "mapping-migration/migration",
  action: "status",
  index: "iot",
  collection: "sensors",
});
// result.reindex: { state: "failed", total: 50000, updated: 1000, failed: 1,
//   failures: [{ documentId: "s-42", reason: "failed to parse field [battery] of type [integer]…" }], … }

The task id comes from Kuzzle's storage driver: the built-in Elasticsearch 8 driver of Kuzzle ≥ 2.62.0 reports it.