From 5164fb3aed331f2d3cc95267b0423d3357989580 Mon Sep 17 00:00:00 2001 From: Dustin Sallings Date: Sat, 22 Sep 2012 09:25:26 -0700 Subject: [PATCH] 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. --- nodes.go | 15 ++++++++++++++- nodes_test.go | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nodes.go b/nodes.go index e8278e6..f221644 100644 --- a/nodes.go +++ b/nodes.go @@ -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) } } diff --git a/nodes_test.go b/nodes_test.go index 8e04eaa..4fd202d 100644 --- a/nodes_test.go +++ b/nodes_test.go @@ -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)