Repository navigation
test(sim): drive backend selection deterministically under moonpool - #1710
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 30, 2026 07:11
Add sim/tests/backend_selection_sim.rs. It drives Router::backend_from_request through a BackendSelector the harness owns, over a real BackendMap::with_seed whose backends are built with Backend::new_at, and plays the embedder's half of the reservation rule: dials connect or fail, a failure is released with release_failed_dial, connections close, and the harness keeps its own clock, advanced only by seeded draws. After every step each backend's active_connections must equal the harness's shadow count, and the final drain must bring every count to zero. The sweep runs 64 seeds by default and becomes a step of the per-PR simulation job. backend_selection_simulation_is_deterministic runs eight seeds twice and requires byte-identical traces of every decision (cluster, backend, BackendId slot, sticky answer, reservation, release, close; no instant), plus absolute checks: work happened, a map-seeded policy was drawn, and a fresh backoff window closes one second later on the harness's clock, which a leaked wall-clock read fails. Also remove BackendMap::backend_from_sticky_session, whose only callers were its own tests since the mux reserves through reserve_sticky_backend; the tests now exercise reserve_sticky_backend with no affinity key. The harness selector forwards the affinity key Router::backend_from_request derives to reserve_backend and reserve_sticky_backend, as the mux RegistrySelector does. Document the simulator recipe in doc/testing.md section 5. Closes #1684 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
test/h2-deterministic-backend-selection
branch
from
September 30, 2026 09:22
ac4160e to
e51dfd7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1684
This is the fourth and last PR of the sequence the maintainer set in #1684 (comment): a deterministic simulation that drives backend selection through a selector the simulator owns. It builds on:
L7Proxy::backends;It is based on
origin/main4420d132.Design
sim/tests/backend_selection_sim.rsruns under moonpool-sim, gated on--cfg tokio_unstablelike every other simulator insozu-sim.The simulator owns the selector.
SimSelectorimplementsrouter::BackendSelector:BackendMap::with_seed, seeded from moonpool's RNG, and callsreserve_backend/reserve_sticky_backend.BackendId::new.Router::backend_from_requestwith it, exactly asMux::dial_backendcalls it withRegistrySelector.It plays the embedding proxy's side. A dial either connects or fails, as the seeded RNG decides:
set_connection_timeandretry_policy.succeed.Backend::release_failed_dial, with the map'srng().Its clock is its own. One base
Instantis captured per run, and every call receivesbase + offset. The offset advances only by seeded draws, sometimes past the longest backoff window.Workload::runnever awaits, for the same reason ash2_simulation.rs:BackendMapholdsRcs, and moonpool'sWorkloadfuture must beSend.Each seed picks a policy for each of the two clusters. The seven choices are RoundRobin; Random; LeastLoaded on Connections or ConnectionTime; PowerOfTwo on Connections or ConnectionTime; and HRW. So
PeakEWMAruns on the injected clock.Invariant, checked after every step: each backend's
active_connectionsequals a shadow count the harness keeps (+1 per reservation, −1 per failed-dial release and per close). This is LIFECYCLE §9 invariant 14's "selection reserves, failure releases", observed from outside the core. After the final drain every count is 0.backend_selection_simulation_seed_sweepruns at least 64 seeds (default 64, 400 steps each) and assertssuccessful_runs == seeds.backend_selection_simulation_is_deterministicruns 8 seeds twice each and requires byte-identical traces and equal counts.The trace records decisions only:
BackendIdslot, backend, and sticky answer;It never holds an
Instant.git grep -q mio -- sim/Cargo.tomlexits 1: a selector holds no socket, so the simulator needs nomio.CI: the new sweep is a step of the per-PR
udp-simulationjob, next to the UDP, TCP-preread and H2 sweeps.Two nits from the #1709 review are folded in:
BackendMap::backend_from_cluster_idis removed.BackendMap::backend_from_sticky_sessionis removed. Since refactor(mux): selection reserves, a failed dial releases #1709 its only callers were its own three tests, which now exercisereserve_sticky_backend. The test that finds a recorded backend used to accept any connected result; it now asserts that it reachedmycluster-2and that the reservation was taken. This is a library API removal (CHANGELOG ➖ Removed), with no runtime change.Determinism evidence
I replayed seed
0x1684twice withSOZU_BACKEND_SELECTION_SIM_SEED=0x1684 RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked --test backend_selection_sim backend_selection_simulation_seed_sweep -- --nocapture, then kept the trace lines:The trace holds 243 selections, 43 failed-and-released dials, 104 closes and 75 sticky answers. It opens with:
Red, then green
For each mutation below I changed the code, quoted the failing assertion, then restored it. Both tests pass afterwards.
now()returnsInstant::now()(a leaked wall clock):seed=0x1684: the window must close one second later on the harness's clock(andseed=0x0: …in the sweep).BackendMap::new()replaces the seededBackendMap::with_seed, with the draw still consumed so every other draw stays aligned:seed=0x1684: one seed must yield one byte-identical trace.Backend::reserve_connectionwithout its increment (inlib/src/backends.rs):seed=0x1684 step=2: slot 1 (cluster-b-0) holds 0 connections, the reservations, releases and closes this harness made add up to 1(andseed=0x0 step=0in the sweep).Allocations
This PR changes no code on the dial path. I measured with a throwaway probe (
test_allocations::allocations()aroundMux::dial_backend, plussize_of), not committed:origin/main4420d132(as measured for #1709)Mux::dial_backend, 3 runsbackend_selection_allocates_nothing_in_steady_state)stamping_a_dialled_backend_allocates_nothing)Backend/BackendList/BackendMap/BackendChange/BackendDelta/MuxClearProposed follow-up: the three reservation-leak exits
LIFECYCLE §9 invariant 14 names three exits of
Mux::dial_backendpast a successfulconnect(2)that drop the connection without releasing its reservation:Connection::new_h2_clientreturningNone, when the buffer pool is exhausted (BackendConnectionError::MaxBuffers).Connection::start_stream, when the backend's maximum concurrent streams is reached.register_socketrollback, which releasesactive_requestsand the gauges but notactive_connections.Before #1709 they leaked the count
Backend::try_connecttook; now they leak the reservation, the same+1.Proposal: one small PR.
BackendDelta { slot: backend.slot(), change: BackendChange::ConnectionClosed }before returning. This is the same release a closing connection emits, applied by the drain before the next selection.active_connections == 0afterapply_all:register_socket.Validation
Run on head
ac4160e8, based on4420d132. Each exit code was checked directly, never through a pipe:cargo +nightly fmt --all -- --check-> exit 0cargo clippy --all-targets --locked -- -D warnings-> exit 0cargo clippy --all-targets --all-features --locked -- -D warnings-> exit 0cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings-> exit 0RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked-> exit 0cargo test --workspace --locked --exclude sozu-e2e-> exit 0 (1869 passed, 0 failed, 17 ignored, all ignores already on main)RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked-> exit 0 (33 passed, both new tests included)cargo test -p sozu-e2e --locked -- --skip tests::fuzz_tests::-> exit 0 (522 passed)cd fuzz && cargo +nightly fuzz build-> exit 0python3 .github/scripts/check_doc_citations.py-> exit 0python3 .github/scripts/check_doc_citations.py --base origin/main-> exit 0python3 .github/scripts/check_doc_citations.py --self-test-> exit 0python3 .github/scripts/check_doc_citations.py --root .github/scripts/testdata/citations-> exit 1 (the broken fixture must fail)cargo build --all-features, thencargo clippy --all-features --all-targets -- -D warnings) passed on push.Docs:
doc/testing.md§5 gains the simulator recipe, a taxonomy row and the run command. The sim-harness list in thesim/Cargo.tomlcomment is updated. CHANGELOG has ✨ Added for the simulation and ➖ Removed forbackend_from_sticky_session.