Skip to main content

Module claim

Module claim 

Source
Expand description

Turn claims: one replica at a time runs a task, however many share its store.

§The problem

Within one process a second SendMessage to a task whose executor is still running is refused, and a continuation of a task parked at input-required waits for the turn that parked it to store its last events (handler/messaging/admission.rs). Both decisions read state the process keeps in memory, so before claims they did not cross replicas: a continuation that reached replica B while the task’s turn ran on replica A started a second executor, and B’s save replaced artifacts A had stored after B read the task (a lost update).

§What a claim is

A row in the store’s database, keyed by the task’s partition and id, that names the replica running the task’s current turn (owner) and a random token for that one turn. The handler takes it before it reads the task for a continuation, and deletes it once the turn’s events are stored. A send that finds the row held waits, bounded as the in-process wait is, and is refused if it is still held at the bound.

The owner renews every claim it holds in one statement per renewal interval. A replica that dies stops renewing; once its claims expire, the next send for one of its tasks takes the claim over and continues the task. Expiry is measured on the database’s clock where it has one (PostgreSQL), so replica clocks need not agree.

§Fencing

Expiry alone is not enough. A replica that stalls past the time to live (a long pause, a partition from the database that heals) can wake up after another replica took its claim over, and write once more before its next renewal tells it so. So every task write and event-log append a turn makes carries its token (current_fence), and the SQL stores refuse it, in the same statement, unless that token still holds the claim on the task written. The refusal is a ClaimLost error; the turn’s processor then cancels its executor and stops writing, as it does when another writer finished the task.

A write outside a turn (CancelTask, the handler’s own status writes) carries no token and is not fenced: a cancel must win whoever runs the task, and the terminal-state rule (store::terminal) already keeps the running turn from overwriting it.

§Cancelling across replicas

A CancelTask that reaches a replica not running the task marks the claim (TurnClaimStore::request_cancel). The owner learns it at its next renewal and fires the executor’s cancellation token, so a cancel stops an executor elsewhere within about one renewal interval rather than at its next write.

§Which stores provide one

TaskStore::turn_claims answers Some for the PostgreSQL and SQLite stores, plain and tenant-aware, with claims in the same database as the tasks, which is what makes the fence possible. The in-memory stores answer None: one process, one handler, nothing to coordinate. InMemoryTurnClaims exists for tests that run several handlers over one in-memory store.

Structs§

ClaimLost
A turn’s write was refused because its claim on the task was taken over: another replica runs the task now. See the module documentation.
HeldClaim
A claim the owner still holds, as a renewal reports it.
InMemoryTurnClaims
Claims in process memory, for tests that run several handlers over one in-memory store. Expiry is measured on this process’s monotonic clock.
PendingSteering
A steering message stored for the turn that holds token, as TurnClaimStore::peek_steering reports it.
PostgresTurnClaims
Turn claims in a PostgreSQL table, a2a_task_claims.
SqliteTurnClaims
Turn claims in a SQLite table, a2a_task_claims.

Enums§

Handed
What TurnClaimStore::hand_steering did with a message.

Constants§

CLAIM_LOST_MARKER
The marker a ClaimLost error carries in its data.

Traits§

TurnClaimStore
Where turn claims are kept. See the module documentation.

Functions§

current_fence
The token of the turn whose write is being made, if the write is made inside one; a SQL store binds it into the fence of every task write and event-log append. Carried as the tenant is, a task-local.