Repository navigation
fix(fault): each rule draws from its own PRNG (#510) - #584
Merged
Merged
Conversation
FaultController rolled probability from one PRNG shared by every rule, so a rule's outcomes depended on how many rolls the rules before it had taken. A fixed seed therefore reproduced a run only while the whole sequence of requests was unchanged: adding a request upstream, or a retry, shifted every later roll — including for rules that request never touched. That is the opposite of what a deterministic emulator is for. A rule now has its own stream, so its outcome sequence depends only on how many requests that rule itself matched. Streams are seeded from the controller's seed and the rule's index and re-derived whenever a config is armed, so re-arming resets them exactly as it resets each rule's fired count. Keying by index rather than by the rule's matchers is deliberate: two rules with identical matchers are legitimate and useful — fail with one error, then with another — and a matcher hash would make them share a stream, which is the coupling this removes. A matcher hash would also make outcomes depend on matcher text, so editing a path suffix would silently reshuffle results. The cost is that reordering rules changes their outcomes, which the field now documents. Seed and index are mixed through FNV rather than added. Measured, both schemes decorrelate adjacent rules equally well — Go's source scrambles nearby seeds on its own — so the mixing buys no measurable independence. It is there because that scrambling is unspecified rather than documented, and because seed+i makes one controller's streams collide with an adjacent controller's. Compatibility: a consumer asserting on specific outcomes of a seeded intermediate probability must re-record them. Nothing changes for probability 0 or 1, or for a rule bounded by times, which needs no roll at all.
6 tasks done
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.
The gap
FaultControllerrolledprobabilityfrom one PRNG shared by every rule, so arule's outcomes depended on how many rolls the rules before it had taken. A fixed
seed reproduced a run only while the whole sequence of requests was unchanged:
adding a request upstream, or a retry, shifted every later roll — including for
rules that request never touched. That is the opposite of what a deterministic
emulator is for, and the caveat documenting it was the largest asterisk on the
fault tier.
The fix
Each rule gets its own PRNG stream, so its outcome sequence depends only on how
many requests that rule matched. Streams are re-derived whenever a config is
armed, so re-arming resets them exactly as it resets each rule's
firedcount —which matters for a fixture that re-arms between phases.
The index identifies a stream, not a hash of the rule's matchers — the decision
#510 asks to be made in the issue. Two rules with identical matchers are legitimate
and useful (fail with one error, then with another), and a matcher hash would make
them share a stream, which is the exact coupling this removes. A matcher hash would
also make a rule's outcomes depend on its matcher text, so editing a path suffix
would silently reshuffle results — a worse surprise than reordering rules, which is
at least visible in the config. The cost is that reordering rules does change their
outcomes, and
FaultRule.Probabilitynow says so.On the FNV mixing, and a correction
Seed and index are mixed through FNV rather than added. My earlier comment on #510
justified this by claiming adjacent seeds give correlated streams in Go's default
source. That claim is wrong and is corrected in
#510 (comment). Measured
over 5000 draws per stream across 60 base seeds, worst-case |Pearson r| between
adjacent indices:
seed+i0.030, FNV 0.036. Neither scheme shows a correlation theother lacks.
The mixing stays, for reasons that survive measurement: that scrambling is an
unspecified property of
math/randrather than a documented one, so mixing meansper-rule independence does not rest on it; and
seed+imakes one controller'sstreams collide with an adjacent controller's, since
seed+1at index 0 andseedat index 1 are the same source. Both the code comment and the test now say what was
actually measured rather than asserting a correlation that does not hold.
Compatibility
A consumer asserting on specific outcomes of a seeded intermediate
probabilitywill see different results and must re-record them. Nothing changes for
probabilityof 0 or 1, or for a rule bounded bytimes, which needs no roll atall. In-repo this cost is zero: every
probabilitythat actually reachesInjectFaultin the test suite is1.0or0.0. The one0.5(config_test.go)only asserts the field round-trips through
ToFaultConfigand never runs a request.Tests
Five new tests in
emulator/fault_prng_test.go, driving unboundedProbability: 0.5rules so the assertions are on PRNG traces rather thanTimesbounds:rule 1 matched 0 or 7 requests, and unchanged when the two are interleaved.
trace contains both outcomes), and a different seed does not.
appended after it.
Mutation tested, 3 mutants:
rngs[i]→rngs[0](restoring #510) CAUGHT;the
rngsrebuild dropped fromUpdateConfigCAUGHT;seed+int64(i)in placeof the FNV mix EQUIVALENT, with the correlation measurement above as its written
proof — the mutant is observably indistinguishable at this scale, which is precisely
what the corrected comment now claims.
Docs
The ordering caveat and its
#510pointers are gone fromFaultRule.Probability,docs/services.mdanddocs/testing-guide.md, replaced by what now holds: per-rulestreams, keyed by index, reset on arming.
Verification
gofmt -l .,go build ./...,go vet ./...,golangci-lint run ./...(0issues),
make test(race),make docs-reference docs-reference-check docs-versions,npm --prefix docs run build,go vet -C test/e2e ./...,go test -C test/e2e ./...— all clean.Closes #510