Skip to content

fix(backends): preserve backend identity and incarnation; document SetHealthCheck's draining boundary - #1839

Merged
FlorentinDUBOIS merged 6 commits into
mainfrom
fix/backend-identity-and-health-policy
Oct 3, 2026
Merged

FlorentinDUBOIS merged 6 commits into
mainfrom
fix/backend-identity-and-health-policy

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Two commits, one per issue. They touch the same files (lib/src/backends.rs, lib/src/health_check.rs, lib/src/udp/health.rs, lib/src/server.rs).

fix(backends): keep backend identity and incarnation through removal and health checks (#1821)

CAUSE (removal): lib/src/backends.rs:1398 BackendList::remove_backend matched on address only -> BackendMap::remove_backend (backends.rs:749) -> Server::remove_backend (server.rs:2974) -> SYMPTOM: removing id A also removed same-address sibling B from the worker. The e2e test showed same-address sibling reached: false; response after targeted removal: None, while ConfigState (keyed on (backend_id, address), command/src/state.rs:2255) still listed B.

CAUSE (results): lib/src/health_check.rs:598 / lib/src/udp/health.rs:517 find_backend(&address) -> the result went to the first backend at the probed address, and to whatever backend was live at that address when it completed -> SYMPTOM: a failed probe of B marked A DOWN, and a probe started before a remove completed after the same id/address was re-added and marked the new backend DOWN.

Fix:

  • BackendList::remove_backend(backend_id, address) and BackendMap::remove_backend(cluster_id, backend_id, address) remove only the matching (backend_id, address) entry and return bool. Server::remove_backend passes the requested id. Metrics cleanup is still keyed on the requested id.
  • Each HTTP/TCP InFlightCheck and UDP InFlightProbe holds a Weak<RefCell<Backend>> to the backend it was launched for. record_check_result and UdpHealthChecker::record apply a result only if that exact entry is still in the cluster's list (BackendList::find_incarnation, a pointer comparison that is safe because the Weak keeps the allocation alive, so it cannot be reused). A result for a removed backend is dropped. A re-added backend is a new entry: it starts healthy, and the next cycle probes it, because duplicate in-flight probes are now detected per entry rather than per backend_id.
  • RemoveCluster still removes the whole cluster: cluster_removal_requests sends one RemoveBackend per ConfigState backend with its real backend_id (command/src/state.rs:735-748), and remove_cluster_objects then calls BackendMap::remove_cluster (server.rs:2956). test_remove_cluster_stops_http_routing, …_tcp_routing and test_readded_cluster_does_not_reuse_removed_backend pass.
  • Out of scope, left unchanged: BackendMap::close_backend_connection and BackendMap::has_backend still look backends up by address. Neither has a caller in the tree outside backends.rs debug assertions. The remaining find_backend call outside backends.rs is a router test.
  • Docs: doc/health_checks.md (How it works), doc/configure.md (UDP health), doc/configure_cli.md (backend remove). CHANGELOG under Fixed, marked BREAKING (library API) because the public remove_backend signatures changed.

docs(health-check): SetHealthCheck's draining policy boundary (#1824)

There is no production change: the behaviour is intended. doc/health_checks.md now says that SetHealthCheck acknowledges the new policy without cancelling probes already in flight. Those probes finish under the policy they captured at launch, only later probes use the new one, and the next probe is scheduled from the existing last launch time. The real-socket e2e test in_flight_probe_finishes_with_old_policy_then_future_probe_uses_new_policy pins this. The file's set_health_check helper now returns the worker's answer to the request it sent instead of draining all answers. The whole health_check_mode_tests module was re-run. CHANGELOG under Changed.

Red / green evidence

  • On unmodified origin/main (bd19a78), with the audit's prepared tests:
    • health_check::tests::late_result_after_remove_and_readd_same_identity_does_not_mutate_replacement failed: "a late result from the removed generation must not mark its replacement DOWN".
    • udp::health::tests::probe_result_updates_the_backend_id_that_was_probed failed: left: (false, true) right: (true, false).
    • e2e test_removing_one_backend_id_keeps_same_address_sibling failed: sibling not reached, response None.
    • The Document and test SetHealthCheck's draining policy boundary #1824 e2e passed on main, as expected for a test that pins intended behaviour.
  • The adapted lib tests cannot run on unmodified main because the record/remove_backend signatures changed. Instead they were shown red with a mutation that restores the pre-fix address lookup in find_incarnation: all four failed (health_check + udp::health × {sibling, late result after remove/re-add}). The late-result tests also hold a strong Rc, as a session would, and include a positive control: a result for the live backend does apply.
  • Document and test SetHealthCheck's draining policy boundary #1824 e2e red under mutation: making SetHealthCheck call health_checker.remove_cluster (cancelling in-flight probes) fails it with NotConnected at health_check_mode_tests.rs:580.
  • All mutations were reverted. The tree contains no MUTATION marker from this change.

Validation (toolchain 1.93.1)

  • Verified: cargo +nightly fmt --check -> exit 0
  • Verified: cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings -> exit 0, 0 warnings
  • Verified: RUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib -p sozu-command-lib -p sozu -> exit 0
  • Verified: cargo +1.93.1 test -p sozu-lib --lib --all-features --locked -> test result: ok. 1597 passed; 0 failed
  • Verified: cargo test -p sozu-e2e --locked --features opentelemetry,splice,simd -- --test-threads=1 tests::health_check_mode_tests:: tests::remove_cluster_tests:: -> test result: ok. 11 passed; 0 failed
  • Verified, earlier batch: tests::metrics_lifecycle_tests::, tests::tests::test_http_behaviors and tests::tests::test_blue_green all passed.
  • Verified: python3 .github/scripts/check_doc_citations.py --root . --base origin/main -> exit 0
  • Flake note: in the first e2e batch, test_remove_health_check_restores_down_backend failed once. Its precondition request got a client EAGAIN while the shared host was at load average 65–86 (success=1 failure=1 down=1). It then passed 3/3 in isolated runs and again in the second full-module batch. It does not involve backend removal.
  • The full e2e suite was not run locally (shared host); CI runs it. No UDP-health e2e test exists, so UDP coverage is unit-level only.

Fixes #1821
Fixes #1824

…al and health checks

AddBackend and ConfigState identify a backend by (backend_id, address)
and admit two ids at one address, but the worker dropped that identity:

- RemoveBackend removed every backend at the address, so removing id A
  answered ok while its sibling B, still listed by ConfigState, stopped
  receiving traffic;
- HTTP/TCP and UDP health results were applied to the first backend at
  the probed address, so a failed probe of B marked A DOWN;
- a probe in flight when its backend was removed could complete after
  the same id and address were added again and mark the new backend
  DOWN.

BackendList::remove_backend and BackendMap::remove_backend now take the
backend id and remove only the backend with that id at that address.
Each HTTP/TCP and UDP probe captures a Weak handle on the backend it was
launched for and applies its result only while that entry is still in
the cluster's list, so a result for a removed incarnation is discarded
and never lands on a sibling or on a re-added replacement. In-flight
deduplication is per incarnation, so the replacement is probed on the
next cycle. RemoveCluster still drops the whole cluster list.

Fixes #1821

Signed-off-by: Florentin Dubois <[email protected]>
…y boundary

SetHealthCheck validates and stores the replacement policy and
acknowledges it without cancelling the probes already in flight. Each
InFlightCheck captured its mode and request, timeout, accepted statuses
and thresholds at launch, so it completes under the old policy and can
still change a backend's health after the acknowledgement; only probes
launched afterwards use the new policy. Updating a policy launches no
probe and keeps the last launch time, so the new interval counts from
it.

Document that boundary in doc/health_checks.md and pin it with a
real-socket e2e test: two HTTP probes in flight finish with the old
accepted status and failure threshold after a TCP policy is
acknowledged, then a later probe uses TCP mode and the new threshold.
The file's set_health_check helper now returns the worker's answer to
the request it sent. No behaviour change.

Fixes #1824

Signed-off-by: Florentin Dubois <[email protected]>
The HTTP and UDP health checkers skip a backend while a probe is in
flight for the same backend incarnation, so a probe still draining for
a removed backend never delays the first probe of a replacement re-added
with the same id and address. No test held that: comparing in-flight
probes by backend id again passed the whole suite. Two unit tests now
launch a real probe, remove and re-add the backend, and assert the
replacement is probed on the next cycle.

BackendMap::remove_backend now logs a warning when the cluster exists
but no backend matches the requested (id, address), and the
hash_backend comment no longer claims the address is the removal key.
The CHANGELOG notes that a RemoveBackend with a mismatched id now
removes nothing.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 84f12bb into main Oct 3, 2026
23 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 4, 2026
…1856)

* fix(load-balancing): give a shared address the share of one backend

Two backend ids at one address are distinct backends since #1839
(identity `(backend_id, address)`), but only HRW gave their address the
share of one endpoint: round robin, random, least loaded, power of two
and Maglev gave it one share per id (A,B@X + C@Y, equal weights: X took
about 2/3 of the selections instead of 1/2).

`BackendList` now hands every policy one candidate per address, after
the tier, retry-exclusion and shard filters. The representative is the
heaviest eligible id, the first in list order among equal weights
(`outranks_at_address`), which is exactly the id HRW already picked, so
HRW is unchanged and a down or backing-off id never hides an eligible
sibling. The address weighs what its representative weighs. Maglev
builds one table entry per distinct address, weighted by the heaviest of
its ids. Shuffle sharding ranks and counts distinct primary addresses
and puts every primary id of a selected address in the shard.

A list without a shared address skips the collapse (flag maintained on
the control plane); with one, the collapse is a quadratic scan of the
candidates into a reserved scratch buffer, without allocating.

Signed-off-by: Florentin Dubois <[email protected]>

* fix(load-balancing): assert the shared-address flag in the list invariants

check_invariants now recomputes shares_address and asserts it matches, so a backend-list mutation that bypasses add_backend/remove_backend fails loudly instead of silently skipping the collapse. The CHANGELOG entry links #1856 and states that the keyless round-robin fallback of HRW and Maglev collapses too.

Signed-off-by: Florentin Dubois <[email protected]>

* perf(load-balancing): link same-address ids on the control plane

The collapse compared every candidate with every other one and the
shard ranking re-derived each address's representative by scanning the
list, so one shared pair put every selection of the cluster on a
quadratic path (release, round robin, 1000 backends: 3.9 us -> 2.5 ms).

`refresh_siblings` now builds, on every list or configuration mutation,
a sibling ring linking the ids of each address and each id's shard
rank. The collapse walks only the rings of candidates that have a
sibling, looking siblings up among the candidates by binary search, and
compute_shard reads the precomputed ranks and walks rings to take in a
shared address's other ids. A list without a shared address selects
exactly as before; one with a shared pair is back to linear (1000
backends: 5.2 us). A test counts the shared-address work per selection
and fails on a per-candidate scan.

LEAST_LOADED and POWER_OF_TWO now compare an address's load as the sum
of its ids' connection or request counts, walked through the same ring
(`Candidates::siblings`), without allocating; CONNECTION_TIME still
reads the representative's own average.

Signed-off-by: Florentin Dubois <[email protected]>

* fix(load-balancing): tolerate a directly edited backend list

`BackendList.backends` is public, so a library user may push or pop a
backend without add_backend/remove_backend, leaving the sibling state of
another length. The collapse is now skipped and compute_shard ranks
every primary as its own address when that state's length differs from
the list, and Candidates::with_siblings ignores a ring of another
length, so a selection never indexes it out of bounds. Lists kept in
sync select exactly as before.

State the cost bound precisely: linear in the candidates plus, per
shared address of g ids, up to g² ring steps (refresh_siblings: linear
plus g² per shared address). Public docs of Candidates and Maglev no
longer link the private outranks_at_address, which failed cargo doc
under -D warnings.

Signed-off-by: Florentin Dubois <[email protected]>

---------

Signed-off-by: Florentin Dubois <[email protected]>
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.

Document and test SetHealthCheck's draining policy boundary Preserve backend identity and incarnation across removal and health-check completion

1 participant