Skip to content

Commit

Permalink
fix: use 32bit murmur calculation (64 is not stable) (#913)
Browse files Browse the repository at this point in the history
* fixes murmur3 hash calculation to be cross language compatible (32bit).

Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Sep 14, 2023
1 parent 2eda6ab commit db8dca4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
9 changes: 4 additions & 5 deletions core/pkg/eval/fractional_evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluatio
return feDistributions, nil
}

// distributeValue calculate hash for given hash key and find the bucket distributions belongs to
func distributeValue(value string, feDistribution []fractionalEvaluationDistribution) string {
hashValue := murmur3.StringSum64(value)

hashRatio := float64(hashValue) / math.MaxUint64

bucket := int(hashRatio * 100) // integer in range [0, 99]
hashValue := int32(murmur3.StringSum32(value))
hashRatio := math.Abs(float64(hashValue)) / math.MaxInt32
bucket := int(hashRatio * 100) // in range [0, 100]

rangeEnd := 0
for _, dist := range feDistribution {
Expand Down
24 changes: 12 additions & 12 deletions core/pkg/eval/fractional_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"email": "monica@faas.com",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedReason: model.TargetingMatchReason,
},
"joey@faas.com": {
Expand All @@ -89,8 +89,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"email": "joey@faas.com",
},
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"ross@faas.com": {
Expand All @@ -99,8 +99,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"email": "ross@faas.com",
},
expectedVariant: "red",
expectedValue: "#FF0000",
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"ross@faas.com with different flag key": {
Expand Down Expand Up @@ -152,8 +152,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"email": "ross@faas.com",
},
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"non even split": {
Expand Down Expand Up @@ -201,8 +201,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"email": "test4@faas.com",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedVariant: "red",
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"fallback to default variant if no email provided": {
Expand Down Expand Up @@ -346,8 +346,8 @@ func TestFractionalEvaluation(t *testing.T) {
context: map[string]any{
"targetingKey": "foo@foo.com",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedVariant: "blue",
expectedValue: "#0000FF",
expectedReason: model.TargetingMatchReason,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,9 @@ func FractionalEvaluation(values, data interface{}) interface{} {
return nil
}

// 4. Calculate the hash of the target property and map it to a number between [0, 99]
hashValue := murmur3.HashString(value)

// divide the hash value by the largest possible value, integer 2^64
hashRatio := float64(hashValue) / math.Pow(2, 64)

// integer in range [0, 99]
// 4. Calculate the hash of the target property and map it to a number between [0, 99]
hashValue := int32(murmur3.StringSum32(value))
hashRatio := math.Abs(float64(hashValue)) / math.MaxInt32
bucket := int(hashRatio * 100)

// 5. Iterate through the variant and increment the threshold by the percentage of each variant.
Expand Down
2 changes: 1 addition & 1 deletion test-harness

0 comments on commit db8dca4

Please sign in to comment.