Switches to the median over all DC nodes with known coordinates.

This commit is contained in:
James Phillips 2015-07-24 14:53:26 -07:00
parent e47eea3f3a
commit 497f6782af
2 changed files with 11 additions and 11 deletions

View File

@ -223,23 +223,22 @@ func getDatacenterDistance(s serfer, dc string) (float64, error) {
return 0.0, err
}
// Fetch all the nodes in the DC.
// Fetch all the nodes in the DC and record their distance, if available.
nodes := s.GetNodesForDatacenter(dc)
subvec := make([]float64, len(nodes))
for j, node := range nodes {
subvec := make([]float64, 0, len(nodes))
for _, node := range nodes {
if other, ok := s.GetCachedCoordinate(node); ok {
subvec[j] = computeDistance(coord, other)
} else {
subvec[j] = computeDistance(coord, nil)
subvec = append(subvec, computeDistance(coord, other))
}
}
// Compute the median by sorting and taking the middle item.
sort.Float64s(subvec)
if len(subvec) > 0 {
sort.Float64s(subvec)
return subvec[len(subvec)/2], nil
}
// Return the default infinity value.
return computeDistance(coord, nil), nil
}

View File

@ -337,12 +337,13 @@ func TestRtt_getDatacenterDistance(t *testing.T) {
}
// Check the more interesting median case, note that there's a mystery
// node4 in there that should make the distances sort like this:
// node4 in there that should be excluded to make the distances sort
// like this:
//
// [0] node3 (0.005), [1] node1 (0.007), [2] node2 (0.008), [3] node4 (+Inf)
// [0] node3 (0.005), [1] node1 (0.007), [2] node2 (0.008)
//
// So the median should be at index 4 / 2 = 2 -> 0.008.
if dist, err := getDatacenterDistance(s, "dc1"); err != nil || dist != 0.008 {
// So the median should be at index 3 / 2 = 1 -> 0.007.
if dist, err := getDatacenterDistance(s, "dc1"); err != nil || dist != 0.007 {
t.Fatalf("bad: %v err: %v", dist, err)
}
}