Repository navigation
feat(app): [#2697] mark the tracker not ready during shutdown - #2720
josecelano merged 6 commits into
Conversation
Bump torrust-tracker-primitives to 3.1.0 for the new public type. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…le the application is not ready The health check handler reads the injected Readiness first and returns 503 with a 'shutting down' report, so no service probe fans out. AppContainer owns the Readiness. Bump the health check server package to 0.2.0 for the changed start_with_cancellation signature. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… component JobManager::reserve_stop_last reserves the component that stops last and adds that edge to every component spawned afterwards, so a component added later cannot stop concurrently with it. Bootstrap reserves the health check API first and still binds and spawns it last. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…g on shutdown begin_shutdown marks the application not ready and then asks the components to stop. main calls it instead of cancelling directly, and a test checks the order without OS signals. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…e stop-last decision Add an operator section with a Kubernetes readiness probe, a note in the health check package README, and an ADR for the invariant that the health check API stops after every other component. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…-binary evidence Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
ACK 89f15f9 |
da2ce7
left a comment
There was a problem hiding this comment.
This is a post-merge review of the change from #2720 as it stands at 39d6d7e1b on the default branch.
What still runs: AppContainer owns a one-way Readiness flag (packages/primitives/src/readiness.rs). app::begin_shutdown (src/app.rs:130) marks it not ready and only then calls JobManager::cancel. app::run_until_shutdown_signal (src/app.rs:155) calls it on SIGINT or SIGTERM; 989edcc moved the shutdown sequence there from main.rs. While the flag is not ready, /health_check answers 503 with the shutting down report and runs no probe (packages/axum-health-check-api-server/src/handlers.rs:28-34). The health check API is reserved first with reserve_stop_last (src/app.rs:320) and spawned last. spawn_with adds an edge from every later component to it (src/bootstrap/jobs/manager.rs:660-666), so it is cancelled only after every other component has completed in any state, or aborted at the shared deadline. Since the merge, 6e71339 brought udp_ban_cleanup under the manager, which closes the one gap the ADR recorded at the time.
[Minor] The crates that now use Readiness still accept a torrust-tracker-primitives release that does not have it. Readiness first exists in 3.1.0, but packages/axum-health-check-api-server/Cargo.toml:37 and the root Cargo.toml:125 still require version = "3.0.0", that is ^3.0.0. Inside the workspace the path dependency hides this. Once health check crate 0.2.0 is published, two builds resolve primitives 3.0.0 and fail with an unresolved import of torrust_tracker_primitives::Readiness: a consumer whose lockfile already holds 3.0.0, and a -Z minimal-versions build. docs/release-process/workspace-packages.md:73-79 (added in c5e33f8) requires a dependent that starts using a newer API to raise its requirement before it is published. Smallest fix: raise both requirements to version = "3.1.0". The other dependents use nothing new and may stay as they are.
[Minor] The operator readiness contract added in docs/features/shutdown-process/README.md:196-226 recommends an httpGet readiness probe on port 1313 and names the default bind, 127.0.0.1:1313. It does not say that the kubelet connects to the pod IP, not to the container's loopback. Several configurations leave the health check API on that loopback default. One is the built-in default configuration. The others are tracker.container.sqlite3.toml, tracker.container.mysql.toml and tracker.container.postgresql.toml, which share/container/entry_script_sh installs whenever a database driver is set and none of which has a [health_check_api] section. Under any of them the probe is refused, which the contract's own table counts as not ready. With failureThreshold: 1 the pod never becomes Ready and its Service never routes to it. Only tracker.container.no-persistence.toml binds 0.0.0.0:1313. Smallest fix: state that the health check API must listen on an address the kubelet can reach, for example [health_check_api] bind_address = "0.0.0.0:1313" as the no-persistence default does, and that port 1313 should stay off any Service or ingress that publishes it beyond the cluster, because a ready answer lists every registered service binding. The fragment would also read better with terminationGracePeriodSeconds shown at pod level, apart from the container's readinessProbe.
[Nit] The automatic stop-last edge makes spawn fail in two cases that a manager without a stop-last component accepts, and neither case is documented. First, after the stop-last reservation is dropped unspawned, self.stop_last still holds its id. Every later spawn then pushes a target that release_if_reserved has already removed, so add_stop_before returns UnknownStopBeforeTarget (src/bootstrap/jobs/manager.rs:437-438) and the component is dropped without running. The doc at manager.rs:576-577 says only that the dropped reservation and its edges are released. Second, after cancel() every spawn carries a target, so add_stop_before returns ShutdownRequested (manager.rs:444-446). On a plain manager the same spawn succeeds with a cancelled token, as Reservation::cancellation_token (manager.rs:257-258) and the test at manager.rs:1135 describe. The startup code at 39d6d7e1b reaches neither case: the health check reservation is consumed by the last start call, and nothing spawns after a startup failure or after cancel(). The dropped-reservation test at manager.rs:2014 spawns before it drops, so it does not cover the first case. Smallest fix: in spawn_with, add the automatic target under the registry lock only while the stop-last component is still registered and shutdown has not been requested.
Checked and found sound:
- The flag uses Release/Acquire and never returns to ready.
- Marking runs before cancelling, and the registry mutex makes the mark visible to every component that observes its token.
mark_finishedruns on every outcome, so a failed or panicked predecessor still releases the health check API, and a hung one leaves it serving503until the deadline aborts it with exit code 1.- The edges cannot form a cycle: they point only at earlier reservations, and
reserve_stop_lastasserts that it comes first. - There is no self-edge and no duplicate edge.
- A failed startup never binds the health check API, so it never advertises readiness.
- The
503status and body match the documented report. - The test
Environmentstarts a restarted server ready. - The version bumps fit the changes: 3.1.0 for an added type, 0.2.0 for the changed
start_with_cancellationparameters, with the root pin and lockfile updated. - The readiness contract tests and stop-last manager tests still cover this behaviour at the tip.
- The health check API's own drain runs after the others and outside the
max(H, U) + L < Prule. The ADR records this, and the drain stays short because a not-ready answer returns at once.
Summary
Implements sub-issue SI-21 of the shutdown EPIC: while the tracker shuts down, the health check API now reports not ready instead of probing services that are being stopped. Closes #2697.
On SIGTERM or SIGINT,
app::begin_shutdownfirst marks a one-wayReadinessflag as not ready and only then cancels the components. While not ready,/health_checkanswers503with{"status":"Error","message":"shutting down","details":[]}before touching theRegistar, so no probe fan-out happens. The health check API is reserved first withJobManager::reserve_stop_lastand spawned last, and the manager adds a stop-before edge so it is stopped after every other component. It keeps answering503for the whole drain. A failed or panicked predecessor still releases it, and a hung one keeps it serving503until the shared deadline aborts everything (exit 1).Changes
torrust-tracker-primitives3.0.0 -> 3.1.0: newReadiness(default ready,is_ready(),mark_not_ready()).torrust-tracker-axum-health-check-api-server0.1.0 -> 0.2.0: the router takesReadiness, answers503while not ready, and the testEnvironmentexposes it. The root dependency pin is updated.src/bootstrap/jobs/manager.rs:reserve_stop_lastand automatic stop-before edges to the stop-last component.src/app.rs,src/main.rs,src/container.rs: readiness owned byAppContainer,begin_shutdown, health check API reserved first and spawned last.readinessProbeexample) indocs/features/shutdown-process/README.md, the health crate README, a new ADR (20261104120915_stop_the_health_check_api_last_to_report_not_ready_during_shutdown.md) with its index row, the issue spec with the recorded design decisions, andmanual-verification-evidence.md.Validation
503without any probe running,200with the probe running,503on every later request, stop-last ordering, panicked and hung predecessors, dropped reservation, and a multi-thread test proving readiness is marked before any component observes cancellation (mutation-checked by swapping the order).manual-verification-evidence.md.linter all, the pre-commit checks and the pre-push suite pass. No temporary local-only patches are part of this branch.Known limits (follow-ups)
udp_ban_cleanupjob has no component id and is not ordered by the stop-last edge.Registarstill lists stopped services.🤖 Generated with Claude Code