Skip to content

feat(node): add admin_v1_drain + decdn node drain subcommand (#244) - #382

Merged
thiras merged 3 commits into
mainfrom
feat/244-node-drain
May 1, 2026
Merged

thiras merged 3 commits into
mainfrom
feat/244-node-drain

Conversation

@thiras

@thiras thiras commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Test plan

  • `cargo build -p decdn-node` — clean
  • `cargo clippy -p decdn-node --all-targets -- -D warnings` — clean (anti-panic policy honoured)
  • `cargo fmt --check` — clean
  • `cargo nextest run -p decdn-node` — 221/221 pass, including the new tests `admin_v1_drain_returns_initiated_true`, `cli_drain_rejects_zero_timeout`, `cli_drain_surfaces_connection_refused`, `drain_trigger_fire_then_wait_resolves`, `drain_trigger_double_fire_does_not_deadlock_wait`, `admin_drain_fires_trigger_and_returns_initiated`, and the updated `shutdown_signal_display_is_stable`.
  • `cargo run -p decdn-node -- node drain --help` renders the new subcommand with full help text.

Closes #244.

🤖 Generated with Claude Code

Adds a loopback JSON-RPC entry point for graceful shutdown that mirrors
the existing admin_v1_peersList / decdn node peers pair. Fire-and-forget
semantics: the RPC fires a runtime DrainTrigger and returns
initiated=true immediately, after which the runtime runs the same
shutdown sequence SIGTERM triggers (stops accept loops, awaits in-flight
ProtocolHandler::shutdown, flushes cache, drains within
SHUTDOWN_DEADLINE). Operators observe completion via process exit
(systemd/K8s) or by polling decdn node health until ECONNREFUSED.

ADR 025 already pre-allocated this method name in its "Future methods"
list, so no new ADR is needed. The ShutdownSignal enum gains an
AdminDrain variant whose Display impl renders as "admin-drain" for the
existing structured "shutdown signal received" log line.

Drive-by: fix a pre-existing test-target compile error in
runtime/reload.rs's test fixture. PR #312/#372 added decompress and
pinned_hashes fields to ResolvedCache but didn't update this fixture,
which blocked cargo nextest. Mirrors the defaults used at reload.rs:744.

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

@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 introduces a graceful shutdown mechanism for the node, accessible via a new admin_v1_drain RPC endpoint and a decdn node drain CLI command. It utilizes a DrainTrigger based on tokio::sync::Notify to signal the runtime loop to initiate shutdown. The review feedback focuses on improving security by preventing the potential leakage of credentials in error messages that include raw URLs.

Comment thread crates/node/src/commands/node.rs Resolved
Comment thread crates/node/src/commands/node.rs Resolved

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

Adds a graceful “drain” control-plane action to the deCDN node, exposed consistently via the loopback admin JSON-RPC surface and the decdn node CLI, and wires it into the runtime’s existing graceful shutdown path.

Changes:

  • Add admin_v1_drain RPC method backed by a DrainTrigger and a DrainResponse { initiated: true } response.
  • Add decdn node drain CLI subcommand that calls admin_v1_drain, with consistent timeout validation and error classification.
  • Wire drain-triggered shutdown into the runtime’s main select loop and extend shutdown logging with signal=admin-drain; fix a test-fixture compile break in runtime reload tests.

Reviewed changes

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

Show a summary per file
File Description
crates/node/src/admin.rs Adds DrainTrigger, DrainResponse, and admin_v1_drain RPC wiring into the admin server state/trait/impl.
crates/node/src/runtime/mod.rs Installs a drain trigger in the runtime and maps it to a new ShutdownSignal::AdminDrain with stable display string.
crates/node/src/commands/node.rs Implements decdn node drain command path, including timeout validation and JSON/plain output.
crates/node/src/commands/mod.rs Re-exports the new drain command function from the commands module.
crates/node/src/cli/node.rs Adds the Drain subcommand and its CLI args/help text.
crates/node/src/cli/mod.rs Re-exports DrainArgs alongside other node subcommand args.
crates/node/tests/admin_peers.rs Extends integration tests to cover admin_v1_drain and the CLI drain error/timeout paths; updates admin state construction.
crates/node/src/runtime/reload.rs Fixes a test-target compile error by adding missing ResolvedCache fixture fields (decompress, pinned_hashes).

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

Comment thread crates/node/src/admin.rs Resolved
Comment thread crates/node/src/admin.rs Resolved
Comment thread crates/node/src/cli/node.rs Resolved
thiras and others added 2 commits May 1, 2026 15:24
Three doc-only fixes from review:

- AdminState::drain_trigger doc said "Always Some" but the field is
  Arc<DrainTrigger> not Option<…>. Reword to "always present" and call
  out the contrast with the Option-typed announce_trigger / reload_hook.

- DrainResponse and DrainArgs docs called the admin server "the first
  surface to stop" during shutdown, but runtime::run signals metrics
  before admin. Both are stopped before router.shutdown, so "one of the
  first surfaces" is the accurate phrasing.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
)

- `admin_v1_drain` trait rustdoc: SHUTDOWN_DEADLINE bounds only the
  post-router task drain, not router.shutdown() — a stuck
  ProtocolHandler::shutdown hangs the runtime, the deadline doesn't
  cut it short. Previous wording implied the deadline gated the whole
  router-shutdown path.
- `drain()` handler: comment distinguishes "trigger fired" from
  "runtime is now in the AdminDrain branch". If SIGTERM/SIGINT raced
  the RPC, the runtime has already passed `drain_trigger.wait()` and
  this `fire()` lands in an abandoned arm; functionally fine but a
  future reader shouldn't infer causation from `initiated=true`.
- Coalesce test renamed `_double_fire_` → `_repeated_fire_` and adds a
  third `fire()` so it's robust against a "burns one permit per fire"
  reimplementation. Dropped the "oneshot" framing — a oneshot swap
  fails to compile (Sender::send consumes self) so that wasn't the
  regression class the test actually defends against.
- `NodeCommand::Drain` variant rustdoc echoes the connection-closes-
  before-exit warning that `DrainArgs` already carries, so operators
  reading just the variant doc get the same heads-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thiras
thiras merged commit 2bea4c9 into main May 1, 2026
9 checks passed
@thiras
thiras deleted the feat/244-node-drain branch May 1, 2026 12:53
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 a decdn node drain subcommand that stops accepting new connections and waits for in-flight transfers to complete before shutdown

3 participants