Skip to content

Commit

Permalink
also compare rpelica point index on collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Menno Pruijssers committed Dec 19, 2016
1 parent 163a02e commit 348b16f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 13 additions & 5 deletions hashring/hashring.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,23 @@ type replicaPoint struct {

// address of the member that owns this replicaPoint.
address string

// index of the replica point for a member
index int
}

func (r replicaPoint) Compare(other interface{}) int {
func (r replicaPoint) Compare(other interface{}) (result int) {
o := other.(replicaPoint)
result := r.hash - o.hash
if result != 0 {
return result
result = r.hash - o.hash
if result == 0 {
result = strings.Compare(r.address, o.address)
}

if result == 0 {
result = r.index - o.index
}

return strings.Compare(r.address, o.address)
return result
}

// HashRing stores strings on a consistent hash ring. HashRing internally uses
Expand Down Expand Up @@ -193,6 +200,7 @@ func (r *HashRing) replicaPointForServer(server membership.Member, replica int)
hash: r.hashfunc(replicaStr),
identity: identity,
address: server.GetAddress(),
index: replica,
}
}

Expand Down
10 changes: 6 additions & 4 deletions hashring/hashring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,17 @@ func TestLookupsWithIdentities(t *testing.T) {
func TestReplicaPointCompare(t *testing.T) {
address := "127.0.0.1:3000"

pointA := replicaPoint{hash: 100, address: address}
pointB := replicaPoint{hash: 200, address: address}
pointC := replicaPoint{hash: 300, address: address}
pointD := replicaPoint{hash: 200, address: address}
pointA := replicaPoint{hash: 100, address: address, index: 0}
pointB := replicaPoint{hash: 200, address: address, index: 0}
pointC := replicaPoint{hash: 300, address: address, index: 0}
pointD := replicaPoint{hash: 200, address: address, index: 0}
pointE := replicaPoint{hash: 200, address: address, index: 1}

assert.True(t, pointB.Compare(pointA) > 0)
assert.True(t, pointB.Compare(pointC) < 0)
assert.True(t, pointB.Compare(pointB) == 0)
assert.True(t, pointB.Compare(pointD) == 0)
assert.True(t, pointB.Compare(pointE) < 0)
}

func genMembers(host, fromPort, toPort int, overrideIdentity bool) (members []membership.Member) {
Expand Down

0 comments on commit 348b16f

Please sign in to comment.