Add in-memory cache for getLastEntryInLedger to reduce RocksDB getFloor CPU cost
Motivation
EntryLocationIndex.getLastEntryInLedgerInternal() calls locationsDb.getFloor() on every invocation, which is an expensive RocksDB seek operation. When a large number of ledgers query their last entry concurrently — for example, during a Pulsar broker failover where thousands of topics are loaded by another broker and send get-LAC requests — this causes severe CPU saturation on the bookie.
In a production Pulsar cluster with 3000+ topics and a bookie configured with 1 CPU, a single broker OOM event triggered massive concurrent getFloor() calls that throttled the bookie CPU for over 1 hour.
The existing WriteCache already caches last entry IDs for recent unflushed entries, but once entries are flushed to RocksDB, every getLastEntryInLedger() call falls through to the database with no caching layer.
Changes
Add a bounded in-memory ConcurrentLongLongHashMap cache in EntryLocationIndex that maps ledgerId → lastEntryId:
- Read path (
getLastEntryInLedgerInternal): Check the cache before callinggetFloor(). On cache hit, return immediately without touching RocksDB. On cache miss, populate the cache after a
successfulgetFloor()lookup. - Write path (
addLocation): Eagerly update the cache when a newer entry is added, so the cache stays current even with interleaved reads and writes. - Delete path (
delete): Remove the ledger from the cache. - Bounded size: Configurable via
dbStorage_lastEntryCacheMaxSize(default: 10,000). When the cache exceeds this limit and a new ledger needs to be inserted, the cache is cleared and repopulates
naturally with hot ledgers.
Using ConcurrentLongLongHashMap (lock-striped open hash map with primitive long keys/values) ensures minimal overhead on the write-hot path — no boxing, no LRU bookkeeping, no timestamp computation
per update.
Test plan
- [x] Added
testGetLastEntryInLedgerCachecovering cache hit, update on new entry, and invalidation on delete - [x] All existing
EntryLocationIndexTesttests pass
The scenario description and development motivation are well written,
but I think if performance optimization is being done,it would be helpful to add a performance comparison before and after optimization. @hangc0276
I added a JMH benchmark branch to make the cache-size tradeoff easier to evaluate:
https://github.com/void-ptr974/bookkeeper/tree/pr4732-last-entry-cache-benchmark
The benchmark models the mixed workload this PR is targeting:
- 10,000 ledgers
- 128 entries per ledger
- 12 reader threads calling
getLastEntryInLedger - 4 writer threads calling
addLocation - different
dbStorage_lastEntryCacheMaxSizevalues - JMH
readWritegroup, with-prof gc
The numbers below were collected on the same machine with non-forked JMH runs, so they should be read as relative trend data. The baseline is the same benchmark with the last-entry cache disabled, which represents the pre-cache RocksDB getFloor() path.
| cache max size | read latency | allocation | vs baseline |
|---|---|---|---|
| baseline / no cache | ~26.079 us/op | ~120.1 B/op | 1.0x |
| 10000 | ~0.072 us/op | ~0 B/op | ~362x |
| 9500 | ~8.819 us/op | ~38.1 B/op | ~3.0x |
| 9000 | ~10.417 us/op | ~47.0 B/op | ~2.5x |
| 8000 | ~13.562 us/op | ~60.1 B/op | ~1.9x |
| 7000 | ~16.220 us/op | ~69.8 B/op | ~1.6x |
| 5000 | ~19.316 us/op | ~86.7 B/op | ~1.3x |
| 3000 | ~22.244 us/op | ~100.9 B/op | ~1.2x |
| 1000 | ~25.065 us/op | ~113.9 B/op | ~1.0x |
| 256 | ~25.761 us/op | ~118.5 B/op | ~1.0x |
| 32 | ~26.158 us/op | ~119.9 B/op | ~1.0x |
The cache is very effective when it can cover the active ledger working set. Once the configured cache size is materially smaller than the active ledger count, the benefit drops quickly and the workload approaches the RocksDB lookup path again.
This suggests the setting is useful, but the default and documentation should make it clear that the value needs to be sized close to the expected active ledger working set to get the full benefit.
Further optimization is needed. The decision on which version to release it on will be made later.
The "release" label has been removed for now.
cc @AnonHxy @lhotari @hangc0276 @zymap