Skip to content
Open
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
67 changes: 54 additions & 13 deletions lib/mixpanel-ruby/flags/local_flags_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class LocalFlagsProvider < FlagsProvider
exposure_executor: nil
}.freeze

ROLLOUT_TYPE_EXPERIMENT = 'experiment'.freeze
EXPERIMENT_STATUS_DRAFT = 'draft'.freeze

# @param token [String] Mixpanel project token
# @param config [Hash] Local flags configuration
# @param tracker_callback [Proc] Callback to track events
Expand Down Expand Up @@ -280,35 +283,73 @@ def get_assigned_rollout(flag, context_value, context)
return nil unless rollouts

rollouts.each_with_index do |rollout, index|
salt = if flag['hash_salt']
"#{flag['key']}#{flag['hash_salt']}#{index}"
else
"#{flag['key']}rollout"
end

rollout_hash = Utils.normalized_hash(context_value.to_s, salt)
if experiment_rollout?(rollout)
is_in_rollout = context_in_experiment_allocation?(flag, context_value, rollout)
else
salt = if flag['hash_salt']
"#{flag['key']}#{flag['hash_salt']}#{index}"
else
"#{flag['key']}rollout"
end

rollout_hash = Utils.normalized_hash(context_value.to_s, salt)
is_in_rollout = rollout_hash < rollout['rollout_percentage']
end

if rollout_hash < rollout['rollout_percentage'] &&
is_runtime_evaluation_satisfied?(rollout, context)
if is_in_rollout && is_runtime_evaluation_satisfied?(rollout, context)
return rollout
end
end

nil
end

# Experiment entries of a multi-experiment flag assign subjects by
# allocation bucket and ignore rollout_percentage.
def experiment_rollout?(rollout)
rollout['type'] == ROLLOUT_TYPE_EXPERIMENT
end

def context_in_experiment_allocation?(flag, context_value, rollout)
return false if rollout['experiment_status'] == EXPERIMENT_STATUS_DRAFT

hash_salt = flag['hash_salt']
return false if hash_salt.nil? || hash_salt.empty?

bucket = Utils.allocation_bucket(context_value.to_s, "#{flag['key']}#{hash_salt}")
(rollout['allocation'] || []).any? do |allocation_range|
allocation_range['start'] <= bucket && bucket < allocation_range['end']
end
end

def experiment_attribution(flag, rollout)
if experiment_rollout?(rollout)
[rollout['experiment_id'], rollout['is_experiment_active'] == true]
else
[flag['experiment_id'], flag['is_experiment_active']]
end
end

def get_assigned_variant(flag, context_value, flag_key, rollout)
experiment_id, is_experiment_active = experiment_attribution(flag, rollout)

if rollout['variant_override']
variant = get_matching_variant(rollout['variant_override']['key'], flag)
if variant
variant.is_qa_tester = false
variant.experiment_id = experiment_id
variant.is_experiment_active = is_experiment_active
return variant
end
end

stored_salt = flag['hash_salt'] || ''
salt = "#{flag_key}#{stored_salt}variant"
variant_hash = Utils.normalized_hash(context_value.to_s, salt)
variant_hash = if experiment_rollout?(rollout)
salt = "#{flag_key}#{stored_salt}#{rollout['experiment_id']}variant"
Utils.mixed_normalized_hash(context_value.to_s, salt)
else
Utils.normalized_hash(context_value.to_s, "#{flag_key}#{stored_salt}variant")
end

variants = flag['ruleset']['variants'].map(&:dup)
if rollout['variant_splits']
Expand All @@ -328,8 +369,8 @@ def get_assigned_variant(flag, context_value, flag_key, rollout)
SelectedVariant.new(
variant_key: selected['key'],
variant_value: selected['value'],
experiment_id: flag['experiment_id'],
is_experiment_active: flag['is_experiment_active'],
experiment_id: experiment_id,
is_experiment_active: is_experiment_active,
is_qa_tester: false
)
end
Expand Down
34 changes: 34 additions & 0 deletions lib/mixpanel-ruby/flags/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ def self.normalized_hash(key, salt)
(hash_value % 100) / 100.0
end

# Allocation bucket for the experiment entries of a multi-experiment flag.
# Uses the same mixed hash as mixed_normalized_hash, so every bucket bit
# depends on every input byte.
#
# @param key [String] Key to hash (typically distinct_id)
# @param salt [String] Salt value (flag key followed by the flag's hash salt)
# @return [Integer] Bucket between 0 and 99
def self.allocation_bucket(key, salt)
fmix64(fnv1a_64(key.to_s + salt.to_s)) % 100
end

# Normalized hash for variant assignment within an experiment entry of a
# multi-experiment flag. Passes FNV-1a through the MurmurHash3 64-bit
# finalizer so every output bit depends on every input byte.
#
# @param key [String] Key to hash (typically distinct_id)
# @param salt [String] Salt value (flag-specific)
# @return [Float] Value between 0.0 and 1.0 (non-inclusive upper bound)
def self.mixed_normalized_hash(key, salt)
(fmix64(fnv1a_64(key.to_s + salt.to_s)) % 100) / 100.0
end

# MurmurHash3 64-bit finalizer
#
# @param value [Integer] 64-bit value
# @return [Integer] 64-bit mixed value
def self.fmix64(value)
value ^= value >> 33
value = (value * 0xff51afd7ed558ccd) & 0xffffffffffffffff
value ^= value >> 33
value = (value * 0xc4ceb9fe1a85ec53) & 0xffffffffffffffff
value ^ (value >> 33)
end

# Prepare common query parameters for flags API
#
# @param token [String] Mixpanel project token
Expand Down
233 changes: 233 additions & 0 deletions spec/fixtures/multi_experiment_flag_test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"TestMultiExperimentFlagEvaluationScenarios": [
{
"name": "multi-experiment flag assigns subject to the experiment allocating its position",
"context": {
"distinct_id": "user123"
},
"flags": [
{
"id": "multi-experiment-1",
"project_id": 31,
"workspace_id": 1,
"name": "Checkout",
"key": "checkout",
"status": "enabled",
"context": "distinct_id",
"hash_salt": "salt1",
"ruleset": {
"variants": [
{
"key": "control",
"value": "control",
"is_control": true,
"split": 0.5
},
{
"key": "treatment",
"value": "treatment",
"is_control": false,
"split": 0.5
}
],
"rollout": [
{
"type": "experiment",
"experiment_id": "e_cta",
"experiment_status": "active",
"is_experiment_active": true,
"allocation": [
{
"start": 0,
"end": 40
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.5,
"treatment": 0.5
}
},
{
"type": "experiment",
"experiment_id": "e_search",
"experiment_status": "concluded",
"allocation": [
{
"start": 40,
"end": 65
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.1,
"treatment": 0.9
}
}
]
}
}
],
"expected": {
"checkout": {
"variant_key": "control",
"variant_value": "control",
"experiment_id": "e_cta",
"is_experiment_active": true
}
}
},
{
"name": "multi-experiment flag attributes a concluded experiment as inactive",
"context": {
"distinct_id": "user456"
},
"flags": [
{
"id": "multi-experiment-1",
"project_id": 31,
"workspace_id": 1,
"name": "Checkout",
"key": "checkout",
"status": "enabled",
"context": "distinct_id",
"hash_salt": "salt1",
"ruleset": {
"variants": [
{
"key": "control",
"value": "control",
"is_control": true,
"split": 0.5
},
{
"key": "treatment",
"value": "treatment",
"is_control": false,
"split": 0.5
}
],
"rollout": [
{
"type": "experiment",
"experiment_id": "e_cta",
"experiment_status": "active",
"is_experiment_active": true,
"allocation": [
{
"start": 0,
"end": 40
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.5,
"treatment": 0.5
}
},
{
"type": "experiment",
"experiment_id": "e_search",
"experiment_status": "concluded",
"allocation": [
{
"start": 40,
"end": 65
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.1,
"treatment": 0.9
}
}
]
}
}
],
"expected": {
"checkout": {
"variant_key": "treatment",
"variant_value": "treatment",
"experiment_id": "e_search"
}
}
},
{
"name": "multi-experiment flag returns nothing for an unallocated position",
"context": {
"distinct_id": "alice"
},
"flags": [
{
"id": "multi-experiment-1",
"project_id": 31,
"workspace_id": 1,
"name": "Checkout",
"key": "checkout",
"status": "enabled",
"context": "distinct_id",
"hash_salt": "salt1",
"ruleset": {
"variants": [
{
"key": "control",
"value": "control",
"is_control": true,
"split": 0.5
},
{
"key": "treatment",
"value": "treatment",
"is_control": false,
"split": 0.5
}
],
"rollout": [
{
"type": "experiment",
"experiment_id": "e_cta",
"experiment_status": "active",
"is_experiment_active": true,
"allocation": [
{
"start": 0,
"end": 40
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.5,
"treatment": 0.5
}
},
{
"type": "experiment",
"experiment_id": "e_search",
"experiment_status": "concluded",
"allocation": [
{
"start": 40,
"end": 65
}
],
"rollout_percentage": 0,
"cohort_hash": "abc123",
"variant_splits": {
"control": 0.1,
"treatment": 0.9
}
}
]
}
}
],
"expected": {}
}
]
}
Loading
Loading