Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache: fix race when clearning a cache #261

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ loop:
for {
select {
case i := <-c.setBuf:
if i.wg != nil {
i.wg.Done()
continue
}
if i.flag != itemUpdate {
// In itemUpdate, the value is already set in the store. So, no need to call
// onEvict here.
Expand Down
31 changes: 31 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,37 @@ func init() {
bucketDurationSecs = 1
}

func TestBlockOnClear(t *testing.T) {
c, err := NewCache(&Config{
NumCounters: 100,
MaxCost: 10,
BufferItems: 64,
Metrics: false,
})
require.NoError(t, err)
defer c.Close()

done := make(chan struct{})

go func() {
for i := 0; i < 10; i++ {
c.Wait()
}
close(done)
}()

for i := 0; i < 10; i++ {
c.Clear()
}

select {
case <-done:
// We're OK
case <-time.After(1 * time.Second):
t.Fatalf("timed out while waiting on cache")
}
}

// Regression test for bug https://github.com/dgraph-io/ristretto/issues/167
func TestDropUpdates(t *testing.T) {
originalSetBugSize := setBufSize
Expand Down