Permalink
Browse files

Don't prefer nodes strictly by heartbeat time.

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 5164fb3aed331f2d3cc95267b0423d3357989580
Showing with 14 additions and 2 deletions.
  1. +14 −1 nodes.go
  2. +0 −1 nodes_test.go
View
@@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"log"
+ "math/rand"
"net/http"
"sort"
"strings"
@@ -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)
}
@@ -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)
}
}
View
@@ -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)

0 comments on commit 5164fb3

Please sign in to comment.