Skip to content

fix(piecerequest): select pieces uniformly in the default policy - #760

Merged
thijmv merged 2 commits into
uber:masterfrom
yuvrajtiwary-bitmesraece:fix/default-policy-uniform-sampling
Nov 20, 2026
Merged

thijmv merged 2 commits into
uber:masterfrom
yuvrajtiwary-bitmesraece:fix/default-policy-uniform-sampling

Conversation

@yuvrajtiwary-bitmesraece

Copy link
Copy Markdown
Contributor

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] with rand.Intn(k + 1), as Algorithm R does.

Tests

TestDefaultPolicySelectsPiecesUniformly is a table test over limits 1, 2 and 3, following the review of #706. Each case calls selectPieces 20,000 times and checks that every piece is selected within 10% of its expected share, which is at least 8 standard deviations. On master all four cases fail; for one piece out of two:

Max difference between 10000 and 0 allowed is 1000, but difference was 10000
Messages:   	piece 0

With the change, go test -race ./lib/torrent/... passes in all 11 packages on Linux (Go 1.27.4), including the scheduler tests; with master's default_policy.go, the new test is the only failure. golangci-lint 2.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

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>
@cla-assistant

cla-assistant Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented Nov 19, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@thijmv
thijmv requested a balanced review from Copilot November 19, 2026 14:04
@thijmv thijmv self-assigned this Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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


// 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix this integer division?

Suggested change
expected := float64(trials * test.limit / test.numPieces)
expected := float64(trials * test.limit) / float64(test.numPieces)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Fixed in eaf73f0.

Co-authored-by: thijmv <59415467+thijmv@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@thijmv thijmv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thijmv
thijmv merged commit 3e2c8c8 into uber:master Nov 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default piece request policy does not sample pieces uniformly

3 participants