hangc0276 commented on PR #4732: URL: https://github.com/apache/bookkeeper/pull/4732#issuecomment-7332375827
@merlimat thanks for the detailed review. Pushed 4c28b253d, which addresses all points: 1. **Stale-cache race (main blocker).** The CAS loop closed the read-vs-write race, but a `clear()` in between could still drop the newer writer value and let a stale reader value in (reproduced with a test that pauses `getFloor`). Publishing is now lock-free with a per-stripe write stamp: a writer bumps the stamp (after its batch is flushed) before publishing; a reader that read RocksDB publishes and then verifies the stamp and the deleted set did not change, otherwise it removes its value. This is independent of the eviction policy. Test: `testCacheNeverRegressesAfterClearRace`. 2. **Clear-everything eviction.** Replaced with a bounded Caffeine cache (strict cap, W-TinyLFU, evicting on the calling thread). See the numbers below. I also prototyped "stop inserting when full" and a two-generation cache; "stop inserting" gets stuck at 0% hit rate once the working set moves on, and two-generation is only slightly better than clear-all. 3. **Latency histogram.** Hit/miss counters, no zero-latency samples (unchanged from the previous round). 4. **Logging style.** Uses slog `attr(...)`. 5. **Minor.** `bk_server.conf` documents the setting (default 100000, ~140 bytes/entry measured, i.e. ~14 MB; <= 0 disables); concurrent test added; the ordering comment in `delete()` is updated. Also fixed in the same commit: a deleted ledger could be resurrected in the cache by a late `addLocation` (in-flight flush/compaction) after `delete()` and before `removeOffsetFromDeletedLedgers()`; the cache could report a last entry before its batch was flushed (the cache is now updated after `flush()`); and small `dbStorage_lastEntryCacheMaxSize` values (<= 10) failed to construct the index. New dependency: `com.github.ben-manes.caffeine:caffeine:3.2.3` (annotation-only transitive deps excluded; added to the `LICENSE-*.bin.txt` files; `dev/check-binary-license` passes on the server/all/bkctl tarballs). ### Benchmark `EntryLocationIndexBenchmark` from @void-ptr974's branch (`readWrite` group: 10,000 ledgers x 128 entries, 12 reader + 4 writer threads), with two extra params, the eviction policy and an optional 80/20 skew (80% of reads hit the hottest 10% of ledgers). Mean `getLastEntryInLedger` latency in ns/op; no cache: **17,376** (uniform) / **17,740** (skew). One run per cell on a laptop, 1 warmup + 3 measured iterations of 3 s, so read them as relative numbers. Uniform access: | cache max size | clear-all (before) | Caffeine (this PR) | |---:|---:|---:| | 12000 (>= ledgers) | 12 | 62 | | 9500 | 5501 | 887 | | 8000 | 8693 | 3480 | | 5000 | 13019 | 8674 | | 2000 | 15810 | 13730 | 80/20 skew: | cache max size | clear-all (before) | Caffeine (this PR) | |---:|---:|---:| | 12000 (>= ledgers) | 11 | 51 | | 9500 | 1147 | 190 | | 8000 | 1837 | 721 | | 5000 | 2934 | 1756 | | 2000 | 5800 | 2868 | When the cache covers the active set, a hit costs about 50-60 ns with Caffeine (boxing) instead of about 12 ns, still roughly 300x cheaper than a RocksDB lookup. Below the active set clear-all collapses, while Caffeine degrades gracefully (about 6x faster at 9500 / 10000 ledgers). Hit rates (single-threaded simulation, cap 5000): | scenario | clear-all | Caffeine | |---|---:|---:| | uniform over 10k ledgers | 27.9% | 50.0% (the ideal cap/N) | | 80/20 skew over 10k | 83.0% | 90.0% | | 2k active window drifting over 100k ledgers | 85.4% | 90.0% | | failover storm of 10k ledgers, then steady 2k hot set (cap 1.5k) | 45.9% | 75.0% | -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
