-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change seed for variantutils to ensure fair distribution (#220)
Background After a customer reported that variant distribution seemed skewed we performed some testing and found that since we use the same hash string for both gradual rollout and variant allocation we'd reduced the set of groups we could get to whatever percentage our gradual rollout was set. Example Take a gradualRollout of 10%, this will select normalized hashes between 1 and 10, when we then again hash the same string that gave us between 1 and 10, but with modulo 1000 for variants, this will only give us 100 possible groups, instead of the expected 1000. Fix Force the normalization to accept a seed, and make sure to use a new seed when normalizing the variant distribution hash. Worth noting This will require release 9.0.0, since we're changing the signature of public methods.
- Loading branch information
Christopher Kolstad
authored
Oct 27, 2023
1 parent
4193824
commit 11a9625
Showing
13 changed files
with
290 additions
and
39 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
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
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
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
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
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
50 changes: 48 additions & 2 deletions
50
src/test/java/io/getunleash/strategy/StrategyUtilsTest.java
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 |
---|---|---|
@@ -1,14 +1,60 @@ | ||
package io.getunleash.strategy; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.getunleash.variant.VariantUtil; | ||
import java.util.UUID; | ||
import org.assertj.core.data.Offset; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StrategyUtilsTest { | ||
|
||
@Test | ||
public void normalized_values_are_the_same_across_node_java_and_go_clients() { | ||
assertEquals(73, StrategyUtils.getNormalizedNumber("123", "gr1")); | ||
assertEquals(25, StrategyUtils.getNormalizedNumber("999", "groupX")); | ||
assertEquals(73, StrategyUtils.getNormalizedNumber("123", "gr1", 0)); | ||
assertEquals(25, StrategyUtils.getNormalizedNumber("999", "groupX", 0)); | ||
} | ||
|
||
@Test | ||
public void normalized_values_with_variant_seed_are_the_same_across_node_java() { | ||
assertThat( | ||
StrategyUtils.getNormalizedNumber( | ||
"123", "gr1", VariantUtil.VARIANT_NORMALIZATION_SEED)) | ||
.isEqualTo(96); | ||
assertThat( | ||
StrategyUtils.getNormalizedNumber( | ||
"999", "groupX", VariantUtil.VARIANT_NORMALIZATION_SEED)) | ||
.isEqualTo(60); | ||
} | ||
|
||
@Test | ||
public void | ||
selecting_ten_percent_of_users_and_then_finding_variants_should_still_have_variants_evenly_distributed() { | ||
int ones = 0, twos = 0, threes = 0, loopSize = 500000, selectionSize = 0; | ||
for (int i = 0; i < loopSize; i++) { | ||
String id = UUID.randomUUID().toString(); | ||
int featureRollout = | ||
StrategyUtils.getNormalizedNumber(id, "feature.name.that.is.quite.long", 0); | ||
if (featureRollout < 11) { | ||
int variantGroup = | ||
StrategyUtils.getNormalizedNumber( | ||
id, | ||
"feature.name.that.is.quite.long", | ||
1000, | ||
VariantUtil.VARIANT_NORMALIZATION_SEED); | ||
if (variantGroup <= 333) { | ||
ones++; | ||
} else if (variantGroup <= 666) { | ||
twos++; | ||
} else if (variantGroup <= 1000) { | ||
threes++; | ||
} | ||
selectionSize++; | ||
} | ||
} | ||
assertThat(ones / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
assertThat(twos / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
assertThat(threes / (double) (selectionSize)).isCloseTo(0.33, Offset.offset(0.01)); | ||
} | ||
} |
Oops, something went wrong.