Skip to main content

Desired state and reconciliation

Workspaces and volumes are reconciled resources. An API request records what should exist; the reconciler makes the cluster match and records what it saw. The reconciler is level-triggered: it acts on the current row, not on the event that changed it, so it is safe to run on every replica, to crash and to repeat.

The columns​

Every reconciled table carries the same columns (see core/reconcile):

ColumnMeaning
generationIncremented on every change to the desired state.
observed_generationThe generation the recorded phase describes.
reconcile_atWhen the row is next due. NULL parks it until its next change.
lease_untilUntil when a replica holds the row; NULL while none does.
claimsHow many times the row was claimed: the fencing token of an outcome.
failuresAttempts that failed since the last one that did not, for the backoff.
deleted_atSet when deletion was requested. The row stays until the external resource is gone.
phaseWhat was last observed.
status_code, status_messageWhy the last attempt failed, as an error code and words that may be shown; or, after a success, what the worker said of the resource.

A request handler changes the row and calls reconcile.Request(), which increments generation and, after the commit, publishes a wake-up on Valkey. A trigger on the table, reconcile_requested, makes a row whose generation changed due at once and forgets its failures, whatever code changed it.

Claim, act, record​

  1. Claim. A reconciler takes rows of the workers it holds that are due and not leased, or whose lease ran out, with FOR UPDATE SKIP LOCKED. It leases each for 60 seconds, counts the claim and clears reconcile_at.
  2. Act. Outside any transaction, it calls the driver with a spec built from the row. Up to 32 rows of a table are handled at once on each replica, and a row that finishes makes room for the next at once. The lease is renewed while the handler runs; a handler that cannot have its lease renewed before it may have run out is cancelled, and so is one that runs longer than 2 minutes.
  3. Record. It locks the row again. If claims is still that of its claim and nothing made the row due meanwhile, it stores the outcome, schedules the next visit and lets the lease go. If the row was changed or nudged meanwhile, what was observed may be stale: the outcome is dropped and the lease let go, and the row, already due, is claimed again at once. If another replica claimed it after the lease ran out, nothing is recorded.

Each replica runs one such loop per reconciled table. A loop wakes on a bus message, whenever a handler finishes, and at least every 2 seconds. When the replica stops, the handlers get 15 seconds to finish; those that do not are cancelled and their rows let go, due at once.

Why this is safe across replicas​

  • No double handling. SKIP LOCKED lets concurrent claims pass over each other's rows, and the lease keeps a claimed row from other replicas until it is let go or runs out. A change of the row does not break the lease, so a row is never acted on by two replicas at once, even while it is being deleted.
  • A crash loses nothing. If a replica dies between claim and record, the lease expires and another replica repeats the work. Driver calls are idempotent, so repeating is harmless.
  • A change during reconciliation is not lost. It makes the row due, so the outcome of the visit it overtook is dropped and the row is reconciled again as soon as the visit has let it go.
  • A late replica cannot overwrite a newer outcome. If a replica stalls past its lease and another one claims the row, claims no longer matches and the first one records nothing.
  • A lost wake-up only costs latency. The loop polls the database regardless.

Failures​

A handler that fails keeps the row's phase. The error is classified once, as for the API (apierr.Errors.Classify):

  • status_code and status_message tell what may be shown: worker_unreachable while the worker's agent is away, worker_error or worker_outdated when the agent refused, internal_error for anything else. What went wrong inside goes to the log with the row's ID, not to the user.
  • The row is tried again after 5 s, 10 s, 20 s… up to 5 minutes while it goes on failing. When asking again cannot help, e.g. the worker's agent does not know what the row asks (worker_outdated), the row is parked instead: it is tried again when it changes, or when a snapshot of its worker's cluster finds it otherwise than it says, which happens whenever an agent connects and every 10 minutes. A row being deleted is looked at again at every snapshot until it is gone. A row is never given up: what an operator fixes is picked up without anyone touching it.
  • A failure that may pass is logged as a warning, a fault of the platform as an error, and a panic in a handler fails that row's attempt only.

Workers​

Every workspace and volume belongs to a worker, in its worker_id column (a volume waiting for its first workspace has none, and nothing to reconcile), and only a replica that an agent of that worker is connected to can act on it. A replica therefore claims only the due rows of the workers it holds, and wakes its reconcilers whenever an agent connects.

  • A worker whose agents are all disconnected is not reconciled. Its rows stay due and are picked up by whichever replica its agent next connects to. The UI shows the worker as offline.
  • The cluster nudges. An agent reports objects that changed; their rows are made due at once, without a change of generation (reconcile.Nudge). On every link, and every ten minutes, it also sends a snapshot of all the cluster holds, and the rows that differ from it are nudged (see catching up): no row needs a timer to notice what happened in the cluster. The fencing above drops the outcome of a visit that a nudge overtook, so that the nudge is not lost under the visit's requeue.
  • What should run on a silent worker is not charged. The job workspace.mark_unreachable moves workspaces that are running or starting on a worker not heard from for five minutes to unreachable. They keep their quota but cost no credit. The agent reports their objects when it is back, which makes them be looked at again.

Next visit​

The handler returns an outcome that says when to look again:

ResourceObservedNext visit
Workspacerunningparked until the next change, or a nudge
Workspacestarting, stopping, deleting2 s
Workspacefailed15 s
Workspacestoppedparked until the next change
Volumereadyparked until the next change
Volumepending, failed30 s
Volumedeleting10 s

Deletion​

Deleting sets deleted_at and requests reconciliation. The handler then calls the driver's delete method until it reports that nothing is left. Only then is the row removed, in the recording transaction, together with the module's clean-up: for a workspace, the volumes made with it are deleted unless another workspace mounts them, and its mounts of the others are removed.

Deleting a user works the same way one level up: the user is marked deleting, every module releases what the user owns, and a scheduled job removes the row once no workspace or volume references it.