Commit 9e7454c
Replace SubscribeEvictions with IRecordTriggers.OnEvict for heap-size tracking
Garnet's `CacheSizeTracker` used `LogAccessor.SubscribeEvictions` to drive a
`LogSizeTracker.OnNext` observer that, on every page eviction, allocated a
scan iterator and walked the page to sum `MemoryUtils.CalculateHeapMemorySize`
over each non-null / non-closed record. This is heavyweight for a hot path
(buffer-pool allocation, iterator bookkeeping, epoch resume/suspend, and a
virtual dispatch per page).
This PR migrates the per-page heap-size decrement onto the per-record
`IRecordTriggers.OnEvict` hook introduced in #1695, which the object
allocator already walks during `EvictRecordsInRange`. That collapses the
"scan iterator + observer + sum" path into a single per-record callback
directly on the record we would have visited anyway.
### Tsavorite changes
- Add `EvictionSource { MainLog, ReadCache }` and thread it through
`IRecordTriggers.OnEvict`, `IStoreFunctions.OnEvict`, and
`IAllocator.EvictRecordsInRange`. Garnet uses this to route decrements to
the correct counter (`AddHeapSize` vs `AddReadCacheHeapSize`).
- Change `IRecordTriggers.CallOnEvict` / `IStoreFunctions.CallOnEvict` from
a property to `CallOnEvict(EvictionSource)` so the allocator can skip the
per-record eviction walk entirely when the application has no work on
that side (e.g. a read-cache-only budget should not force walks of the
main-log allocator).
- Add `AllocatorSettings.IsReadCache` and `AllocatorBase.IsReadCache`, set
to `true` by `Tsavorite.cs` when constructing the read-cache allocator.
This is the cleanest way to distinguish the two allocators at
`OnPagesClosedWorker` time without relying on the `evictCallback`
sentinel, and is used to pass the correct `EvictionSource` to `CallOnEvict`.
- `AllocatorBase.EvictPageForRecovery` routes through the per-record
`EvictRecordsInRange` when `storeFunctions.CallOnEvict(source)` is set.
The legacy `MemoryPageScan(observer)` path is preserved as a fallback
for consumers that still use `SubscribeEvictions`.
- Collapse `AllocatorBase`'s constructor to accept `AllocatorSettings`
directly instead of unpacking individual fields at each concrete
allocator (`ObjectAllocatorImpl`, `SpanByteAllocatorImpl`,
`TsavoriteLogAllocatorImpl`).
- Tighten `ObjectAllocatorImpl.EvictRecordsInRange` to match
`ObjectScanIterator`'s single-page invariant: clip `stopAddress` to the
start page, bail on `offset == 0`, and document that both callers
(`OnPagesClosedWorker`, `EvictPageForRecovery`) hand single-page ranges.
### Garnet changes
- `GarnetRecordTriggers.CallOnEvict(EvictionSource)` returns true only for
the sides that are actually configured (`mainLogTracker` and/or
`readCacheTracker` non-null), avoiding pointless per-record walks on the
untracked allocator.
- `GarnetRecordTriggers.OnEvict(ref LogRecord, EvictionSource)` computes
`MemoryUtils.CalculateHeapMemorySize(in logRecord)` and dispatches to
`AddHeapSize(-size)` or `AddReadCacheHeapSize(-size)` based on the
source. This goes through the standard asserting path — the counter must
never undershoot zero.
- `CacheSizeTracker.Initialize` replaces the two `SubscribeEvictions`
calls with `SetLogSizeTracker` calls so the fast-path size tracking
during `TryCopyToTail`, `TryCopyToReadCache`, and object-page growth
(`UpdateSize`, `IncrementSize`) continues to work unchanged.
### MainStore heap-tracking fix (root cause of negative counter)
Routing the eviction decrement through the asserting `AddHeapSize` path
surfaced a pre-existing gap: `MainStore` record-creation paths never
emitted a positive heap-size bump when a record's key or value spilled to
overflow (large inline field backed by a heap-allocated byte array). The
legacy observer path masked this silently because `OnNext` did a raw
decrement with no assertion — the counter quietly went negative on every
HLL sparse→dense transition (≈-12 KB per key) and accumulated drift
elsewhere.
`MainStore` now emits balanced heap accounting at all create/update/delete
entry points that go through a typed function callback rather than the
shared `SessionFunctionsUtils` writer (which already tracks deltas):
- `RMWMethods.PostInitialUpdater`:
`+logRecord.CalculateHeapMemorySize()` for the freshly-created record.
- `RMWMethods.PostCopyUpdater`: `+dstLogRecord.CalculateHeapMemorySize()`
for the new record only. The source is not subtracted — the
`TSourceLogRecord` may be an in-memory main-log record (whose heap is
tracked in `mainLogTracker` and will leak upward by a bounded sealed-
source amount, parity with `ObjectStore`'s `ClearSource=false` branch
and the legacy observer path), a read-cache record (heap lives in
`readCacheTracker`, unrelated to this write), or a pending-IO
`DiskLogRecord` (heap never counted in any tracker). Subtracting
unconditionally would undercount in the last two cases and drive the
counter negative.
- `RMWMethods.InPlaceUpdater`: `GetValueHeapMemorySize()` pre/post delta
on `Succeeded`, so value-heap changes (e.g. APPEND, SETRANGE, or any
path that triggers `ReallocateValueOverflow` /
`ConvertInlineToOverflow` / `ConvertOverflowToInline`) are tracked.
- `UpsertMethods.PostInitialWriter` (all three overloads):
`+logRecord.CalculateHeapMemorySize()` for SET-style inserts that
create a new record through the Upsert path.
- `DeleteMethods.InPlaceDeleter`:
`-logRecord.CalculateHeapMemorySize()` before the wrapper sets
Tombstone. After tombstone is set, `CalculateHeapMemorySize` short-
circuits to zero and `EvictRecordsInRange` skips the record, so
without this decrement the creation-side increments would leak for
every DEL of an overflow record.
The `HyperLogLogPFADD_LTM` suite is the regression that first surfaced
this — HLL sparse→dense goes through CopyUpdater and allocates ≈12 KB of
overflow for the dense representation. With the fix the counter stays
balanced and the assertion `heapSize.Total >= 0` holds throughout.
### Parity
Behavior at every record state encountered during page eviction is
bit-for-bit identical to the prior `SubscribeEvictions` path:
| Record state | Old path (iterator) | New path (per-record) | Delta |
| ------------------------------------------------ | ---------------------------------- | ---------------------------- | ----- |
| Valid, !Sealed, !Tombstone, !IsNull | yielded → -CalculateHeapMemorySize | OnEvict → -Calculate... | same |
| `IsNull` | skipped by iterator | skipped by filter | 0 |
| `SkipOnScan` (Invalid or Sealed) | skipped by iterator | skipped by filter | 0 |
| `Tombstone` (post-delete, already ValueIsInline) | yielded → 0 via `!Info.Tombstone` | skipped by filter → 0 | 0 |
### Hot-path cost
Fast paths (SET / GET / INCR on inline-sized values) pay only a few
aggressive-inlined inline-bit checks; `GetValueHeapMemorySize` on an
inline record early-returns 0 before any tracker call, and a
`heap != 0` guard skips the `AddHeapSize` dispatch entirely. Tracker
work occurs only when a record genuinely created, resized, or freed
an overflow/object allocation — which was already a heavyweight event.
### Testing
All on net10.0 Debug:
- `HyperLogLogTests` (incl. `HyperLogLogPFADD_LTM{32,4096}`,
`HyperLogLogTestPFMERGE_LTM_*`) ✓
- `CacheSizeTrackerTests` ✓
- `RespListTests` (incl. `ListPushPopStressTest` ×10),
`RespHashTests`, `RespSetTests`, `RespSortedSetTests`,
`RespEtagTests`, `RespBitmapTests`, `RespTests.Set*`, `RespTests.Del*`:
517/517 ✓
- Clean build on both `Garnet.slnx` and `Tsavorite.slnx` (0 warnings,
0 errors), `dotnet format --verify-no-changes` clean.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 0740ff9 commit 9e7454c
File tree
19 files changed
+183
-46
lines changed- libs
- server/Storage
- Functions
- MainStore
- SizeTracker
- storage/Tsavorite/cs
- src/core
- Allocator
- Index
- StoreFunctions
- Tsavorite
- test
19 files changed
+183
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | | - | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
33 | 46 | | |
34 | 47 | | |
35 | 48 | | |
| |||
49 | 62 | | |
50 | 63 | | |
51 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
52 | 86 | | |
53 | 87 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
31 | 39 | | |
32 | 40 | | |
33 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
398 | 398 | | |
399 | 399 | | |
400 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
401 | 407 | | |
402 | 408 | | |
403 | 409 | | |
| |||
409 | 415 | | |
410 | 416 | | |
411 | 417 | | |
| 418 | + | |
412 | 419 | | |
413 | 420 | | |
414 | 421 | | |
| |||
419 | 426 | | |
420 | 427 | | |
421 | 428 | | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
422 | 432 | | |
423 | 433 | | |
424 | 434 | | |
| |||
1701 | 1711 | | |
1702 | 1712 | | |
1703 | 1713 | | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
1704 | 1726 | | |
1705 | 1727 | | |
1706 | 1728 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
| |||
61 | 67 | | |
62 | 68 | | |
63 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
64 | 74 | | |
65 | 75 | | |
66 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
69 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
80 | | - | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
87 | | - | |
| 88 | + | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| |||
Lines changed: 30 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
205 | 205 | | |
206 | 206 | | |
207 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
208 | 214 | | |
209 | 215 | | |
210 | 216 | | |
| |||
553 | 559 | | |
554 | 560 | | |
555 | 561 | | |
556 | | - | |
557 | | - | |
| 562 | + | |
| 563 | + | |
558 | 564 | | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
559 | 571 | | |
560 | 572 | | |
561 | 573 | | |
| |||
1403 | 1415 | | |
1404 | 1416 | | |
1405 | 1417 | | |
1406 | | - | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
1407 | 1429 | | |
1408 | | - | |
1409 | | - | |
| 1430 | + | |
| 1431 | + | |
1410 | 1432 | | |
1411 | 1433 | | |
1412 | 1434 | | |
| |||
1489 | 1511 | | |
1490 | 1512 | | |
1491 | 1513 | | |
1492 | | - | |
1493 | | - | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
1494 | 1517 | | |
1495 | 1518 | | |
1496 | 1519 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
26 | 32 | | |
27 | 33 | | |
28 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
121 | 124 | | |
122 | 125 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
0 commit comments