Repository navigation
Conversation
The default policy picks the pieces to request with reservoir sampling, but it drew the replacement index with rand.Intn(k), which can never be the index of the current piece. The first piece after the reservoir filled was therefore always selected, and earlier pieces were selected less often than they should be. With a limit of 1, the first candidate was never requested at all. Draw the index from [0, k] with rand.Intn(k+1), as Algorithm R does. Fixes uber#697 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the reservoir-sampling correction and its accompanying distribution tests.
No open issues 🎉
What changed in this PR
Fixes #697 by correcting reservoir sampling in Kraken’s default piece request policy so eligible pieces have equal selection probability.
Changes:
- Uses
rand.Intn(k + 1)to include the current candidate in the replacement draw. - Adds four distribution test cases covering request limits of 1, 2, and 3, each using 20,000 trials.
| File | Description |
|---|---|
lib/torrent/scheduler/dispatch/piecerequest/default_policy.go |
Corrects the reservoir sampling bound to eliminate selection bias. |
lib/torrent/scheduler/dispatch/piecerequest/default_policy_test.go |
Adds table-driven tests checking approximately uniform piece selection. |
Review effort: Balanced
thijmv
reviewed
Nov 19, 2026
|
|
||
| // Every piece should be selected in limit/numPieces of the trials. | ||
| // The allowed deviation is at least 8 standard deviations. | ||
| expected := float64(trials * test.limit / test.numPieces) |
Collaborator
There was a problem hiding this comment.
Can we fix this integer division?
Suggested change
| expected := float64(trials * test.limit / test.numPieces) | |
| expected := float64(trials * test.limit) / float64(test.numPieces) |
Co-authored-by: thijmv <59415467+thijmv@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
Fixes #697.
The default piece request policy picks pieces with reservoir sampling, but it drew the replacement index with
rand.Intn(k). That index can never be the current piece's own, so the first piece after the reservoir filled always went into the reservoir, and earlier pieces were selected less often than they should be. With a limit of 1, the first candidate was never requested at all.This draws the index from
[0, k]withrand.Intn(k + 1), as Algorithm R does.Tests
TestDefaultPolicySelectsPiecesUniformlyis a table test over limits 1, 2 and 3, following the review of #706. Each case callsselectPieces20,000 times and checks that every piece is selected within 10% of its expected share, which is at least 8 standard deviations. Onmasterall four cases fail; for one piece out of two:With the change,
go test -race ./lib/torrent/...passes in all 11 packages on Linux (Go 1.27.4), including the scheduler tests; withmaster'sdefault_policy.go, the new test is the only failure.golangci-lint2.15.0 reports no issues for the package.#706 proposed the same one-line change and was closed after it went stale; this PR is a separate implementation with the table test asked for there.
🤖 Generated with Claude Code