Skip to content

Commit

Permalink
picker: modify constants so donweights occur rarely
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Morozov <lk4d4math@gmail.com>
  • Loading branch information
LK4D4 committed Jul 28, 2016
1 parent 7e6cb5d commit 881bb2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit 881bb2a

Please sign in to comment.