Skip to content

Commit

Permalink
fix wrong weightedsize caused by event race
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Oct 29, 2024
1 parent 0ecf1cf commit 252cbf8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cache_correctness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions internal/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 4 additions & 1 deletion internal/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions internal/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}

0 comments on commit 252cbf8

Please sign in to comment.