Skip to content

Commit

Permalink
Don't prefer nodes strictly by heartbeat time.
Browse files Browse the repository at this point in the history
By randomly picking nodes with similar heartbeat times, we can avoid
ganging up on individual nodes too much.
  • Loading branch information
dustin committed Sep 22, 2012
1 parent e3afa0f commit 5164fb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 14 additions & 1 deletion nodes.go
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -58,6 +59,18 @@ func (a NodeList) Len() int {
}

func (a NodeList) Less(i, j int) bool {
tdiff := a[i].Time.Sub(a[j].Time)
if tdiff < 0 {
tdiff = -tdiff
}
// Nodes that have heartbeated within a heartbeat time of each
// other are sorted randomly. This generally happens when
// they're heartbeating regularly and we don't want to prefer
// one over the other just because it happened to talk most
// frequently.
if tdiff < globalConfig.HeartbeatFreq {
return rand.Intn(1) == 0
}
return a[i].Time.After(a[j].Time)
}

Expand Down Expand Up @@ -214,7 +227,7 @@ func (nl NodeList) candidatesFor(oid string, exclude NodeList) NodeList {
rv := NodeList{}
// Find a good destination candidate.
for _, node := range nl.minus(owners) {
if node.Free > ownership.Length {
if node.Free > uint64(ownership.Length) {
rv = append(rv, node)
}
}
Expand Down
1 change: 0 additions & 1 deletion nodes_test.go
Expand Up @@ -20,7 +20,6 @@ func TestNodeSorting(t *testing.T) {
}

sort.Sort(nl)
t.Logf("Sorted: %v", nl)

if nl[0].Time.UnixNano() < nl[1].Time.UnixNano() {
t.Fatalf("Error: wrong order: %v", nl)
Expand Down

0 comments on commit 5164fb3

Please sign in to comment.