Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

picker: modify constants so donweights occur rarely #1266

Merged
merged 1 commit into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion picker/picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (mwr *remotesWeightedRandom) Select(excludes ...string) (api.Peer, error) {

// bias to zero-weighted remotes have same probability. otherwise, we
// always select first entry when all are zero.
const bias = 0.1
const bias = 0.001

// clear out workspace
mwr.cdf = mwr.cdf[:0]
Expand Down
34 changes: 34 additions & 0 deletions picker/picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,40 @@ func TestRemotesLargeRanges(t *testing.T) {
}
}

func TestRemotesDownweight(t *testing.T) {
peers := []api.Peer{{Addr: "one"}, {Addr: "two"}, {Addr: "three"}}
index := make(map[api.Peer]struct{}, len(peers))
remotes := NewRemotes(peers...)

for _, peer := range peers {
index[peer] = struct{}{}
}

for _, p := range peers {
remotes.Observe(p, 1)
}

remotes.Observe(peers[0], -1)

samples := 100000
choosen := 0

for i := 0; i < samples; i++ {
p, err := remotes.Select()
if err != nil {
t.Fatalf("error selecting remote: %v", err)
}
if p == peers[0] {
choosen++
}
}
ratio := float32(choosen) / float32(samples)
t.Logf("ratio: %f", ratio)
if ratio > 0.001 {
t.Fatalf("downweighted peer is choosen too often, ratio: %f", ratio)
}
}

var peers = []api.Peer{
{Addr: "one"}, {Addr: "two"}, {Addr: "three"},
{Addr: "four"}, {Addr: "five"}, {Addr: "six"},
Expand Down