Repository navigation
fix: Write FDv2 data back to the persistent store after a store outage - #503
kinyoklion wants to merge 1 commit into
Conversation
When a persistent store becomes available again after an outage, the FDv2 data system must write its in-memory data back to the store. The status listener that starts the write-back called persistent_store_outage_recovery from a block given to define_singleton_method, where self is the listener object, so every call raised NoMethodError and the write-back never ran. The listener now calls the method through a Method object. The write-back also left out deleted items, because InMemoryFeatureStoreV2#all filters them, so a flag deleted during the outage lost its tombstone. Store#commit now writes deleted items too. Tombstones now carry the key as a String, as other items do; with a Symbol key, the DynamoDB store wrote and deleted the same item in one batch and the write-back failed. The contract test service now advertises persistent-data-store-recovery and passes the Consul DSN to the Consul store, so that the store recovery tests run and the Consul tests use the store address that the harness gives. Generated-by: termag
There was a problem hiding this comment.
Risk: medium.
Automated risk review (dry run): Medium
This PR would not be automatically approved. Human approval required.
Why: Persistent-store write-back after an FDv2 outage is a data-correctness change in lib/ldclient-rb/impl/data_system/fdv2.rb and lib/ldclient-rb/impl/data_store/store.rb (tombstones and the recovery listener), so this is not a narrow low-risk fix. Human review is needed, and no reviewers were assigned.
If you disagree with this review, share this PR in #feedback-agent-approvals or submit a changes-requested review explaining why.
Sent by Cursor Approval Agent: Dry-Run PR Approver
keelerm84
left a comment
There was a problem hiding this comment.
The three fixes look correct: the bound Method for the recovery listener, deleted items in the write back, and String keys on tombstones. Both new specs fail with the lib changes reverted. I found nothing blocking in the diff itself. Before merge, please rebase on main and re-run the store recovery contract tests (see item 3). Findings, most severe first:
1. Write back blocks evaluations for its whole duration. Inline comment on store.rb.
2. The remaining DynamoDB contract failure is the test service's AWS client config. Inline comment on client_entity.rb.
3. A persistent write failure inside apply interacts badly with main. This is pre-existing, but it matters once the branch is rebased. When init or upsert raises in set_basis or apply_delta, the rescue in apply skips send_change_events and the changeset broadcast. Memory is already updated, so flag change listeners miss every change made during an outage. On main, #518 also makes apply return false in this case. The synchronizer then reports INTERRUPTED / STORE_ERROR and does not set ready, and an initializer falls through to the next one. The 17/18 contract result was measured on the pre-#518 base. A fix is to rescue the persistent write inside set_basis and apply_delta (the wrapper has already marked the store unavailable), then send the events and report success.
4. TXNS 1.4.3.2: file data is written back after an outage. The root cause is pre-existing, but this PR makes the write back reachable. Synchronizer changesets always apply with persist = true (fdv2.rb, @store.apply(update.change_set, true)). The file synchronizer's basis sets persist: false, but only the initializer path honours it. A scratch test with a file synchronizer and a read_write store, wiped and then recovered, writes the file flag back on this branch and not on main. The harness service spec says file data must not be written to the store, "including after a store outage". Ruby does not advertise file-data-source, so the contract tests do not catch this. Normal mirroring of file data has the same root cause.
5. A write back that always fails retries with no backoff (minor). An item over the DynamoDB or Consul size limit fails init every time. available? still succeeds, so the cycle repeats about every 0.5s: the poller reports available, the full dataset is written, the write fails, and the store is marked unavailable again. This meets the spec, but backoff or a rate-limited log would be kinder.
6. Write back can run after stop (minor). A recovery task already queued on the shared executor still runs during shutdown, after @store.close. commit does not check whether the store is closed, so init reaches a stopped store. The window is narrow.
7. commit gates on @persist, not on the memory store being initialized (minor, pre-existing). apply_delta can set @persist without a basis. Nothing does that today, but && @memory_store.initialized? in the guard would make TXNS 1.4.3.1 explicit.
8. DynamoDBUtil.batch_write_requests ignores UnprocessedItems (pre-existing). Under throttling, an init or write back can drop items and still report success.
Test gaps:
- Nothing tests
all_including_deletedagainstalldirectly. - The new
store_spectest uses the raw stub store, so it does not go throughFeatureStoreClientWrapperV2and the sorter with deserialized tombstones. - No unit test covers 1.4.4 (write back fails, poller, second write back). The new FDv2 spec calls
update_statusdirectly.
Checked, no issue found:
- Symbol/String key consistency across Redis, Consul, DynamoDB, CachingStoreWrapper, the sorter, and in-memory lookups.
- Tombstones reaching evaluation or listeners.
- Snapshot consistency against a concurrent
apply, since both hold the Store lock. - The Consul DSN mapping, including a nil DSN and one that is already a URL.
| all_data[kind] = @memory_store.all(kind) | ||
| all_data[kind] = @memory_store.all_including_deleted(kind) | ||
| end | ||
| @persistent_store.init(all_data) |
There was a problem hiding this comment.
Write back blocks evaluations. commit holds @lock across this network write. Every evaluation reaches get_active_store, which takes the same lock. So variation, all_flags_state and the rest block for the whole write back. That is a full Redis MULTI, sequential Consul transactions, or sequential DynamoDB batches, and on a flaky store it can run to AWS retry and timeout lengths.
apply already writes to the store under this lock, but recovery was unreachable before this PR. A fix that keeps 1.4.3.3 is to make get_active_store lock-free (the ivar is a single reference). Another is to take the snapshot under @lock and do the write under a separate persistence lock that apply also takes.
| 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( |
There was a problem hiding this comment.
Likely cause of the remaining failure (dynamodb/store recovery/retries write back after another failure). This client uses the default AWS retry settings. The harness v3 service spec (persistent-data-store-recovery) requires a failed write to surface as an error well within one second, and lists MaxAttempts 1 for the Go and Node AWS SDKs. With retries, the client replays the batch that was cut, and the replay succeeds after the restore. The batch's deletes were planned before the second ghost key was injected, so that key survives.
Something like retry_limit: 0 (or retry_mode: 'standard', max_attempts: 1), plus short http_open_timeout / http_read_timeout, should make the test valid. I have not run it.


After a persistent store outage, the FDv2 data system never wrote its in-memory data back to the store. The status listener calls
persistent_store_outage_recoveryfrom a block given todefine_singleton_method, whereselfis the listener, so each call raised NoMethodError, which the broadcaster logged and dropped. The listener now calls the method through a Method object. The write-back also left out deleted items, becauseInMemoryFeatureStoreV2#allfilters them, and tombstones carried a Symbol key, which made the DynamoDB store put and delete the same item in one batch. Both are fixed. The contract test service now advertisespersistent-data-store-recoveryand gives the harness Consul DSN to the Consul store; without these the store recovery tests do not run, and the Consul ones bypass the harness store proxy.Still failing:
persistent data store/dynamodb/store recovery/retries write back after another failure. The AWS client retries the cut write-back request itself and reports success, so the SDK never starts a fresh write-back. Whether client retries should count as a failed write-back is a separate question.Testing: the new specs fail without the change; the v3 store recovery contract tests go from 2/18 to 17/18 passing.
SDK-3704
Opened from termag
Note
Overview
Fixes FDv2 persistent store recovery so in-memory state is written back when the store comes online with stale data, and so recovery listeners actually run.
The outage listener now invokes
persistent_store_outage_recoverythrough a boundMethodinstead of calling it on the listener object (which previously raisedNoMethodErrorand skipped write-back).Store#commitpersists deletions by using newall_including_deletedand encoding delete tombstones with string keys, preventing resurrected items and DynamoDB put/delete conflicts.Contract harness changes advertise
persistent-data-store-recoveryand normalize Consul DSNs tohttp://host:portURLs. Specs cover tombstone commits and status-driven recovery write-back.Reviewed by Cursor Bugbot for commit cb9c099. Bugbot is set up for automated code reviews on this repo. Configure here.