Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contract-tests/client_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions contract-tests/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions lib/ldclient-rb/impl/data_store/in_memory_feature_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol, Object>] 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?)
#
Expand Down
10 changes: 7 additions & 3 deletions lib/ldclient-rb/impl/data_store/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/ldclient-rb/impl/data_system/fdv2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
65 changes: 65 additions & 0 deletions spec/impl/data_store/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions spec/impl/data_system/fdv2_persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down