Client applications #
While a copy runs, the collection refuses writes. This page shows how a client can wait them out, and how to call the plugin without the SDK, over HTTP.
Writes refused during a copy #
From the lock to the switch, every write on the collection (create, update, delete, bulk…) is refused by Kuzzle with:
| Field | Value |
|---|---|
id | services.storage.collection_locked |
status | 503 |
message | The collection "iot":"sensors" is locked by "mapping-migration" (mapping migration <jobId>): write actions are rejected until it is unlocked. |
Reads (get, search, count…) keep working on the current data. The error is temporary: retry the write later. A write the client cannot hold back (a device report…) can be retried with a growing delay, long enough to cover the expected window (see Planning):
/**
* Runs a write, and retries it while the collection is locked by a
* migration: 1 s, 2 s, 4 s… up to 30 s between attempts, 5 minutes overall
*/
async function retryWhileLocked<T>(
write: () => Promise<T>,
{ maxWait = 300_000 } = {},
): Promise<T> {
const deadline = Date.now() + maxWait;
for (let delay = 1000; ; delay = Math.min(delay * 2, 30_000)) {
try {
return await write();
} catch (error) {
// The SDK rejects with a KuzzleError, which carries the API error id
if (
(error as { id?: string })?.id !== "services.storage.collection_locked" ||
Date.now() + delay > deadline
) {
throw error;
}
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
await retryWhileLocked(() =>
sdk.document.create("iot", "sensors", { code: 7, name: "s-7" }),
);Match on id, not on status: other errors are 503 too (api.process.not_enough_nodes, Kuzzle overloaded…). Keep the retries bounded, so that a client never piles up writes for a migration that takes longer than planned; for large imports, pause them while a migration runs instead.
HTTP #
Every action can be called over WebSocket, MQTT or HTTP. Over HTTP, the actions that change data are POST, so that a cache, a proxy or a followed link never starts or purges a migration:
| Action | Route | Arguments |
|---|---|---|
| start | POST /_/mapping-migration/migration/start | query string: index, collection, apply, allowFailures; JSON body: mappings, renames, settings, fingerprint |
| status | GET /_/mapping-migration/migration/status | jobId; or index, collection |
| failures | GET /_/mapping-migration/migration/failures | jobId, from, size, after |
| purge | POST /_/mapping-migration/migration/purge | jobId |
# Dry run
curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
"http://localhost:7512/_/mapping-migration/migration/start?index=iot&collection=sensors" \
-d '{ "mappings": { "properties": { "code": { "type": "long" } } } }'
# Apply the reviewed plan
curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
"http://localhost:7512/_/mapping-migration/migration/start?index=iot&collection=sensors&apply=true" \
-d '{ "mappings": { "properties": { "code": { "type": "long" } } }, "fingerprint": "<fingerprint>" }'
# Follow
curl -H "Authorization: Bearer $TOKEN" \
"http://localhost:7512/_/mapping-migration/migration/status?jobId=<jobId>"Over HTTP, apply, allowFailures and subscribe take true (or the bare flag, ?apply) or false; any other value is refused (invalid_flag, 400). Unlike Kuzzle's own HTTP booleans, ?apply=false is a dry run.
subscribe: true is refused over HTTP (subscribe_unsupported): an HTTP request cannot receive realtime messages. Poll status instead.
The routes, their arguments and their error ids are also described in the application's OpenAPI definition: GET /_openapi?scope=app (server:openapi).