Lance
Docs /Integrations /Lance Context /Design /SQL query owner recovery

SQL query owner recovery

Status: design and decision. Owner recovery (another process taking over the queries of an owner that stopped) is not implemented, and should not be until the conditions under Decision hold. This page records what an owner holds, what a takeover would have to make durable and fence, what it would cost, and the order to build it in. It is the separate design the SQL engine's issue (#1123) asks for before any owner recovery: event logs, replayable results or durable intermediate outputs alone do not let another process take over a query.

Today

The member that receives a query owns it: it binds the tables' versions, plans the query, routes it, coordinates its tasks across the pool and holds its results. The query's id names that node and process instance (<node>.<instance>.<token>). When the owner stops:

  • Its queries end. A shutdown cancels them with shutting_down; a crash ends them with the process.
  • Tasks other members run for those queries see their streams or calls break, and stop.
  • Requests for those queries fail explicitly: owner_lost (410) once the node is back as another instance, owner_unreachable (502) while it is down. Nothing is re-run under an old id.
  • A staged (scales) query's committed intermediate outputs stay in the intermediate storage until a member's sweep removes them, after SQL_SCALES_RETENTION_SECONDS (24 hours by default). No one reads them.

What the owner holds, and whether it outlives the owner:

State Where Outlives the owner
SQL text, parameters, session settings, bound table versions, deadline, requested mode Owner memory No
Logical and physical plan, routing decision, distributed stages and task assignments Owner memory; re-derivable from the above with the same build No
Streaming exchanges between tasks Live gRPC streams No
scales task outputs and stage manifests Intermediate storage; each task attempt writes its own objects, and each stage generation's manifest is created once (put_if_absent) Yes, until swept
Lineage of scales stages (which tasks produced what, generations) Owner memory, partly in the manifests Partly
Results not yet delivered (JSON buffer, NDJSON stream position) Owner memory No
Replayable results ("replayable": true) Spool on the owner's local disk No: a restarted node removes it
Admission slot, memory reservation, task slots Owner and workers Released when it stops

What a takeover needs

A takeover means another process (or the same node restarted) finishes a query under its original id, with the same results the original owner would have returned. That needs four things the engine does not have.

1. A durable query record

Written by the owner once the query is bound and routed, before any task runs, in the intermediate storage under the query's prefix:

  • the id, SQL text and typed parameters;
  • every table's dataset URI and bound version;
  • every setting that affects results: time zone, now() and other constants fixed when the query was bound, the DataFusion options the engine sets;
  • the absolute deadline, the requested and selected mode, the build id (pool members already must run the same build);
  • the result contract: JSON or NDJSON, replayable or not.

A takeover re-plans from this record and must arrive at the same stages: the record also keeps a fingerprint of the distributed plan (stage boundaries, partitioning, task count), and a takeover whose plan differs fails with owner_lost instead of reusing outputs. Volatile functions (random(), uuid()) do not prevent a takeover, since the new owner either adopts a stage's committed output or runs the stage itself. Only a stage that has to run again after consumers read its earlier output must be reproducible, the rule recomputation from lineage already applies.

2. Ownership fencing

At most one process may act as the owner of a query at a time, including when the old owner is not dead but paused or partitioned from the pool.

  • Epochs. The record names an owner epoch, starting at 0. A takeover creates <query>/owner-e<n+1>.json with put_if_absent, which the intermediate storage already provides for stage manifests. Exactly one claimant wins each epoch.
  • Leases. The owner renews its epoch every lease period (for example 10 seconds; one conditional write per running query per period). A claim for epoch n+1 is valid only after epoch n's lease has expired by more than the allowed clock skew.
  • Epoch-scoped commits. Stage manifests carry the epoch in their name (s<stage>/manifest-e<epoch>-g<generation>.json). A new owner adopts the previous epoch's committed manifests explicitly, by copying them into its own epoch after verifying their objects (as the last stage already does). A late commit by a fenced owner lands under its old epoch, and no one reads it.
  • Fenced tasks. Task requests carry the query's epoch. A worker that has seen epoch n+1 of a query rejects epoch n's tasks, and outputs written by epoch n's attempts are never in an epoch n+1 manifest unless adopted.
  • Fenced delivery. Before delivering results, and every lease period while delivering them, an owner checks that its epoch is still current. A fenced owner ends its streams with owner_lost.

3. Restart and reconciliation

The claimant is either the restarted node itself, scanning the records of its previous instance at start, or any member that receives a request for a query whose owner is unreachable and whose lease has expired. It:

  1. claims the next epoch, then admits the query like a new one (slots, memory), or gives up the claim with overloaded;
  2. checks the build id, binds the recorded versions (a version that was cleaned up fails the query with source_unavailable, as it does today), re-plans, and compares the plan fingerprint;
  3. adopts each committed stage whose manifest and objects verify, and runs only the stages and tasks that are missing, with the lineage rules recomputation already follows;
  4. delivers the results under the original id (see below), within the original deadline, which a takeover never extends;
  5. removes the query's prefix when the query ends, as today.

Routing must follow the new epoch. Today a node answers a request for an unknown instance of itself with owner_lost, and forwards one for another node to that node. With recovery, a node that cannot reach the owner named in the id, or that is the owner's node with a different instance, reads the query's newest owner record and forwards to the owner it names. The record read is one small object per such request; requests to a live owner are unchanged.

4. Results across a takeover

The REST contract already rules out the hard part: once rows of a query have been published to a client, no other attempt's rows may follow them. So:

  • No rows published yet (the query was still running its stages, or its JSON or NDJSON results were not yet requested): the new owner delivers all results. This is the case worth building for. A long staged query spends nearly all its time before its last stage.
  • NDJSON rows published by the old owner: the client's stream broke with the old owner. The new owner cannot continue it, so the query fails with owner_lost, unless the query is replayable from a shared spool (next point).
  • Replayable queries: the result spool would have to move from the owner's local disk to shared storage, with a result manifest committed under the owner epoch (chunks, row counts, offsets). A new owner could then serve ?offset=N from the committed chunks, and continue a query whose results were partially spooled only if its last stage is reproducible.
  • JSON results already fetched: their delivery is not recorded durably, so a new owner cannot tell whether the client got them. Either record delivery in the record (one more conditional write per query), or answer repeated reads after a takeover with owner_lost. The second is simpler, and clients already handle it.

single and streaming queries keep no durable intermediate state, so a takeover would re-run them from the start: that is a resubmission, which the client can make itself with full knowledge. Owner recovery would therefore cover scales queries only.

Cost

  • Per query: one record write, one lease write per lease period, a conditional create per epoch, and the record read on requests that find the owner gone. Negligible for queries long enough to run in scales; the record would not be written for single and streaming queries.
  • Code: the coordinator's commit paths (epoch-scoped manifests, adoption), the worker protocol (epoch on every task, rejection of stale epochs), the service (records, leases, claims at start and on requests, fenced delivery), routing and forwarding, admission of adopted queries, and, for replay across owners, a shared result spool.
  • Tests: the fault-injection harness would need to kill or pause the owner at each step: before and after the record, mid-stage, between a task's write and its commit, during the last stage, after the first result rows, during adoption; and a paused old owner resuming after a takeover, with skewed clocks.

Decision

Not now. Owner recovery pays off when losing a query's owner loses more work than the machinery costs, and when that happens often enough to matter. Today:

  • At TPC-H scale factor 100 every query takes under a minute on one 15-core node. auto selects scales only for queries estimated at an hour or more on a reference node, and only with SQL_AUTO_SCALES, which is off by default until scales is validated at scale.
  • The common cause of an owner stopping is a planned restart, which a drain handles without any durable state: stop admitting queries, let running ones finish within their deadlines, then stop. Today a shutdown cancels running queries with shutting_down at once.

Revisit owner recovery when all of these hold:

  1. scales queries run in production routinely, and many of them take longer than about 30 minutes.
  2. Their owners stop while they run often enough, after a drain is in place, that resubmitting costs real capacity or misses deadlines: preemptible or spot nodes, crashes, or deploys more frequent than the queries are long.
  3. Clients need such queries to finish under their original ids rather than resubmit.

Until then, owner loss stays explicit (owner_lost, owner_unreachable), clients resubmit, and the sweep removes what a stopped owner left.

Order of work, when the conditions hold

  1. Drain (independent of recovery, and worth doing first): a stopping node admits nothing new, waits for its running queries up to a bound, then cancels the rest with shutting_down.
  2. Records and epochs without takeover: write the record, renew the lease, scope manifests by epoch. On owner_lost, report how far the query had got (stages committed, rows, time), which also tells whether takeover would have saved anything. Measure the write overhead.
  3. Takeover before any result is published, for scales queries: claims at start and on requests, adoption, fenced tasks and delivery, with the fault-injection cases above.
  4. A shared result spool, so replayable queries can be read from any offset after a takeover.