Skip to content

fix: use 32bit murmur calculation (64 is not stable) #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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