Upstream Substitution¶
A build with outputs already available in an upstream cache is fetched, not built. The upstream-probe loop will ask upstream caches only about shared builds wanted by another build or an evaluation. The answer must decide between a passthrough (fetched from upstream, cache_available) and a real build.
flowchart LR
D[Shared build is needed] --> P[upstream-probe]
P -->|every output available| R[Passthrough: Substitute job]
P -->|miss| B[Build: Build job]
P -->|no answer| P
R -->|narinfo References| D
B -->|build inputs| D
Probe Loop¶
Probe work happens over HTTP, in gradient-scheduler/src/probe.rs. The loop is a supervised periodic child, kept outside the graph writer. A round trip inside a graph transaction can block the single graph writer, so the two stay apart.
| Constant | Value | Role |
|---|---|---|
PROBE_TICK |
1 s | Pass interval while no request arrived |
PROBE_BUDGET |
120 s | Supervision budget of one pass |
PROBE_DESCENT |
60 s | A pass will stop descending after this. The rest must wait for the next tick |
PROBE_BATCH |
256 | Outputs per request round, and rows per recovery check |
PROBE_SWEEP |
60 s | Recovery check interval, only on an idle tick |
- Requests: probe work enters through an in-memory channel (
ProbeRequests). Batch recording must enqueue every shared build the batch walked, plus the needs-build marks the batch moved. The transition emitter (gradient-db/src/status/effects.rs) and the graph writer afterUpstreamHits/UpstreamProbedenqueue as well. - Wake-Up: requests inside a graph transaction stay staged until the commit. The commit can then wake the loop without waiting for the tick.
- Descent: each round answer can update the needs-build marks and hand back the next level directly. One pass can follow the closure down instead of one level per tick.
- Look-Ahead: a round can also ask about the unanswered dependencies of the wanted shared builds. A miss makes these dependencies needed, and the next level then costs no extra round. Only the dependencies of an upstream hit go unused.
- Recovery check: every walked shared build needing a build, with
probed = falseset. The check must cover a process stopped between a commit and the channel send. - Retry:
probedandprobed_athold the answer. A user retry of a build and evaluation retries that keep the entry points clear the flag on every requeued shared build with no output in any cache. The probe can then ask again. Evaluation retries that did not finish listing entry points evaluate again from the start. All other evaluation retries inherit the entry points. Evaluation retries that evaluate from the start clear only an upstream miss older than one hour. - Stale Miss: an upstream cache can gain a missed path later. Every other requeue can clear the flag on a miss older than one hour.
One Round¶
Two functions handle the round. plan_probes build the plan, probe_round apply it.
- Keep every wanted shared build without an answer (
wanted,probed = false). Add dependencies that lack an answer and that no cache hold. - Drop each shared build without
derivation_outputrows. These unwalked stubs remain unanswered until a later batch walk the stub and send it again. - Skip outputs already stored in our cache (a
cached_pathrow of the hash with afile_hash) or with anexternal_url. - Group the remaining outputs by the evaluation that name the shared build (
build_job). The project of that evaluation pick the upstream caches. probe_outputs(gradient-scheduler/src/eval.rs) must request the<hash>.narinfofor each output and flushupstream_metricafterwards.- Send hits as a
GraphMsg::UpstreamHitsmessage. Then send all shared builds from step 2 in aGraphMsg::UpstreamProbedmessage, hit or miss, unless one of their outputs got no answer.
A shared build with probed = false do not count as a real build yet. Nothing below such a shared build can get built or handed out before the answer arrive. A build queued with "no answer yet" cannot be recalled after a worker take the job.
No Answer¶
A miss count as final only after every upstream cache answered with 404 or with an unsigned narinfo. A timeout, an error status, a tripped breaker or an unreadable upstream list do not count as an answer.
- The shared build keep
probed = falseand stay unbuildable. - The recovery check ask again within one
PROBE_SWEEPinterval. - The server log one warning per silent upstream cache and round.
- The evaluation show that warning once per upstream cache, with
upstream-probeas the source. - An admin can deactivate a silent upstream cache. The next round then ask only the remaining upstream caches.
Applying a Hit¶
apply_upstream_hits in gradient-graph/src/record.rs does the following.
- Persist: write
external_url,nar_hash,file_hash,file_size,references_listandderiverto everyderivation_outputrow with the same hash and no cache entry. Also writenar_sizeandcawhere unset. - Runtime dependencies: record the narinfo
References:line as runtime dependencies on the referenced producers. - Passthrough: set
cache_available = trueonly if every output of the shared build is available upstream. A terminal-success shared build with this flag can skip its build. A failed one can become a passthrough. - Needs build: update the mark for touched shared builds and for builds newly available in a cache. Send the newly needed set back to the probe.
mark_probed must set probed = true (MARK_PROBED) and update the needs-build marks. On a miss, the shared build remains a real build and from then on needs its build inputs.
Misses follow the gain walk (gain_need). Builds that are newly available in the cache follow the full walk (update_need). Requested builds that already missed in an earlier probe still go through the substitution check. Our cache can now hold the needed outputs.
Upstream Selection¶
gradient-core/src/upstream.rs holds this logic. The probe and the worker cache query both use it.
| Rule | Detail |
|---|---|
| Candidates | Rows in cache_upstream with kind = Http and a URL. Rows with WriteOnly on the upstream or project subscription get excluded (upstream_endpoints_for_project) |
| Trust | A hit needs a Sig that verifies against the upstream public_key and a StorePath that matches the asked hash (verified_narinfo). An upstream without a key can never produce a hit |
| Order | Hit rate first, then average latency over the last 60 min of upstream_metric. Upstream caches without measurements rank last |
| At most 4 upstream caches | Queried in parallel. The fastest hit wins |
| More than 4 | Queried in order. The first hit wins |
| Timeout | 2 s per narinfo request. 30 s for a upstream_query semaphore permit |
| Concurrency | GRADIENT_CACHE_UPSTREAM_QUERY_CONCURRENCY (default 32), server-wide |
Breaker: 3 consecutive errors trip an upstream for 60 s. Transport failures count, as do any status codes outside 2xx except 404. After the cooldown, requests pass again and a single further error trips the breaker again. A 404 counts as a miss and resets the error count. Breakers live process-wide and the cache narinfo endpoint shares them.
HTTP/1.1 only: requests offer HTTP/2 by default. Some upstream caches negotiate HTTP/2 and then reset streams mid-body. A NAR passed through then breaks after the 200. Nix clients then see HTTP error 200 (curl error: Stream error in the HTTP/2 framing layer).
- The add and edit forms of an HTTP upstream cache write the
cache_upstream.http1_onlycolumn. - Declared caches take the value from
http1_onlyinservices.gradient.state. - Requests to such an upstream cache use a client limited to the HTTP/1.1 ALPN (
upstream_download_clientingradient-util/src/http.rs). - These upstream caches get an
HTTP/1.1badge in the upstream list. POST /caches/{cache}/upstream-caches/{id}/test(probe_protocolingradient-core/src/upstream_source.rs) can fetchnix-cache-infotwice. Both requests use a single-protocol ALPN, HTTP/1.1 only or HTTP/2 only. The test leaveshttp1_onlyuntouched. A smallnix-cache-infoover HTTP/2 can prove nothing about resets inside large NAR bodies.
Substitute Jobs¶
decide_build_spec_kind in gradient-scheduler/src/assign_mode.rs reads the flag alone. No other input enters that decision.
| Shared build | Kind | Worker |
|---|---|---|
cache_available |
Substitute |
Any |
builtin system, fixed-output |
Download |
Any, no Nix store |
Everything else, builtin:buildenv included |
Build |
Matching system |
The (name, store_path) pairs have to travel inside the BuildSpec. The worker never opens the .drv file (gradient-worker/src/executor/substitute.rs).
- Skip outputs the Gradient cache already stored.
- Locate each remaining output with
CacheQuery { external: true }, one path per query. The server can answer from the persistedexternal_url. Without that, it probesHttpupstream caches first, thenGradientProtoupstream caches. - Download with the redirect-following client. A body length other than the declared
file_sizecan still pass, but only, if the decompressed bytes matchnar_hash. Every other body means a missing output. - Detect compression from the magic bytes, with the URL extension as fallback.
- Check the NAR against
nar_hash, then push the bytes like any other output.
The job fetches nothing below the outputs. The referenced paths are shared builds in their own right, and the server marked them as needed. Build jobs keep use_substitutes = false in the daemon.
Failures and the Miss Budget¶
| Worker error | Kind | Server |
|---|---|---|
| No upstream held the output | SubstituteUnavailable |
Requeue to Queued with no penalty |
Upstream NAR missing, wrong size, or not matching nar_hash (CorruptCachedNar) |
InputsUnavailable |
Run a self-heal repair pass, then retry |
| Everything else, including hash mismatch | Transient |
Retry |
Escalation to a Build¶
- A passthrough that used up its
InputsUnavailable/Transientretries enter the same budget instead of moving toFailedPermanentstatus. - After
build.substituteMissEscalationThreshold(default 2) missed substitutions within one evaluation,exhaust_substitutiontake action. The function clearcache_available, the upstream columns of the outputs, andattempt. The function also set the shared build toCreatedstatus. - The shared build then build through the ordinary queue conditions.
Cache Endpoints¶
The code is in the gradient-web/src/endpoints/caches/ directory. All endpoints ask only the upstream caches of their cache that workers would substitute from (kind = Http, a URL, not WriteOnly, per substitution_sources in gradient-core/src/upstream_source.rs). All endpoints also skip tripped upstream caches and feed the shared breakers. Redirects are followed for NAR and log downloads.
| Endpoint | Upstream behaviour |
|---|---|
narinfo.rs |
The endpoint will ask every such upstream with a public_key in parallel for a narinfo missing from the cache. The Sig must verify against public_key, and the StorePath must match the asked hash. The endpoint will rewrite URL: to nar/upstream/<id>/... and append the cache's own Sig next to the upstream's |
nar.rs (upstream_nar) |
Trying the named upstream first, then every other one. The NAR path is content-addressed, and an upstream row may have been deleted |
build_log.rs |
/log/<drv> will answer with the local log (X-Cache: HIT), else the first non-empty upstream log (X-Cache: MISS) |
Build Log Substitution¶
The code is in the gradient-scheduler/src/log_substitution.rs file. The scheduler will fetch the build log of the upstream after BuildCompleted on a cache_available shared build.
- Source:
<upstream>/log/<drv basename>from the project's candidates (upstream_endpoints_for_project, best first), throughfetch_upstream_log, the same fetch as in the cache log endpoint. - Limits: the first non-empty body will win. 10 s timeout, capped at 16 MiB with a
[truncated]marker. - Target: appended to the latest
build_attemptlog, only while that log is still empty. - Errors: logged and ignored.