modules/opensearch/examples/search/administration.ts. Administrative commands follow the handle and lease rules in Index administration: a step refused with SearchAdminBusy or SearchAdminLeaseLost can be run again as is. A step that has a sonamu search command shows it as CLI:. The commands, their approval flags and their output are under “Running administration from the CLI” in Index administration; the examples omit the logical key, which an application with several keys must add. pnpm sonamu search inspect prints inspect() together with the store binding.
Copied and restored databases
When the store (thesonamu_search schema) is installed, it records the origin of the database it lives in: the system identifier (when the role can read it), the database OID, and the name. When a target is created, the store identity is stamped into the index _meta. The worker and every administrative operation that sends remote requests check at the start that they are connected to that origin. If not, they stop with SearchStoreMismatch before any remote request.
Marking (
markChanged), initialize() and search do not run this check, so they do not stop. On an unconfirmed copy, marking keeps piling up intents, and search reads the index named by the copied read pointer; if the copy points at the source’s cluster, that is the source’s index. The worker shows the mismatch as an error event (code SEARCH_STORE_MISMATCH, reason) and the degraded state, and retries at the configuration-error interval (retryMaxDelayMs). The administrative handle’s inspect().binding shows the same state.
What is detected
A physical copy carries the system identifier, OID and name unchanged, so the recorded origin cannot tell it from the source. The target fence covers that case: the owning store advances the version of a reserved document in each target’s index, and a store that finds the fence moved by someone else stops sending to that target. The fence detects but does not block, so writes the losing side sent before and right after detection stay in the index. How the fence works and how much the losing side still writes are under “Target fence” in Indexing worker. The safest course is still never to point a physical copy at the source’s OpenSearch cluster.
A failover to an asynchronous replica that loses the last renewal commit leaves the remote fence one epoch ahead, with a nonce this database never recorded, so the fence is lost as
behind. This follows from the protocol; no test reproduces it. If the role may not call pg_control_system(), only the OID and the name are compared (details.systemIdentifierReadable is false).
Two consequences of the schema’s version history:
installSearchSchemaapplies every missing version in one call (Setup).- The call that upgrades from version 1 records the database it runs in as the origin. A copy made while the schema was at version 1 and upgraded on its own therefore records its own origin and is not detected as a copy afterwards. Run
adoptCopiedStoreon such a copy right after upgrading it. Running the installation again on a schema already at version 2 does not record the origin again.
Adopting a copy as its own store
Adopt a database that will live apart from its source, such as a staging or developer copy, before any application process starts on it. Before you start:- Adoption cannot be undone. It gives this database a new store identity and marks every target it inherited
detached. Detached targets belong to the source: no operation applies to them in this database, retirement included (target-detached), so manage them from the source. - From adoption until step 4 finishes, search on this database returns
SearchRebuildRequired: adoption suspends capture and detaches the read pointer. - Adoption takes the lease from any administrative operation in progress on this database; that operation’s next write commits nothing. It applies to the whole store, not to one namespace.
search/administration.ts
-
Stop every server and worker that uses the copy.
Check:
inspect().workerslists no worker with a recent heartbeat. If one remains, find and stop it before going on. -
Run
adoptCopiedStore({ confirmCopy: true }, audit). In one transaction it creates a new store identity, records the origin again, suspends capture for every index, detaches the read pointer, marks the inherited targetsdetached, and closes open builds as failed. It sends no remote request, so the source’s indexes are untouched. CLI:pnpm sonamu search adopt-store --execute --confirmCheck:inspect().bindingshows a newstoreId, and the result’sdetachedTargetslists the inherited targets. If the call fails, run it again; called again with the sameoperationId, it changes nothing and returns the same result. -
Start the processes again. A process that called
initialize()before the adoption, or pinned the old identity withexpectedStoreId, has everymarkChangedrefused withSearchStoreMismatch(store-mismatch) until it restarts, so its domain transactions do not commit. Callinginitialize()again does not change the pinned value. WithexpectedStoreId, set the new identity before starting. Check: domain writes commit. If they fail withstore-mismatch, a process still holds the old identity; restart it. -
Create this database’s own index with the same flow as “Source database restore” below (resume capture, backfill, cut over); the example calls
restoreFromSource. CLI:pnpm sonamu search resume --execute --confirm, thenpnpm sonamu search backfill --execute --confirmandpnpm sonamu search cutover --execute --confirm. Check: all three values ininspect().bindingaretrue,storeIdhas changed, and search works on the new target. If cutover refuses, its blockers are explained in Index administration.
Confirming that this is the same store
Use this when this database is the recorded store itself but its origin no longer matches: for example after it was dropped, recreated and restored under the same name, or moved to another server whose old server was retired. Confirming ownership reopens remote operations. Before you start: no other database may use this store. Running this on a copy lets the copy write to and delete the source’s indexes. Forstore-origin-unrecorded on the same store, this procedure records the origin.
search/administration.ts
- Confirm that no other database uses this store. Check: every other database that pointed at the same OpenSearch cluster is stopped or adopted. If you cannot confirm it, stop here; if this database is a copy, adopt it instead.
-
Run
confirmStoreOwnership({ confirmSoleOwner: true }, audit). It acquires every admin lease, records the origin (andinstanceName) again, and leaves the store identity and targets as they are. Then, for each target that may have a remote index (creating,active,paused,retiring), it takes the fence back one epoch above the remote and returns the new epochs infencedTargets. CLI:pnpm sonamu search confirm-ownership --execute --confirmCheck: the call returns. If it refuses withsearch-client-not-configured, nothing changed: such targets exist and the operation writes to OpenSearch, so run it from an administrative handle with an OpenSearch client. If it refuses withSearchAdminBusy, another administrative operation’s lease is alive and nothing changed; run it again later. -
Check the result: all three values in
inspect().bindingaretrue, and the worker’s next drain emits adrainevent instead oferror.
Environment names
The synchronization handle’sinstanceName can carry a different name per environment (production, staging, a developer copy), set from environment configuration. adoptCopiedStore and confirmStoreOwnership record the name in the store; after that, a process configured with a different name stops remote work with store-instance-mismatch. If either side has no name, no comparison is made. If the instanceName passed to an operation differs from the one configured on the handle, the operation is refused. A name fixed in code is the same in every environment and guards nothing.
A lost target fence
This database lost a target’s fence. Another database byte-identical to this one (a physical copy) is writing to the same OpenSearch index, or the index was recreated or restored from an older snapshot, or a failover lost the last renewal. Domain writes and marking continue, and search returns the last state of that index. The worker sends nothing to that target and keeps indexing the others.
Before you start: decide which database is the rightful owner (step 2) and stop the other database’s processes (step 3) before you take the fence back in step 5. Taking it back while they run starts a tug of war in which the two databases keep taking the fence from each other.
-
Recognize the loss. Look at the worker’s
target-fence-lostevents (targetId,class,localEpoch,remoteEpoch),status().fenceLostTargets, andfence.lostandfence.lostDetailof the target ininspect. Cutover, rollback,resumeTargetandretireTargeton that target refuse withSearchStoreMismatch(target-fence-lost). Check: you have the target ID and the loss class. If all three values ininspect().bindingaretrueand a fence is still lost, the other database is one the origin check cannot tell apart. - Decide which database is the rightful owner. If the copy advanced first, the production database is the one that lost, so do not assume the losing side is the copy.
-
Find and stop every process of the other database that uses the same OpenSearch cluster.
Check: the other database’s
inspect().workerslists no worker with a recent heartbeat. -
On the copy, run
adoptCopiedStore, or move the copy to another cluster. After adoption, every process of the copy must be restarted (“Adopting a copy as its own store” above). CLI, on the copy:pnpm sonamu search adopt-store --execute --confirmCheck: on the copy,inspect().bindingshows a newstoreId. -
On the rightful owner, take the fences back with
confirmStoreOwnership. It raises the fence of each target that may have a remote index to one epoch above the remote, so it needs an administrative handle with an OpenSearch client. The example does nothing and returns an empty list when no fence is lost. CLI:pnpm sonamu search confirm-ownership --execute --confirm. Unlike the example, the command takes the fences back even when none is lost. Check:fencedTargetslists each lost target with its new epoch, and workers send to it again on their next drain. If it refuses withSearchAdminPreconditionFailed(fence-takeover-contended), the other side moved the fence in all three attempts: something still writes, so go back to step 3. If it fails with a retryableSearchUnavailable(fence-unreadable: the remote could not be read;fence-takeover-unknown: the write’s outcome is unknown), call it again with the same input; it continues the takeover.
search/administration.ts
-
If the other database ever wrote to this index (
behind,foreign) or the index lost content (regressed), rebuild the logical key (beginRebuild→ backfill →cutover→retireTarget). The copy’s versions may be ahead of this ledger, soremarkmay not be enough. CLI:pnpm sonamu search init --execute --confirmopens the rebuild build; thenpnpm sonamu search backfill --execute --confirm,pnpm sonamu search cutover --execute --confirm, andpnpm sonamu search retire-old --execute --confirmafter the grace period. Check: the read pointer names the new target. -
Check the result: in
inspect, every target hasfence.lostfalseandpendingfalse,renewedAgeMsis below the renewal interval, and the backlog shrinks. No newtarget-fence-lostevents appear.
What is not detected
- Equal-epoch twins (a physical copy and its source starting from the same epoch) both write until one advances. The window is at most about 1.25 ×
fenceRenewIntervalMsplus one drain after the later one starts sending, and whichever advances first keeps the index. That can be the copy. - While neither side has a running worker, nothing is detected. Only a worker’s renewal moves the fence, so the epochs stay equal.
- A destructive administrative step a twin runs inside that window (for example
retireTarget) passes the fence check. A copy with a differentinstanceName, or an adopted copy, is refused earlier by the other checks; a twin with identical configuration is not. - Bulks a verified loser had already sent before handling the loss are written (at most
maxConcurrentBulkRequestsper worker). - If the index is restored from an older snapshot that contains the fence document while some process keeps sending, the fence action of the next bulk raises the fence again and the loss goes unnoticed, along with the documents missing from that snapshot. When the fence document is missing too (the index was recreated), the loss is detected as
regressed. - A failover to an asynchronous replica that loses the last renewal commit produces a false loss (
behind). Take the fence back withconfirmStoreOwnership. - An operator running
confirmStoreOwnershipon the copy, searches on an unadopted copy, and writes to the index from outside the package are not prevented.
Quarantined documents
A revision of a document exhausted its retry budget, or was quarantined at once for a document-caused error. Before you start: fix the cause (step 2) before you send again; otherwise the document is quarantined again.-
Find the quarantined documents. Look at the worker’s
quarantinedevents (targetId,documentId,revision,errorClass) andquarantinedin drain reports.backlog.quarantinedof the target ininspectshows the count, anddelivery_runtime.last_errorkeeps the cause as a stable code. Check: you have the target ID, the document IDs, and the cause. -
Fix the cause. If the mapping rejected the document, fix the definition and rebuild. If the projection is at fault, deploy the fix; if the projection’s meaning changed, raise
projectionVersionand rebuild. If the source data is the problem, fix the data. -
Send the documents again. To resend the same revision with a fresh budget, use
retryQuarantined: its scope is one target, narrowed bydocumentIdsandlimit, and it changes no intents, ledger rows or other documents. CLI:pnpm sonamu search retry-quarantined --target <target-id> --execute --confirm, narrowed with--ids <document-id>(repeat the flag per document).
search/administration.ts
markChanged, or remark by ID); the new revision is delivered with a fresh budget.
Check: in the next drain the document counts as indexed and no new quarantine event appears. If it is quarantined again, the cause is not fixed; go back to step 2.
- Check the result: the target’s
backlog.quarantinedreaches 0. During a build it must reach 0 before cutover. A large pile of quarantined intents slows every claim, so do not leave them for long.
A target paused for revision_divergence
While checking a version conflict, the worker found the index and the ledger out of step: an indexed version ahead of the ledger, behind the revision sent, or the document in another index. Either the ledger went backwards (a database restore) or something outside the package wrote into the index. The target keeps receiving intents and stays readable, but indexing stops. Before you start: if the database was restored, many documents are ahead of the ledger and this procedure cannot fix it; follow “Source database restore” below. If another database writes to the same index, cut it off first.-
Read the target’s
pauseReasonwithinspect(logicalKey). Check: itsclassisrevision_divergence, anddocumentslists{ id, indexed, ledger }for up to 20 documents. - Find the cause, using the precondition above.
-
For a few documents, raise their ledger revision with
remark. Eachremarkcall raises the revision by one, so a document whose indexed version is still above the ledger remains a violation. CLI:pnpm sonamu search remark --ids <document-id> --execute --confirm; each run raises the revision by one. -
Run the verification scan to the end. The scan compares the version of every indexed document with the current ledger.
CLI:
pnpm sonamu search verify --target <target-id> --execute --confirmCheck: the last page reportsdonewithviolations0. If violations remain, the example returns them; go back to step 3, or rebuild if there are too many. If the scan refuses withverification-not-quiescedorverification-raced-writes, see the rules below. -
Resume the target with
resumeTarget. The example runs steps 4 and 5 together. CLI:pnpm sonamu search resume-target --target <target-id> --execute --confirm
search/administration.ts
inspect the target is active, it disappears from pausedTargets in the worker’s status(), and its backlog shrinks. If resumeTarget refuses, the reasons are listed below.
Verification and resume rules:
- The scan starts once the leases and requests from before the pause can be considered finished. Until then it refuses with
verification-not-quiesced(retryable); call it again a little later. - At the start the scan records each shard’s last operation number. After
_refreshit opens one point in time and reads the documents of that moment once each, in shard order. It never rescans earlier pages, so its cost is linear in the number of documents: on OpenSearch 3.5.0, measured on one machine, 200,000 documents took 451 ms. - If the operation numbers have changed by the last page, the index was written during the scan. The scan is reset to the start and refused with
verification-raced-writes(retryable). Stop whatever writes to the index, then run the scan again. - Progress is kept in the database, so another process or another handle can continue the scan. If the PIT has expired, the scan starts over within the same call. Long gaps between calls let the PIT expire, so call the pages back to back.
- A partial result from shard failures or a timeout ends the scan as failed, and so does a cluster that disallows PIT or sorting on
_shard_doc. resumeTargetchecks the target’s state and epoch and its remote identity (_meta, write alias), and forrevision_divergencerequires a clean scan that started after the pause and ran to the end. Its refusals aretarget-not-paused(with a message saying nothing was changed),target-epoch-invalid,verification-incomplete,verification-stale(the scan is older than the pause), andverification-violations.- If violations remain, the target cannot be resumed. The remaining option is a rebuild.
A target paused for write_alias_missing
A bulk gotindex_not_found_exception (404) from the write alias: the alias or the index was deleted outside the package. The package recreates neither, so that it never revives a name that may be retired or belong to something else.
Before you start: you need your own OpenSearch tooling with administrator rights, because the operator, not the package, restores the alias.
-
With
inspect, confirm that the target’spauseReason.classiswrite_alias_missing, and note its physical index and write alias names. -
Check whether the physical index still exists and its
_metanames this target. If the index is gone, go to step 5. -
With your own OpenSearch tooling, recreate the write alias pointing at that index alone, as its write index. In the example,
operatorClientis an SDK client with administrator rights, and its single request stands for that tooling. -
Call
resumeTarget. This cause needs no verification scan;resumeTargetresumes only after confirming that_metamatches and the alias points at exactly this one index as its write index. CLI:pnpm sonamu search resume-target --target <target-id> --execute --confirm
search/administration.ts
active and its backlog shrinks. If the alias is missing or points elsewhere, resumeTarget refuses with SearchTargetUnavailable and changes nothing; fix the alias and call it again.
- If the index is gone, the indexed content is lost. Create a new target by rebuilding, cut over, and retire the paused target.
An ambiguous create
An index creation request ended without a known outcome, from a timeout or a dropped connection, or the creator stopped after recording its attempt. The states and rules are under “Remote create state” in Index administration. Before you start: a target identity that endsabandoned can never be used again, and reconciliation never deletes a leftover index; deleting it is your decision (step 5).
-
In
inspect, check that the target’sremoteCreateStateisambiguousorin_flight, and whenremoteCreateStartedAtwas. Registration, re-creation and retirement of this identity, and the next creation for the logical key, are all refused until it is resolved. - Confirm that the process that was creating this identity has stopped and that its requests can no longer arrive: the process has exited and one request time limit has passed.
-
Run
reconcileCreate(targetId, { confirmQuiescent: true }, audit). It only reads the remote side. CLI:pnpm sonamu search reconcile --target <target-id> --execute --confirm. The approval stands forconfirmQuiescent, so approve only after step 2. Check:remoteCreateStatein the result isverified(the index, the write alias and a matching_metaexist) orabandoned. WithoutconfirmQuiescentit refuses withquiescence-not-confirmed. -
Register the target. If
verified,executeCreatewith the same plan finishes the registration without a remote command. Ifabandoned, create again with a new plan.
search/administration.ts
resumeCaptureForRebuild is registered by calling that function again as is. beginRebuild builds a plan with a new target ID on every call, so it does not continue the same identity.
Check: inspect shows the target registered as verified, or closed as abandoned with a new target registered.
- If it ended
abandoned, check for a leftover index under the abandoned name and delete it with your own tooling if you do not need it.
Changes that bypassed marking
Mark again withremark any change that did not go through markChanged, such as a foreign key CASCADE or SET NULL, or hand-run SQL. remark marks the chosen documents with the same statements as a domain write and gives them a new ledger revision, which is higher than any indexed version and so wins in OpenSearch.
Before you start: capture must be on. While it is suspended,
remark returns "suspended" and changes nothing.
- Choose the scope: a ledger scan to find deleted rows, a source scan to find rows inserted without marking.
-
Run the pages to the end. A ledger scan only goes up to its
throughbound, so it finishes even while writes continue. The example returns the number of documents marked. CLI:pnpm sonamu search remark --scan ledger --through <document-id> --execute --confirm, or--scan sourcefor a source scan. The command runs every page and prints each one.
search/administration.ts
next. If a page fails, continue from the last next you received.
- Check the result: the worker’s drain reports grow by the number of documents marked again, and deleted documents disappear from search.
signal. Each page acquires and releases the admin lease. A call is ordered against domain transactions marking the same document by the ledger row lock: a READ COMMITTED write waits and adds a revision on top, and a REPEATABLE READ write whose snapshot is older fails with 40001 and must be rerun. An administrative handle created without an OpenSearch client can run this operation.
Source database restore
Restoring the source database in place by PITR or a dump moves the ledger, intents, targets and read pointer back too. The index stays ahead, so the ledger cannot be trusted. The only fix is to suspend capture and rebuild in a new epoch. Before you start:- If the restore changed the origin (the database was recreated under the same name), first run “Confirming that this is the same store” above. While remote operations are stopped, resume and cutover cannot run.
- Step 1 cannot be undone: all existing targets and builds become invalid for good, and cursors issued before it never come back.
- From step 1 until step 4, search returns
SearchRebuildRequired; the old target is not served. Domain writes work normally throughout.
search/administration.ts
-
Call
suspendCaptureright after the restore. Later writes leave no intents. CLI:pnpm sonamu search suspend --execute --confirmCheck:markChangedreturns"suspended"and searches returnSearchRebuildRequired. -
Create a new target in a new epoch and turn capture back on with
resumeCaptureForRebuild. CLI:pnpm sonamu search resume --execute --confirmCheck: it returns a build. If it refuses withcapture-not-suspended, step 1 did not take effect. If the create’s outcome is unknown, follow “An ambiguous create” above; callingresumeCaptureForRebuildagain registers the same identity. -
Run the backfill to the end and let the worker drain the backlog.
CLI:
pnpm sonamu search backfill --execute --confirmCheck:inspectBuildshowseligibleForCutovertrue. -
Run
cutover. It refuses while blockers remain, so check them withinspectBuildfirst. CLI:pnpm sonamu search cutoverwithout--executeprints the blockers and changes nothing;pnpm sonamu search cutover --execute --confirmcuts over. Check: searches succeed again. -
After the grace period, retire the old-epoch targets.
CLI:
pnpm sonamu search retire-old --execute --confirmCheck:inspectshows themretired. If retirement refuses withretirement-grace-pending, run it again atdetails.earliestAtMs. -
Check the result: search returns the restored content from the new target, and in
inspectthe read pointer names the new target withrebuildRequiredfalse.
SearchRebuildRequired during the suspension and SearchCursorExpired (capture-epoch-invalidated) after the cutover.
Hash version upgrade
A package version that changes the canonical JSON rules uses a new hash version name. The current package has one hash version,sonamu-c14n-v1. The hash version is a fingerprint input, so every definition’s fingerprint changes and the upgrade is a rebuild.
Before you start:
- Between the cutover (step 3) and moving search processes to the new version (step 4), searches from one of the versions get
SearchDefinitionMismatch(unsupported-hash-version), because a query handle reads only targets with its own package’s hash version. Plan to serve that window as 503. - Rolling back to the old target needs an administrative handle that holds its definition, that is, one from the old package version. Retiring the old target (step 5) ends that option.
-
Deploy workers of the new version alongside the old workers. A worker claims only targets whose
(key, fingerprint, hash version)it matches, so old workers handle the old target and new workers the new one. A worker leaves targets it does not know alone and reports them inunsupportedTargets. -
With the new version’s administrative handle, open a build for the new fingerprint and backfill it.
CLI, from the new version:
pnpm sonamu search init --execute --confirm, thenpnpm sonamu search backfill --execute --confirm. Check:inspectBuildshows the buildcatching_up. -
Once the new target’s backlog is small, run
cutover. CLI:pnpm sonamu search cutover --execute --confirmCheck: the read pointer names the new target. If cutover refuses, see its blockers. -
Move search processes to the new version.
Check: searches no longer get
unsupported-hash-version. -
After the grace period, retire the old target and stop the old workers.
CLI:
pnpm sonamu search retire-old --execute --confirm -
Check the result:
unsupportedTargetsin worker reports is empty and nohash-version-mismatchevents appear.
hash-version-mismatch event, and stays pending until it is marked again or the target is rebuilt.
Next steps
Operations
Monitoring, tuning, permissions, known limits
Index administration
The administrative handle and the rules of each operation