diff --git a/cache_correctness_test.go b/cache_correctness_test.go index 1664b61..e10b473 100644 --- a/cache_correctness_test.go +++ b/cache_correctness_test.go @@ -65,6 +65,8 @@ func getSet(t *testing.T, entrypool bool) { require.Equal(t, client.Len(), int(di.TotalCount())) require.True(t, di.TotalWeight() <= int64(size+size/10)) + require.True(t, di.TotalWeight() >= int64(size-15)) + require.Equal(t, di.TotalWeight(), di.WeightedSize) require.Equal(t, di.WindowWeight, di.WindowWeightField) require.Equal(t, di.ProbationWeight, di.ProbationWeightField) require.Equal(t, di.ProtectedWeight, di.ProtectedWeightField) @@ -135,6 +137,7 @@ func getSetDeleteExpire(t *testing.T, entrypool bool) { require.Equal(t, client.Len(), int(di.TotalCount())) require.True(t, di.TotalWeight() <= int64(size+size/10)) + require.Equal(t, di.TotalWeight(), di.WeightedSize) require.Equal(t, di.WindowWeight, di.WindowWeightField) require.Equal(t, di.ProbationWeight, di.ProbationWeightField) require.Equal(t, di.ProtectedWeight, di.ProtectedWeightField) diff --git a/internal/entry.go b/internal/entry.go index 1240b0f..a749a3e 100644 --- a/internal/entry.go +++ b/internal/entry.go @@ -199,8 +199,15 @@ func (e *Entry[K, V]) PolicyWeight() int64 { } func (e *Entry[K, V]) Position() string { - if e.meta.prev != nil { - return "MAIN" + switch { + case e.flag.IsWindow(): + return "WINDOW" + case e.flag.IsProbation(): + return "PROBATION" + case e.flag.IsProtected(): + return "PROTECTED" + case e.flag.IsRemoved(): + return "REMOVED" } return "UNKNOWN" } diff --git a/internal/store.go b/internal/store.go index 662aecc..c99ec63 100644 --- a/internal/store.go +++ b/internal/store.go @@ -601,7 +601,7 @@ func (s *Store[K, V]) sinkWrite(item WriteBufItem[K, V]) { } s.policy.sketch.Add(item.hash) - entry.policyWeight = item.costChange + entry.policyWeight += item.costChange s.policy.Set(entry) case REMOVE: @@ -968,6 +968,7 @@ func (s *Store[K, V]) Recover(version uint64, reader io.Reader) error { } type debugInfo struct { + WeightedSize int64 WindowWeight int64 WindowWeightField int64 WindowCount int64 @@ -981,6 +982,7 @@ type debugInfo struct { func (i debugInfo) String() string { final := "" + final += fmt.Sprintf("policy weighted size %d\n", i.WeightedSize) final += fmt.Sprintf("total items in window list %d\n", i.WindowCount) final += fmt.Sprintf("sum of weight of window list %v\n", i.WindowWeight) final += fmt.Sprintf("total items in probation list %d\n", i.ProbationCount) @@ -1027,6 +1029,7 @@ func (s *Store[K, V]) DebugInfo() debugInfo { }) return debugInfo{ + WeightedSize: int64(s.policy.weightedSize), WindowWeight: windowSum, WindowWeightField: int64(s.policy.window.Len()), WindowCount: windowCount, diff --git a/internal/store_test.go b/internal/store_test.go index aedc739..4267fd7 100644 --- a/internal/store_test.go +++ b/internal/store_test.go @@ -168,3 +168,36 @@ func TestStore_GetExpire(t *testing.T) { cachedNow := store.timerwheel.clock.NowNanoCached() require.True(t, cachedNow > testNow) } + +func TestStore_SinkWritePolicyWeight(t *testing.T) { + store := NewStore[int, int](10000, false, true, nil, nil, nil, 0, 0, nil) + defer store.Close() + + entry := &Entry[int, int]{key: 1, value: 1} + h := store.hasher.hash(1) + + // wright change 5 -> 1 -> 8 + store.sinkWrite(WriteBufItem[int, int]{ + entry: entry, + costChange: -4, + code: UPDATE, + hash: h, + }) + + store.sinkWrite(WriteBufItem[int, int]{ + entry: entry, + costChange: 5, + code: NEW, + hash: h, + }) + + store.sinkWrite(WriteBufItem[int, int]{ + entry: entry, + costChange: 7, + code: UPDATE, + hash: h, + }) + + require.Equal(t, 8, int(store.policy.weightedSize)) + +}