Bladeren bron

Merge pull request #754 from DataDog/master

Fix incorrect random partioner hash function
Chris Bannister 9 jaren geleden
bovenliggende
commit
e4a5ecdadf
3 gewijzigde bestanden met toevoegingen van 25 en 4 verwijderingen
  1. 2 0
      AUTHORS
  2. 9 4
      token.go
  3. 14 0
      token_test.go

+ 2 - 0
AUTHORS

@@ -71,3 +71,5 @@ Viktor Tönköl <viktor.toenkoel@motionlogic.de>
 Ian Lozinski <ian.lozinski@gmail.com>
 Michael Highstead <highstead@gmail.com>
 Sarah Brown <esbie.is@gmail.com>
+Caleb Doxsey <caleb@datadoghq.com>
+Frederic Hemery <frederic.hemery@datadoghq.com>

+ 9 - 4
token.go

@@ -90,12 +90,17 @@ func (r randomPartitioner) Name() string {
 }
 
 func (p randomPartitioner) Hash(partitionKey []byte) token {
-	hash := md5.New()
-	sum := hash.Sum(partitionKey)
+	// 2 ** 128
+	maxInt := new(big.Int)
+	maxInt.SetString("340282366920938463463374607431768211456", 10)
 
+	sum := md5.Sum(partitionKey)
 	val := new(big.Int)
-	val = val.SetBytes(sum)
-	val = val.Abs(val)
+	val.SetBytes(sum[:])
+	if sum[0] > 127 {
+		val.Sub(val, maxInt)
+		val.Abs(val)
+	}
 
 	return (*randomToken)(val)
 }

+ 14 - 0
token_test.go

@@ -101,6 +101,20 @@ func TestRandomPartitioner(t *testing.T) {
 	}
 }
 
+func TestRandomPartitionerMatchesReference(t *testing.T) {
+	// example taken from datastax python driver
+	//    >>> from cassandra.metadata import MD5Token
+	//    >>> MD5Token.hash_fn("test")
+	//    12707736894140473154801792860916528374L
+	var p randomPartitioner
+	expect := "12707736894140473154801792860916528374"
+	actual := p.Hash([]byte("test")).String()
+	if actual != expect {
+		t.Errorf("expected random partitioner to generate tokens in the same way as the reference"+
+			" python client. Expected %s, but got %s", expect, actual)
+	}
+}
+
 // Tests of the randomToken
 func TestRandomToken(t *testing.T) {
 	if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(42))) {