diff --git a/contract-tests/client_entity.rb b/contract-tests/client_entity.rb index 8e3e99b4..a7d38469 100644 --- a/contract-tests/client_entity.rb +++ b/contract-tests/client_entity.rb @@ -429,7 +429,9 @@ def close store_config[:redis_url] = persistent_store_config[:store][:dsn] LaunchDarkly::Integrations::Redis.new_feature_store(store_config) when 'consul' - store_config[:url] = persistent_store_config[:store][:url] + # The DSN is a bare host:port. The Consul client needs a URL. + dsn = persistent_store_config[:store][:dsn] + store_config[:url] = dsn.include?('://') ? dsn : "http://#{dsn}" if dsn LaunchDarkly::Integrations::Consul.new_feature_store(store_config) when 'dynamodb' client = Aws::DynamoDB::Client.new( diff --git a/contract-tests/service.rb b/contract-tests/service.rb index 906e9522..c4e7fb4a 100644 --- a/contract-tests/service.rb +++ b/contract-tests/service.rb @@ -52,6 +52,7 @@ 'persistent-data-store-consul', 'persistent-data-store-dynamodb', 'persistent-data-store-redis', + 'persistent-data-store-recovery', 'flag-change-listeners', 'flag-value-change-listeners', 'fdv1-fallback', diff --git a/lib/ldclient-rb/impl/data_store/in_memory_feature_store.rb b/lib/ldclient-rb/impl/data_store/in_memory_feature_store.rb index cef3de7f..6cda5629 100644 --- a/lib/ldclient-rb/impl/data_store/in_memory_feature_store.rb +++ b/lib/ldclient-rb/impl/data_store/in_memory_feature_store.rb @@ -48,6 +48,22 @@ def all(kind) end end + # + # Returns all items of a kind, including deleted items. A deleted item is a placeholder + # that keeps the version of the deletion. + # + # @param kind [LaunchDarkly::Impl::DataStore::DataKind] The kind of data + # @return [Hash] Hash of keys to items + # + def all_including_deleted(kind) + @lock.with_read_lock do + items_of_kind = @items[kind] + return {} if items_of_kind.nil? + + items_of_kind.dup + end + end + # # (see LaunchDarkly::Interfaces::DataSystem::ReadOnlyStore#initialized?) # diff --git a/lib/ldclient-rb/impl/data_store/store.rb b/lib/ldclient-rb/impl/data_store/store.rb index 3dd62c5d..977a4212 100644 --- a/lib/ldclient-rb/impl/data_store/store.rb +++ b/lib/ldclient-rb/impl/data_store/store.rb @@ -150,10 +150,12 @@ def commit return nil unless should_persist? begin - # Get all data from memory store and write to persistent store + # Get all data from memory store and write to persistent store. Include the deleted + # items, so that the persistent store keeps the version of each deletion and an older + # update cannot add the item again. all_data = {} [FEATURES, SEGMENTS].each do |kind| - all_data[kind] = @memory_store.all(kind) + all_data[kind] = @memory_store.all_including_deleted(kind) end @persistent_store.init(all_data) rescue => e @@ -361,7 +363,9 @@ def get_data_store_status_provider if change.action == LaunchDarkly::Interfaces::DataSystem::ChangeType::PUT && !change.object.nil? all_data[kind][change.key] = change.object elsif change.action == LaunchDarkly::Interfaces::DataSystem::ChangeType::DELETE - all_data[kind][change.key] = { key: change.key, deleted: true, version: change.version } + # The key in an item is a string, as in the items that LaunchDarkly sends. A persistent + # store compares this key with the keys that it reads back from its database. + all_data[kind][change.key] = { key: change.key.to_s, deleted: true, version: change.version } end end diff --git a/lib/ldclient-rb/impl/data_system/fdv2.rb b/lib/ldclient-rb/impl/data_system/fdv2.rb index 1cf96d52..fec6cd90 100644 --- a/lib/ldclient-rb/impl/data_system/fdv2.rb +++ b/lib/ldclient-rb/impl/data_system/fdv2.rb @@ -77,9 +77,12 @@ def initialize(sdk_key, config, data_system_config) @data_source_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@shared_executor, @logger) @data_store_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@shared_executor, @logger) + # The body of a method that define_singleton_method makes runs with the listener as self. + # Thus, the listener keeps a reference to the recovery method of this object. + recovery = method(:persistent_store_outage_recovery) recovery_listener = Object.new recovery_listener.define_singleton_method(:update) do |data_store_status| - persistent_store_outage_recovery(data_store_status) + recovery.call(data_store_status) end @data_store_broadcaster.add_listener(recovery_listener) diff --git a/spec/impl/data_store/store_spec.rb b/spec/impl/data_store/store_spec.rb index 8e7c24b6..e63179aa 100644 --- a/spec/impl/data_store/store_spec.rb +++ b/spec/impl/data_store/store_spec.rb @@ -565,6 +565,71 @@ def changed_flags expect(stored_flag[:version]).to eq(1) end + it "writes deleted items to persistent store as tombstones with string keys" do + put_changes = [ + LaunchDarkly::Interfaces::DataSystem::Change.new( + action: LaunchDarkly::Interfaces::DataSystem::ChangeType::PUT, + kind: LaunchDarkly::Interfaces::DataSystem::ObjectKind::FLAG, + key: flag_key, + version: 1, + object: flag_data + ), + LaunchDarkly::Interfaces::DataSystem::Change.new( + action: LaunchDarkly::Interfaces::DataSystem::ChangeType::PUT, + kind: LaunchDarkly::Interfaces::DataSystem::ObjectKind::SEGMENT, + key: segment_key, + version: 1, + object: segment_data + ), + ] + subject.apply( + LaunchDarkly::Interfaces::DataSystem::ChangeSet.new( + intent_code: LaunchDarkly::Interfaces::DataSystem::IntentCode::TRANSFER_FULL, + changes: put_changes, + selector: LaunchDarkly::Interfaces::DataSystem::Selector.no_selector + ), + true + ) + + delete_changes = [ + LaunchDarkly::Interfaces::DataSystem::Change.new( + action: LaunchDarkly::Interfaces::DataSystem::ChangeType::DELETE, + kind: LaunchDarkly::Interfaces::DataSystem::ObjectKind::FLAG, + key: flag_key, + version: 2, + object: nil + ), + LaunchDarkly::Interfaces::DataSystem::Change.new( + action: LaunchDarkly::Interfaces::DataSystem::ChangeType::DELETE, + kind: LaunchDarkly::Interfaces::DataSystem::ObjectKind::SEGMENT, + key: segment_key, + version: 2, + object: nil + ), + ] + subject.apply( + LaunchDarkly::Interfaces::DataSystem::ChangeSet.new( + intent_code: LaunchDarkly::Interfaces::DataSystem::IntentCode::TRANSFER_CHANGES, + changes: delete_changes, + selector: LaunchDarkly::Interfaces::DataSystem::Selector.no_selector + ), + true + ) + + # Write the memory store back to the persistent store. + error = subject.commit + + # The persistent store holds a tombstone for each deleted item. + expect(error).to be_nil + [[FEATURES, flag_key], [SEGMENTS, segment_key]].each do |kind, key| + tombstone = persistent_store.data[kind][key] + expect(tombstone).not_to be_nil + expect(tombstone[:deleted]).to be true + expect(tombstone[:version]).to eq(2) + expect(tombstone[:key]).to eq(key.to_s) + end + end + it "returns exception when persistent store fails" do failing_store = StubPersistentStore.new(should_fail: true) subject.with_persistence(failing_store, true, nil) diff --git a/spec/impl/data_system/fdv2_persistence_spec.rb b/spec/impl/data_system/fdv2_persistence_spec.rb index d629cd5b..f3d2244a 100644 --- a/spec/impl/data_system/fdv2_persistence_spec.rb +++ b/spec/impl/data_system/fdv2_persistence_spec.rb @@ -172,6 +172,43 @@ def get_data_snapshot fdv2.stop end + it "writes in-memory store to persistent store when the store status reports a recovery with stale data" do + persistent_store = StubFeatureStore.new + + td_synchronizer = LaunchDarkly::Integrations::TestDataV2.data_source + td_synchronizer.update(td_synchronizer.flag("flagkey").on(true)) + + data_system_config = LaunchDarkly::DataSystem::ConfigBuilder.new + .initializers(nil) + .synchronizers([td_synchronizer.test_data_ds_builder]) + .data_store(persistent_store, :read_write) + .build + + fdv2 = FDv2.new(sdk_key, config, data_system_config) + ready_event = fdv2.start + + expect(ready_event.wait(3)).to be true + + # The persistent store loses its data, as it can during an outage. + persistent_store.init({}) + write_back = Concurrent::Event.new + persistent_store.define_singleton_method(:init) do |all_data| + super(all_data) + write_back.set + end + + # Report that the store is available again and that its data can be stale. + stale_status = LaunchDarkly::Interfaces::DataStore::Status.new(true, true) + fdv2.data_store_status_provider.update_status(stale_status) + + # The data system writes the in-memory data back to the persistent store. + expect(write_back.wait(2)).to be true + snapshot = persistent_store.get_data_snapshot + expect(snapshot[LaunchDarkly::Impl::DataStore::FEATURES]).to have_key(:flagkey) + + fdv2.stop + end + it "does not flush when store comes back online without stale data" do persistent_store = StubFeatureStore.new