From 70c8038649067ba086211d4b212e7b9b674eb9df Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 2 Feb 2021 11:26:59 +0100 Subject: [PATCH] cache: speed up clearing large caches Signed-off-by: Vicent Marti --- go/cache/ristretto/policy.go | 30 +++++++++++++---------- go/vt/vtgate/executor_vschema_ddl_test.go | 8 +++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/go/cache/ristretto/policy.go b/go/cache/ristretto/policy.go index aa3c0b8c26b..b20b903309c 100644 --- a/go/cache/ristretto/policy.go +++ b/go/cache/ristretto/policy.go @@ -68,20 +68,24 @@ func newPolicy(numCounters, maxCost int64) policy { type defaultPolicy struct { sync.Mutex - admit *tinyLFU - evict *sampledLFU - itemsCh chan []uint64 - stop chan struct{} - isClosed bool - metrics *Metrics + admit *tinyLFU + evict *sampledLFU + itemsCh chan []uint64 + stop chan struct{} + isClosed bool + metrics *Metrics + numCounters int64 + maxCost int64 } func newDefaultPolicy(numCounters, maxCost int64) *defaultPolicy { p := &defaultPolicy{ - admit: newTinyLFU(numCounters), - evict: newSampledLFU(maxCost), - itemsCh: make(chan []uint64, 3), - stop: make(chan struct{}), + admit: newTinyLFU(numCounters), + evict: newSampledLFU(maxCost), + itemsCh: make(chan []uint64, 3), + stop: make(chan struct{}), + numCounters: numCounters, + maxCost: maxCost, } go p.processItems() return p @@ -246,8 +250,8 @@ func (p *defaultPolicy) Cost(key uint64) int64 { func (p *defaultPolicy) Clear() { p.Lock() - p.admit.clear() - p.evict.clear() + p.admit = newTinyLFU(p.numCounters) + p.evict = newSampledLFU(p.maxCost) p.Unlock() } @@ -412,6 +416,6 @@ func (p *tinyLFU) reset() { func (p *tinyLFU) clear() { p.incrs = 0 - p.door.Clear() p.freq.Clear() + p.door.Clear() } diff --git a/go/vt/vtgate/executor_vschema_ddl_test.go b/go/vt/vtgate/executor_vschema_ddl_test.go index b6346761cbe..2e974c47cdb 100644 --- a/go/vt/vtgate/executor_vschema_ddl_test.go +++ b/go/vt/vtgate/executor_vschema_ddl_test.go @@ -58,14 +58,14 @@ func waitForVindex(t *testing.T, ks, name string, watch chan *vschemapb.SrvVSche t.Errorf("vschema was not updated as expected") } - // Wait up to 10ms until the vindex manager gets notified of the update + // Wait up to 100ms until the vindex manager gets notified of the update for i := 0; i < 10; i++ { vschema := executor.vm.GetCurrentSrvVschema() vindex, ok := vschema.Keyspaces[ks].Vindexes[name] if ok { return vschema, vindex } - time.Sleep(time.Millisecond) + time.Sleep(10 * time.Millisecond) } t.Fatalf("updated vschema did not contain %s", name) @@ -75,7 +75,7 @@ func waitForVindex(t *testing.T, ks, name string, watch chan *vschemapb.SrvVSche func waitForVschemaTables(t *testing.T, ks string, tables []string, executor *Executor) *vschemapb.SrvVSchema { t.Helper() - // Wait up to 10ms until the vindex manager gets notified of the update + // Wait up to 100ms until the vindex manager gets notified of the update for i := 0; i < 10; i++ { vschema := executor.vm.GetCurrentSrvVschema() gotTables := []string{} @@ -87,7 +87,7 @@ func waitForVschemaTables(t *testing.T, ks string, tables []string, executor *Ex if reflect.DeepEqual(tables, gotTables) { return vschema } - time.Sleep(time.Millisecond) + time.Sleep(10 * time.Millisecond) } t.Fatalf("updated vschema did not contain tables %v", tables)