Skip to content

Commit

Permalink
Rewind after cacheGC reaches the end (tikv#678)
Browse files Browse the repository at this point in the history
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
  • Loading branch information
sticnarf committed Jan 31, 2023
1 parent 08b3793 commit 00e2070
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 7 additions & 1 deletion internal/locate/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,8 @@ func (c *RegionCache) cacheGC() {
ticker := time.NewTicker(cleanCacheInterval)
defer ticker.Stop()

iterItem := newBtreeSearchItem([]byte(""))
beginning := newBtreeSearchItem([]byte(""))
iterItem := beginning
expired := make([]*btreeItem, cleanRegionNumPerRound)
for {
select {
Expand All @@ -1950,6 +1951,11 @@ func (c *RegionCache) cacheGC() {
})
c.mu.RUnlock()

// Reach the end of the region cache, start from the beginning
if count <= cleanRegionNumPerRound {
iterItem = beginning
}

if len(expired) > 0 {
c.mu.Lock()
for _, item := range expired {
Expand Down
26 changes: 23 additions & 3 deletions internal/locate/region_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,8 @@ func (s *testRegionCacheSuite) TestShouldNotRetryFlashback() {
}

func (s *testRegionCacheSuite) TestBackgroundCacheGC() {
// Prepare 50 regions
regionCnt := 50
// Prepare 100 regions
regionCnt := 100
regions := s.cluster.AllocIDs(regionCnt)
regions = append([]uint64{s.region1}, regions...)
peers := [][]uint64{{s.peer1, s.peer2}}
Expand Down Expand Up @@ -1642,6 +1642,26 @@ func (s *testRegionCacheSuite) TestBackgroundCacheGC() {
s.cache.mu.RLock()
defer s.cache.mu.RUnlock()
return len(s.cache.mu.regions) == remaining
}, 2*time.Second, 200*time.Millisecond)
}, 3*time.Second, 200*time.Millisecond)
s.checkCache(remaining)

// Make another part of the regions stale
remaining = 0
s.cache.mu.Lock()
now = time.Now().Unix()
for verID, r := range s.cache.mu.regions {
if verID.id%3 == 1 {
atomic.StoreInt64(&r.lastAccess, now-regionCacheTTLSec-10)
} else {
remaining++
}
}
s.cache.mu.Unlock()

s.Eventually(func() bool {
s.cache.mu.RLock()
defer s.cache.mu.RUnlock()
return len(s.cache.mu.regions) == remaining
}, 3*time.Second, 200*time.Millisecond)
s.checkCache(remaining)
}

0 comments on commit 00e2070

Please sign in to comment.