Skip to content
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

fix: change seed for variantutils to ensure fair distribution #160

Merged
merged 2 commits into from
Oct 30, 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
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: bundle install
- name: Download test cases
run: git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification
run: git clone --depth 5 --branch v5.0.2 https://github.com/Unleash/client-specification.git client-specification
- name: Run tests
run: bundle exec rake
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
This SDK is also built against the Unleash Client Specification tests.
To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:

`git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification`
`git clone --depth 5 --branch v5.0.2 https://github.com/Unleash/client-specification.git client-specification`

After doing this, `rake spec` will also run the client specification tests.

Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def http_headers
{
'UNLEASH-INSTANCEID' => self.instance_id,
'UNLEASH-APPNAME' => self.app_name,
'Unleash-Client-Spec' => '4.5.1'
'Unleash-Client-Spec' => '5.0.2'
}.merge!(generate_custom_http_headers)
end

Expand Down
1 change: 1 addition & 0 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def variant_from_weights(context, stickiness, variant_definitions, group_id)
variant_weight = Unleash::Strategy::Util.get_normalized_number(
variant_salt(context, stickiness),
group_id,
Unleash::Strategy::Util::VARIANT_NORMALIZER_SEED,
sum_variant_defs_weights(variant_definitions)
)
prev_weights = 0
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/strategy/flexible_rollout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def is_enabled?(params = {}, context = nil)
end

group_id = params.fetch('groupId', '')
normalized_number = Util.get_normalized_number(stickiness_id, group_id)
normalized_number = Util.get_normalized_number(stickiness_id, group_id, 0)

return false if stickiness_id.nil?

Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/strategy/gradual_rollout_sessionid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def is_enabled?(params = {}, context = nil)
return false if context.session_id.nil? || context.session_id.empty?

percentage = Integer(params['percentage'] || 0)
(percentage.positive? && Util.get_normalized_number(context.session_id, params['groupId'] || "") <= percentage)
(percentage.positive? && Util.get_normalized_number(context.session_id, params['groupId'] || "", 0) <= percentage)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/strategy/gradual_rollout_userid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def is_enabled?(params = {}, context = nil, _constraints = [])
return false if context.user_id.nil? || context.user_id.empty?

percentage = Integer(params['percentage'] || 0)
(percentage.positive? && Util.get_normalized_number(context.user_id, params['groupId'] || "") <= percentage)
(percentage.positive? && Util.get_normalized_number(context.user_id, params['groupId'] || "", 0) <= percentage)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/unleash/strategy/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module Util
module_function

NORMALIZER = 100
VARIANT_NORMALIZER_SEED = 86_028_157

# convert the two strings () into a number between 1 and base (100 by default)
def get_normalized_number(identifier, group_id, base = NORMALIZER)
MurmurHash3::V32.str_hash("#{group_id}:#{identifier}") % base + 1
def get_normalized_number(identifier, group_id, seed, base = NORMALIZER)
MurmurHash3::V32.str_hash("#{group_id}:#{identifier}", seed) % base + 1
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
'X-API-KEY' => '123',
'UNLEASH-APPNAME' => 'test-app',
'UNLEASH-INSTANCEID' => config.instance_id,
'Unleash-Client-Spec' => '4.5.1'
'Unleash-Client-Spec' => '5.0.2'
}
)
expect(custom_headers_proc).to have_received(:call).exactly(1).times
Expand Down
36 changes: 18 additions & 18 deletions spec/unleash/feature_toggle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@

let(:default_variant) { Unleash::Variant.new(name: 'unknown', default: true) }

it 'should return variant1 for user_id:1' do
it 'should return variant2 for user_id:1' do
context = Unleash::Context.new(user_id: 10)
expect(feature_toggle.get_variant(context, default_variant)).to have_attributes(
name: "variant1",
name: "variant2",
enabled: true,
payload: nil
)
end

it 'should return variant2 for user_id:2' do
it 'should return variant1 for user_id:2' do
context = Unleash::Context.new(user_id: 2)
expect(feature_toggle.get_variant(context, default_variant)).to have_attributes(
name: "variant2",
name: "variant1",
enabled: true,
payload: nil
)
Expand Down Expand Up @@ -208,8 +208,8 @@
)
end

it 'should return variantB for user_id:2' do
context = Unleash::Context.new(user_id: 2)
it 'should return variantB for user_id:5' do
context = Unleash::Context.new(user_id: 5)
expect(feature_toggle.get_variant(context, default_variant)).to have_attributes(
name: "variantB",
enabled: true,
Expand Down Expand Up @@ -285,7 +285,7 @@
},
"overrides" => [{
"contextName" => "userId",
"values" => ["132", "61"]
"values" => ["133", "61"]
}]
},
{
Expand All @@ -311,17 +311,17 @@
)
end

it 'should return variant1 for user_id:132 from override' do
context = Unleash::Context.new("userId" => 132)
it 'should return variant1 for user_id:133 from override' do
context = Unleash::Context.new("userId" => 133)
expect(feature_toggle.get_variant(context)).to have_attributes(
name: "variant1",
enabled: true,
payload: { "type" => "string", "value" => "val1" }
)
end

it 'should return variant2 for user_id:60' do
context = Unleash::Context.new(user_id: 60)
it 'should return variant2 for user_id:856' do
context = Unleash::Context.new(user_id: 856)
expect(feature_toggle.get_variant(context)).to have_attributes(
name: "variant2",
enabled: true,
Expand Down Expand Up @@ -537,10 +537,10 @@
)
end

it 'should return variant1 organization 726' do
it 'should return variant1 organization 222' do
context = Unleash::Context.new(
properties: {
organization: '726'
organization: '222'
}
)

Expand All @@ -553,7 +553,7 @@
it 'should return variant2 organization 48' do
context = Unleash::Context.new(
properties: {
organization: '48'
organization: '49'
}
)

Expand All @@ -563,10 +563,10 @@
)
end

it 'should return variant3 organization 381' do
it 'should return variant3 organization 726' do
context = Unleash::Context.new(
properties: {
organization: '381'
organization: '726'
}
)

Expand All @@ -576,10 +576,10 @@
)
end

it 'should return variant4 organization 222' do
it 'should return variant4 organization 381' do
context = Unleash::Context.new(
properties: {
organization: '222'
organization: '381'
}
)

Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/strategy/gradual_rollout_sessionid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe '#is_enabled?' do
let(:strategy) { Unleash::Strategy::GradualRolloutSessionId.new }
let(:unleash_context) { Unleash::Context.new(session_id: 'secretsessionidhashgoeshere') }
let(:percentage) { Unleash::Strategy::Util.get_normalized_number(unleash_context.session_id, "") }
let(:percentage) { Unleash::Strategy::Util.get_normalized_number(unleash_context.session_id, "", 0) }

it 'return true when percentage set is gt the number returned by the hash function' do
expect(strategy.is_enabled?({ 'percentage' => (percentage + 1).to_s }, unleash_context)).to be_truthy
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/strategy/gradual_rollout_userid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe '#is_enabled?' do
let(:strategy) { Unleash::Strategy::GradualRolloutUserId.new }
let(:unleash_context) { Unleash::Context.new({ 'userId' => 'alice' }) }
let(:percentage) { Unleash::Strategy::Util.get_normalized_number(unleash_context.user_id, "") }
let(:percentage) { Unleash::Strategy::Util.get_normalized_number(unleash_context.user_id, "", 0) }

it 'return true when percentage set is gt the number returned by the hash function' do
expect(strategy.is_enabled?({ 'percentage' => (percentage + 1).to_s }, unleash_context)).to be_truthy
Expand Down
4 changes: 2 additions & 2 deletions spec/unleash/strategy/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
RSpec.describe Unleash::Strategy::Util do
describe '.get_normalized_number' do
it "returns correct values" do
expect(Unleash::Strategy::Util.get_normalized_number('123', 'gr1')).to eq(73)
expect(Unleash::Strategy::Util.get_normalized_number('999', 'groupX')).to eq(25)
expect(Unleash::Strategy::Util.get_normalized_number('123', 'gr1', 0)).to eq(73)
expect(Unleash::Strategy::Util.get_normalized_number('999', 'groupX', 0)).to eq(25)
end
end
end