Skip to content

feat(cache): ordered multi-origin fallback chain (#284) - #564

Merged
alpergundogdu merged 3 commits into
mainfrom
feat/issue-284-multi-origin-fallback
May 15, 2026
Merged

alpergundogdu merged 3 commits into
mainfrom
feat/issue-284-multi-origin-fallback

Conversation

@thiras

@thiras thiras commented May 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements issue #284 — ordered multi-origin fallback for cache pull-through.

  • Adds [[cache.origins]] array-of-tables TOML form alongside the existing [cache.origin] singular (mutually exclusive). Setting both, or origins = [], is rejected at config load.
  • Rewrites CacheEngine::pull_through as a chain walk: per-origin retry, then advance on NotFound / Permanent / Transient-exhausted. Deterministic backend misbehaviour (HashMismatch / BlobTooLarge / Store) short-circuits — surfaces, not masks.
  • All-NotFound → CacheError::NotFound (=> 404); any error in chain → CacheError::OriginError carrying the last origin's failure (=> 5xx with operator-actionable detail).
  • Permanent / retry-exhausted advances emit tracing::warn! (NotFound advances stay at info!) so a degraded primary doesn't hide behind a healthy mirror.
  • New decdn_cache_origin_fallback_total counter distinguishes chain-walk steps from per-origin retries.
  • Operator-local policy, no new ADR (ADR 011's OriginAssignment still governs network-level redundancy).

Breaking surface changes

Test plan

  • cargo fmt --all -- --check clean
  • cargo clippy --workspace --all-targets -- -D warnings clean (anti-panic policy respected; no unwrap/expect/panic/indexing_slicing in non-test code)
  • cargo nextest run --workspace ⇒ 797/797 passed (+19 new tests vs main: 10 engine fallback tests, 7 config-validation tests, 1 CLI multi-kind rendering test, plus inline origin_fetches == 1 regression-guard assertions)
  • Engine coverage — happy path on each origin, advance on NotFound, advance on Permanent, advance on Transient-exhausted (asserting first.fetches() == 1 + max_retries), short-circuit on HashMismatch / BlobTooLarge (asserting second.fetches() == 0), all-NotFound surfaces NotFound, all-error surfaces OriginError with the last origin's marker (verified via distinguishable error strings), 3-origin chain confirms origin_fallback increments per-step (== 2, not 1), single-origin and empty-origin vecs preserve pre-Add support for a [origins] multi-origin table in config allowing ordered fallback across multiple HTTP or filesystem origin backends #284 behaviour
  • Config coverage — accepts array preserving declared order, rejects both forms simultaneously set, rejects empty origins = [], singular → 1-element vec back-compat, absent → empty vec, per-entry error threads cache.origins[i] index, duplicate origins resolve without erroring
  • Manual end-to-end smoke: two HTTP origins with origin 0 always 404'ing and origin 1 serving — verify decdn_cache_origin_fallback_total increments and the tracing::warn!/info! chain-walk log lines fire as expected

Reviews run before submission

Five specialized reviewers (code-reviewer, pr-test-analyzer, silent-failure-hunter, comment-analyzer, type-design-analyzer) — every flagged critical and important item addressed in this branch.

🤖 Generated with Claude Code

Adds `[[cache.origins]]` array-of-tables TOML form (mutually exclusive
with the existing `[cache.origin]` singular form) so operators can
configure an ordered list of HTTP / filesystem / S3 backends with
automatic fallback. The chain advances to the next entry on `NotFound`,
permanent error, or per-origin retry-budget exhaustion; deterministic
backend misbehaviour (`HashMismatch`, `BlobTooLarge`, local `Store`
errors) short-circuits rather than falling through, so a bad backend
gets diagnosed instead of silently fronted by its peers.

Both wire forms collapse to a canonical `ResolvedCache.origins:
Vec<ResolvedOrigin>` in the resolver (singular → 1-element vec,
absent → empty vec ⇒ `NoOrigin`), eliminating an either-one-or-many
branch through wiring. All-NotFound surfaces `CacheError::NotFound`
(=> 404); any in-chain failure surfaces `CacheError::OriginError`
carrying the last origin's error (=> 5xx with operator-actionable
detail). Permanent / retry-exhausted advances emit `tracing::warn!`
so a degraded primary doesn't hide behind a healthy mirror.

Breaking public surface: `EvictionPreview.origin_kind:
Option<OriginKind>` → `origin_kinds: Vec<OriginKind>`, threaded
through admin RPC `EvictPreview` and the CLI `decdn node evict
--dry-run` output (now `origin_kinds=http,s3` instead of
`origin_kind=http`). External consumers pinned to the pre-#284 JSON
schema will need to update.

New `decdn_cache_origin_fallback_total` Prometheus counter
distinguishes chain-walk steps from per-origin retries. Fail-fast on
any per-origin init failure at `build_cache` time with
`cache.origins[i]` threaded into the error context.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 14, 2026 18:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements ordered multi-origin fallback for cache pull-through (#284). Introduces a [[cache.origins]] array-of-tables TOML form alongside the existing [cache.origin] (mutually exclusive), reshapes CacheEngine to take Vec<Arc<dyn Origin>>, and rewrites pull_through as a chain walk that advances on NotFound/Permanent/retry-exhausted but short-circuits on HashMismatch/BlobTooLarge/Store errors. Adds a decdn_cache_origin_fallback_total counter and migrates EvictionPreview.origin_kind → origin_kinds.

Changes:

  • Config: new plural origins field, validation for mutual-exclusion and empty-array rejection, per-entry indexed error context, duplicate-warning logging, env-expansion threading TOML path.
  • Engine: open* constructors now take Vec<Arc<dyn Origin>>; pull_through walks the chain emitting info!/warn! and a per-step origin_fallback counter; surfaces last error or all-NotFound.
  • DTO/CLI/admin: EvictPreview.origin_kinds: Vec<OriginKind> + comma-separated CLI rendering; existing call sites and tests updated.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/cache/src/engine.rs Vec-based origin chain in Inner, chain-walk pull_through, EvictionPreview.origin_kinds; tests migrated.
crates/cache/src/metrics.rs Adds origin_fallback counter doc.
crates/cache/src/origin/mod.rs Doc updates referencing renamed origin_kinds.
crates/cache/tests/pull_through.rs New fallback test suite (10 tests) + signature migrations.
crates/cache/tests/s3_origin.rs Updates for new Vec-based open API.
crates/common/src/config/types.rs Adds origins: Option<Vec<OriginConfig>> with extensive docs.
crates/common/src/config/resolved.rs ResolvedCache.origin → origins: Vec<ResolvedOrigin>.
crates/common/src/config/mod.rs resolve_origins collapse + validation + duplicate warning; env-expansion path threading; 7 new tests.
crates/common/src/admin.rs EvictPreview.origin_kinds: Vec<OriginKind>.
crates/cli/src/commands/node.rs CLI dry-run renders comma-separated origin_kinds; new ordered-rendering test.
crates/cli/tests/{config_validate,admin_peers}.rs API migrations.
crates/node/src/runtime/mod.rs build_cache iterates resolved origins with indexed context; origin_kinds_label.
crates/node/src/runtime/reload.rs Test fixture migration.
crates/node/src/admin.rs Threads new origin_kinds; test updates.
crates/node/src/metrics.rs Registers new origin_fallback metric in tests.
crates/node/tests/sighup_signal.rs Test fixture migration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cache/tests/pull_through.rs Resolved
Comment thread crates/cache/tests/pull_through.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 88.99% (was 89.00%, ▼ 0.01%) — 14034/15770 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 91.45% 1241/1357 ▲ +0.34%
crates/cache/src/origin/mod.rs 86.96% 60/69 ● 0.00%
crates/cli/src/commands/node.rs 79.22% 469/592 ▲ +1.11%
crates/common/src/admin.rs 69.57% 16/23 ● 0.00%
crates/common/src/config/mod.rs 96.15% 3575/3718 ▼ 0.33%
crates/node/src/admin.rs 95.85% 623/650 ▲ +0.01%
crates/node/src/metrics.rs 77.55% 228/294 ▲ +0.15%
crates/node/src/runtime/mod.rs 52.81% 395/748 ▼ 0.93%
crates/node/src/runtime/reload.rs 95.17% 1181/1241 ● 0.00%
History
Commit Total Δ vs base
fb14a02 88.98% ▼ 0.02%
c07698e 88.99% ▼ 0.02%
e65ef12 (HEAD) 88.99% ▼ 0.01%

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a multi-origin fallback chain (#284), transitioning the cache engine from a single origin backend to an ordered list of backends. The CacheEngine now iterates through configured origins on cache misses, advancing to the next entry upon encountering a NotFound result, a permanent error, or retry budget exhaustion, while ensuring that deterministic failures like hash mismatches correctly surface immediately. The configuration layer was updated to support both singular and plural TOML forms, and new metrics were added to track fallback occurrences. Feedback from the review highlights a potential compilation or formatting issue regarding optional S3 prefixes in identity keys and suggests improving error reporting by preserving diagnostic metadata from primary origin failures rather than only returning the final error in the chain.

Comment thread crates/common/src/config/mod.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
thiras and others added 2 commits May 14, 2026 21:31
…#284)

Three test sites had `<expr> as Arc<dyn Origin> as Arc<dyn Origin>` —
the inner expression was already a typed `Arc<dyn Origin>`, so the
outer cast was a no-op artifact from the bulk rewrite that migrated
the `Option<Arc<dyn Origin>>` → `Vec<Arc<dyn Origin>>` test fixtures.
Caught by Copilot review on PR #564.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Six focused improvements surfaced by the second-pass /review-pr run
on PR #564:

- expand_origin's S3 DefaultChain arm hardcoded "cache.origin.credentials.profile"
  even after the prefix-threading refactor; operators with
  [[cache.origins]][i].credentials.profile = "${VAR}" now get the
  correct cache.origins[i].credentials.profile path in expansion
  errors. (mod.rs:1308)
- warn! on error-driven chain advance now carries the underlying
  error string. Operators triaging the user-visible 5xx see *this
  origin's* failure mode, not just the chain-final one surfaced via
  CacheError::OriginError. Addresses the "intermediate errors lost"
  visibility concern raised in both review passes at the log level
  while keeping the response-error contract (last-wins) unchanged.
- The all-exhausted "impossible" branch previously asserted
  any_not_found via debug_assert! and silently fell through to
  NotFound in release. Replaced with a tracing::error! that fires in
  release too, so a future PullThroughOutcome variant that forgets
  to set a flag surfaces in production logs rather than fabricating
  a NotFound for content that might exist. (engine.rs:1303-1325)
- Extracted the chain-advance log + counter bump into
  emit_chain_advance() to keep pull_through's cognitive complexity
  under the workspace clippy threshold; the helper colocates the
  WHY of the warn!-vs-info! choice with one named entry point.
- LabeledPermanentlyFailingOrigin folded into PermanentlyFailingOrigin
  via an optional marker (with_marker constructor). The two test
  stubs were 90% duplicate.
- Two comment-rot fixes: metrics.rs module doc said "Eight counters"
  (now nine after origin_fallback); StubOrigin::kind test-only doc
  said "non-None value" but the field is Vec<OriginKind> post-rename.

cargo fmt + clippy --workspace --all-targets -- -D warnings clean;
797/797 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu merged commit bc5e912 into main May 15, 2026
10 checks passed
@alpergundogdu
alpergundogdu deleted the feat/issue-284-multi-origin-fallback branch May 15, 2026 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for a [origins] multi-origin table in config allowing ordered fallback across multiple HTTP or filesystem origin backends

3 participants