absmartly-sdk 1.3.0.pre.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +55 -1
- data/Gemfile +4 -0
- data/Gemfile.lock +39 -32
- data/README.md +461 -139
- data/lib/a_b_smartly.rb +118 -22
- data/lib/absmartly/version.rb +1 -1
- data/lib/absmartly.rb +4 -0
- data/lib/audience_matcher.rb +93 -1
- data/lib/client.rb +44 -12
- data/lib/client_config.rb +3 -1
- data/lib/context.rb +1302 -222
- data/lib/context_data_result.rb +27 -0
- data/lib/context_event_logger.rb +3 -0
- data/lib/context_event_logger_callback.rb +1 -1
- data/lib/context_publisher.rb +9 -0
- data/lib/default_audience_deserializer.rb +2 -2
- data/lib/default_context_data_deserializer.rb +3 -3
- data/lib/default_context_event_serializer.rb +2 -1
- data/lib/default_context_publisher.rb +9 -0
- data/lib/default_http_client.rb +28 -7
- data/lib/default_http_client_config.rb +10 -1
- data/lib/default_variable_parser.rb +1 -1
- data/lib/hashing.rb +39 -2
- data/lib/json/context_data.rb +26 -3
- data/lib/json/experiment.rb +36 -10
- data/lib/json/exposure.rb +8 -4
- data/lib/json_expr/expr_evaluator.rb +7 -9
- data/lib/json_expr/json_expr.rb +23 -17
- data/lib/json_expr/operators/and_combinator.rb +2 -1
- data/lib/json_expr/operators/in_operator.rb +3 -1
- data/lib/json_expr/operators/match_operator.rb +9 -5
- data/lib/json_expr/operators/or_combinator.rb +4 -2
- data/lib/json_expr/operators/semver_operator.rb +17 -0
- data/lib/json_expr/operators/semver_operators.rb +50 -0
- data/lib/json_expr/semver_comparator.rb +98 -0
- data/lib/publish_error.rb +11 -0
- data/lib/scheduled_thread_pool_executor.rb +8 -0
- data/lib/type_utils.rb +59 -0
- data/lib/variant_assigner.rb +1 -1
- metadata +14 -5
- data/absmartly.gemspec +0 -45
- data/example/example.rb +0 -85
data/lib/context.rb
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Fiber.current and Fiber#alive? before Ruby 3.1.
|
|
4
|
+
require "fiber"
|
|
3
5
|
require_relative "hashing"
|
|
6
|
+
require_relative "type_utils"
|
|
4
7
|
require_relative "variant_assigner"
|
|
8
|
+
require_relative "audience_matcher"
|
|
9
|
+
require_relative "default_audience_deserializer"
|
|
5
10
|
require_relative "context_event_logger"
|
|
6
11
|
require_relative "json/unit"
|
|
7
12
|
require_relative "json/attribute"
|
|
@@ -10,46 +15,48 @@ require_relative "json/publish_event"
|
|
|
10
15
|
require_relative "json/goal_achievement"
|
|
11
16
|
|
|
12
17
|
class Context
|
|
13
|
-
|
|
18
|
+
# Immutable-by-convention snapshot swapped in with a single assignment so
|
|
19
|
+
# readers see either the complete old or the complete new state.
|
|
20
|
+
State = Struct.new(:data, :index, :index_variables, :custom_fields)
|
|
21
|
+
NO_RULES = ""
|
|
22
|
+
SEAL_FLUSH_PASSES = 2
|
|
23
|
+
private_constant :SEAL_FLUSH_PASSES
|
|
24
|
+
class EventsQueuedWhileClosing < StandardError; end
|
|
25
|
+
private_constant :EventsQueuedWhileClosing
|
|
26
|
+
NO_HOLDOUTS = {}.freeze
|
|
27
|
+
private_constant :NO_HOLDOUTS
|
|
28
|
+
# After this many overtaken walks a read returns the default, exposing nothing.
|
|
29
|
+
VARIABLE_WALK_ATTEMPTS = 3
|
|
30
|
+
private_constant :VARIABLE_WALK_ATTEMPTS
|
|
31
|
+
|
|
32
|
+
attr_reader :pending_count
|
|
14
33
|
|
|
15
34
|
def self.create(clock, config, data_future, data_provider,
|
|
16
|
-
event_handler, event_logger, variable_parser, audience_matcher
|
|
35
|
+
event_handler, event_logger, variable_parser, audience_matcher,
|
|
36
|
+
application: nil, environment: nil)
|
|
17
37
|
Context.new(clock, config, data_future, data_provider,
|
|
18
|
-
event_handler, event_logger, variable_parser, audience_matcher
|
|
38
|
+
event_handler, event_logger, variable_parser, audience_matcher,
|
|
39
|
+
application: application, environment: environment)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.create_async(clock, config, data_provider,
|
|
43
|
+
event_handler, event_logger, variable_parser, audience_matcher,
|
|
44
|
+
application: nil, environment: nil)
|
|
45
|
+
context = Context.allocate
|
|
46
|
+
context.send(:initialize_async, clock, config, data_provider,
|
|
47
|
+
event_handler, event_logger, variable_parser, audience_matcher,
|
|
48
|
+
application: application, environment: environment)
|
|
49
|
+
context
|
|
19
50
|
end
|
|
20
51
|
|
|
21
52
|
def initialize(clock, config, data_future, data_provider,
|
|
22
|
-
event_handler, event_logger, variable_parser, audience_matcher
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
@assignments = {}
|
|
28
|
-
@clock = clock.is_a?(String) ? Time.iso8601(clock) : clock
|
|
29
|
-
@publish_delay = config.publish_delay
|
|
30
|
-
@refresh_interval = config.refresh_interval
|
|
31
|
-
@event_handler = event_handler
|
|
32
|
-
@event_logger = !config.event_logger.nil? ? config.event_logger : event_logger
|
|
33
|
-
@data_provider = data_provider
|
|
34
|
-
@variable_parser = variable_parser
|
|
35
|
-
@audience_matcher = audience_matcher
|
|
36
|
-
@closed = false
|
|
37
|
-
|
|
38
|
-
@units = {}
|
|
39
|
-
@attributes = []
|
|
40
|
-
@overrides = {}
|
|
41
|
-
@cassignments = {}
|
|
42
|
-
@assigners = {}
|
|
43
|
-
@hashed_units = {}
|
|
44
|
-
@pending_count = 0
|
|
45
|
-
@exposures ||= []
|
|
46
|
-
|
|
47
|
-
set_units(config.units) if config.units
|
|
48
|
-
set_attributes(config.attributes) if config.attributes
|
|
49
|
-
set_overrides(config.overrides) if config.overrides
|
|
50
|
-
set_custom_assignments(config.custom_assignments) if config.custom_assignments
|
|
53
|
+
event_handler, event_logger, variable_parser, audience_matcher,
|
|
54
|
+
application: nil, environment: nil)
|
|
55
|
+
init_common(clock, config, data_provider, event_handler, event_logger,
|
|
56
|
+
variable_parser, audience_matcher, application, environment)
|
|
57
|
+
|
|
51
58
|
if data_future.success?
|
|
52
|
-
|
|
59
|
+
@state = build_state(data_future.data_future)
|
|
53
60
|
log_event(ContextEventLogger::EVENT_TYPE::READY, data_future.data_future)
|
|
54
61
|
else
|
|
55
62
|
set_data_failed(data_future.exception)
|
|
@@ -57,8 +64,58 @@ class Context
|
|
|
57
64
|
end
|
|
58
65
|
end
|
|
59
66
|
|
|
67
|
+
def set_data(data_future)
|
|
68
|
+
ready_data = nil
|
|
69
|
+
failure = nil
|
|
70
|
+
# Skip building a payload a refresh has already superseded; ready? is
|
|
71
|
+
# rechecked under the lock before a built snapshot is installed.
|
|
72
|
+
state = data_future.success? && !ready? ? build_state(data_future.data_future) : nil
|
|
73
|
+
@ready_mutex.synchronize do
|
|
74
|
+
# A refresh may already have installed data while the initial fetch was
|
|
75
|
+
# in flight: keep it, so a late success doesn't replace newer data and
|
|
76
|
+
# a late failure is only logged.
|
|
77
|
+
if ready?
|
|
78
|
+
failure = data_future.exception unless data_future.success?
|
|
79
|
+
elsif state
|
|
80
|
+
@state = state
|
|
81
|
+
else
|
|
82
|
+
failure = data_future.exception
|
|
83
|
+
set_data_failed(failure)
|
|
84
|
+
end
|
|
85
|
+
ready_data = @state.data unless @failed
|
|
86
|
+
@ready_condvar.broadcast
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# User callbacks run outside the readiness lock so they may call
|
|
90
|
+
# wait_until_ready.
|
|
91
|
+
log_error(failure) unless failure.nil?
|
|
92
|
+
log_event(ContextEventLogger::EVENT_TYPE::READY, ready_data) unless ready_data.nil?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def wait_until_ready(timeout = nil)
|
|
96
|
+
# Called from a variable parser while this thread builds the data that
|
|
97
|
+
# would make the context ready: waiting here could never finish.
|
|
98
|
+
return self if building?
|
|
99
|
+
|
|
100
|
+
@ready_mutex.synchronize do
|
|
101
|
+
unless ready? || failed?
|
|
102
|
+
if timeout
|
|
103
|
+
deadline = Time.now + timeout
|
|
104
|
+
until ready? || failed?
|
|
105
|
+
remaining = deadline - Time.now
|
|
106
|
+
break if remaining <= 0
|
|
107
|
+
@ready_condvar.wait(@ready_mutex, remaining)
|
|
108
|
+
end
|
|
109
|
+
else
|
|
110
|
+
@ready_condvar.wait(@ready_mutex) until ready? || failed?
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
self
|
|
115
|
+
end
|
|
116
|
+
|
|
60
117
|
def ready?
|
|
61
|
-
!@data.nil?
|
|
118
|
+
!@state.data.nil?
|
|
62
119
|
end
|
|
63
120
|
|
|
64
121
|
def failed?
|
|
@@ -69,20 +126,51 @@ class Context
|
|
|
69
126
|
@closed
|
|
70
127
|
end
|
|
71
128
|
|
|
129
|
+
def finalized?
|
|
130
|
+
closed?
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def finalizing?
|
|
134
|
+
!@closed && @sealing.positive?
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
attr_reader :ready_error
|
|
138
|
+
|
|
139
|
+
def get_unit(unit_type)
|
|
140
|
+
@units[unit_type.to_sym]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def get_units
|
|
144
|
+
@units.dup
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def get_attribute(name)
|
|
148
|
+
result = nil
|
|
149
|
+
@attributes.each do |attr|
|
|
150
|
+
result = attr.value if attr.name == name
|
|
151
|
+
end
|
|
152
|
+
result
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def get_attributes
|
|
156
|
+
user_attributes
|
|
157
|
+
end
|
|
158
|
+
|
|
72
159
|
def experiments
|
|
73
|
-
check_ready?(
|
|
160
|
+
check_ready?(false)
|
|
161
|
+
check_not_closed_by_close?
|
|
74
162
|
|
|
75
|
-
@data.experiments.map(&:name)
|
|
163
|
+
@state.data.experiments.map(&:name)
|
|
76
164
|
end
|
|
77
165
|
|
|
78
166
|
def set_override(experiment_name, variant)
|
|
79
|
-
|
|
167
|
+
check_not_closed_by_close?
|
|
80
168
|
|
|
81
169
|
@overrides[experiment_name.to_s.to_sym] = variant
|
|
82
170
|
end
|
|
83
171
|
|
|
84
172
|
def set_overrides(overrides)
|
|
85
|
-
|
|
173
|
+
check_not_closed_by_close?
|
|
86
174
|
|
|
87
175
|
@overrides.merge!(overrides.transform_keys(&:to_sym))
|
|
88
176
|
end
|
|
@@ -114,17 +202,30 @@ class Context
|
|
|
114
202
|
def set_unit(unit_type, uid)
|
|
115
203
|
check_not_closed?
|
|
116
204
|
|
|
205
|
+
if Absmartly::TypeUtils.numeric?(uid)
|
|
206
|
+
trimmed = uid
|
|
207
|
+
else
|
|
208
|
+
# Like v1.3.0, any other value (Symbol, session id, UUID object) is
|
|
209
|
+
# stored as its to_s text.
|
|
210
|
+
uid = uid.respond_to?(:to_str) ? uid.to_str : uid.to_s
|
|
211
|
+
trimmed = uid.strip
|
|
212
|
+
end
|
|
213
|
+
|
|
117
214
|
previous = @units[unit_type.to_sym]
|
|
118
|
-
|
|
215
|
+
# Compared as text, so 123456 and "123456" are the same unit, as in v1.3.0.
|
|
216
|
+
if !previous.nil? && previous.to_s != trimmed.to_s
|
|
119
217
|
raise IllegalStateException.new("Unit '#{unit_type}' already set.")
|
|
120
218
|
end
|
|
121
219
|
|
|
122
|
-
trimmed
|
|
123
|
-
if trimmed.empty?
|
|
220
|
+
if trimmed.is_a?(String) && trimmed.empty?
|
|
124
221
|
raise IllegalStateException.new("Unit '#{unit_type}' UID must not be blank.")
|
|
125
222
|
end
|
|
126
223
|
|
|
127
|
-
|
|
224
|
+
if previous.nil?
|
|
225
|
+
@units[unit_type.to_sym] = trimmed
|
|
226
|
+
invalidate_assignments_missing_unit(unit_type.to_s)
|
|
227
|
+
end
|
|
228
|
+
trimmed.to_s
|
|
128
229
|
end
|
|
129
230
|
|
|
130
231
|
def set_units(units)
|
|
@@ -137,6 +238,7 @@ class Context
|
|
|
137
238
|
check_not_closed?
|
|
138
239
|
|
|
139
240
|
@attributes.push(Attribute.new(name, value, @clock.to_i))
|
|
241
|
+
@attrs_seq += 1
|
|
140
242
|
end
|
|
141
243
|
|
|
142
244
|
def set_attributes(attributes)
|
|
@@ -147,35 +249,23 @@ class Context
|
|
|
147
249
|
|
|
148
250
|
def treatment(experiment_name)
|
|
149
251
|
check_ready?(true)
|
|
150
|
-
assignment = assignment(experiment_name)
|
|
151
|
-
unless assignment.exposed
|
|
152
|
-
queue_exposure(assignment)
|
|
153
|
-
end
|
|
154
252
|
|
|
155
|
-
|
|
253
|
+
while true # rubocop:disable Style/InfiniteLoop -- no block allocation on the read path
|
|
254
|
+
assignment = assignment(experiment_name)
|
|
255
|
+
# set_unit may evict it between the lookup and the exposure claim:
|
|
256
|
+
# then it is resolved again, so the variant served is the one exposed.
|
|
257
|
+
return assignment.variant unless claim_and_queue_exposure(assignment, experiment_name) == :evicted
|
|
258
|
+
end
|
|
156
259
|
end
|
|
157
260
|
|
|
261
|
+
# A held-out experiment publishes no exposure of its own, unless an override
|
|
262
|
+
# or rule chose its variant. The holdouts covering it then expose once each,
|
|
263
|
+
# in id order, from the arms pinned when the assignment was made, so a
|
|
264
|
+
# refresh in between can't publish another holdout epoch (as in JS).
|
|
158
265
|
def queue_exposure(assignment)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
exposure = Exposure.new
|
|
163
|
-
exposure.id = assignment.id || 0
|
|
164
|
-
exposure.name = assignment.name
|
|
165
|
-
exposure.unit = assignment.unit_type
|
|
166
|
-
exposure.variant = assignment.variant
|
|
167
|
-
exposure.exposed_at = @clock.to_i
|
|
168
|
-
exposure.assigned = assignment.assigned
|
|
169
|
-
exposure.eligible = assignment.eligible
|
|
170
|
-
exposure.overridden = assignment.overridden
|
|
171
|
-
exposure.full_on = assignment.full_on
|
|
172
|
-
exposure.custom = assignment.custom
|
|
173
|
-
exposure.audience_mismatch = assignment.audience_mismatch
|
|
174
|
-
|
|
175
|
-
@pending_count += 1
|
|
176
|
-
@exposures.push(exposure)
|
|
177
|
-
log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, exposure)
|
|
178
|
-
end
|
|
266
|
+
result = nil
|
|
267
|
+
claim_and_queue_exposure(assignment) { |logged| result = logged }
|
|
268
|
+
result
|
|
179
269
|
end
|
|
180
270
|
|
|
181
271
|
def peek_treatment(experiment_name)
|
|
@@ -184,31 +274,42 @@ class Context
|
|
|
184
274
|
assignment(experiment_name).variant
|
|
185
275
|
end
|
|
186
276
|
|
|
277
|
+
alias peek peek_treatment
|
|
278
|
+
|
|
279
|
+
# Maps each variable key to one experiment name, as in v1.3.0: the
|
|
280
|
+
# lowest-id experiment defining it, which is not necessarily the one whose
|
|
281
|
+
# value variable_value returns (see variable_keys_list).
|
|
187
282
|
def variable_keys
|
|
188
283
|
check_ready?(true)
|
|
189
284
|
|
|
190
285
|
hsh = {}
|
|
191
|
-
@index_variables.each { |key,
|
|
286
|
+
@state.index_variables.each { |key, values| hsh[key] = values.first.data.name }
|
|
192
287
|
hsh
|
|
193
288
|
end
|
|
194
289
|
|
|
195
|
-
|
|
290
|
+
# Maps each variable key to the names of every experiment that defines it.
|
|
291
|
+
def variable_keys_list
|
|
196
292
|
check_ready?(true)
|
|
197
293
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return assignment.variables[key.to_s.to_sym] if assignment.variables.key?(key.to_s.to_sym)
|
|
294
|
+
hsh = {}
|
|
295
|
+
@state.index_variables.each do |key, values|
|
|
296
|
+
hsh[key] = values.map { |v| v.data.name }
|
|
202
297
|
end
|
|
298
|
+
hsh
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def variable_value(key, default_value)
|
|
302
|
+
check_ready?(true)
|
|
203
303
|
|
|
204
|
-
default_value
|
|
304
|
+
resolve_variable_value(key, default_value, true)
|
|
205
305
|
end
|
|
206
306
|
|
|
207
307
|
def custom_field_keys
|
|
208
308
|
check_ready?(true)
|
|
309
|
+
|
|
209
310
|
keys = []
|
|
210
311
|
|
|
211
|
-
@data.experiments.each do |experiment|
|
|
312
|
+
@state.data.experiments.each do |experiment|
|
|
212
313
|
custom_field_values = experiment.custom_field_values
|
|
213
314
|
if custom_field_values != nil
|
|
214
315
|
custom_field_values.each do |custom_field|
|
|
@@ -217,60 +318,54 @@ class Context
|
|
|
217
318
|
end
|
|
218
319
|
end
|
|
219
320
|
|
|
220
|
-
|
|
321
|
+
keys.sort.uniq
|
|
221
322
|
end
|
|
222
323
|
|
|
223
324
|
def custom_field_value(experimentName, key)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
experiment_custom_fields = @context_custom_fields[experimentName]
|
|
227
|
-
|
|
228
|
-
if experiment_custom_fields != nil
|
|
229
|
-
field = experiment_custom_fields[key]
|
|
230
|
-
if field != nil
|
|
231
|
-
return field.value
|
|
232
|
-
end
|
|
233
|
-
end
|
|
325
|
+
custom_field(experimentName, key)&.value
|
|
326
|
+
end
|
|
234
327
|
|
|
235
|
-
|
|
328
|
+
def custom_field_value_type(experimentName, key)
|
|
329
|
+
custom_field(experimentName, key)&.type
|
|
236
330
|
end
|
|
237
331
|
|
|
238
332
|
def custom_field_type(experimentName, key)
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
experiment_custom_fields = @context_custom_fields[experimentName]
|
|
242
|
-
|
|
243
|
-
if experiment_custom_fields != nil
|
|
244
|
-
field = experiment_custom_fields[key]
|
|
245
|
-
if field != nil
|
|
246
|
-
return field.type
|
|
247
|
-
end
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
return nil
|
|
333
|
+
custom_field_value_type(experimentName, key)
|
|
251
334
|
end
|
|
252
335
|
|
|
253
336
|
def peek_variable_value(key, default_value)
|
|
254
337
|
check_ready?(true)
|
|
255
338
|
|
|
256
|
-
|
|
257
|
-
return assignment.variables[key.to_s.to_sym] if !assignment.nil? &&
|
|
258
|
-
!assignment.variables.nil? &&
|
|
259
|
-
assignment.variables.key?(key.to_s.to_sym)
|
|
260
|
-
|
|
261
|
-
default_value
|
|
339
|
+
resolve_variable_value(key, default_value, false)
|
|
262
340
|
end
|
|
263
341
|
|
|
264
|
-
def track(goal_name, properties)
|
|
342
|
+
def track(goal_name, properties = nil)
|
|
265
343
|
check_not_closed?
|
|
344
|
+
# Hash-like objects such as ActionController::Parameters are accepted
|
|
345
|
+
# through to_unsafe_h or to_h, as v1.3.0 accepted any properties object.
|
|
346
|
+
# to_unsafe_h comes first because Parameters#to_h raises unless permitted.
|
|
347
|
+
if !properties.nil? && !Absmartly::TypeUtils.object?(properties) && !properties.is_a?(Array)
|
|
348
|
+
if properties.respond_to?(:to_unsafe_h)
|
|
349
|
+
properties = properties.to_unsafe_h
|
|
350
|
+
elsif properties.respond_to?(:to_h)
|
|
351
|
+
properties = properties.to_h
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
unless properties.nil? || Absmartly::TypeUtils.object?(properties)
|
|
355
|
+
raise ArgumentError, "Goal '#{goal_name}' properties must be of type object."
|
|
356
|
+
end
|
|
266
357
|
|
|
267
358
|
achievement = GoalAchievement.new
|
|
268
359
|
achievement.achieved_at = @clock.to_i
|
|
269
360
|
achievement.name = goal_name
|
|
270
|
-
achievement.properties = properties
|
|
361
|
+
achievement.properties = properties.nil? ? nil : properties.dup
|
|
271
362
|
|
|
272
|
-
@
|
|
273
|
-
|
|
363
|
+
@queue_mutex.synchronize do
|
|
364
|
+
check_not_closed?
|
|
365
|
+
|
|
366
|
+
@pending_count += 1
|
|
367
|
+
@achievements.push(achievement)
|
|
368
|
+
end
|
|
274
369
|
log_event(ContextEventLogger::EVENT_TYPE::GOAL, achievement)
|
|
275
370
|
end
|
|
276
371
|
|
|
@@ -282,83 +377,433 @@ class Context
|
|
|
282
377
|
|
|
283
378
|
def refresh
|
|
284
379
|
check_not_closed?
|
|
380
|
+
return nil if @failed
|
|
285
381
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
382
|
+
data_future = @data_provider.context_data
|
|
383
|
+
if data_future.success?
|
|
384
|
+
state = build_state(data_future.data_future)
|
|
385
|
+
# Committed under the readiness lock, so a waiter on a context created
|
|
386
|
+
# asynchronously wakes, and an initial fetch that failed while this
|
|
387
|
+
# refresh was in flight no longer leaves the context failed.
|
|
388
|
+
Thread.handle_interrupt(Object => :never) do
|
|
389
|
+
@ready_mutex.synchronize do
|
|
390
|
+
@state = state
|
|
391
|
+
@failed = false
|
|
392
|
+
@data_failed = nil
|
|
393
|
+
@ready_error = nil
|
|
394
|
+
@ready_condvar.broadcast
|
|
395
|
+
end
|
|
294
396
|
end
|
|
397
|
+
log_event(ContextEventLogger::EVENT_TYPE::REFRESH, data_future.data_future)
|
|
398
|
+
else
|
|
399
|
+
log_error(data_future.exception)
|
|
295
400
|
end
|
|
296
401
|
end
|
|
297
402
|
|
|
403
|
+
# Publishes pending events, then seals the context and logs "close", as in
|
|
404
|
+
# v1.3.0: a publish the collector rejects is logged as an error and the
|
|
405
|
+
# context is still sealed. A publisher that raises propagates the error and
|
|
406
|
+
# leaves the context open with the events queued.
|
|
298
407
|
def close
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
408
|
+
seal(ContextEventLogger::EVENT_TYPE::CLOSE)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# Like close, but as the JavaScript SDK does: if the final publish fails, the
|
|
412
|
+
# failure is raised and the context stays open with the events queued, so
|
|
413
|
+
# finalize can be retried. Logs "finalize" once sealed, and set_override,
|
|
414
|
+
# set_overrides and experiments keep working afterwards.
|
|
415
|
+
def finalize
|
|
416
|
+
seal(ContextEventLogger::EVENT_TYPE::FINALIZE)
|
|
306
417
|
end
|
|
307
418
|
|
|
308
419
|
def data
|
|
309
420
|
check_ready?(true)
|
|
310
421
|
|
|
311
|
-
@data
|
|
422
|
+
@state.data
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
def inspect
|
|
426
|
+
"#<Context ready=#{ready?} closed=#{closed?}>"
|
|
312
427
|
end
|
|
313
428
|
|
|
314
429
|
private
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
430
|
+
# SDK reads check the cache under the assignment lock; a direct
|
|
431
|
+
# queue_exposure call has nothing to check and may already hold it (from a
|
|
432
|
+
# custom matcher), so it skips it.
|
|
433
|
+
def with_assignment_lock_if(locked, &block)
|
|
434
|
+
locked ? @assignment_mutex.synchronize(&block) : yield
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
# Claims and queues assignment's exposure. With experiment_name, the claim
|
|
438
|
+
# is taken only while the assignment is still the cached one for that
|
|
439
|
+
# experiment (checked under the assignment lock, which set_unit also
|
|
440
|
+
# takes to evict), and a no-unit split assignment whose unit has since
|
|
441
|
+
# been set counts as stale; then nothing is queued and :evicted is
|
|
442
|
+
# returned so the caller resolves it again. Returns nil otherwise, never
|
|
443
|
+
# the logger's result (that is passed to the block).
|
|
444
|
+
def claim_and_queue_exposure(assignment, experiment_name = nil)
|
|
445
|
+
return if assignment.exposed
|
|
446
|
+
|
|
447
|
+
claimed = false
|
|
448
|
+
begin
|
|
449
|
+
loop do
|
|
450
|
+
status = nil
|
|
451
|
+
# Waiting for the assignment lock stays interruptible; only the claim
|
|
452
|
+
# bookkeeping under it is masked.
|
|
453
|
+
with_assignment_lock_if(experiment_name) do
|
|
454
|
+
Thread.handle_interrupt(Object => :never) do
|
|
455
|
+
@queue_mutex.synchronize do
|
|
456
|
+
check_not_closed?
|
|
457
|
+
owner = @exposing[assignment]
|
|
458
|
+
status =
|
|
459
|
+
if owner
|
|
460
|
+
# Another thread's exposure is in flight; this thread's own
|
|
461
|
+
# (a reentrant read from its clock) uses the outer one.
|
|
462
|
+
owner.equal?(Thread.current) ? :reentered : :waiting
|
|
463
|
+
elsif assignment.exposed
|
|
464
|
+
:exposed
|
|
465
|
+
# On the SDK's read paths (experiment_name given), a no-unit
|
|
466
|
+
# split assignment whose unit has since been set is stale too
|
|
467
|
+
# (a failed exposure left it cached): resolved again. A
|
|
468
|
+
# direct queue_exposure call queues what it is given.
|
|
469
|
+
elsif experiment_name.nil? ||
|
|
470
|
+
(@assignment_cache[experiment_name.to_s].equal?(assignment) && !unit_now_set?(assignment))
|
|
471
|
+
@exposing[assignment] = Thread.current
|
|
472
|
+
claimed = true
|
|
473
|
+
:claimed
|
|
474
|
+
else
|
|
475
|
+
:evicted
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
return :evicted if status == :evicted
|
|
481
|
+
break unless status == :waiting
|
|
482
|
+
|
|
483
|
+
@queue_mutex.synchronize do
|
|
484
|
+
owner = @exposing[assignment]
|
|
485
|
+
@exposure_condvar.wait(@queue_mutex) if owner && !owner.equal?(Thread.current)
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
return unless claimed
|
|
489
|
+
|
|
490
|
+
# Only this assignment is claimed, without holding a lock over the
|
|
491
|
+
# supplied clock; distinct nested or concurrent exposures can proceed.
|
|
492
|
+
exposed_at = @clock.to_i
|
|
493
|
+
exposure = nil
|
|
494
|
+
holdout_exposures = nil
|
|
495
|
+
@queue_mutex.synchronize do
|
|
496
|
+
# Checked under the queue lock so an exposure cannot slip in after close
|
|
497
|
+
# sealed the queue and be stranded.
|
|
498
|
+
check_not_closed?
|
|
499
|
+
|
|
500
|
+
unless assignment.exposed
|
|
501
|
+
assignment.exposed = true
|
|
502
|
+
|
|
503
|
+
# A held-out experiment publishes no exposure of its own, unless an
|
|
504
|
+
# override or rule chose its variant. The holdouts covering it then
|
|
505
|
+
# expose once each, in id order, from the arms pinned when the
|
|
506
|
+
# assignment was made (as in JS).
|
|
507
|
+
if !assignment.suppressed || assignment.overridden || assignment.rule_override
|
|
508
|
+
exposure = enqueue_exposure(assignment.name, assignment, exposed_at)
|
|
326
509
|
end
|
|
510
|
+
assignment.holdout_assignments&.each_with_index do |holdout_assignment, i|
|
|
511
|
+
next if holdout_assignment.nil? || holdout_assignment.exposed
|
|
512
|
+
|
|
513
|
+
holdout_assignment.exposed = true
|
|
514
|
+
(holdout_exposures ||= []) << enqueue_exposure(assignment.holdouts[i].name, holdout_assignment, exposed_at)
|
|
515
|
+
end
|
|
516
|
+
end
|
|
517
|
+
end
|
|
518
|
+
ensure
|
|
519
|
+
# Only the claim is released here (no assignment lock, so an unwinding
|
|
520
|
+
# timeout is not held up). A no-unit assignment left unexposed after its
|
|
521
|
+
# unit was set is rebuilt by the next read (see cached_or_new_assignment).
|
|
522
|
+
if claimed
|
|
523
|
+
Thread.handle_interrupt(Object => :never) do
|
|
524
|
+
@queue_mutex.synchronize do
|
|
525
|
+
@exposing.delete(assignment)
|
|
526
|
+
@exposure_condvar.broadcast
|
|
527
|
+
end
|
|
528
|
+
end
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
# User callbacks run outside the lock so they can call publish/track.
|
|
532
|
+
logged = log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, exposure) unless exposure.nil?
|
|
533
|
+
holdout_exposures&.each { |e| log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, e) }
|
|
534
|
+
yield logged if block_given? && !exposure.nil?
|
|
535
|
+
nil
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
def seal(event_type)
|
|
539
|
+
return nil if @closed
|
|
540
|
+
|
|
541
|
+
# Called from a logger callback while a publish is in flight: defer so the
|
|
542
|
+
# events queued by the callback are still sent before finalizing. A seal
|
|
543
|
+
# that is already running does not claim it, so the request survives if
|
|
544
|
+
# that seal fails or gives up.
|
|
545
|
+
if @publish_mutex.owned?
|
|
546
|
+
@deferred_mutex.synchronize do
|
|
547
|
+
@close_pending = event_type
|
|
548
|
+
@close_requests += 1
|
|
549
|
+
end
|
|
550
|
+
return nil
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
close = event_type == ContextEventLogger::EVENT_TYPE::CLOSE
|
|
554
|
+
registered = false
|
|
555
|
+
begin
|
|
556
|
+
Thread.handle_interrupt(Object => :never) do
|
|
557
|
+
@queue_mutex.synchronize { @sealing += 1 }
|
|
558
|
+
registered = true
|
|
559
|
+
end
|
|
560
|
+
# One extra pass sends events a logger callback queued during the
|
|
561
|
+
# first publish; after that raise with the remaining events queued,
|
|
562
|
+
# so a callback can't keep close publishing forever or lose events.
|
|
563
|
+
SEAL_FLUSH_PASSES.times do |pass|
|
|
564
|
+
# close drops a batch the collector rejected, as in v1.3.0, but only
|
|
565
|
+
# that batch; anything queued meanwhile is still sent or kept.
|
|
566
|
+
result = flush(sealing: true, drop_rejected: close)
|
|
567
|
+
last_pass = pass == SEAL_FLUSH_PASSES - 1
|
|
568
|
+
raise_publish_failure(result) if !close && publish_failed?(result)
|
|
569
|
+
|
|
570
|
+
sealed = @queue_mutex.synchronize do
|
|
571
|
+
next :done if @closed
|
|
572
|
+
|
|
573
|
+
unless @exposures.empty? && @achievements.empty?
|
|
574
|
+
raise EventsQueuedWhileClosing.new("Events queued while closing; retry close or finalize") if last_pass
|
|
575
|
+
|
|
576
|
+
next :retry
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
Thread.handle_interrupt(Object => :never) do
|
|
580
|
+
@sealed_by = event_type
|
|
581
|
+
@closed = true
|
|
582
|
+
end
|
|
583
|
+
:sealed
|
|
584
|
+
end
|
|
585
|
+
return nil if sealed == :done
|
|
586
|
+
next if sealed == :retry
|
|
587
|
+
|
|
588
|
+
logged = log_event(event_type, nil)
|
|
589
|
+
return close ? logged : nil
|
|
590
|
+
end
|
|
591
|
+
nil
|
|
592
|
+
ensure
|
|
593
|
+
if registered
|
|
594
|
+
Thread.handle_interrupt(Object => :never) do
|
|
595
|
+
@queue_mutex.synchronize { @sealing -= 1 }
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
end
|
|
599
|
+
end
|
|
327
600
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
601
|
+
# sealing: this flush is a seal's own pass, so it neither starts another
|
|
602
|
+
# seal nor claims a deferred one. Any other publish (on any thread) may.
|
|
603
|
+
def flush(sealing: false, drop_rejected: false)
|
|
604
|
+
dropped, failure = @failed ? @ready_mutex.synchronize { drop_if_failed(sealing) } : [false, nil]
|
|
605
|
+
return failure if dropped
|
|
606
|
+
|
|
607
|
+
# Serialize publishes so a queued event is part of at most one in-flight
|
|
608
|
+
# request; on failure the events stay queued for the next attempt.
|
|
609
|
+
# A logger callback that calls publish from inside a publish must not
|
|
610
|
+
# deadlock or re-send the in-flight snapshot; the outer call owns it.
|
|
611
|
+
return nil if @publish_mutex.owned?
|
|
612
|
+
|
|
613
|
+
claimed = nil
|
|
614
|
+
begin
|
|
615
|
+
result = @publish_mutex.synchronize do
|
|
616
|
+
exposures = achievements = nil
|
|
617
|
+
sent = nil
|
|
618
|
+
event_count = 0
|
|
619
|
+
# Failure installation and recovery both take this lock. Keep the
|
|
620
|
+
# decision and snapshot together, but never hold it across publishing.
|
|
621
|
+
dropped, failure = @ready_mutex.synchronize do
|
|
622
|
+
failed_result = drop_if_failed(sealing)
|
|
623
|
+
unless failed_result.first
|
|
624
|
+
@queue_mutex.synchronize do
|
|
625
|
+
exposures = @exposures.dup
|
|
626
|
+
achievements = @achievements.dup
|
|
627
|
+
event_count = exposures.size + achievements.size
|
|
628
|
+
# Which arrays were sent and how much of each: the queues only
|
|
629
|
+
# grow by appending or are replaced whole, so a handler that
|
|
630
|
+
# mutates its payload can't change what is removed.
|
|
631
|
+
sent = [@exposures, exposures.size, @achievements, achievements.size]
|
|
632
|
+
end
|
|
331
633
|
end
|
|
634
|
+
failed_result
|
|
635
|
+
end
|
|
636
|
+
next failure if dropped
|
|
637
|
+
|
|
638
|
+
if event_count.zero?
|
|
639
|
+
# Assigned under the mask, so an interrupt can't land between
|
|
640
|
+
# clearing the request and recording that this publish holds it.
|
|
641
|
+
Thread.handle_interrupt(Object => :never) { claimed = take_close_pending } unless sealing
|
|
642
|
+
next nil
|
|
643
|
+
end
|
|
332
644
|
|
|
333
|
-
|
|
645
|
+
event = PublishEvent.new
|
|
646
|
+
event.hashed = true
|
|
647
|
+
event.published_at = @clock.to_i
|
|
648
|
+
event.units = @units.map do |key, value|
|
|
649
|
+
Unit.new(key.to_s, unit_hash(key, value))
|
|
650
|
+
end
|
|
651
|
+
event.exposures = exposures unless exposures.empty?
|
|
652
|
+
event.attributes = @attributes unless @attributes.empty?
|
|
653
|
+
event.goals = achievements unless achievements.empty?
|
|
654
|
+
# Like the JavaScript SDK, the publish event is logged once the batch is
|
|
655
|
+
# delivered; a failed delivery logs an error event instead.
|
|
656
|
+
begin
|
|
657
|
+
response = @event_handler.publish(self, event)
|
|
658
|
+
rescue StandardError => e
|
|
659
|
+
log_error(e)
|
|
660
|
+
raise
|
|
661
|
+
end
|
|
334
662
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
663
|
+
if publish_failed?(response)
|
|
664
|
+
if response.is_a?(Exception)
|
|
665
|
+
log_error(response)
|
|
666
|
+
elsif response.respond_to?(:exception) && response.exception
|
|
667
|
+
log_error(response.exception)
|
|
668
|
+
else
|
|
669
|
+
log_error(StandardError.new("Publish failed (status=#{response.respond_to?(:status) ? response.status : 'unknown'})"))
|
|
340
670
|
end
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
671
|
+
remove_published(sent) if drop_rejected
|
|
672
|
+
response
|
|
673
|
+
else
|
|
344
674
|
log_event(ContextEventLogger::EVENT_TYPE::PUBLISH, event)
|
|
345
|
-
|
|
675
|
+
remove_published(sent)
|
|
676
|
+
# Claim a deferred close while still holding the lock, so only this
|
|
677
|
+
# successful publish can carry it out; a request made by another
|
|
678
|
+
# thread after a rejected publish waits for its own success.
|
|
679
|
+
# Assigned under the mask, so an interrupt can't land between
|
|
680
|
+
# clearing the request and recording that this publish holds it.
|
|
681
|
+
Thread.handle_interrupt(Object => :never) { claimed = take_close_pending } unless sealing
|
|
682
|
+
response
|
|
346
683
|
end
|
|
347
684
|
end
|
|
348
|
-
|
|
685
|
+
|
|
686
|
+
if claimed
|
|
687
|
+
claimed = nil if run_deferred_seal(claimed)
|
|
688
|
+
end
|
|
689
|
+
result
|
|
690
|
+
ensure
|
|
691
|
+
# A deferred seal claimed by this publish that did not finish stays
|
|
692
|
+
# pending for the next successful publish: one not started (an
|
|
693
|
+
# interrupt landed after the claim), one that left events queued, or
|
|
694
|
+
# one whose own publish was rejected, raised or was interrupted (a
|
|
695
|
+
# Timeout or Thread#kill). Any error still propagates.
|
|
696
|
+
restore_close_pending(claimed) if claimed
|
|
697
|
+
end
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
# A failed context drops its queued events. Called under the readiness
|
|
701
|
+
# lock, which a recovering refresh also holds while it clears @failed, so
|
|
702
|
+
# goals tracked after recovery are never dropped by a publish that saw the
|
|
703
|
+
# context failed. publish gets the load error, as in v1.3.0; a seal's own
|
|
704
|
+
# pass gets nil, since dropping them is not a publish failure.
|
|
705
|
+
def drop_if_failed(sealing)
|
|
706
|
+
return [false, nil] unless @failed
|
|
707
|
+
|
|
708
|
+
@queue_mutex.synchronize do
|
|
349
709
|
@exposures = []
|
|
350
710
|
@achievements = []
|
|
351
711
|
@pending_count = 0
|
|
352
|
-
|
|
712
|
+
end
|
|
713
|
+
[true, sealing ? nil : @data_failed]
|
|
714
|
+
end
|
|
715
|
+
|
|
716
|
+
# Removes the sent prefix of each queue, unless a failed-context publish
|
|
717
|
+
# replaced that queue while the request was in flight (then the batch is
|
|
718
|
+
# already gone and the new queue holds only newer events).
|
|
719
|
+
def remove_published((exposures, exposure_count, achievements, achievement_count))
|
|
720
|
+
@queue_mutex.synchronize do
|
|
721
|
+
@exposures.shift(exposure_count) if @exposures.equal?(exposures)
|
|
722
|
+
@achievements.shift(achievement_count) if @achievements.equal?(achievements)
|
|
723
|
+
@pending_count = @exposures.size + @achievements.size
|
|
724
|
+
end
|
|
725
|
+
end
|
|
726
|
+
|
|
727
|
+
def take_close_pending
|
|
728
|
+
@deferred_mutex.synchronize do
|
|
729
|
+
next nil unless @close_pending
|
|
730
|
+
|
|
731
|
+
claimed = [@close_pending, @close_requests]
|
|
732
|
+
@close_pending = false
|
|
733
|
+
claimed
|
|
734
|
+
end
|
|
735
|
+
end
|
|
736
|
+
|
|
737
|
+
# True once the seal finished; false when events were still queued.
|
|
738
|
+
def run_deferred_seal(claimed)
|
|
739
|
+
seal(claimed.first)
|
|
740
|
+
true
|
|
741
|
+
rescue EventsQueuedWhileClosing
|
|
742
|
+
false
|
|
743
|
+
end
|
|
744
|
+
|
|
745
|
+
# Unless the context sealed or a newer request was made meanwhile (even
|
|
746
|
+
# one another publish has already claimed).
|
|
747
|
+
def restore_close_pending((event_type, request))
|
|
748
|
+
Thread.handle_interrupt(Object => :never) do
|
|
749
|
+
@deferred_mutex.synchronize do
|
|
750
|
+
@close_pending = event_type unless @closed || @close_requests != request
|
|
751
|
+
end
|
|
752
|
+
end
|
|
753
|
+
end
|
|
754
|
+
|
|
755
|
+
def publish_failed?(result)
|
|
756
|
+
result.is_a?(Exception) ||
|
|
757
|
+
(result.respond_to?(:exception) && result.exception) ||
|
|
758
|
+
(result.respond_to?(:success?) && !result.success?)
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
def raise_publish_failure(result)
|
|
762
|
+
if result.is_a?(StandardError)
|
|
763
|
+
raise result
|
|
764
|
+
elsif result.is_a?(Exception)
|
|
765
|
+
raise StandardError.new(result.message), cause: result
|
|
766
|
+
elsif result.respond_to?(:exception) && result.exception
|
|
767
|
+
raise_publish_failure(result.exception)
|
|
768
|
+
else
|
|
769
|
+
status = result.respond_to?(:status) ? result.status : "unknown"
|
|
770
|
+
raise StandardError.new("Publish failed (status=#{status})")
|
|
353
771
|
end
|
|
354
772
|
end
|
|
355
773
|
|
|
774
|
+
# Called under the queue lock; exposed_at is read before taking it, since a
|
|
775
|
+
# supplied clock may call back into the context.
|
|
776
|
+
def enqueue_exposure(name, assignment, exposed_at)
|
|
777
|
+
exposure = Exposure.new
|
|
778
|
+
exposure.id = assignment.id || 0
|
|
779
|
+
exposure.name = name
|
|
780
|
+
exposure.unit = assignment.unit_type
|
|
781
|
+
exposure.variant = assignment.variant
|
|
782
|
+
exposure.exposed_at = exposed_at
|
|
783
|
+
exposure.assigned = assignment.assigned
|
|
784
|
+
exposure.eligible = assignment.eligible
|
|
785
|
+
exposure.overridden = assignment.overridden
|
|
786
|
+
exposure.full_on = assignment.full_on
|
|
787
|
+
exposure.custom = assignment.custom
|
|
788
|
+
exposure.audience_mismatch = assignment.audience_mismatch
|
|
789
|
+
exposure.rule_override = assignment.rule_override
|
|
790
|
+
|
|
791
|
+
@pending_count += 1
|
|
792
|
+
@exposures.push(exposure)
|
|
793
|
+
exposure
|
|
794
|
+
end
|
|
795
|
+
|
|
356
796
|
def check_not_closed?
|
|
357
797
|
if @closed
|
|
358
798
|
raise IllegalStateException.new("ABSmartly Context is closed")
|
|
359
799
|
end
|
|
360
800
|
end
|
|
361
801
|
|
|
802
|
+
# After finalize these still work, as in the JavaScript SDK.
|
|
803
|
+
def check_not_closed_by_close?
|
|
804
|
+
check_not_closed? if @sealed_by == ContextEventLogger::EVENT_TYPE::CLOSE
|
|
805
|
+
end
|
|
806
|
+
|
|
362
807
|
def check_ready?(expect_not_closed)
|
|
363
808
|
if !ready?
|
|
364
809
|
raise IllegalStateException.new("ABSmartly Context is not yet ready")
|
|
@@ -367,6 +812,11 @@ class Context
|
|
|
367
812
|
end
|
|
368
813
|
end
|
|
369
814
|
|
|
815
|
+
def custom_field(experiment_name, key)
|
|
816
|
+
check_ready?(true)
|
|
817
|
+
@state.custom_fields.dig(experiment_name, key)
|
|
818
|
+
end
|
|
819
|
+
|
|
370
820
|
def experiment_matches(experiment, assignment)
|
|
371
821
|
experiment.id == assignment.id &&
|
|
372
822
|
experiment.unit_type == assignment.unit_type &&
|
|
@@ -375,74 +825,348 @@ class Context
|
|
|
375
825
|
experiment.traffic_split == assignment.traffic_split
|
|
376
826
|
end
|
|
377
827
|
|
|
828
|
+
# System values go in first, then user-set attributes overlay them, so a
|
|
829
|
+
# user attribute always wins over the system value for the same name, even
|
|
830
|
+
# when set after the context was created (the @attrs_seq bump re-evaluates
|
|
831
|
+
# the cached assignment). Empty system values were skipped at init, so a
|
|
832
|
+
# missing config leaves the key out rather than matching on "".
|
|
833
|
+
# The latest value per name, keyed as the caller set it. Private so an
|
|
834
|
+
# override of the public get_attributes doesn't change matcher inputs.
|
|
835
|
+
def user_attributes
|
|
836
|
+
result = {}
|
|
837
|
+
@attributes.each { |attr| result[attr.name] = attr.value }
|
|
838
|
+
result
|
|
839
|
+
end
|
|
840
|
+
|
|
841
|
+
def attributes_map
|
|
842
|
+
# User attributes keep the caller's key, as v1.3.0 passed them to custom
|
|
843
|
+
# matchers. A system entry is added only when no user attribute has that
|
|
844
|
+
# name as a String or Symbol, the forms audience conditions can read.
|
|
845
|
+
user = user_attributes
|
|
846
|
+
attrs = {}
|
|
847
|
+
@system_attributes.each do |name, value|
|
|
848
|
+
attrs[name] = value unless user.key?(name) || user.key?(name.to_sym)
|
|
849
|
+
end
|
|
850
|
+
attrs.merge!(user)
|
|
851
|
+
end
|
|
852
|
+
|
|
853
|
+
# The built-in evaluator reads a name as a String or a Symbol, so it gets
|
|
854
|
+
# one entry per name: the latest value set under either spelling.
|
|
855
|
+
def evaluator_attributes
|
|
856
|
+
attrs = {}
|
|
857
|
+
@system_attributes.each { |name, value| attrs[name] = value }
|
|
858
|
+
@attributes.each { |attr| attrs[attr.name.to_s] = attr.value }
|
|
859
|
+
attrs
|
|
860
|
+
end
|
|
861
|
+
|
|
862
|
+
# Custom matchers get the caller's keys (as in v1.3.0); the built-in one
|
|
863
|
+
# gets the latest value per name.
|
|
864
|
+
def matcher_attributes(matcher)
|
|
865
|
+
matcher.instance_of?(AudienceMatcher) ? evaluator_attributes : attributes_map
|
|
866
|
+
end
|
|
867
|
+
|
|
868
|
+
# A built-in audience matcher is also the rule matcher, and the evaluator
|
|
869
|
+
# never changes its input, so the map built for the rules is reused.
|
|
870
|
+
def audience_attributes(rule_attrs)
|
|
871
|
+
return rule_attrs if !rule_attrs.nil? && @audience_matcher.instance_of?(AudienceMatcher)
|
|
872
|
+
|
|
873
|
+
matcher_attributes(@audience_matcher)
|
|
874
|
+
end
|
|
875
|
+
|
|
876
|
+
# A split rule or a holdout resolved while its unit was missing gave the
|
|
877
|
+
# no-unit result; once the unit is set, unexposed ones are rebuilt. Exposed ones
|
|
878
|
+
# stay, so the user never switches variant or gets a second exposure.
|
|
879
|
+
# One being exposed right now (its clock is running) is kept: it is
|
|
880
|
+
# about to be queued with its variant, so evicting it would serve one
|
|
881
|
+
# variant and expose two. If that exposure fails, the next read or claim
|
|
882
|
+
# finds it stale and resolves it again.
|
|
883
|
+
def invalidate_assignments_missing_unit(unit_type)
|
|
884
|
+
@assignment_mutex.synchronize do
|
|
885
|
+
exposing = @queue_mutex.synchronize { @exposing.dup }
|
|
886
|
+
@assignment_cache.delete_if do |_name, assignment|
|
|
887
|
+
assignment.unit_type.to_s == unit_type && !assignment.exposed && missing_unit?(assignment) &&
|
|
888
|
+
!exposing.key?(assignment)
|
|
889
|
+
end
|
|
890
|
+
end
|
|
891
|
+
end
|
|
892
|
+
|
|
893
|
+
# A no-unit split assignment whose unit has since been set, that is not
|
|
894
|
+
# exposed and not being exposed: set_unit skipped evicting it because its
|
|
895
|
+
# exposure was in progress, and that exposure did not complete.
|
|
896
|
+
def stale_missing_unit?(assignment)
|
|
897
|
+
return false unless unit_now_set?(assignment)
|
|
898
|
+
|
|
899
|
+
!exposed_or_exposing?(assignment)
|
|
900
|
+
end
|
|
901
|
+
|
|
902
|
+
# Read together under the queue lock: an exposure completing sets exposed
|
|
903
|
+
# and releases its claim under that lock, so a reader can't see neither.
|
|
904
|
+
def exposed_or_exposing?(assignment)
|
|
905
|
+
# exposed only moves false -> true: the positive result is stable.
|
|
906
|
+
return true if assignment.exposed
|
|
907
|
+
|
|
908
|
+
@queue_mutex.synchronize { assignment.exposed || @exposing.key?(assignment) }
|
|
909
|
+
end
|
|
910
|
+
|
|
911
|
+
# Resolved while its unit was missing: a no-unit split rule, or a holdout
|
|
912
|
+
# whose arm could not be computed yet.
|
|
913
|
+
def missing_unit?(assignment)
|
|
914
|
+
assignment.rule_missing_unit || (!assignment.holdout_assignments.nil? && assignment.holdout_assignments.include?(nil))
|
|
915
|
+
end
|
|
916
|
+
|
|
917
|
+
def unit_now_set?(assignment)
|
|
918
|
+
missing_unit?(assignment) && !assignment.unit_type.nil? && !@units[assignment.unit_type.to_sym].nil?
|
|
919
|
+
end
|
|
920
|
+
|
|
921
|
+
# Arm 0 always holds the unit out. In a 3-arm holdout arm 1 is "full-on
|
|
922
|
+
# only", so it holds out experiments that are not full-on. The arm count
|
|
923
|
+
# comes from the split, never from holdoutType.
|
|
924
|
+
def held_out_by?(variant, arm_count, full_on_variant)
|
|
925
|
+
return true if variant == 0
|
|
926
|
+
|
|
927
|
+
arm_count == 3 && variant == 1 && full_on_variant == 0
|
|
928
|
+
end
|
|
929
|
+
|
|
930
|
+
# Only (id, iteration) per holdout: a same-iteration seed or split edit
|
|
931
|
+
# must not rebuild an exposed assignment and expose it again.
|
|
932
|
+
def holdout_set_matches(entry, assignment)
|
|
933
|
+
fresh = entry.holdouts
|
|
934
|
+
pinned = assignment.holdouts
|
|
935
|
+
return true if fresh.equal?(pinned) || (!fresh.nil? && fresh.equal?(assignment.matched_holdouts))
|
|
936
|
+
return false if fresh.nil? || pinned.nil?
|
|
937
|
+
return false unless fresh.length == pinned.length
|
|
938
|
+
|
|
939
|
+
matches = fresh.each_with_index.all? do |holdout, i|
|
|
940
|
+
holdout_id(holdout.id) == holdout_id(pinned[i].id) && holdout.iteration == pinned[i].iteration
|
|
941
|
+
end
|
|
942
|
+
# The pinned list still names the exposures, so an equivalent list from a
|
|
943
|
+
# refresh is remembered apart and later reads compare it by identity.
|
|
944
|
+
assignment.matched_holdouts = fresh if matches
|
|
945
|
+
matches
|
|
946
|
+
end
|
|
947
|
+
|
|
948
|
+
# The arm is computed with the covered experiment's unit type, cached per
|
|
949
|
+
# (holdout id, unit type) and kept while the holdout's iteration is
|
|
950
|
+
# unchanged, together with the arm count it was computed against. nil
|
|
951
|
+
# means the unit is not set yet; it is never cached.
|
|
952
|
+
def holdout_assignment(holdout, unit_type)
|
|
953
|
+
id = holdout_id(holdout.id)
|
|
954
|
+
key = [id, unit_type]
|
|
955
|
+
cached = @holdout_assignments&.[](key)
|
|
956
|
+
return cached if cached && cached.iteration == holdout.iteration
|
|
957
|
+
|
|
958
|
+
uid = @units[unit_type.to_sym]
|
|
959
|
+
return nil if uid.nil?
|
|
960
|
+
|
|
961
|
+
assignment = Assignment.new
|
|
962
|
+
assignment.id = id
|
|
963
|
+
assignment.iteration = holdout.iteration
|
|
964
|
+
assignment.unit_type = unit_type
|
|
965
|
+
assignment.variant = variant_assigner(unit_type, uid).assign(holdout.split, holdout.seed_hi || 0,
|
|
966
|
+
holdout.seed_lo || 0)
|
|
967
|
+
assignment.assigned = true
|
|
968
|
+
assignment.holdout_arm_count = holdout.split.length
|
|
969
|
+
(@holdout_assignments ||= {})[key] = assignment
|
|
970
|
+
end
|
|
971
|
+
|
|
972
|
+
def audience_matches(entry, assignment)
|
|
973
|
+
experiment = entry.data
|
|
974
|
+
key = entry.rule_key
|
|
975
|
+
cached_key = assignment.rule_key || NO_RULES
|
|
976
|
+
# The environment is fixed per context, so the rules text alone
|
|
977
|
+
# identifies them. Equal text from a refresh is acknowledged by
|
|
978
|
+
# adopting the new object, so later reads compare by identity.
|
|
979
|
+
key_changed = !key.equal?(cached_key)
|
|
980
|
+
if key_changed && key == cached_key
|
|
981
|
+
assignment.rule_key = key
|
|
982
|
+
key_changed = false
|
|
983
|
+
end
|
|
984
|
+
|
|
985
|
+
return false if key_changed && key.empty? && assignment.rule_override
|
|
986
|
+
|
|
987
|
+
if @attrs_seq > (assignment.attrs_seq || 0) || key_changed
|
|
988
|
+
rule_attrs = nil
|
|
989
|
+
unless key.empty?
|
|
990
|
+
# Exposed, or being exposed right now (its clock may set the unit):
|
|
991
|
+
# the no-unit variant stays, so a unit arriving can't switch it.
|
|
992
|
+
pinned_missing = assignment.rule_missing_unit && exposed_or_exposing?(assignment)
|
|
993
|
+
rule_attrs = matcher_attributes(rule_matcher)
|
|
994
|
+
rule = resolve_rule(entry, rule_attrs, pinned_missing)
|
|
995
|
+
return false if rule&.first != assignment.rule_variant
|
|
996
|
+
|
|
997
|
+
assignment.rule_missing_unit = rule ? rule.last : false
|
|
998
|
+
end
|
|
999
|
+
|
|
1000
|
+
# A held-out unit skipped the audience when assigned, so it is not
|
|
1001
|
+
# evaluated here either; only a rule can take it out of suppression.
|
|
1002
|
+
if !assignment.rule_override && !assignment.suppressed &&
|
|
1003
|
+
!experiment.audience.nil? && experiment.audience.size > 0
|
|
1004
|
+
match = @audience_matcher.evaluate(experiment.audience, audience_attributes(rule_attrs))
|
|
1005
|
+
# An indeterminate result (no filter, bad JSON) keeps the cached flag.
|
|
1006
|
+
new_audience_mismatch = match.nil? ? assignment.audience_mismatch : !match.result
|
|
1007
|
+
|
|
1008
|
+
return false if new_audience_mismatch != assignment.audience_mismatch
|
|
1009
|
+
end
|
|
1010
|
+
|
|
1011
|
+
assignment.rule_key = key
|
|
1012
|
+
assignment.attrs_seq = @attrs_seq
|
|
1013
|
+
end
|
|
1014
|
+
true
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
# A custom audience matcher written for v1.3.0 only implements
|
|
1018
|
+
# evaluate(audience, attributes); rules then use the default engine.
|
|
1019
|
+
def rule_matcher
|
|
1020
|
+
@rule_matcher ||= begin
|
|
1021
|
+
rule_capable = @audience_matcher.respond_to?(:parse_rules) &&
|
|
1022
|
+
@audience_matcher.respond_to?(:evaluate_parsed_rules)
|
|
1023
|
+
rule_capable ? @audience_matcher : AudienceMatcher.new(DefaultAudienceDeserializer.new)
|
|
1024
|
+
end
|
|
1025
|
+
end
|
|
1026
|
+
|
|
1027
|
+
# Returns [variant, missing_unit], or nil to fall back to normal
|
|
1028
|
+
# assignment (as JS and Java do for an out-of-range variant or a split
|
|
1029
|
+
# without one percentage per variant).
|
|
1030
|
+
def resolve_rule(entry, attrs, unit_pinned_missing)
|
|
1031
|
+
experiment = entry.data
|
|
1032
|
+
return nil if entry.rules.nil?
|
|
1033
|
+
|
|
1034
|
+
action = rule_matcher.evaluate_parsed_rules(entry.rules, @environment, attrs)
|
|
1035
|
+
return nil if action.nil?
|
|
1036
|
+
|
|
1037
|
+
variant_count = experiment.variants.length
|
|
1038
|
+
unless action.split?
|
|
1039
|
+
return action.variant >= 0 && action.variant < variant_count ? [action.variant, false] : nil
|
|
1040
|
+
end
|
|
1041
|
+
return nil if action.split.length != variant_count
|
|
1042
|
+
|
|
1043
|
+
unit_type = experiment.unit_type
|
|
1044
|
+
uid = unit_type.nil? || unit_pinned_missing ? nil : @units[unit_type.to_sym]
|
|
1045
|
+
return [experiment.full_on_variant || 0, true] if uid.nil?
|
|
1046
|
+
|
|
1047
|
+
assigner = variant_assigner(unit_type, uid)
|
|
1048
|
+
[assigner.assign(action.split, experiment.seed_hi, experiment.seed_lo), false]
|
|
1049
|
+
end
|
|
1050
|
+
|
|
1051
|
+
# The state is read under the lock, so each holder sees data at least as
|
|
1052
|
+
# new as the previous one: a read that started before a refresh can't
|
|
1053
|
+
# overwrite assignments (or holdout arms) already built from newer data,
|
|
1054
|
+
# which would reset their exposed flag and expose them again.
|
|
378
1055
|
def assignment(experiment_name)
|
|
1056
|
+
@assignment_mutex.synchronize { cached_or_new_assignment(experiment_name, @state) }
|
|
1057
|
+
end
|
|
1058
|
+
|
|
1059
|
+
def cached_or_new_assignment(experiment_name, state)
|
|
379
1060
|
assignment = @assignment_cache[experiment_name.to_s]
|
|
1061
|
+
exp_key = experiment_name.to_s.to_sym
|
|
1062
|
+
custom = @cassignments[exp_key]
|
|
1063
|
+
override = @overrides[exp_key]
|
|
1064
|
+
experiment = experiment(experiment_name.to_s, state)
|
|
1065
|
+
|
|
1066
|
+
# Built from the empty snapshot of a failed context that a refresh has
|
|
1067
|
+
# since recovered with the experiment: resolved again against the
|
|
1068
|
+
# recovered data. The marker stays on the old assignment, so a rebuild
|
|
1069
|
+
# that raises leaves it ineligible for reuse.
|
|
1070
|
+
# (The failed snapshot has no experiments, so a read on it never matches.)
|
|
1071
|
+
if @failed_marked && !assignment.nil? && !experiment.nil? &&
|
|
1072
|
+
assignment.instance_variable_get(:@failed_snapshot)
|
|
1073
|
+
assignment = nil
|
|
1074
|
+
end
|
|
1075
|
+
# Inline guard: only an unexposed no-unit result can be stale, and the
|
|
1076
|
+
# check itself takes the queue lock.
|
|
1077
|
+
if !assignment.nil? && !assignment.exposed &&
|
|
1078
|
+
(assignment.rule_missing_unit || assignment.holdout_assignments&.include?(nil)) &&
|
|
1079
|
+
stale_missing_unit?(assignment)
|
|
1080
|
+
assignment = nil
|
|
1081
|
+
end
|
|
380
1082
|
|
|
381
1083
|
if !assignment.nil?
|
|
382
|
-
custom = @cassignments.transform_keys(&:to_sym)[experiment_name.to_s.to_sym]
|
|
383
|
-
override = @overrides.transform_keys(&:to_sym)[experiment_name.to_s.to_sym]
|
|
384
|
-
experiment = experiment(experiment_name.to_s)
|
|
385
1084
|
if !override.nil?
|
|
386
|
-
if assignment.overridden && assignment.variant == override
|
|
1085
|
+
if assignment.overridden && assignment.variant == override &&
|
|
1086
|
+
(experiment.nil? || holdout_set_matches(experiment, assignment))
|
|
387
1087
|
return assignment
|
|
388
1088
|
end
|
|
389
1089
|
elsif experiment.nil?
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
elsif custom.nil? ||
|
|
394
|
-
|
|
1090
|
+
# A rule-won assignment came from the experiment's rules, so it is
|
|
1091
|
+
# rebuilt once the experiment is gone.
|
|
1092
|
+
return assignment if !assignment.assigned && !assignment.rule_override && !assignment.overridden
|
|
1093
|
+
elsif (custom.nil? || assignment.rule_override || assignment.suppressed ||
|
|
1094
|
+
(custom == assignment.variant && (assignment.custom || assignment.exposed))) &&
|
|
1095
|
+
experiment_matches(experiment.data, assignment) &&
|
|
1096
|
+
# Inline nil check: a method call here costs ~4% on the no-holdout path.
|
|
1097
|
+
(experiment.holdouts.nil? ? assignment.holdouts.nil? : holdout_set_matches(experiment, assignment)) &&
|
|
1098
|
+
((experiment.unconditional && assignment.rule_key.equal?(NO_RULES)) ||
|
|
1099
|
+
audience_matches(experiment, assignment))
|
|
1100
|
+
# A rule wins over a custom assignment, and a held-out unit never
|
|
1101
|
+
# takes one. An unexposed assignment that merely equals a new custom
|
|
1102
|
+
# variant is rebuilt to flag it custom.
|
|
1103
|
+
return assignment
|
|
395
1104
|
end
|
|
396
1105
|
end
|
|
397
1106
|
|
|
398
|
-
custom = @cassignments.transform_keys(&:to_sym)[experiment_name.to_s.to_sym]
|
|
399
|
-
override = @overrides.transform_keys(&:to_sym)[experiment_name.to_s.to_sym]
|
|
400
|
-
experiment = experiment(experiment_name.to_s)
|
|
401
|
-
|
|
402
1107
|
assignment = Assignment.new
|
|
403
1108
|
assignment.name = experiment_name
|
|
404
1109
|
assignment.eligible = true
|
|
405
1110
|
|
|
1111
|
+
if !experiment.nil? && !experiment.holdouts.nil?
|
|
1112
|
+
data = experiment.data
|
|
1113
|
+
unit_type = data.unit_type
|
|
1114
|
+
assignment.holdouts = experiment.holdouts
|
|
1115
|
+
assignment.holdout_assignments = experiment.holdouts.map do |holdout|
|
|
1116
|
+
unit_type.nil? ? nil : holdout_assignment(holdout, unit_type)
|
|
1117
|
+
end
|
|
1118
|
+
assignment.suppressed = assignment.holdout_assignments.any? do |holdout_assignment|
|
|
1119
|
+
!holdout_assignment.nil? &&
|
|
1120
|
+
held_out_by?(holdout_assignment.variant, holdout_assignment.holdout_arm_count, data.full_on_variant)
|
|
1121
|
+
end
|
|
1122
|
+
end
|
|
1123
|
+
|
|
406
1124
|
if !override.nil?
|
|
407
1125
|
unless experiment.nil?
|
|
408
1126
|
assignment.id = experiment.data.id
|
|
409
1127
|
assignment.unit_type = experiment.data.unit_type
|
|
410
1128
|
end
|
|
411
|
-
|
|
412
1129
|
assignment.overridden = true
|
|
413
1130
|
assignment.variant = override
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
1131
|
+
elsif !experiment.nil?
|
|
1132
|
+
data = experiment.data
|
|
1133
|
+
assignment.unit_type = data.unit_type
|
|
1134
|
+
assignment.id = data.id
|
|
1135
|
+
assignment.iteration = data.iteration
|
|
1136
|
+
assignment.traffic_split = data.traffic_split
|
|
1137
|
+
assignment.full_on_variant = data.full_on_variant
|
|
1138
|
+
assignment.rule_key = experiment.rule_key
|
|
1139
|
+
rule_attrs = assignment.rule_key.empty? ? nil : matcher_attributes(rule_matcher)
|
|
1140
|
+
rule = rule_attrs.nil? ? nil : resolve_rule(experiment, rule_attrs, false)
|
|
1141
|
+
assignment.rule_variant = rule&.first
|
|
1142
|
+
assignment.rule_missing_unit = rule ? rule.last : false
|
|
1143
|
+
|
|
1144
|
+
# A matching rule wins over holdout suppression, as an override does;
|
|
1145
|
+
# suppression skips the audience, full-on, traffic and custom paths.
|
|
1146
|
+
if rule
|
|
1147
|
+
assignment.rule_override = true
|
|
1148
|
+
assignment.variant = rule.first
|
|
1149
|
+
elsif assignment.suppressed
|
|
1150
|
+
assignment.variant = 0
|
|
1151
|
+
else
|
|
1152
|
+
if !data.audience.nil? && data.audience.size > 0
|
|
1153
|
+
match = @audience_matcher.evaluate(data.audience, audience_attributes(rule_attrs))
|
|
1154
|
+
assignment.audience_mismatch = true if match && !match.result
|
|
427
1155
|
end
|
|
428
1156
|
|
|
429
|
-
if
|
|
1157
|
+
if data.audience_strict && assignment.audience_mismatch
|
|
430
1158
|
assignment.variant = 0
|
|
431
|
-
elsif
|
|
432
|
-
uid = @units
|
|
1159
|
+
elsif data.full_on_variant == 0
|
|
1160
|
+
uid = @units[data.unit_type.to_sym]
|
|
433
1161
|
unless uid.nil?
|
|
434
1162
|
assigner = VariantAssigner.new(uid)
|
|
435
|
-
eligible = assigner.assign(
|
|
436
|
-
experiment.data.traffic_seed_hi,
|
|
437
|
-
experiment.data.traffic_seed_lo) == 1
|
|
1163
|
+
eligible = assigner.assign(data.traffic_split, data.traffic_seed_hi, data.traffic_seed_lo) == 1
|
|
438
1164
|
if eligible
|
|
439
1165
|
if !custom.nil?
|
|
440
1166
|
assignment.variant = custom
|
|
441
1167
|
assignment.custom = true
|
|
442
1168
|
else
|
|
443
|
-
assignment.variant = assigner.assign(
|
|
444
|
-
experiment.data.seed_hi,
|
|
445
|
-
experiment.data.seed_lo)
|
|
1169
|
+
assignment.variant = assigner.assign(data.split, data.seed_hi, data.seed_lo)
|
|
446
1170
|
end
|
|
447
1171
|
else
|
|
448
1172
|
assignment.eligible = false
|
|
@@ -452,38 +1176,179 @@ class Context
|
|
|
452
1176
|
end
|
|
453
1177
|
else
|
|
454
1178
|
assignment.assigned = true
|
|
455
|
-
assignment.variant =
|
|
1179
|
+
assignment.variant = data.full_on_variant
|
|
456
1180
|
assignment.full_on = true
|
|
457
1181
|
end
|
|
458
|
-
|
|
459
|
-
assignment.unit_type = unit_type
|
|
460
|
-
assignment.id = experiment.data.id
|
|
461
|
-
assignment.iteration = experiment.data.iteration
|
|
462
|
-
assignment.traffic_split = experiment.data.traffic_split
|
|
463
|
-
assignment.full_on_variant = experiment.data.full_on_variant
|
|
464
1182
|
end
|
|
1183
|
+
assignment.attrs_seq = @attrs_seq
|
|
465
1184
|
end
|
|
466
1185
|
|
|
467
|
-
if !experiment.nil? && assignment.variant < experiment.data.variants.length
|
|
1186
|
+
if !experiment.nil? && assignment.variant >= 0 && assignment.variant < experiment.data.variants.length
|
|
468
1187
|
assignment.variables = experiment.variables[assignment.variant] || {}
|
|
469
1188
|
end
|
|
470
1189
|
|
|
1190
|
+
failed_built = state.equal?(@failed_state)
|
|
1191
|
+
# A read that finishes after a refresh recovered the context must not
|
|
1192
|
+
# replace what was resolved against the recovered data, unless that
|
|
1193
|
+
# data lacks the experiment too and would resolve it the same way.
|
|
1194
|
+
if failed_built && !@state.equal?(@failed_state) && !experiment(experiment_name.to_s).nil?
|
|
1195
|
+
return assignment
|
|
1196
|
+
end
|
|
1197
|
+
|
|
1198
|
+
# Provenance belongs to the new assignment before the single cache
|
|
1199
|
+
# write. An interrupted replacement leaves the old assignment marked;
|
|
1200
|
+
# a marker on an uncached replacement has no effect on later reads.
|
|
1201
|
+
if failed_built
|
|
1202
|
+
assignment.instance_variable_set(:@failed_snapshot, true)
|
|
1203
|
+
@failed_marked = true
|
|
1204
|
+
end
|
|
471
1205
|
@assignment_cache[experiment_name.to_s] = assignment
|
|
472
1206
|
assignment
|
|
473
1207
|
end
|
|
474
1208
|
|
|
475
|
-
|
|
476
|
-
|
|
1209
|
+
# As java-sdk Context.getVariableAssignment resolves a key shared by
|
|
1210
|
+
# several experiments: the first one, in experiment id order, that the
|
|
1211
|
+
# unit is assigned to, overridden in or rule-assigned in wins, and only
|
|
1212
|
+
# that one is exposed. A winner whose variant lacks the key gives the
|
|
1213
|
+
# default; the walk never moves past it. When none wins, a held-out unit
|
|
1214
|
+
# reads the first held-out experiment (control, or the default if its
|
|
1215
|
+
# control lacks the key). A non-peek read still exposes the holdouts of
|
|
1216
|
+
# each candidate it visits, as java-sdk does.
|
|
1217
|
+
def resolve_variable_value(key, default_value, expose)
|
|
1218
|
+
key = key.to_s.to_sym
|
|
1219
|
+
winner = nil
|
|
1220
|
+
attempts = 0
|
|
1221
|
+
while true # rubocop:disable Style/InfiniteLoop -- no block allocation on the read path
|
|
1222
|
+
return default_value if attempts >= VARIABLE_WALK_ATTEMPTS
|
|
1223
|
+
|
|
1224
|
+
attempts += 1
|
|
1225
|
+
state = @state
|
|
1226
|
+
winner = variable_assignment(key, expose, state)
|
|
1227
|
+
visited = nil
|
|
1228
|
+
winner, visited = winner if winner.is_a?(Array)
|
|
1229
|
+
# A refresh, even one from a holdout's logger, restarts the walk.
|
|
1230
|
+
next unless @state.equal?(state)
|
|
1231
|
+
|
|
1232
|
+
unless visited.nil?
|
|
1233
|
+
i = 0
|
|
1234
|
+
while i < visited.length && @state.equal?(state)
|
|
1235
|
+
queue_holdout_exposures(visited[i])
|
|
1236
|
+
i += 1
|
|
1237
|
+
end
|
|
1238
|
+
next unless @state.equal?(state)
|
|
1239
|
+
end
|
|
1240
|
+
break if winner.nil? || !expose || winner.exposed
|
|
1241
|
+
# The unit was set in between, so the walk may now pick another
|
|
1242
|
+
# candidate: it starts again, as java-sdk's settleExposure does.
|
|
1243
|
+
break unless claim_and_queue_exposure(winner, winner.name) == :evicted
|
|
1244
|
+
end
|
|
1245
|
+
return default_value if winner.nil? || winner.variables.nil? || !winner.variables.key?(key)
|
|
1246
|
+
|
|
1247
|
+
winner.variables[key]
|
|
1248
|
+
end
|
|
1249
|
+
|
|
1250
|
+
# Returns the winner, or, when exposing and a visited candidate still has
|
|
1251
|
+
# a holdout to expose, [winner, those candidates] (only then allocated);
|
|
1252
|
+
# the caller exposes them once the walk stands.
|
|
1253
|
+
def variable_assignment(key, expose, state = @state)
|
|
1254
|
+
candidates = state.index_variables[key]
|
|
1255
|
+
return nil if candidates.nil?
|
|
1256
|
+
|
|
1257
|
+
visited = nil
|
|
1258
|
+
suppressed_fallback = nil
|
|
1259
|
+
i = 0
|
|
1260
|
+
# An index loop, not each: no block frame per candidate on the read path.
|
|
1261
|
+
while i < candidates.length
|
|
1262
|
+
# Resolved from the state current under the assignment lock, so a read
|
|
1263
|
+
# that a refresh overtook can't reinstall an old assignment.
|
|
1264
|
+
assignment = assignment(candidates[i].data.name)
|
|
1265
|
+
# As in java-sdk, visited candidates' holdouts precede the winner's exposure.
|
|
1266
|
+
if expose && !assignment.holdout_assignments.nil? &&
|
|
1267
|
+
(!assignment.exposed || assignment.overridden) && holdouts_pending?(assignment)
|
|
1268
|
+
(visited ||= []) << assignment
|
|
1269
|
+
end
|
|
1270
|
+
if assignment.assigned || assignment.overridden || assignment.rule_override
|
|
1271
|
+
return visited ? [assignment, visited] : assignment
|
|
1272
|
+
end
|
|
477
1273
|
|
|
478
|
-
|
|
1274
|
+
suppressed_fallback ||= assignment if assignment.suppressed
|
|
1275
|
+
i += 1
|
|
1276
|
+
end
|
|
1277
|
+
visited ? [suppressed_fallback, visited] : suppressed_fallback
|
|
479
1278
|
end
|
|
480
1279
|
|
|
481
|
-
|
|
482
|
-
|
|
1280
|
+
# Whether a candidate has a holdout arm still to expose (or, for an
|
|
1281
|
+
# override, one still to resolve). A warm read past an exposed loser then
|
|
1282
|
+
# collects nothing.
|
|
1283
|
+
def holdouts_pending?(assignment)
|
|
1284
|
+
arms = assignment.holdout_assignments
|
|
1285
|
+
i = 0
|
|
1286
|
+
while i < arms.length
|
|
1287
|
+
arm = arms[i]
|
|
1288
|
+
if arm.nil?
|
|
1289
|
+
unit_type = assignment.unit_type
|
|
1290
|
+
return true if assignment.overridden && !unit_type.nil? && !@units[unit_type.to_sym].nil?
|
|
1291
|
+
elsif !arm.exposed
|
|
1292
|
+
return true
|
|
1293
|
+
end
|
|
1294
|
+
|
|
1295
|
+
i += 1
|
|
1296
|
+
end
|
|
1297
|
+
false
|
|
483
1298
|
end
|
|
484
1299
|
|
|
485
|
-
|
|
486
|
-
|
|
1300
|
+
# Replaces the arms of an override still missing them, once its unit is
|
|
1301
|
+
# set; the holdout arm cache is written under the assignment lock.
|
|
1302
|
+
def resolve_late_holdout_arms(assignment)
|
|
1303
|
+
arms = assignment.holdout_assignments
|
|
1304
|
+
return unless unit_now_set?(assignment) && arms.include?(nil)
|
|
1305
|
+
|
|
1306
|
+
@assignment_mutex.synchronize do
|
|
1307
|
+
arms = assignment.holdout_assignments
|
|
1308
|
+
# The live holdouts (a refresh's equivalent list, if any): the arms are
|
|
1309
|
+
# decided now, so they and their exposures' names come from those, as
|
|
1310
|
+
# java-sdk resolves an override's holdouts against the installed data.
|
|
1311
|
+
holdouts = assignment.matched_holdouts || assignment.holdouts
|
|
1312
|
+
if arms.include?(nil)
|
|
1313
|
+
assignment.holdout_assignments = arms.each_with_index.map do |arm, i|
|
|
1314
|
+
arm || holdout_assignment(holdouts[i], assignment.unit_type)
|
|
1315
|
+
end
|
|
1316
|
+
assignment.holdouts = holdouts
|
|
1317
|
+
end
|
|
1318
|
+
end
|
|
1319
|
+
end
|
|
1320
|
+
|
|
1321
|
+
# Exposes the holdouts covering a visited candidate, from the arms pinned
|
|
1322
|
+
# in its assignment, without exposing the candidate itself.
|
|
1323
|
+
def queue_holdout_exposures(assignment)
|
|
1324
|
+
return if assignment.exposed && !assignment.overridden
|
|
1325
|
+
|
|
1326
|
+
# As java-sdk resolves an override's holdouts live on each visit, an
|
|
1327
|
+
# arm an override could not get before its unit was set is resolved
|
|
1328
|
+
# once the unit is, so its holdout is exposed even after the override.
|
|
1329
|
+
resolve_late_holdout_arms(assignment) if assignment.overridden
|
|
1330
|
+
holdout_assignments = assignment.holdout_assignments
|
|
1331
|
+
return if holdout_assignments.nil?
|
|
1332
|
+
|
|
1333
|
+
# Checked again under the lock; this keeps warm reads off the clock and lock.
|
|
1334
|
+
return if holdout_assignments.all? { |holdout_assignment| holdout_assignment.nil? || holdout_assignment.exposed }
|
|
1335
|
+
|
|
1336
|
+
exposed_at = @clock.to_i
|
|
1337
|
+
holdout_exposures = nil
|
|
1338
|
+
@queue_mutex.synchronize do
|
|
1339
|
+
check_not_closed?
|
|
1340
|
+
holdout_assignments.each_with_index do |holdout_assignment, i|
|
|
1341
|
+
next if holdout_assignment.nil? || holdout_assignment.exposed
|
|
1342
|
+
|
|
1343
|
+
holdout_assignment.exposed = true
|
|
1344
|
+
(holdout_exposures ||= []) << enqueue_exposure(assignment.holdouts[i].name, holdout_assignment, exposed_at)
|
|
1345
|
+
end
|
|
1346
|
+
end
|
|
1347
|
+
holdout_exposures&.each { |e| log_event(ContextEventLogger::EVENT_TYPE::EXPOSURE, e) }
|
|
1348
|
+
end
|
|
1349
|
+
|
|
1350
|
+
def experiment(experiment, state = @state)
|
|
1351
|
+
state.index[experiment.to_s.to_sym]
|
|
487
1352
|
end
|
|
488
1353
|
|
|
489
1354
|
def unit_hash(unit_type, unit_uid)
|
|
@@ -494,23 +1359,138 @@ class Context
|
|
|
494
1359
|
@assigners[unit_type] ||= VariantAssigner.new(unit_hash)
|
|
495
1360
|
end
|
|
496
1361
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
1362
|
+
# Builds the snapshot for data, calling the variable parser; callers run
|
|
1363
|
+
# it outside the readiness lock so a parser may call wait_until_ready.
|
|
1364
|
+
# The membership bookkeeping defers asynchronous interrupts (Timeout,
|
|
1365
|
+
# Thread#raise), so one landing while it waits for the lock can't leave
|
|
1366
|
+
# this thread registered or unbalanced. The parsing keeps the caller's
|
|
1367
|
+
# interrupt mask: interruptible by default, deferred if the caller masked.
|
|
1368
|
+
# Ruby can't run the parse under the caller's mask inside a masked
|
|
1369
|
+
# cleanup, so an interrupt landing as the cleanup starts can skip it; a
|
|
1370
|
+
# registration only counts while the thread is inside build_state (see
|
|
1371
|
+
# building?), and the next build or wait drops one left behind.
|
|
1372
|
+
def build_state(data)
|
|
1373
|
+
thread = Thread.current
|
|
1374
|
+
nested = build_state_on_stack?(1)
|
|
1375
|
+
registered = false
|
|
1376
|
+
begin
|
|
1377
|
+
Thread.handle_interrupt(Object => :never) do
|
|
1378
|
+
@ready_mutex.synchronize do
|
|
1379
|
+
# [count, fiber that registered first]. A count left by an
|
|
1380
|
+
# interrupted cleanup in this fiber, with no build of it still
|
|
1381
|
+
# running further up the stack, is dropped first.
|
|
1382
|
+
entry = @building_threads[thread]
|
|
1383
|
+
# (The stack is checked above, before entering these blocks.)
|
|
1384
|
+
entry = nil if entry && (entry[1].equal?(Fiber.current) ? !nested : !entry[1].alive?)
|
|
1385
|
+
@building_threads[thread] = entry ? [entry[0] + 1, entry[1]] : [1, Fiber.current]
|
|
1386
|
+
end
|
|
1387
|
+
registered = true
|
|
1388
|
+
end
|
|
1389
|
+
build_state_maps(data)
|
|
1390
|
+
ensure
|
|
1391
|
+
if registered
|
|
1392
|
+
Thread.handle_interrupt(Object => :never) do
|
|
1393
|
+
@ready_mutex.synchronize do
|
|
1394
|
+
entry = @building_threads[thread]
|
|
1395
|
+
if entry && entry[0] > 1
|
|
1396
|
+
@building_threads[thread] = [entry[0] - 1, entry[1]]
|
|
1397
|
+
elsif entry
|
|
1398
|
+
@building_threads.delete(thread)
|
|
1399
|
+
end
|
|
1400
|
+
end
|
|
1401
|
+
end
|
|
1402
|
+
end
|
|
1403
|
+
end
|
|
1404
|
+
end
|
|
1405
|
+
|
|
1406
|
+
# True while this thread is building a snapshot (a variable parser calling
|
|
1407
|
+
# back). A registration whose build has unwound without its cleanup is
|
|
1408
|
+
# dropped here, so the thread waits like any other.
|
|
1409
|
+
def building?
|
|
1410
|
+
thread = Thread.current
|
|
1411
|
+
entry = @ready_mutex.synchronize { @building_threads[thread] }
|
|
1412
|
+
return false if entry.nil?
|
|
1413
|
+
return true unless stale_build_entry?(entry)
|
|
1414
|
+
|
|
1415
|
+
Thread.handle_interrupt(Object => :never) do
|
|
1416
|
+
@ready_mutex.synchronize { @building_threads.delete(thread) if @building_threads[thread].equal?(entry) }
|
|
1417
|
+
end
|
|
1418
|
+
false
|
|
1419
|
+
end
|
|
1420
|
+
|
|
1421
|
+
# A backtrace covers only the current fiber, so the stack is checked only
|
|
1422
|
+
# from the fiber that registered; a parser that waits from a nested fiber
|
|
1423
|
+
# still counts as building. A registering fiber that has ended is stale.
|
|
1424
|
+
def stale_build_entry?(entry)
|
|
1425
|
+
fiber = entry[1]
|
|
1426
|
+
return !build_state_on_stack?(0) if fiber.equal?(Fiber.current)
|
|
1427
|
+
return true unless fiber.alive?
|
|
1428
|
+
# Ruby 3.0+ can read the registering fiber's stack from a fiber it
|
|
1429
|
+
# resumed; 2.7 cannot, so there a live registering fiber still counts.
|
|
1430
|
+
return false unless fiber.respond_to?(:backtrace_locations)
|
|
1431
|
+
|
|
1432
|
+
!build_state_on_stack?(0, fiber.backtrace_locations || [])
|
|
1433
|
+
end
|
|
1434
|
+
|
|
1435
|
+
# Whether more than skip build_state frames are on this thread's stack.
|
|
1436
|
+
# Called only outside build_state's blocks, so each frame is one build.
|
|
1437
|
+
def build_state_on_stack?(skip, locations = caller_locations)
|
|
1438
|
+
locations.count { |location| location.path == __FILE__ && location.base_label == "build_state" } > skip
|
|
1439
|
+
end
|
|
1440
|
+
|
|
1441
|
+
def build_state_maps(data)
|
|
1442
|
+
index = {}
|
|
1443
|
+
index_variables = {}
|
|
1444
|
+
custom_fields = {}
|
|
1445
|
+
holdouts_by_id = NO_HOLDOUTS
|
|
1446
|
+
|
|
1447
|
+
# Only holdouts with a numeric split and seeds can assign a unit; the
|
|
1448
|
+
# others are ignored, and so are holdoutIds that name none of them.
|
|
1449
|
+
# A custom deserializer's data may predate holdouts, so they are optional.
|
|
1450
|
+
holdouts = data.holdouts if data.respond_to?(:holdouts)
|
|
1451
|
+
holdouts.each do |holdout|
|
|
1452
|
+
next unless holdout.is_a?(Experiment)
|
|
1453
|
+
|
|
1454
|
+
id = holdout_id(holdout.id)
|
|
1455
|
+
split = holdout.split
|
|
1456
|
+
next if id.nil? || !split.is_a?(Array) || split.empty? || !split.all?(Numeric)
|
|
1457
|
+
next unless [holdout.seed_hi, holdout.seed_lo].all? { |seed| seed.nil? || seed.is_a?(Integer) }
|
|
1458
|
+
|
|
1459
|
+
holdouts_by_id = {} if holdouts_by_id.frozen?
|
|
1460
|
+
holdouts_by_id[id] = holdout
|
|
1461
|
+
end if holdouts.is_a?(Array)
|
|
501
1462
|
|
|
502
1463
|
if data && !data.experiments.nil? && !data.experiments.empty?
|
|
503
1464
|
data.experiments.each do |experiment|
|
|
504
|
-
|
|
1465
|
+
experiment_custom_field_values = {}
|
|
505
1466
|
|
|
506
1467
|
experiment_variables = ExperimentVariables.new
|
|
507
1468
|
experiment_variables.data = experiment
|
|
1469
|
+
rules = experiment.assignment_rules
|
|
1470
|
+
if rules.is_a?(String) && !rules.empty?
|
|
1471
|
+
experiment_variables.rule_key = rules
|
|
1472
|
+
experiment_variables.rules = rule_matcher.parse_rules(rules)
|
|
1473
|
+
else
|
|
1474
|
+
experiment_variables.rule_key = NO_RULES
|
|
1475
|
+
end
|
|
1476
|
+
experiment_variables.unconditional = experiment_variables.rule_key.equal?(NO_RULES) &&
|
|
1477
|
+
(experiment.audience.nil? || experiment.audience.empty?)
|
|
1478
|
+
holdout_ids = experiment.holdout_ids unless holdouts_by_id.empty? || !experiment.respond_to?(:holdout_ids)
|
|
1479
|
+
if holdout_ids.is_a?(Array) && !holdout_ids.empty?
|
|
1480
|
+
holdouts = holdout_ids.filter_map { |id| holdouts_by_id[holdout_id(id)] }.uniq.sort_by { |h| holdout_id(h.id) }
|
|
1481
|
+
experiment_variables.holdouts = holdouts unless holdouts.empty?
|
|
1482
|
+
end
|
|
508
1483
|
experiment_variables.variables ||= []
|
|
509
1484
|
experiment.variants.each do |variant|
|
|
510
1485
|
if !variant.config.nil? && !variant.config.empty?
|
|
511
1486
|
variables = @variable_parser.parse(self, experiment.name, variant.name,
|
|
512
|
-
variant.config)
|
|
513
|
-
variables.keys.each
|
|
1487
|
+
variant.config) || {}
|
|
1488
|
+
variables.keys.each do |key|
|
|
1489
|
+
# Kept sorted and unique by experiment id, as the JavaScript SDK does.
|
|
1490
|
+
list = (index_variables[key] ||= [])
|
|
1491
|
+
at = list.bsearch_index { |other| other.data.id >= experiment.id } || list.length
|
|
1492
|
+
list.insert(at, experiment_variables) unless list[at]&.data&.id == experiment.id
|
|
1493
|
+
end
|
|
514
1494
|
experiment_variables.variables.push(variables)
|
|
515
1495
|
else
|
|
516
1496
|
experiment_variables.variables.push({})
|
|
@@ -524,38 +1504,49 @@ class Context
|
|
|
524
1504
|
|
|
525
1505
|
if !custom_field_value.value.nil?
|
|
526
1506
|
custom_value = custom_field_value.value
|
|
1507
|
+
type = custom_field_value.type.to_s
|
|
527
1508
|
|
|
528
|
-
if
|
|
1509
|
+
if type.start_with?("json")
|
|
529
1510
|
value.value = @variable_parser.parse(self, experiment.name, custom_field_value.name, custom_value)
|
|
530
1511
|
|
|
531
|
-
elsif
|
|
532
|
-
value.value = custom_value
|
|
1512
|
+
elsif type.start_with?("boolean")
|
|
1513
|
+
value.value = custom_value == "true"
|
|
533
1514
|
|
|
534
|
-
elsif
|
|
1515
|
+
elsif type.start_with?("number")
|
|
535
1516
|
value.value = custom_value.to_i
|
|
536
1517
|
|
|
537
1518
|
else
|
|
538
1519
|
value.value = custom_field_value.value
|
|
539
1520
|
end
|
|
540
1521
|
|
|
541
|
-
|
|
1522
|
+
experiment_custom_field_values[custom_field_value.name] = value
|
|
542
1523
|
|
|
543
1524
|
end
|
|
544
|
-
|
|
545
1525
|
end
|
|
546
1526
|
end
|
|
547
1527
|
|
|
548
|
-
|
|
549
|
-
|
|
1528
|
+
index[experiment.name.to_sym] = experiment_variables
|
|
1529
|
+
custom_fields[experiment.name] = experiment_custom_field_values
|
|
550
1530
|
end
|
|
551
1531
|
end
|
|
1532
|
+
|
|
1533
|
+
State.new(data, index, index_variables, custom_fields)
|
|
1534
|
+
end
|
|
1535
|
+
|
|
1536
|
+
# Collector IDs are integers; integral floats and canonical decimal text
|
|
1537
|
+
# are accepted too. Other text is ignored, even when identical on both sides.
|
|
1538
|
+
def holdout_id(id)
|
|
1539
|
+
case id
|
|
1540
|
+
when Integer then id
|
|
1541
|
+
when Float then id.to_i if id.finite? && id == id.floor
|
|
1542
|
+
when String then Integer(id, 10) if id.match?(/\A(?:0|-?[1-9]\d*)\z/)
|
|
1543
|
+
end
|
|
552
1544
|
end
|
|
553
1545
|
|
|
554
1546
|
def set_data_failed(exception)
|
|
555
1547
|
@data_failed = exception
|
|
556
|
-
@
|
|
557
|
-
@
|
|
558
|
-
@data = ContextData.new
|
|
1548
|
+
@ready_error = exception
|
|
1549
|
+
@state = @failed_state = State.new(ContextData.new, {}, {}, {})
|
|
559
1550
|
@failed = true
|
|
560
1551
|
end
|
|
561
1552
|
|
|
@@ -563,12 +1554,80 @@ class Context
|
|
|
563
1554
|
unless @event_logger.nil?
|
|
564
1555
|
@event_logger.handle_event(event, data)
|
|
565
1556
|
end
|
|
1557
|
+
rescue StandardError
|
|
1558
|
+
nil
|
|
566
1559
|
end
|
|
567
1560
|
|
|
568
1561
|
def log_error(error)
|
|
569
1562
|
unless @event_logger.nil?
|
|
570
1563
|
@event_logger.handle_event(ContextEventLogger::EVENT_TYPE::ERROR, error.message)
|
|
571
1564
|
end
|
|
1565
|
+
rescue StandardError
|
|
1566
|
+
nil
|
|
1567
|
+
end
|
|
1568
|
+
|
|
1569
|
+
def init_common(clock, config, data_provider, event_handler, event_logger,
|
|
1570
|
+
variable_parser, audience_matcher, application = nil, environment = nil)
|
|
1571
|
+
@state = State.new(nil, {}, {}, {})
|
|
1572
|
+
@achievements = []
|
|
1573
|
+
@assignment_cache = {}
|
|
1574
|
+
@holdout_assignments = nil
|
|
1575
|
+
@assignment_mutex = Mutex.new
|
|
1576
|
+
@failed_marked = false
|
|
1577
|
+
@exposing = {}.compare_by_identity
|
|
1578
|
+
@exposure_condvar = ConditionVariable.new
|
|
1579
|
+
@assignments = {}
|
|
1580
|
+
@clock = clock.is_a?(String) ? Time.iso8601(clock) : clock
|
|
1581
|
+
@publish_delay = config.publish_delay
|
|
1582
|
+
@refresh_interval = config.refresh_interval
|
|
1583
|
+
@event_handler = event_handler
|
|
1584
|
+
@event_logger = !config.event_logger.nil? ? config.event_logger : event_logger
|
|
1585
|
+
@data_provider = data_provider
|
|
1586
|
+
@variable_parser = variable_parser
|
|
1587
|
+
@audience_matcher = audience_matcher
|
|
1588
|
+
@environment = environment
|
|
1589
|
+
@closed = false
|
|
1590
|
+
@sealing = 0
|
|
1591
|
+
@failed = false
|
|
1592
|
+
@ready_error = nil
|
|
1593
|
+
@sealed_by = nil
|
|
1594
|
+
@building_threads = {}
|
|
1595
|
+
|
|
1596
|
+
# System attributes (application/environment names from the client
|
|
1597
|
+
# config) seed the audience evaluation map. The collector already
|
|
1598
|
+
# attaches both server-side, so they are never added to the publish
|
|
1599
|
+
# payload; empty values are skipped so a missing key stays absent.
|
|
1600
|
+
@system_attributes = {}
|
|
1601
|
+
@system_attributes["application"] = application if application.is_a?(String) && !application.empty?
|
|
1602
|
+
@system_attributes["environment"] = environment if environment.is_a?(String) && !environment.empty?
|
|
1603
|
+
|
|
1604
|
+
@units = {}
|
|
1605
|
+
@attributes = []
|
|
1606
|
+
@overrides = {}
|
|
1607
|
+
@cassignments = {}
|
|
1608
|
+
@assigners = {}
|
|
1609
|
+
@hashed_units = {}
|
|
1610
|
+
@pending_count = 0
|
|
1611
|
+
@exposures = []
|
|
1612
|
+
@attrs_seq = 0
|
|
1613
|
+
@ready_mutex = Mutex.new
|
|
1614
|
+
@ready_condvar = ConditionVariable.new
|
|
1615
|
+
@queue_mutex = Mutex.new
|
|
1616
|
+
@publish_mutex = Mutex.new
|
|
1617
|
+
@deferred_mutex = Mutex.new
|
|
1618
|
+
@close_pending = false
|
|
1619
|
+
@close_requests = 0
|
|
1620
|
+
|
|
1621
|
+
set_units(config.units) if config.units
|
|
1622
|
+
set_attributes(config.attributes) if config.attributes
|
|
1623
|
+
set_overrides(config.overrides) if config.overrides
|
|
1624
|
+
set_custom_assignments(config.custom_assignments) if config.custom_assignments
|
|
1625
|
+
end
|
|
1626
|
+
|
|
1627
|
+
def initialize_async(clock, config, data_provider, event_handler, event_logger,
|
|
1628
|
+
variable_parser, audience_matcher, application: nil, environment: nil)
|
|
1629
|
+
init_common(clock, config, data_provider, event_handler, event_logger,
|
|
1630
|
+
variable_parser, audience_matcher, application, environment)
|
|
572
1631
|
end
|
|
573
1632
|
|
|
574
1633
|
attr_accessor :clock,
|
|
@@ -603,7 +1662,9 @@ end
|
|
|
603
1662
|
class Assignment
|
|
604
1663
|
attr_accessor :id, :iteration, :full_on_variant, :name, :unit_type,
|
|
605
1664
|
:traffic_split, :variant, :assigned, :overridden, :eligible,
|
|
606
|
-
:full_on, :custom, :audience_mismatch, :variables, :exposed
|
|
1665
|
+
:full_on, :custom, :audience_mismatch, :variables, :exposed, :attrs_seq,
|
|
1666
|
+
:rule_override, :rule_variant, :rule_missing_unit, :rule_key,
|
|
1667
|
+
:suppressed, :holdouts, :matched_holdouts, :holdout_assignments, :holdout_arm_count
|
|
607
1668
|
|
|
608
1669
|
def initialize
|
|
609
1670
|
@variant = 0
|
|
@@ -616,11 +1677,30 @@ class Assignment
|
|
|
616
1677
|
@full_on = false
|
|
617
1678
|
@custom = false
|
|
618
1679
|
@audience_mismatch = false
|
|
1680
|
+
@rule_override = false
|
|
1681
|
+
@rule_missing_unit = false
|
|
1682
|
+
@attrs_seq = 0
|
|
1683
|
+
@holdouts = nil
|
|
1684
|
+
@matched_holdouts = nil
|
|
1685
|
+
# Private: set by the context on an assignment built from a failed load's
|
|
1686
|
+
# empty snapshot. Initialized so every assignment keeps one layout.
|
|
1687
|
+
@failed_snapshot = false
|
|
619
1688
|
end
|
|
620
1689
|
end
|
|
621
1690
|
|
|
622
1691
|
class ExperimentVariables
|
|
623
|
-
attr_accessor :data, :variables
|
|
1692
|
+
attr_accessor :data, :variables, :rules, :rule_key, :unconditional, :holdouts
|
|
1693
|
+
|
|
1694
|
+
# Every field is set up front so all instances share one object shape and
|
|
1695
|
+
# the cached read path's attribute reads stay monomorphic.
|
|
1696
|
+
def initialize
|
|
1697
|
+
@data = nil
|
|
1698
|
+
@variables = nil
|
|
1699
|
+
@rules = nil
|
|
1700
|
+
@rule_key = nil
|
|
1701
|
+
@unconditional = nil
|
|
1702
|
+
@holdouts = nil
|
|
1703
|
+
end
|
|
624
1704
|
end
|
|
625
1705
|
|
|
626
1706
|
class ContextCustomFieldValues
|