-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
283 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package hashring | ||
|
||
import ( | ||
"bytes" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/dgryski/go-farm" | ||
) | ||
|
||
// Checksum computes a checksum for an instance of a HashRing. The | ||
// checksum can be used to compare two rings for equality. | ||
type Checksum interface { | ||
// Compute calculates the checksum for the hashring that is passed in. | ||
// Compute will be called while having at least a read-lock on the hashring so | ||
// it is safe to read from the ring, but not safe to change the ring. There | ||
// might be multiple Checksum Computes initiated at the same time, but every | ||
// Checksum will only be called once per hashring at once | ||
Compute(ring *HashRing) (checksum uint32) | ||
} | ||
|
||
// addressChecksum calculates checksums for all addresses that are added to the | ||
// hashring. | ||
type addressChecksum struct{} | ||
|
||
func (i *addressChecksum) Compute(ring *HashRing) uint32 { | ||
addresses := ring.copyServersNoLock() | ||
sort.Strings(addresses) | ||
bytes := []byte(strings.Join(addresses, ";")) | ||
return farm.Fingerprint32(bytes) | ||
} | ||
|
||
type identityChecksum struct{} | ||
|
||
func (i *identityChecksum) Compute(ring *HashRing) uint32 { | ||
identitySet := make(map[string]struct{}) | ||
ring.tree.root.walk(func(node *redBlackNode) bool { | ||
identitySet[node.key.(replicaPoint).identity] = struct{}{} | ||
return true | ||
}) | ||
|
||
identities := make([]string, 0, len(identitySet)) | ||
for identity := range identitySet { | ||
identities = append(identities, identity) | ||
} | ||
|
||
sort.Strings(identities) | ||
bytes := []byte(strings.Join(identities, ";")) | ||
return farm.Fingerprint32(bytes) | ||
} | ||
|
||
type replicaPointChecksum struct{} | ||
|
||
func (r *replicaPointChecksum) Compute(ring *HashRing) uint32 { | ||
buffer := bytes.Buffer{} | ||
|
||
ring.tree.root.walk(func(node *redBlackNode) bool { | ||
buffer.WriteString(strconv.Itoa(node.key.(replicaPoint).hash)) | ||
buffer.WriteString("-") | ||
buffer.WriteString(node.value.(string)) | ||
buffer.WriteString(";") | ||
return true | ||
}) | ||
|
||
return farm.Fingerprint32(buffer.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2015 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package hashring | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/dgryski/go-farm" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAddressChecksum_Compute(t *testing.T) { | ||
members := genMembers(1, 1, 10, false) | ||
ring := New(farm.Fingerprint32, 1) | ||
ring.AddMembers(members...) | ||
checksum := &addressChecksum{} | ||
|
||
addresses := make([]string, 0, 10) | ||
for _, members := range members { | ||
addresses = append(addresses, members.GetAddress()) | ||
} | ||
|
||
sort.Strings(addresses) | ||
bytes := []byte(strings.Join(addresses, ";")) | ||
|
||
expected := farm.Fingerprint32(bytes) | ||
actual := checksum.Compute(ring) | ||
|
||
assert.Equal(t, expected, actual) | ||
} | ||
|
||
func TestIdentityChecksum_Compute(t *testing.T) { | ||
identityChecksummer := &identityChecksum{} | ||
|
||
ringWithoutIdentities := New(farm.Fingerprint32, 1) | ||
ringWithoutIdentities.AddMembers(genMembers(1, 1, 10, false)...) | ||
|
||
legacyChecksum := (&addressChecksum{}).Compute(ringWithoutIdentities) | ||
identityChecksum := identityChecksummer.Compute(ringWithoutIdentities) | ||
|
||
assert.Equal(t, legacyChecksum, identityChecksum, "Identity checksum should be the same as legacy on ring without identities") | ||
|
||
ringWithIdentities := New(farm.Fingerprint32, 1) | ||
ringWithIdentities.AddMembers(genMembers(1, 1, 10, true)...) | ||
|
||
identityChecksum = identityChecksummer.Compute(ringWithIdentities) | ||
|
||
assert.NotEqual(t, legacyChecksum, identityChecksum, "IdentityChecksummer should not match legacy checksummer on ring with identites ") | ||
} | ||
|
||
func TestReplicaPointChecksum_Compute(t *testing.T) { | ||
replicaPointChecksummer := &replicaPointChecksum{} | ||
members := genMembers(1, 1, 10, false) | ||
|
||
ring1ReplicaPoint := New(farm.Fingerprint32, 1) | ||
ring1ReplicaPoint.AddMembers(members...) | ||
|
||
ring2ReplicaPoints := New(farm.Fingerprint32, 2) | ||
ring2ReplicaPoints.AddMembers(members...) | ||
|
||
checksum1ReplicaPoint := replicaPointChecksummer.Compute(ring1ReplicaPoint) | ||
checksum2ReplicaPoints := replicaPointChecksummer.Compute(ring2ReplicaPoints) | ||
|
||
assert.NotEqual(t, checksum1ReplicaPoint, checksum2ReplicaPoints, "Checksum should not match with different replica point counts") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.