Skip to content

Commit

Permalink
Adaptive LRU window (#55)
Browse files Browse the repository at this point in the history
* use single LRU as window

* minor change

* refactor and add some policy tests

* fix tests

* fix tests

* fix cost update

* fix demote bug

* fix demote not run

* add sketch resize test

* fix adaptive should climb check

* fix tlfu tests

* improve policy update schedule

* fix test

* sketch use uint64 counter

* remove atomic from list

* add hash check to policy read if sync pool enabled

* update test name

* update ci

* update ci

* update hit ratio benchmark results

* add twitter cluster52 sample10 result

* fix flaky test

* fix low hit ratio climb not run

* remove wrong rehash function

* minor improvement

* update hit ratio bench results

* fix estimated size

* fix race when using entry pool

* add some comments to policy test

* fix policy updateCost

* fix window evict test

* fix wrong weightedsize caused by event race
  • Loading branch information
Yiling-J authored Oct 29, 2024
1 parent 61cd803 commit e0555fa
Show file tree
Hide file tree
Showing 29 changed files with 1,341 additions and 1,030 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ jobs:
- name: Upload coverage to codecov.io
uses: codecov/codecov-action@v3

test-race-nopool:
name: test-race-nopool
test-correctness-nopool:
name: test-correctness-nopool
strategy:
matrix:
go: ["1.23.x"]
Expand All @@ -59,10 +59,10 @@ jobs:
- uses: actions/checkout@v2

- name: Test
run: go test ./... -run=TestCacheRace_NoPool -count=1 -race
run: go test ./... -run=TestCacheCorrectness_NoPool -count=1 -race

test-race-pool:
name: test-race-pool
test-correctness-pool:
name: test-correctness-pool
strategy:
matrix:
go: ["1.23.x"]
Expand All @@ -76,7 +76,7 @@ jobs:
- uses: actions/checkout@v2

- name: Test
run: go test ./... -run=TestCacheRace_EntryPool -count=1
run: go test ./... -run=TestCacheCorrectness_EntryPool -count=1

test-os:
name: test-os
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.PHONY: test test-race-pool test-race-nopool testx lint bench cover

test:
go test -race -skip=TestCacheRace_ ./...
go test -race -skip=TestCacheCorrectness_ ./...

test-race-pool:
go test ./... -run=TestCacheRace_EntryPool -count=1
test-correct-pool:
go test ./... -run=TestCacheCorrectness_EntryPool -count=1

test-race-nopool:
go test ./... -run=TestCacheRace_NoPool -count=1 -race
test-correct-nopool:
go test ./... -run=TestCacheCorrectness_NoPool -count=1 -race

testx:
go test ./... -v -failfast
Expand All @@ -16,5 +16,5 @@ lint:
golangci-lint run

cover:
go test -timeout 2000s -race -coverprofile=cover.out -coverpkg=./... -skip=TestCacheRace_ ./...
go test -timeout 2000s -race -coverprofile=cover.out -coverpkg=./... -skip=TestCacheCorrectness_ ./...
go tool cover -html=cover.out -o cover.html
Binary file modified benchmarks/results/ds1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added benchmarks/results/mix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified benchmarks/results/oltp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified benchmarks/results/s3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added benchmarks/results/twitter-c52s10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added benchmarks/results/wikicdn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified benchmarks/results/zipf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func (c *Cache[K, V]) Stats() Stats {
return c.store.Stats()
}

// Wait write buffer sync to policy.
func (c *Cache[K, V]) Wait() {
c.store.Wait()
}

type LoadingCache[K comparable, V any] struct {
store *internal.LoadingStore[K, V]
}
Expand Down Expand Up @@ -157,6 +162,11 @@ func (c *LoadingCache[K, V]) Stats() Stats {
return c.store.Stats()
}

// Wait write buffer sync to policy.
func (c *LoadingCache[K, V]) Wait() {
c.store.Wait()
}

// Close closes all goroutines created by cache.
func (c *LoadingCache[K, V]) Close() {
c.store.Close()
Expand Down
21 changes: 9 additions & 12 deletions cache_race_test.go → cache_correctness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ 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)

for i := 0; i < len(di.QueueWeight); i++ {
require.Equal(t, di.QueueWeight[i], di.QueueWeightField[i])
}

client.store.RangeEntry(func(entry *internal.Entry[uint64, uint64]) {
require.Equal(t, entry.Weight(), entry.PolicyWeight(), entry.Position())
})
Expand All @@ -80,11 +79,11 @@ func getSet(t *testing.T, entrypool bool) {
}
}

func TestCacheRace_EntryPool_GetSet(t *testing.T) {
func TestCacheCorrectness_EntryPool_GetSet(t *testing.T) {
getSet(t, true)

}
func TestCacheRace_NoPool_GetSet(t *testing.T) {
func TestCacheCorrectness_NoPool_GetSet(t *testing.T) {
getSet(t, false)

}
Expand Down Expand Up @@ -138,13 +137,11 @@ 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)

for i := 0; i < len(di.QueueWeight); i++ {
require.Equal(t, di.QueueWeight[i], di.QueueWeightField[i])
}

client.store.RangeEntry(func(entry *internal.Entry[uint64, uint64]) {
require.Equal(t, entry.Weight(), entry.PolicyWeight(), entry.Position())
})
Expand All @@ -154,10 +151,10 @@ func getSetDeleteExpire(t *testing.T, entrypool bool) {
}
}

func TestCacheRace_EntryPool_GetSetDeleteExpire(t *testing.T) {
func TestCacheCorrectness_EntryPool_GetSetDeleteExpire(t *testing.T) {
getSetDeleteExpire(t, true)
}

func TestCacheRace_NoPool_GetSetDeleteExpire(t *testing.T) {
func TestCacheCorrectness_NoPool_GetSetDeleteExpire(t *testing.T) {
getSetDeleteExpire(t, false)
}
Loading

0 comments on commit e0555fa

Please sign in to comment.