From ca35f302ecb04baaa5ebec84679fc9cf0368f00f Mon Sep 17 00:00:00 2001 From: sjaanus Date: Thu, 26 Oct 2023 15:59:54 +0300 Subject: [PATCH 1/6] fix: new variant hashing --- api/feature.go | 3 ++- client.go | 2 +- internal/strategies/flexible_rollout.go | 2 +- internal/strategies/gradual_rollout_session_id.go | 2 +- .../strategies/gradual_rollout_session_id_test.go | 4 ++-- internal/strategies/gradual_rollout_user_id.go | 2 +- .../strategies/gradual_rollout_user_id_test.go | 4 ++-- internal/strategies/helpers.go | 12 +++++++++--- internal/strategies/helpers_test.go | 15 ++++++++++----- 9 files changed, 29 insertions(+), 17 deletions(-) diff --git a/api/feature.go b/api/feature.go index 085d4bb..859671c 100644 --- a/api/feature.go +++ b/api/feature.go @@ -7,6 +7,7 @@ import ( "time" "github.com/Unleash/unleash-client-go/v3/context" + "github.com/Unleash/unleash-client-go/v3/internal/strategies" "github.com/twmb/murmur3" ) @@ -105,7 +106,7 @@ func (vc VariantCollection) getVariantFromWeights(ctx *context.Context) *Variant } stickiness := vc.Variants[0].Stickiness - target := getNormalizedNumber(getSeed(ctx, stickiness), vc.GroupId, totalWeight) + target := strategies.NormalizedVariantValue(getSeed(ctx, stickiness), vc.GroupId, totalWeight, strategies.VariantNormalizationSeed) counter := uint32(0) for _, variant := range vc.Variants { counter += uint32(variant.Weight) diff --git a/client.go b/client.go index 7c6caf7..db801a3 100644 --- a/client.go +++ b/client.go @@ -17,7 +17,7 @@ import ( const ( deprecatedSuffix = "/features" clientName = "unleash-client-go" - clientVersion = "3.9.0" + clientVersion = "4.0.0-beta.1" ) var defaultStrategies = []strategy.Strategy{ diff --git a/internal/strategies/flexible_rollout.go b/internal/strategies/flexible_rollout.go index 5d2cebb..e07ad90 100644 --- a/internal/strategies/flexible_rollout.go +++ b/internal/strategies/flexible_rollout.go @@ -62,6 +62,6 @@ func (s flexibleRolloutStrategy) IsEnabled(params map[string]interface{}, ctx *c return false } - normalizedID := normalizedValue(stickinessID, groupID) + normalizedID := normalizedRolloutValue(stickinessID, groupID) return percentage > 0 && float64(normalizedID) <= percentage } diff --git a/internal/strategies/gradual_rollout_session_id.go b/internal/strategies/gradual_rollout_session_id.go index 18541d9..66536bc 100644 --- a/internal/strategies/gradual_rollout_session_id.go +++ b/internal/strategies/gradual_rollout_session_id.go @@ -38,7 +38,7 @@ func (s gradualRolloutSessionId) IsEnabled(params map[string]interface{}, ctx *c return false } - normalizedId := normalizedValue(ctx.SessionId, groupId) + normalizedId := normalizedRolloutValue(ctx.SessionId, groupId) return percentage > 0.0 && float64(normalizedId) <= percentage } diff --git a/internal/strategies/gradual_rollout_session_id_test.go b/internal/strategies/gradual_rollout_session_id_test.go index b402872..6cc9744 100644 --- a/internal/strategies/gradual_rollout_session_id_test.go +++ b/internal/strategies/gradual_rollout_session_id_test.go @@ -42,7 +42,7 @@ func TestGradualRolloutSessionId_IsEnabled(t *testing.T) { t.Run("p1=p2", func(t *testing.T) { sessionId := "123123" groupId := "group1" - percentage := normalizedValue(sessionId, groupId) + percentage := normalizedRolloutValue(sessionId, groupId) params := map[string]interface{}{ strategy.ParamPercentage: percentage, @@ -57,7 +57,7 @@ func TestGradualRolloutSessionId_IsEnabled(t *testing.T) { t.Run("p1 0.0 && float64(normalizedId) <= percentage } diff --git a/internal/strategies/gradual_rollout_user_id_test.go b/internal/strategies/gradual_rollout_user_id_test.go index ac8cadf..8674796 100644 --- a/internal/strategies/gradual_rollout_user_id_test.go +++ b/internal/strategies/gradual_rollout_user_id_test.go @@ -43,7 +43,7 @@ func TestGradualRolloutUserId_IsEnabled(t *testing.T) { t.Run("p1=p2", func(t *testing.T) { userId := "123123" groupId := "group1" - percentage := normalizedValue(userId, groupId) + percentage := normalizedRolloutValue(userId, groupId) params := map[string]interface{}{ strategy.ParamPercentage: percentage, @@ -58,7 +58,7 @@ func TestGradualRolloutUserId_IsEnabled(t *testing.T) { t.Run("p1 Date: Thu, 26 Oct 2023 16:01:38 +0300 Subject: [PATCH 2/6] Fix --- api/feature.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/api/feature.go b/api/feature.go index 859671c..58cbbfa 100644 --- a/api/feature.go +++ b/api/feature.go @@ -1,14 +1,12 @@ package api import ( - "fmt" "math/rand" "strconv" "time" "github.com/Unleash/unleash-client-go/v3/context" "github.com/Unleash/unleash-client-go/v3/internal/strategies" - "github.com/twmb/murmur3" ) type ParameterMap map[string]interface{} @@ -149,7 +147,3 @@ func getSeed(ctx *context.Context, stickiness string) string { } return strconv.Itoa(rand.Intn(10000)) } - -func getNormalizedNumber(identifier, groupId string, normalizer int) uint32 { - return (murmur3.Sum32([]byte(fmt.Sprintf("%s:%s", groupId, identifier))) % uint32(normalizer)) + 1 -} From 5a3c0c26383a5251bd0f30a06fe0d69c7cd6af54 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 27 Oct 2023 09:11:49 +0300 Subject: [PATCH 3/6] update spec --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aa20796..e7891be 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ jobs: name: Checkout client specifications with: repository: Unleash/client-specification - ref: refs/tags/v4.3.1 + ref: refs/tags/v5.0.0 path: testdata/client-specification - uses: actions/setup-go@v2 name: Setup go From 1a0189f19967ba4e70f4ba2eb4d234a5064e25a4 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 27 Oct 2023 09:15:24 +0300 Subject: [PATCH 4/6] update test --- api/variant_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/variant_test.go b/api/variant_test.go index 47c6761..54d3da1 100644 --- a/api/variant_test.go +++ b/api/variant_test.go @@ -238,7 +238,7 @@ func (suite *VariantTestSuite) TestGetVariant_ShouldReturnVarE() { GroupId: mockFeature.Name, Variants: mockFeature.Variants, }.GetVariant(mockContext) - suite.Equal("VarE", variantSetup.Name, "Should return VarE") + suite.Equal("VarF", variantSetup.Name, "Should return VarF") suite.Equal(true, variantSetup.Enabled, "Should be equal") } From 69d4caa368ef2b002272e6322c96729fcfdad99a Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 27 Oct 2023 09:16:42 +0300 Subject: [PATCH 5/6] update test --- api/variant_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/variant_test.go b/api/variant_test.go index 54d3da1..087a3b1 100644 --- a/api/variant_test.go +++ b/api/variant_test.go @@ -255,7 +255,7 @@ func (suite *VariantTestSuite) TestGetVariant_ShouldReturnVarF() { GroupId: mockFeature.Name, Variants: mockFeature.Variants, }.GetVariant(mockContext) - suite.Equal("VarF", variantSetup.Name, "Should return VarF") + suite.Equal("VarE", variantSetup.Name, "Should return VarE") suite.Equal(true, variantSetup.Enabled, "Should be equal") } From fa94f3ceccb0710d43d1ea1771014b6aab4aecbb Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 27 Oct 2023 09:53:18 +0300 Subject: [PATCH 6/6] update test --- api/variant_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/variant_test.go b/api/variant_test.go index 087a3b1..2bce29d 100644 --- a/api/variant_test.go +++ b/api/variant_test.go @@ -221,7 +221,7 @@ func (suite *VariantTestSuite) TestGetVariant_ShouldReturnVarD() { GroupId: mockFeature.Name, Variants: mockFeature.Variants, }.GetVariant(mockContext) - suite.Equal("VarD", variantSetup.Name, "Should return VarD") + suite.Equal("VarE", variantSetup.Name, "Should return VarE") suite.Equal(true, variantSetup.Enabled, "Should be equal") }