Skip to content

feat(nodes): drain transcode nodes before they stop - #2340

Merged
Quick104 merged 4 commits into
mainfrom
feat/node-drain
Oct 12, 2026
Merged

Quick104 merged 4 commits into
mainfrom
feat/node-drain

Conversation

@Quick104

@Quick104 Quick104 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2320
Related issue: #1176
Validation tasks: none

Stopping, upgrading or scaling down a transcode node cuts every stream on it. On SIGTERM the node closes its listener and kills FFmpeg within milliseconds, so every viewer on it stalls and only recovers through a fresh transcode elsewhere. Disabling the node first does not help: it drops the node from the pools, which also stops its health checks, so nothing tells the operator when the node is empty. This change lets a transcode node drain: it takes no new work, finishes what it runs, and says when it is safe to stop.

Approach

A node drains for one of two reasons:

  • An administrator's request. POST /api/v2/admin/nodes/{id}/drain and /resume set and clear stream_nodes.drain_requested. The web nodes page has Drain and Resume buttons and a status line: Draining · N transcodes left, then Drained · safe to stop once the node reports it. The request is operational state, so it does not move the node's config_etag.
  • SIGTERM. The node drains for up to SILO_NODE_DRAIN_TIMEOUT (default 10 minutes, 0 turns it off) before the existing shutdown runs. A second signal stops it at once.

While draining, the node:

  • answers 503 node is draining to a new /transcode/start, which both playback retry loops already treat as "try the next node". It still accepts a start that replaces a session it runs, which is how a Jellyfin audio-track switch arrives.
  • refuses download preparation, trickplay, chapter thumbnails and media sampling with the unavailable responses their clients already handle.
  • keeps serving its sessions, including FFmpeg restarts on seeks. Once it has reported drained it rebuilds no lost session and starts no fresh progressive remux, since whoever read that answer may stop it.
  • answers /api/v1/health with 200 and status draining, then drained once no session, job or work request is left.

The node announces each change on the event bus (node_drain_changed), and every API server rechecks it at once. The API stores the reported state in stream_nodes.drain_state. A draining node stays pooled, so its health and GPU load keep counting. New placement, side-work reservations, native v3 capability planning and Jellyfin capability negotiation skip it. The one exception is a session that is already on the node, so a Jellyfin client asking for its master playlist again keeps its stream; once the node reports drained, that client is planned elsewhere. The node reads its own drain request with a one-column query of its row. That read happens on every config load and on node_pool_changed, and it does not reload the config.

Proxies do not drain. A proxy holds no session it could finish on its own, so nothing could say when stopping it is safe, and the drain operations answer 409 for one. The example compose silo-transcode service gets stop_grace_period: 11m. docs/architecture/node-drain.md covers the protocol and the Docker and Kubernetes settings (terminationGracePeriodSeconds, probes). The admin API, probe contract and worker protocol docs are updated.

Neither the Apple nor the Android app uses the admin nodes API, so they need no change; their playback sessions keep running on a draining node like any other client's. Failing API readiness during shutdown is #2343.

Validation

  • Before, on a sandbox with one proxy and two transcode nodes: SIGTERM to the node serving a web-player stream stopped it within 90 ms, and the player stalled and never recovered.
  • After, admin drain: the existing stream kept playing for 167 s without a stall, and a new session went to the other node. The page showed Draining · 1 transcode left, then Drained · safe to stop (desktop and phone) once the viewer stopped. Resume put the node back to Healthy.
  • After, SIGTERM: the existing stream kept playing for 138 s, a new session went to the other node, and the node exited as soon as the stream ended. A second SIGTERM stopped it at once.
  • Unit tests cover planning and reservations, node refusals and health states, the watcher read, the SIGTERM runner, the v2 operations and the web status line. Database tests for the new columns ran against a migrated PostgreSQL.
  • go build, go vet, make lint-changed (0 issues), the web lint, format, build, budget and test runs, and the generated-contract checks pass. The v2 contract diff is additive.
  • The full Go suite passes except internal/mediasample's real-FFmpeg tests, which fail the same way on main with this machine's FFmpeg.
  • Not run: a Kubernetes rolling update.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/issue-2320/

Risks

  • The migration adds two columns with defaults and rewrites no data.
  • A draining node's health status is now draining or drained instead of ok; the status code stays 200, and the probe contract records it as an additive revision, like degraded on readiness. A monitor that matches the body text "ok" would alert during a drain.
  • With the 10-minute default, stopping a busy node waits until its streams end. A second signal, a shorter SILO_NODE_DRAIN_TIMEOUT, or 0 restores the old behavior.
  • Mixed builds during an upgrade: an older API server ignores the health status, and the node's 503 sends it to another node; an older node never reports drained, so the page stays at Draining for it.
  • Known limits, documented: a download preparation sent in the moment before the API learns of a SIGTERM drain falls back to a local encode; drained does not account for prepared downloads stored on the node.
  • Parallel work on dead-node detection and signed node requests touches nearby code. CheckNode, UpdateHealth and ApplyHealth keep their signatures.

Follow-ups

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code, running in T3 Code
  • Tool(s): Claude Code subagents; Codex CLI through T3 Code task delegation
  • Model(s): claude-opus-5-5 (implementation, review finders and verifiers), claude-sonnet-5-5 (review finder and validation check), gpt-6.1-sol (design and adversarial review)
  • Involvement: Fully AI-generated; maintainer review pending
  • Adversarial review: Codex (gpt-6.1-sol) reviewed the design before implementation. Its findings led to node-side enforcement of admin drains, keeping a session's current node eligible for Jellyfin re-planning, and counting in-flight work. A multi-agent Claude review (three finders, four verifiers and a sweep) then checked the diff for bugs, contract and multi-node failure cases. It confirmed 17 defects and cleanups, all fixed, among them: uncounted trickplay, remux and rebuild work; a full config reload on pool events; an upgrade-order break in the node's override query; stale drain state after resume and URL repoint; an undeclared 409; Jellyfin negotiation counting draining nodes; and lost or missing announcements. A background Codex (gpt-6.1-sol) review of the pushed branch then found three more, all fixed: a drained node still started a fresh progressive remux, kept a session's current-node affinity, and was counted in native v3 capability planning. A Kody review found three races, all fixed: health could report drained while a rebuild that had just counted itself started; a sweep answer could overwrite a newer recheck; and lifting a never-set drain request cleared a SIGTERM-draining node's state.

🤖 Generated with Claude Code

A transcode node that is stopped or upgraded no longer cuts its streams.
An administrator can drain a node from the admin API or the nodes page, and a
node that receives SIGTERM drains on its own for up to SILO_NODE_DRAIN_TIMEOUT
(default 10m). A draining node stays pooled and health-checked, takes no new
transcodes, downloads, trickplay, chapter thumbnails or samples, keeps serving
the sessions it runs, and reports draining, then drained, in its health
answer. It announces each change so every API server rechecks it at once.

Refs #2320

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-kody

silo-kody Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 13 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e1547492-37d2-47d2-b0f3-6fe8c61ee8b5

📥 Commits

Reviewing files that changed from the base of the PR and between 1747a12 and d8a2d0f.


📒 Files selected for processing (54)
  • cmd/silo/main.go
  • cmd/silo/node_drain.go
  • cmd/silo/node_drain_test.go
  • contracts/api/v2/openapi.json
  • docker-compose.yml
  • docs/admin-api.md
  • docs/architecture/api-contract.md
  • docs/architecture/node-drain.md
  • docs/architecture/worker-http-protocol.md
  • internal/api/handlers/admin_node_commands.go
  • internal/api/handlers/admin_node_drain.go
  • internal/api/handlers/admin_node_drain_test.go
  • internal/api/handlers/nodes.go
  • internal/api/handlers/playback_v3.go
  • internal/api/handlers/playback_v3_drain_test.go
  • internal/api/router.go
  • internal/api/testdata/media_routes.txt
  • internal/apiv2/admin_node_drain.go
  • internal/apiv2/admin_node_drain_test.go
  • internal/apiv2/admin_nodes_read.go
  • internal/apiv2/document.go
  • internal/apiv2/router.go
  • internal/cache/redis.go
  • internal/contractledger/ledger_test.go
  • internal/jellycompat/dv_strip.go
  • internal/jellycompat/handlers_playback.go
  • internal/jellycompat/hevc_encoding.go
  • internal/jellycompat/remote_transcode_reconstruct_test.go
  • internal/nodeconfig/watcher.go
  • internal/nodeconfig/watcher_drain_test.go
  • internal/nodepool/drain.go
  • internal/nodepool/drain_test.go
  • internal/nodepool/health.go
  • internal/nodepool/planner.go
  • internal/nodepool/proxy_pool.go
  • internal/nodepool/repository.go
  • internal/nodepool/repository_capabilities_test.go
  • internal/nodepool/repository_drain_test.go
  • internal/nodepool/reservations.go
  • internal/nodepool/transcode_pool.go
  • internal/transcodenode/drain.go
  • internal/transcodenode/drain_test.go
  • internal/transcodenode/media_sample.go
  • internal/transcodenode/server.go
  • internal/transcodenode/trickplay.go
  • migrations/sql/20261012003019_add_stream_node_drain.sql
  • web/src/api/types.ts
  • web/src/api/v2/operations.ts
  • web/src/api/v2/schema.ts
  • web/src/hooks/queries/admin/nodeCommands.test.tsx
  • web/src/hooks/queries/admin/nodes.ts
  • web/src/pages/AdminNodes.tsx
  • web/src/pages/adminNodesPresentation.test.ts
  • web/src/pages/adminNodesPresentation.ts

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Quick104 Quick104 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adversarial review (gpt-6.1-sol, reviewed 1ca013e)

Three findings reproduced with production handlers and planners in focused Go tests under -race, using existing FFmpeg stubs. The focused drain and playback race checks also passed. Database tests skipped without a configured test database; web tests and deployed playback, device, and Kubernetes validation were not run.

AI disclosure: gpt-6.1-sol, Codex via T3 Code.

Comment thread internal/transcodenode/server.go Resolved
Comment thread internal/nodepool/drain.go Resolved
Comment thread internal/nodepool/planner.go Resolved
A whole-seconds SILO_NODE_DRAIN_TIMEOUT beyond what time.Duration holds
wrapped around; it now falls back to the default like any invalid value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ning

A node that has reported drained may be stopped at once, so it now refuses a
fresh progressive remux, planning no longer keeps a session on it, and
Jellyfin does not adopt the runtime recorded there. Native v3 capability
planning skips draining nodes, which refuse every new transport.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread internal/nodepool/drain.go Resolved
Comment thread internal/nodepool/drain.go Resolved
Comment thread internal/transcodenode/drain.go Resolved
Comment thread internal/transcodenode/server.go Resolved
… resume

- A node could report drained while a rebuild or remux that had just counted
  itself started FFmpeg. The work check and the latch now share a lock with
  the admission check.
- A sweep answer from before a node began draining could overwrite the
  recheck its announcement caused. Health checks of one node now run one at
  a time.
- Lifting a drain request that was never set cleared the state a
  SIGTERM-draining node had reported, and the node announced nothing to put
  it back. Only lifting a set request clears it now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-kody

silo-kody Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@silo-triage silo-triage Bot added enhancement New feature or request go Pull requests that update go code impact: playback Can't play, stalls, or wrong media priority: P1 Core-path bug; painful workaround — next after P0 labels Oct 12, 2026
@Quick104
Quick104 merged commit f16f51f into main Oct 12, 2026
36 checks passed
@Quick104
Quick104 deleted the feat/node-drain branch October 12, 2026 13:16
Quick104 added a commit that referenced this pull request Oct 12, 2026
Resolves the conflicts with node draining (#2340). The sweep now publishes
through ApplyHealthReport, so that clears the unreachable mark too. A
recheck takes the node's check lock and publishes through the sweep's
publishHealth, so it stores the drain state the node answered with and
cannot overwrite a newer check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code impact: playback Can't play, stalls, or wrong media priority: P1 Core-path bug; painful workaround — next after P0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Stopping a transcode node cuts every stream on it

1 participant