Waiting and Recovery¶
Four recovery cases exist for the scheduler. The scheduler can split a flake job across multiple workers. Evaluations that make no progress go to the Waiting state. Jobs that workers return go back to the queue. The scheduler can clean up leftover state after a server restart.
flowchart LR
P[Queued / Fetching / Evaluating*] -- no capable worker --> W[Waiting]
B[Building] -- no worker fitting / graph stuck --> W
W -- eval_workers, draining cleared --> P
W -- workers fit / heal freeing a shared build --> B
Fetch and Eval Split¶
assign_queued_evals (gradient-scheduler/src/loops/eval.rs) can ask the pool once per pass for an idle evaluation-only worker (WorkerPool::has_idle_eval_only_worker). Such workers are active, with eval, without network and without a job.
| Pool at assignment | Job steps |
|---|---|
| An idle evaluation-only worker present | [FetchFlake], then a follow-up [EvaluateFlake, EvaluateDerivations] |
| Otherwise | [FetchFlake, EvaluateFlake, EvaluateDerivations] in one job |
- Decision per pass: one idle worker will split every evaluation queued in that pass.
- Source: a repository evaluation must carry
FlakeSource::Repositoryas source. A/nix/store/...source (gradient buildupload) must carryFlakeSource::Cachedas source. That source will still runFetchFlaketo resolve inputs with the project SSH key (flake_job_for_eval_source). - Routing:
WorkerCaps::can_eval(gradient-pool/src/worker_caps.rs) will requirenetworkfor aFetchFlakestep andevalfor an evaluation step. Repositories behind an SSH URL also needsecrets(WorkerCaps::can_fetch). - Follow-up:
handle_job_completed(job_handlers/build_status.rs) will react toJobCompletedof a fetch-only job (is_fetch_only). The handler must readevaluation.flake_sourceand enqueuePendingEvalJob::cached_followupunder the sameeval:{id}key. The follow-up will carryFlakeSource::Cachedplus the source as a required path. A missingflake_sourcewill fail the evaluation permanently. - Worker side: Evaluation workers fetch the source from the cache instead and evaluate
path:<store path>?rev=<commit>.self.revcan keep the commit (gradient-worker/src/executor/eval.rs) from the source fetch. - Steering:
ReserveFetchWorkersRule(gradient-pool/src/score/rules/builtin.rs) will score a worker withnetworkat-300 * (1 - idle / total)for a job withoutFetchFlake. The penalty will fade as workers go idle. The rule will never refuse the job. See Scoring.
Waiting Reasons¶
The waiting pass refresh_waiting_state (gradient-scheduler/src/waiting_state.rs) can run at the end of every build assignment pass (5 s tick or kick). It scans all in-flight evaluations. It writes a reason into evaluation.waiting_reason only when the JSON changes.
WaitingReason |
Parked from | Unparked | Owner |
|---|---|---|---|
eval_workers { capability, connected_workers } |
Fetching with no network worker, or no worker with network and secrets for an SSH repository (capability secrets). Queued, EvaluatingFlake, EvaluatingDerivation with no eval worker |
To Queued once the capability can connect |
Waiting pass |
workers { unmet, connected_workers, available_architectures } |
Building with no worker that can fit the (architecture, required_features) of any blocking shared build |
To Building once one worker can fit. To Aborted after 300 s on the same unmet set, unless wait_for_workers can be set on the task |
Waiting pass |
graph_stuck { pending_shared_builds } |
Building with a fitting worker for every blocking shared build, yet none can be handed out |
To Building once the heal can free a shared build |
Waiting pass |
upstream_caches { pending_shared_builds, failing_upstream_caches } |
Building with a fitting worker, but a blocking shared build without an upstream answer. The list holds every upstream cache with errors in the last 5 minutes |
To Building once every upstream cache can answer |
Waiting pass |
draining |
Every in-flight evaluation during an instance drain | At the end of draining or at startup (resume_drained_evals): to Building when the walk had finished (status_after_draining), otherwise to Queued |
Waiting pass |
approval, no_cache |
Trigger conditions | Webhook and cache hooks (gradient-ci/src/unpark.rs) |
Never touched by the waiting pass |
cache_storage_full |
Queued with every writable project cache full, before the job enters the queue |
To Queued by cache maintenance (unpark_storage_full_all) once one cache has room |
Eval dispatch pass (gradient-scheduler/src/full_caches.rs) |
- Pre-build parks can skip each shared build the evaluation already batched. A stall mid-walk can still park.
- Blocking builds name the shared builds kept by
blocks_evaluation, in a status that can still be wanted. The pass can leave out a shared build that nothing wants. - Counters first:
phase_from_counterscan decide without reading a shared build.named = 0can hand the evaluation to the pre-build rules.active = 0can letcheck_evaluation_donefinalize the evaluation.building > 0can keep theBuildingstatus. - Unbuildable abort:
unbuildable(gradient-scheduler/src/unbuildable.rs) can hand aworkerspark with a non-emptyunmetset back to the scheduler. Two conditions apply: the park is older than 300 s, and the task can havewait_for_workers = falseset.abort_unbuildable_evaluationcan mark the evaluationAbortedand abort its shared builds. The function can also record a warning naming each missing architecture and feature set. The grace period can keep a server restart or worker redeploy from aborting work before the pool is back. - Shared build read: otherwise
BuildabilityCheckercan read every blocking shared build.AssessmentMemocan reuse the verdict for up to 60 s while the counters and the pool fingerprint stay equal. An empty read can recount the drifted counters and finalize. - Own Pool: Two Postgres connections in a separate pool serve the waiting pass. Those connections get session limits
statement_timeout5 seconds andlock_timeout2 seconds (waiting_state.rs). No configuration option can change these values. - Time Budget: Passes refresh evaluations for up to 30 s (
WAITING_REFRESH_TIME_BUDGET). Evaluations that passes skip come first in the next pass. Logs record the messagewaiting refresh exhausted time budgetat INFO on the first pass of a streak of passes that use up the budget. Later passes in that streak log it at DEBUG. Passes within the budget end the streak. - Failures: Failed refreshes of one evaluation produce a warning with the log fields
evaluation_id,error_kind,sqlstate(55P03lock timeout,57014statement timeout). The pass continues with the next evaluation. Consecutive failures of that evaluation with the same error log only at DEBUG after the first warning. A successful refresh or a different error ends that streak.
Graph-Stuck Heal¶
A workers verdict with an empty unmet set triggers the heal. attempt_graph_unstick then sends Transition::Repair { scope: RepairScope::Unstick } to the graph writer and re-assesses without the memo. The repair pass repair_build_graph (gradient-db/src/graph/repair.rs) performs the steps below. The first failed step fails the transition.
requeue_failed_closure: thaw failed shared builds in the closure back toCreated. UnderUnstick, a deterministic build failure and its subtree stay untouched.repair_cached_shared_builds_for_eval: complete shared builds whose outputs all sit in the cache, then runadvance_fetchablefor the builds that need it.repair_dependency_failed: mark builds as failed when they depend on a build with a lasting failure.adopt_pending_closure: name orphaned pending shared builds for the evaluation, then update their needs-build marks.promote_closure: queue every build that now meets the queue conditions.
| Driver | When |
|---|---|
| Waiting pass | On entry to graph_stuck and on every change of pending_shared_builds (unstick_due) |
graph-stuck-reheal pass |
Every metrics.graphConsistencyIntervalSecs (300 s) for each graph_stuck evaluation |
| Counters | Queueing the set the moment its queue conditions hold |
- Lost
.drv:recover_drv_stuck_evalschecks evaluations stuck ingraph_stuckfor over 120 s. The target is a walked shared build that needs a build and is not available in a cache. Such a build has every dependency done and no.drvNAR.trigger_drv_recoverystarts aDrvRecoveryevaluation of the same commit for it. ADrvRecoveryevaluation stuck the same way fails.
Returned Jobs¶
Returned jobs go back to the pending jobs. The server can score such a job again at the next request of any worker. Workers keep no job list of their own.
| Event | Mechanism |
|---|---|
| Enqueue | SchedulerMsg::Enqueue can add the job to pending jobs and signal workers with live idle slots. Cached follow-ups that reuse eval:{id} take the same path |
| Reject | SchedulerMsg::Rejected returning the job to pending jobs |
| Assignment pass | SchedulerMsg::ReOffer message to wake workers with live idle slots while a job is pending |
Startup Recovery¶
At server start, gradient-web/src/lib.rs runs recover_interrupted_work (gradient-db/src/maintenance/recovery.rs) one time. unpark_draining_evals can only start after that. The function restores work left behind by the previous run.
- Assignments: open
dispatched_jobrows of a cluster attempt go toAbandoned. Other open rows stay open so their worker can reclaim them. - Attempts: build attempts in
Runningstate go toAborted. - Shared builds: shared builds in
Buildingstate go toQueuedfirst.unpromote_ungatedthen moves builds with failing queue conditions back toCreated. - Evaluations: evaluations in
Fetching,EvaluatingFlakeorEvaluatingDerivationstate turnQueued, each with a phase event. Their eval job lived on a worker. Evaluations inBuildingstate keep their status and their re-queued builds. -
Clusters: every open cluster attempt closes (
PrepareFailedwhen it never started,Abortedotherwise). A cluster inRunningstate goes back toQueued, or toAbortedwhen a member evaluation or shared build can no longer finish. -
Jobs with an open assignment skip steps 2-4. They keep their status and attempt.
- Kept assignments wait in the scheduler for
proto.assignmentReclaimGraceSecs. Rows with no reclaim inside the grace return to the queue as a lost worker. Queuedevaluations come back from the eval assignment pass. The waiting pass returnsWaitingones.- Recovery omits the live effects of
update_evaluation_statuscalls. The rows stay consistent on their own.