launchdarkly-server-sdk 8.19.1 → 8.20.1

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/ldclient-rb/config.rb +11 -1
  4. data/lib/ldclient-rb/data_system/config_builder.rb +35 -1
  5. data/lib/ldclient-rb/data_system.rb +4 -4
  6. data/lib/ldclient-rb/evaluation_detail.rb +37 -4
  7. data/lib/ldclient-rb/events.rb +31 -16
  8. data/lib/ldclient-rb/impl/data_source/stream.rb +4 -1
  9. data/lib/ldclient-rb/impl/data_store/store.rb +84 -3
  10. data/lib/ldclient-rb/impl/data_system/fdv1.rb +5 -0
  11. data/lib/ldclient-rb/impl/data_system/fdv2.rb +85 -0
  12. data/lib/ldclient-rb/impl/data_system/polling.rb +18 -0
  13. data/lib/ldclient-rb/impl/data_system/protocolv2.rb +18 -0
  14. data/lib/ldclient-rb/impl/data_system/streaming.rb +16 -4
  15. data/lib/ldclient-rb/impl/data_system.rb +13 -0
  16. data/lib/ldclient-rb/impl/evaluator.rb +62 -18
  17. data/lib/ldclient-rb/impl/event_summarizer.rb +13 -2
  18. data/lib/ldclient-rb/impl/event_types.rb +11 -1
  19. data/lib/ldclient-rb/impl/file_data/document.rb +267 -0
  20. data/lib/ldclient-rb/impl/file_data/merge.rb +160 -0
  21. data/lib/ldclient-rb/impl/file_data/poller.rb +87 -0
  22. data/lib/ldclient-rb/impl/file_data/reloader.rb +578 -0
  23. data/lib/ldclient-rb/impl/file_data/watcher.rb +519 -0
  24. data/lib/ldclient-rb/impl/file_data.rb +7 -0
  25. data/lib/ldclient-rb/impl/integrations/file_override_source.rb +174 -0
  26. data/lib/ldclient-rb/impl/model/feature_flag.rb +34 -0
  27. data/lib/ldclient-rb/impl/model/segment.rb +34 -0
  28. data/lib/ldclient-rb/impl/overrides/layer.rb +133 -0
  29. data/lib/ldclient-rb/impl/overrides/overlay.rb +71 -0
  30. data/lib/ldclient-rb/impl/overrides/sink.rb +240 -0
  31. data/lib/ldclient-rb/impl/overrides.rb +5 -0
  32. data/lib/ldclient-rb/integrations/file_data.rb +181 -3
  33. data/lib/ldclient-rb/integrations/test_data/flag_builder.rb +1 -1
  34. data/lib/ldclient-rb/interfaces/overrides.rb +90 -0
  35. data/lib/ldclient-rb/interfaces.rb +1 -0
  36. data/lib/ldclient-rb/ldclient.rb +109 -28
  37. data/lib/ldclient-rb/version.rb +1 -1
  38. metadata +14 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d366d24c602af1745d66666e96f8cfe4787e78edd6d60d1c4f1187d16ec4c04
4
- data.tar.gz: 8d0ba31b21833541435c5cebf89570e46257968b2e4eed4b4e84411c61eaa232
3
+ metadata.gz: 51ed8a36fa8b62fd49704843cb52d112124928c848f90ac56a1fd8ea5083e55d
4
+ data.tar.gz: 8e0f1117b85cb62cf6eb438ad553ac054eebec7c4e460cd1067145d490f68a42
5
5
  SHA512:
6
- metadata.gz: 46aac7f3c80150b9bfc3f368722c2ae8f39e2e5890a3f90fbaed76097b1dd955924d8e07bbbb945ff8c512f326dcb858660aca038b9a4b70fd4a7934e702da13
7
- data.tar.gz: '08bbcd1074bbe1a067370514b5d522ba210604d0fc4153ac041ace13a1468788c3c260d23699bbaa064ab07f6e7f78f4dae2aec7b7966cf1a98acce466f40f4b'
6
+ metadata.gz: 84c989baca7d7279ead6701472a2c81d06034a42b91bf480b13e17df7c23c08b8b1eb6a2aec136a856ed332fe799b2fb95294f21e5ee4463224e115747311d29
7
+ data.tar.gz: 48f505b6190092f7f09aacbaf30b73fb318d09679f746d5faf59d143c0eee34db3236aacbd848391f651b717ee54c22825e0e312e2faa789ecd98632d3f260f0
data/README.md CHANGED
@@ -16,7 +16,7 @@ LaunchDarkly overview
16
16
  Supported Ruby versions
17
17
  -----------------------
18
18
 
19
- This version of the LaunchDarkly SDK has a minimum Ruby version of 3.1.0.
19
+ While this version of the LaunchDarkly SDK requires Ruby 3.1.0 or later (or JRuby 9.4.0 or later), you should use the latest patched version of a supported Ruby or JRuby release.
20
20
 
21
21
  Getting started
22
22
  -----------
@@ -736,14 +736,18 @@ module LaunchDarkly
736
736
  # @param data_store [LaunchDarkly::Interfaces::FeatureStore, nil] The (optional) data store
737
737
  # @param fdv1_fallback_synchronizer [#build(String, Config), nil]
738
738
  # The (optional) builder for FDv1-compatible fallback synchronizer
739
+ # @param overrides [#build(String, Config), nil] The (optional) builder for an override source.
740
+ # Flag overrides are currently experimental and subject to change.
739
741
  #
740
742
  def initialize(initializers: nil, synchronizers: nil,
741
- data_store_mode: LaunchDarkly::Interfaces::DataSystem::DataStoreMode::READ_ONLY, data_store: nil, fdv1_fallback_synchronizer: nil)
743
+ data_store_mode: LaunchDarkly::Interfaces::DataSystem::DataStoreMode::READ_ONLY, data_store: nil, fdv1_fallback_synchronizer: nil,
744
+ overrides: nil)
742
745
  @initializers = initializers
743
746
  @synchronizers = synchronizers
744
747
  @data_store_mode = data_store_mode
745
748
  @data_store = data_store
746
749
  @fdv1_fallback_synchronizer = fdv1_fallback_synchronizer
750
+ @overrides = overrides
747
751
  end
748
752
 
749
753
  # The initializer builders for the data system. Each builder responds to build(sdk_key, config) and returns an Initializer.
@@ -765,5 +769,11 @@ module LaunchDarkly
765
769
  # The FDv1-compatible fallback synchronizer builder. Responds to build(sdk_key, config) and returns a Synchronizer.
766
770
  # @return [#build(String, Config), nil]
767
771
  attr_reader :fdv1_fallback_synchronizer
772
+
773
+ # The override source builder. Responds to build(sdk_key, config) and returns an
774
+ # {LaunchDarkly::Interfaces::Overrides::OverrideSource}. Flag overrides are currently
775
+ # experimental and subject to change.
776
+ # @return [#build(String, Config), nil]
777
+ attr_reader :overrides
768
778
  end
769
779
  end
@@ -23,6 +23,7 @@ module LaunchDarkly
23
23
  @fdv1_fallback_synchronizer = nil
24
24
  @data_store_mode = LaunchDarkly::Interfaces::DataSystem::DataStoreMode::READ_ONLY
25
25
  @data_store = nil
26
+ @overrides = nil
26
27
  end
27
28
 
28
29
  #
@@ -85,6 +86,38 @@ module LaunchDarkly
85
86
  self
86
87
  end
87
88
 
89
+ #
90
+ # Configures the SDK with an override source. Flag overrides are currently experimental and
91
+ # subject to change.
92
+ #
93
+ # Overrides are flag and segment definitions that take precedence over data received from
94
+ # LaunchDarkly at evaluation time, on a per-key basis. They exist for resilience during an
95
+ # incident. Overrides let an operator force one or more flags to a known state on a running
96
+ # client, whether or not the client can reach LaunchDarkly. Flags not present in the override
97
+ # data are completely unaffected.
98
+ #
99
+ # The override source is not a data source. It has no effect on the client's initialization
100
+ # status, data availability, or data source status. Without a configured source, or while
101
+ # the source supplies no overrides, the SDK behaves as it would without this feature.
102
+ #
103
+ # At most one override source can be configured. A later call replaces an earlier one.
104
+ #
105
+ # When the client is offline, the source is not built or started: no overrides are served,
106
+ # and a configuration error in the source is not reported. The SDK logs a warning instead.
107
+ #
108
+ # @example
109
+ # overrides = LaunchDarkly::Integrations::FileData.override_source(paths: ["/etc/launchdarkly/overrides.json"])
110
+ # config = LaunchDarkly::Config.new(data_system_config: LaunchDarkly::DataSystem.default.overrides(overrides).build)
111
+ #
112
+ # @param source [#build(String, Config), nil] a builder that responds to build(sdk_key, config)
113
+ # and returns a {LaunchDarkly::Interfaces::Overrides::OverrideSource}, or nil to remove one
114
+ # @return [ConfigBuilder] self for chaining
115
+ #
116
+ def overrides(source)
117
+ @overrides = source
118
+ self
119
+ end
120
+
88
121
  #
89
122
  # Builds the data system configuration.
90
123
  #
@@ -96,7 +129,8 @@ module LaunchDarkly
96
129
  synchronizers: @synchronizers,
97
130
  data_store_mode: @data_store_mode,
98
131
  data_store: @data_store,
99
- fdv1_fallback_synchronizer: @fdv1_fallback_synchronizer
132
+ fdv1_fallback_synchronizer: @fdv1_fallback_synchronizer,
133
+ overrides: @overrides
100
134
  )
101
135
  end
102
136
  end
@@ -22,17 +22,17 @@ module LaunchDarkly
22
22
  #
23
23
  # # Use the default strategy (recommended)
24
24
  # config = LaunchDarkly::Config.new(
25
- # data_system: LaunchDarkly::DataSystem.default
25
+ # data_system_config: LaunchDarkly::DataSystem.default.build
26
26
  # )
27
27
  #
28
28
  # # Use streaming only
29
29
  # config = LaunchDarkly::Config.new(
30
- # data_system: LaunchDarkly::DataSystem.streaming
30
+ # data_system_config: LaunchDarkly::DataSystem.streaming.build
31
31
  # )
32
32
  #
33
33
  # # Use polling only
34
34
  # config = LaunchDarkly::Config.new(
35
- # data_system: LaunchDarkly::DataSystem.polling
35
+ # data_system_config: LaunchDarkly::DataSystem.polling.build
36
36
  # )
37
37
  #
38
38
  # == Custom Configurations
@@ -52,7 +52,7 @@ module LaunchDarkly
52
52
  # .initializers([polling])
53
53
  # .synchronizers([streaming, polling])
54
54
  #
55
- # config = LaunchDarkly::Config.new(data_system: data_system)
55
+ # config = LaunchDarkly::Config.new(data_system_config: data_system.build)
56
56
  #
57
57
  module DataSystem
58
58
  #
@@ -150,6 +150,15 @@ module LaunchDarkly
150
150
  # @return [Symbol]
151
151
  attr_reader :big_segments_status
152
152
 
153
+ # True if an override affected this evaluation, directly or transitively. It is true when the
154
+ # evaluated flag came from the SDK's override store. It is also true when a prerequisite flag at
155
+ # any depth, or a segment read during the evaluation, came from the override store. In the JSON
156
+ # representation, the `overrideAffected` property appears only when this is true.
157
+ #
158
+ # Flag overrides are currently experimental and subject to change.
159
+ # @return [Boolean]
160
+ attr_reader :override_affected
161
+
153
162
  # Returns an instance whose {#kind} is {#OFF}.
154
163
  # @return [EvaluationReason]
155
164
  def self.off
@@ -216,12 +225,14 @@ module LaunchDarkly
216
225
  if other.is_a? EvaluationReason
217
226
  @kind == other.kind && @rule_index == other.rule_index && @rule_id == other.rule_id &&
218
227
  @prerequisite_key == other.prerequisite_key && @error_kind == other.error_kind &&
219
- @big_segments_status == other.big_segments_status
228
+ @big_segments_status == other.big_segments_status &&
229
+ @override_affected == other.override_affected
220
230
  elsif other.is_a? Hash
221
231
  @kind.to_s == other[:kind] && @rule_index == other[:ruleIndex] && @rule_id == other[:ruleId] &&
222
232
  @prerequisite_key == other[:prerequisiteKey] &&
223
233
  (other[:errorKind] == @error_kind.nil? ? nil : @error_kind.to_s) &&
224
- (other[:bigSegmentsStatus] == @big_segments_status.nil? ? nil : @big_segments_status.to_s)
234
+ (other[:bigSegmentsStatus] == @big_segments_status.nil? ? nil : @big_segments_status.to_s) &&
235
+ !!other[:overrideAffected] == @override_affected
225
236
  end
226
237
  end
227
238
 
@@ -286,6 +297,8 @@ module LaunchDarkly
286
297
  unless @big_segments_status.nil?
287
298
  ret[:bigSegmentsStatus] = @big_segments_status
288
299
  end
300
+ # The property is written only when true, so the wire format of an ordinary evaluation is unchanged.
301
+ ret[:overrideAffected] = true if @override_affected
289
302
  ret
290
303
  end
291
304
 
@@ -312,6 +325,8 @@ module LaunchDarkly
312
325
  @error_kind.nil? ? nil : @error_kind.to_s
313
326
  when :bigSegmentsStatus
314
327
  @big_segments_status.nil? ? nil : @big_segments_status.to_s
328
+ when :overrideAffected
329
+ @override_affected
315
330
  else
316
331
  nil
317
332
  end
@@ -319,7 +334,24 @@ module LaunchDarkly
319
334
 
320
335
  def with_big_segments_status(big_segments_status)
321
336
  return self if @big_segments_status == big_segments_status
322
- EvaluationReason.new(@kind, @rule_index, @rule_id, @prerequisite_key, @error_kind, @in_experiment, big_segments_status)
337
+ EvaluationReason.new(@kind, @rule_index, @rule_id, @prerequisite_key, @error_kind, @in_experiment,
338
+ big_segments_status, @override_affected)
339
+ end
340
+
341
+ #
342
+ # Returns a reason that is the same as this one apart from the {#override_affected} indicator.
343
+ # Returns this instance when the indicator already has the given value.
344
+ #
345
+ # Flag overrides are currently experimental and subject to change.
346
+ #
347
+ # @param override_affected [Boolean]
348
+ # @return [EvaluationReason]
349
+ #
350
+ def with_override_affected(override_affected)
351
+ override_affected = !!override_affected
352
+ return self if @override_affected == override_affected
353
+ EvaluationReason.new(@kind, @rule_index, @rule_id, @prerequisite_key, @error_kind, @in_experiment,
354
+ @big_segments_status, override_affected)
323
355
  end
324
356
 
325
357
  #
@@ -327,7 +359,7 @@ module LaunchDarkly
327
359
  # but should use class methods like {#off} to avoid creating unnecessary instances.
328
360
  #
329
361
  def initialize(kind, rule_index, rule_id, prerequisite_key, error_kind, in_experiment=nil,
330
- big_segments_status = nil)
362
+ big_segments_status = nil, override_affected = false)
331
363
  @kind = kind.to_sym
332
364
  @rule_index = rule_index
333
365
  @rule_id = rule_id
@@ -337,6 +369,7 @@ module LaunchDarkly
337
369
  @error_kind = error_kind
338
370
  @in_experiment = in_experiment
339
371
  @big_segments_status = big_segments_status
372
+ @override_affected = !!override_affected
340
373
  end
341
374
 
342
375
  private_class_method def self.make_error(error_kind)
@@ -44,7 +44,8 @@ module LaunchDarkly
44
44
  debug_until = nil,
45
45
  prereq_of = nil,
46
46
  sampling_ratio = nil,
47
- exclude_from_summaries = false
47
+ exclude_from_summaries = false,
48
+ override_affected = false
48
49
  )
49
50
  end
50
51
 
@@ -176,10 +177,11 @@ module LaunchDarkly
176
177
  debug_until = nil,
177
178
  prereq_of = nil,
178
179
  sampling_ratio = nil,
179
- exclude_from_summaries = false
180
+ exclude_from_summaries = false,
181
+ override_affected = false
180
182
  )
181
183
  post_to_inbox(LaunchDarkly::Impl::EvalEvent.new(timestamp, context, key, version, variation, value, reason,
182
- default, track_events, debug_until, prereq_of, sampling_ratio, exclude_from_summaries))
184
+ default, track_events, debug_until, prereq_of, sampling_ratio, exclude_from_summaries, override_affected))
183
185
  end
184
186
 
185
187
  def record_identify_event(context)
@@ -188,6 +190,13 @@ module LaunchDarkly
188
190
  end
189
191
 
190
192
  def record_custom_event(context, key, data = nil, metric_value = nil)
193
+ if metric_value.is_a?(Float) && !metric_value.finite?
194
+ # NaN and +/-Infinity cannot be serialized to JSON; sending them would make the whole
195
+ # batch fail to serialize and drop every other event in it.
196
+ @logger.warn { "[LDClient] Custom event '#{key}' has a non-finite metric value (#{metric_value}); the event will be dropped" }
197
+ return
198
+ end
199
+
191
200
  post_to_inbox(LaunchDarkly::Impl::CustomEvent.new(timestamp, context, key, data, metric_value))
192
201
  end
193
202
 
@@ -328,8 +337,10 @@ module LaunchDarkly
328
337
  will_add_full_event = false
329
338
  debug_event = nil
330
339
  if event.is_a?(LaunchDarkly::Impl::EvalEvent)
331
- will_add_full_event = event.track_events
332
- if should_debug_event(event)
340
+ # An override-affected evaluation appears only in the summary counters. It produces no
341
+ # individual feature event and no debug event, whatever the flag's configuration requests.
342
+ will_add_full_event = event.track_events && !event.override_affected
343
+ if !event.override_affected && should_debug_event(event)
333
344
  debug_event = LaunchDarkly::Impl::DebugEvent.new(event)
334
345
  end
335
346
  else
@@ -630,18 +641,22 @@ module LaunchDarkly
630
641
  summary.counters.each do |flagKey, flagInfo|
631
642
  counters = []
632
643
  flagInfo.versions.each do |version, variations|
633
- variations.each do |variation, counter|
634
- c = {
635
- value: counter.value,
636
- count: counter.count,
637
- }
638
- c[:variation] = variation unless variation.nil?
639
- if version.nil?
640
- c[:unknown] = true
641
- else
642
- c[:version] = version
644
+ variations.each do |variation, counters_by_marker|
645
+ counters_by_marker.each do |override_affected, counter|
646
+ c = {
647
+ value: counter.value,
648
+ count: counter.count,
649
+ }
650
+ c[:variation] = variation unless variation.nil?
651
+ if version.nil?
652
+ c[:unknown] = true
653
+ else
654
+ c[:version] = version
655
+ end
656
+ # The marker is present only for override-affected counters, like the unknown marker.
657
+ c[:overrideAffected] = true if override_affected
658
+ counters.push(c)
643
659
  end
644
- counters.push(c)
645
660
  end
646
661
  end
647
662
  flags[flagKey] = { default: flagInfo.default, counters: counters, contextKinds: flagInfo.context_kinds.to_a }
@@ -192,10 +192,13 @@ module LaunchDarkly
192
192
  end
193
193
  elsif method == DELETE
194
194
  data = JSON.parse(message.data, symbolize_names: true)
195
+ version = data[:version]
196
+ # The SSE client restarts the stream when this method raises.
197
+ raise ArgumentError, "delete event for #{data[:path]} has a version that is not an integer" unless version.is_a?(Integer)
195
198
  for kind in [Impl::DataStore::FEATURES, Impl::DataStore::SEGMENTS]
196
199
  key = key_for_path(kind, data[:path])
197
200
  if key
198
- update_sink_or_data_store.delete(kind, key, data[:version])
201
+ update_sink_or_data_store.delete(kind, key, version)
199
202
  break
200
203
  end
201
204
  end
@@ -42,6 +42,10 @@ module LaunchDarkly
42
42
  # the form in which the data model names prerequisites and segments.
43
43
  @dependency_tracker = LaunchDarkly::Impl::DependencyTracker.new(logger)
44
44
 
45
+ # A second dependency graph, such as the override layer's, whose items may depend on
46
+ # items in this store. Set once when the data system is built, before any data.
47
+ @external_dependencies = nil
48
+
45
49
  # Broadcasters for events
46
50
  @flag_change_broadcaster = flag_change_broadcaster
47
51
  @change_set_broadcaster = change_set_broadcaster
@@ -180,6 +184,45 @@ module LaunchDarkly
180
184
  get_active_store.initialized?
181
185
  end
182
186
 
187
+ def memory_initialized?
188
+ @memory_store.initialized?
189
+ end
190
+
191
+ #
192
+ # Retrieves an item from the active store.
193
+ #
194
+ # @param kind [LaunchDarkly::Impl::DataStore::DataKind]
195
+ # @param key [String, Symbol]
196
+ # @return [Object, nil] the item, or nil if it is unknown or deleted
197
+ #
198
+ def get(kind, key)
199
+ get_active_store.get(kind, key)
200
+ end
201
+
202
+ #
203
+ # Retrieves all items of a kind from the active store.
204
+ #
205
+ # @param kind [LaunchDarkly::Impl::DataStore::DataKind]
206
+ # @return [Hash{Symbol => Object}]
207
+ #
208
+ def all(kind)
209
+ get_active_store.all(kind)
210
+ end
211
+
212
+ #
213
+ # Registers a second dependency graph whose items may depend on items in this store. A
214
+ # change to an item in this store then also notifies the external items that depend on
215
+ # it, directly or through any chain of dependencies in either graph.
216
+ #
217
+ # @param dependencies [#add_affected_items, nil] a graph answering `add_affected_items(items_out, { kind:, key: })`
218
+ # @return [void]
219
+ #
220
+ def set_external_dependencies(dependencies)
221
+ @lock.synchronize do
222
+ @external_dependencies = dependencies
223
+ end
224
+ end
225
+
183
226
  #
184
227
  # Get the data store status provider for the persistent store, if configured.
185
228
  #
@@ -257,7 +300,7 @@ module LaunchDarkly
257
300
  collection.each do |key, item|
258
301
  @dependency_tracker.update_dependencies_from(kind, key.to_s, item)
259
302
  if has_listeners
260
- @dependency_tracker.add_affected_items(affected_items, { kind: kind, key: key.to_s })
303
+ add_affected_items(affected_items, { kind: kind, key: key })
261
304
  end
262
305
  end
263
306
  end
@@ -344,6 +387,44 @@ module LaunchDarkly
344
387
  end
345
388
  end
346
389
 
390
+ #
391
+ # Adds the item and everything that depends on it to the set, following this store's own
392
+ # dependency graph and the external one. Every item reached through either graph is
393
+ # expanded through both, so a dependency chain may cross between them any number of
394
+ # times. Keys are compared as strings, so a change keyed by symbol reaches the same
395
+ # dependents as one keyed by string. Without an external graph, the store's own tracker
396
+ # expands the item in one traversal, as it did before external graphs existed.
397
+ #
398
+ # @param items_out [Set<Hash>] Set of {kind:, key:} hashes
399
+ # @param initial_modified_item [Hash] {kind:, key:} hash
400
+ # @return [void]
401
+ #
402
+ private def add_affected_items(items_out, initial_modified_item)
403
+ start = { kind: initial_modified_item[:kind], key: initial_modified_item[:key].to_s }
404
+ if @external_dependencies.nil?
405
+ @dependency_tracker.add_affected_items(items_out, start)
406
+ return
407
+ end
408
+
409
+ pending = [start]
410
+ until pending.empty?
411
+ current = pending.pop
412
+ # Each graph gets its own working set: a tracker does not expand an item that is
413
+ # already present, so a shared set would stop the second graph at the start.
414
+ via_store = Set.new
415
+ @dependency_tracker.add_affected_items(via_store, current)
416
+ via_external = Set.new
417
+ @external_dependencies&.add_affected_items(via_external, current)
418
+ (via_store | via_external).each do |reached|
419
+ item = { kind: reached[:kind], key: reached[:key].to_s }
420
+ next if items_out.include?(item)
421
+
422
+ items_out.add(item)
423
+ pending << item
424
+ end
425
+ end
426
+ end
427
+
347
428
  #
348
429
  # Send flag change events for affected items.
349
430
  #
@@ -381,9 +462,9 @@ module LaunchDarkly
381
462
 
382
463
  # If either is missing or versions differ, it's a change
383
464
  if old_item.nil? || new_item.nil?
384
- @dependency_tracker.add_affected_items(affected_items, { kind: kind, key: key.to_s })
465
+ add_affected_items(affected_items, { kind: kind, key: key })
385
466
  elsif old_item[:version] != new_item[:version]
386
- @dependency_tracker.add_affected_items(affected_items, { kind: kind, key: key.to_s })
467
+ add_affected_items(affected_items, { kind: kind, key: key })
387
468
  end
388
469
  end
389
470
  end
@@ -101,6 +101,11 @@ module LaunchDarkly
101
101
  @store_wrapper
102
102
  end
103
103
 
104
+ # (see DataSystem#override_items)
105
+ def override_items(_kind)
106
+ {}
107
+ end
108
+
104
109
  # (see DataSystem#set_diagnostic_accumulator)
105
110
  def set_diagnostic_accumulator(diagnostic_accumulator)
106
111
  @diagnostic_accumulator = diagnostic_accumulator
@@ -8,7 +8,9 @@ require "ldclient-rb/impl/data_store/feature_store_client_wrapper"
8
8
  require "ldclient-rb/impl/data_source/status_provider"
9
9
  require "ldclient-rb/impl/data_store/status_provider"
10
10
  require "ldclient-rb/impl/broadcaster"
11
+ require "ldclient-rb/impl/overrides"
11
12
  require "ldclient-rb/impl/repeating_task"
13
+ require "ldclient-rb/impl/util"
12
14
  require "ldclient-rb/interfaces/data_system"
13
15
 
14
16
  module LaunchDarkly
@@ -118,6 +120,28 @@ module LaunchDarkly
118
120
  # Track configuration
119
121
  @configured_with_data_sources = (@data_system_config.initializers && !@data_system_config.initializers.empty?) ||
120
122
  !@synchronizer_builders.empty?
123
+
124
+ # The following are nil unless an override source is configured. The layer holds override
125
+ # entries. The overlay serves them in preference to the store's data at the store read
126
+ # boundary. The source populates the layer at runtime. The override system is separate
127
+ # from the initializer and synchronizer pipeline and never affects data availability.
128
+ @override_layer = nil
129
+ @overlay = nil
130
+ @override_source = nil
131
+ @override_sink = nil
132
+ if @data_system_config.overrides && @disabled
133
+ @logger.warn { "[LDClient] Offline mode is enabled; the override source is not started and no overrides are served" }
134
+ elsif @data_system_config.overrides
135
+ # An invalid override source configuration raises here, so LDClient.new reports it
136
+ # the same way as other invalid component configuration.
137
+ @override_source = @data_system_config.overrides.build(@sdk_key, @config)
138
+ @override_layer = LaunchDarkly::Impl::Overrides::Layer.new
139
+ @overlay = LaunchDarkly::Impl::Overrides::Overlay.new(@store, @override_layer)
140
+ # A LaunchDarkly change to a flag or segment that an override entry depends on must
141
+ # notify that entry's flag as well. The store reaches those entries through the
142
+ # layer's dependency graph.
143
+ @store.set_external_dependencies(@override_layer)
144
+ end
121
145
  end
122
146
 
123
147
  # (see DataSystem#start)
@@ -131,6 +155,10 @@ module LaunchDarkly
131
155
  @stop_event.reset
132
156
  @ready_event.reset
133
157
 
158
+ # The override source starts before the run loop, so a source that loads synchronously has
159
+ # its overrides in place before the client begins evaluating.
160
+ start_override_source if @override_source
161
+
134
162
  # Start the main coordination thread
135
163
  main_thread = Thread.new { run_main_loop }
136
164
  main_thread.name = "FDv2-main"
@@ -141,6 +169,14 @@ module LaunchDarkly
141
169
 
142
170
  # (see DataSystem#stop)
143
171
  def stop
172
+ if @override_source
173
+ # The sink stops accepting snapshots first, so a snapshot that the source supplies while
174
+ # it stops cannot update the layer or notify listeners while the system shuts down. The
175
+ # sink does not exist if the system was never started.
176
+ @override_sink&.close
177
+ stop_override_source("Error stopping override source")
178
+ end
179
+
144
180
  @stop_event.set
145
181
 
146
182
  @lock.synchronize do
@@ -175,9 +211,18 @@ module LaunchDarkly
175
211
 
176
212
  # (see DataSystem#store)
177
213
  def store
214
+ return @overlay if @overlay
215
+
178
216
  @store.get_active_store
179
217
  end
180
218
 
219
+ # (see DataSystem#override_items)
220
+ def override_items(kind)
221
+ return {} if @override_layer.nil?
222
+
223
+ @override_layer.all(kind)
224
+ end
225
+
181
226
  # (see DataSystem#data_source_status_provider)
182
227
  def data_source_status_provider
183
228
  @data_source_status_provider
@@ -215,6 +260,45 @@ module LaunchDarkly
215
260
 
216
261
  private
217
262
 
263
+ #
264
+ # Starts the override source. The source has no effect on initialization, and a source that
265
+ # fails to start does not prevent the client from operating: the failure is logged, the
266
+ # source is stopped, and the client runs without overrides. The SDK's own file-based source
267
+ # does not raise from start; it logs a failed load and retries it. A custom source could
268
+ # raise, and an application in an incident is better served by a client that runs without
269
+ # its overrides than by one that does not start.
270
+ #
271
+ def start_override_source
272
+ @override_sink = LaunchDarkly::Impl::Overrides::Sink.new(@override_layer, @store, @flag_change_broadcaster, @logger)
273
+ begin
274
+ @override_source.start(@override_sink)
275
+ rescue StandardError, ScriptError => e
276
+ # ScriptError covers the NotImplementedError that the interface's default start raises
277
+ # for a source that does not implement it.
278
+ LaunchDarkly::Impl::Util.log_exception(@logger, "Unable to start the override source; the client runs without overrides", e)
279
+ stop_override_source("Error stopping override source after it failed to start")
280
+ # The source may have supplied a snapshot before it failed. Clearing the layer removes
281
+ # it, so the client runs without overrides as logged, and a late snapshot from the
282
+ # stopped source is ignored.
283
+ @override_sink.close_and_clear
284
+ end
285
+ end
286
+
287
+ #
288
+ # Stops the override source and forgets it. A custom source can raise from stop, including
289
+ # the NotImplementedError of the interface's default stop; that is logged and not raised,
290
+ # so the rest of the system still stops, or still starts after a failed start.
291
+ #
292
+ def stop_override_source(failure_message)
293
+ source = @override_source
294
+ @override_source = nil
295
+ begin
296
+ source.stop
297
+ rescue StandardError, ScriptError => e
298
+ @logger.error { "[LDClient] #{failure_message}: #{e.message}" }
299
+ end
300
+ end
301
+
218
302
  #
219
303
  # Main coordination loop that manages initializers and synchronizers.
220
304
  #
@@ -324,6 +408,7 @@ module LaunchDarkly
324
408
  end
325
409
  end
326
410
 
411
+ @ready_event.set if @store.memory_initialized?
327
412
  false
328
413
  end
329
414
 
@@ -497,6 +497,15 @@ module LaunchDarkly
497
497
  builder.start(server_intent.payload.code)
498
498
 
499
499
  when LaunchDarkly::Interfaces::DataSystem::EventName::PUT_OBJECT
500
+ unless event[:data].is_a?(Hash)
501
+ return LaunchDarkly::Result.fail("Invalid payload: 'put-object' data must be an object")
502
+ end
503
+ unless event[:data][:kind].is_a?(String)
504
+ return LaunchDarkly::Result.fail("Invalid payload: 'put-object' kind must be a string")
505
+ end
506
+
507
+ next if LaunchDarkly::Impl::DataSystem::ProtocolV2.unsupported_kind?(event[:data])
508
+
500
509
  begin
501
510
  put = LaunchDarkly::Impl::DataSystem::ProtocolV2::PutObject.from_h(event[:data])
502
511
  rescue ArgumentError => e
@@ -506,6 +515,15 @@ module LaunchDarkly
506
515
  builder.add_put(put.kind, put.key, put.version, put.object)
507
516
 
508
517
  when LaunchDarkly::Interfaces::DataSystem::EventName::DELETE_OBJECT
518
+ unless event[:data].is_a?(Hash)
519
+ return LaunchDarkly::Result.fail("Invalid payload: 'delete-object' data must be an object")
520
+ end
521
+ unless event[:data][:kind].is_a?(String)
522
+ return LaunchDarkly::Result.fail("Invalid payload: 'delete-object' kind must be a string")
523
+ end
524
+
525
+ next if LaunchDarkly::Impl::DataSystem::ProtocolV2.unsupported_kind?(event[:data])
526
+
509
527
  begin
510
528
  delete_object = LaunchDarkly::Impl::DataSystem::ProtocolV2::DeleteObject.from_h(event[:data])
511
529
  rescue ArgumentError => e
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'ldclient-rb/interfaces/data_system'
2
3
 
3
4
  module LaunchDarkly
4
5
  module Impl
@@ -9,6 +10,23 @@ module LaunchDarkly
9
10
  # LaunchDarkly data system version 2 (FDv2).
10
11
  #
11
12
 
13
+ SUPPORTED_KINDS = [
14
+ LaunchDarkly::Interfaces::DataSystem::ObjectKind::FLAG,
15
+ LaunchDarkly::Interfaces::DataSystem::ObjectKind::SEGMENT,
16
+ ].freeze
17
+
18
+ #
19
+ # Returns true if put-object or delete-object data names a kind this SDK
20
+ # does not support. Such objects are ignored so that payloads carrying
21
+ # newer kinds can still be applied.
22
+ #
23
+ # @param data [Hash] The hash representation of the event data, with a String kind
24
+ # @return [Boolean]
25
+ #
26
+ def self.unsupported_kind?(data)
27
+ !SUPPORTED_KINDS.include?(data[:kind])
28
+ end
29
+
12
30
  #
13
31
  # DeleteObject specifies the deletion of a particular object.
14
32
  #