Skip to content

feat: Drop persistent-store cache after FDv2 in-memory store init#373

Open
keelerm84 wants to merge 2 commits intov7from
mk/sdk-2221/drop-persistent-cache
Open

feat: Drop persistent-store cache after FDv2 in-memory store init#373
keelerm84 wants to merge 2 commits intov7from
mk/sdk-2221/drop-persistent-cache

Conversation

@keelerm84
Copy link
Copy Markdown
Member

@keelerm84 keelerm84 commented May 8, 2026

Summary

With FDv2 the in-memory store retains every flag and segment, so the persistent-store wrapper's go-cache instance becomes dead weight once setBasis flips the active store to memory. This change drops the cache at that point to reclaim the in-memory footprint and to eliminate the CacheForever leak.

  • persistentDataStoreWrapper now holds its cache behind atomic.Pointer[cache.Cache]. Every method does a single Load at the top of the call and works with that snapshot, so concurrent reads never block on a mutex and the previous status-poller-vs-Store.mu race is closed by atomic semantics. DropCache is a one-shot Swap(nil) followed by Flush on the returned cache; safe to call multiple times.
  • The FDv2 store coordinator captures any DropCache-capable persistent store via an interface{ DropCache() } assertion and invokes the drop in setBasis, between s.active = s.memoryStore and the persistent Init() call. Order matters: dropping first means the wrapper's Init skips the cache-rebuild loop on the cache we are about to discard. Custom subsystems.DataStore implementations that don't satisfy the interface continue to work unchanged.
  • Godoc on CacheTime / CacheSeconds / CacheForever / NoCaching now notes that under FDv2 the cache only governs the bootstrap window and may be deprecated in a future major version. No public API change.
  • New tests cover the wrapper's drop semantics (idempotency, write-through after drop) and the coordinator's invocation (drop on full payload, no drop on deltas, no-op for non-cache-dropping stores). All -race clean.

Note

Medium Risk
Touches core data-store caching and initialization paths, adding atomic cache swapping and new lifecycle behavior; mistakes could cause stale reads or missed cache writes during concurrency.

Overview
Drops the persistent-store in-process cache once FDv2 switches to the in-memory store, reclaiming memory and avoiding long-lived CacheForever caches after initialization.

The persistent store wrapper now keeps its go-cache behind an atomic.Pointer and adds DropCache() (also called on Close()), updating all cache-using methods to operate on a single loaded snapshot and to tolerate concurrent cache removal.

The FDv2 Store coordinator detects persistent stores that implement DropCache() and calls it during full-basis initialization (not deltas) before persisting via Init(). Tests were added/expanded to cover cache dropping semantics and coordinator invocation, and persistent-store cache configuration docs now clarify it only affects the bootstrap window under FDv2.

Reviewed by Cursor Bugbot for commit c245085. Bugbot is set up for automated code reviews on this repo. Configure here.

keelerm84 added 2 commits May 8, 2026 10:17
With FDv2 the in-memory store retains every flag and segment, so the
persistent-store wrapper's cache becomes dead weight once setBasis flips
the active store to memory. Drop the cache at that point to reclaim the
in-memory footprint and to eliminate the CacheForever leak.

The wrapper now holds its cache behind atomic.Pointer[cache.Cache]; each
method does a single Load at the top and works with that snapshot, and
DropCache is a one-shot Swap. The store coordinator captures any
DropCache-capable persistent store via interface assertion and invokes
the drop between the active-store switch and the persistent Init call,
so the wrapper's Init does not rebuild a cache we are about to discard.
Apply De Morgan's law to two conditional expressions in Init and Upsert
that staticcheck (QF1001) flagged, and remove the unused hasInfiniteCache
helper. Behavior is unchanged.
@keelerm84 keelerm84 marked this pull request as ready for review May 8, 2026 14:35
@keelerm84 keelerm84 requested a review from a team as a code owner May 8, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant