Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <[email protected]>

address comment

Signed-off-by: Cabinfever_B <[email protected]>
  • Loading branch information
CabinfeverB committed Dec 20, 2023
1 parent c4d277e commit 713e6ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions internal/locate/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,10 +1503,10 @@ func (mu *regionIndexMu) removeVersionFromCache(oldVer RegionVerID, regionID uin
// It should be protected by c.mu.l.Lock().
// if `invalidateOldRegion` is false, the old region cache should be still valid,
// and it may still be used by some kv requests.
// Moreover, it will return whether the region is stale.
// Moreover, it will return false if the region is stale.
func (mu *regionIndexMu) insertRegionToCache(cachedRegion *Region, invalidateOldRegion bool, shouldCount bool) bool {
newVer := cachedRegion.VerID()
oldVer, ok := mu.latestVersions[cachedRegion.VerID().id]
oldVer, ok := mu.latestVersions[newVer.id]
// There are two or more situations in which the region we got is stale.
// The first case is that the process of getting a region is concurrent.
// The stale region may be returned later due to network reasons.
Expand All @@ -1517,12 +1517,12 @@ func (mu *regionIndexMu) insertRegionToCache(cachedRegion *Region, invalidateOld
logutil.BgLogger().Debug("get stale region",
zap.Uint64("region", newVer.GetID()), zap.Uint64("new-ver", newVer.GetVer()), zap.Uint64("new-conf", newVer.GetConfVer()),
zap.Uint64("old-ver", oldVer.GetVer()), zap.Uint64("old-conf", oldVer.GetConfVer()))
return true
return false
}
// Also check and remove the intersecting regions including the old region.
intersectedRegions, stale := mu.sorted.removeIntersecting(cachedRegion, &newVer)
intersectedRegions, stale := mu.sorted.removeIntersecting(cachedRegion, newVer)
if stale {
return true
return false
}
// Insert the region (won't replace because of above deletion).
mu.sorted.ReplaceOrInsert(cachedRegion)
Expand Down Expand Up @@ -1564,9 +1564,9 @@ func (mu *regionIndexMu) insertRegionToCache(cachedRegion *Region, invalidateOld
}
}
// update related vars.
mu.regions[cachedRegion.VerID()] = cachedRegion
mu.latestVersions[cachedRegion.VerID().id] = newVer
return false
mu.regions[newVer] = cachedRegion
mu.latestVersions[newVer.id] = newVer
return true
}

// searchCachedRegion finds a region from cache by key. Like `getCachedRegion`,
Expand Down
2 changes: 1 addition & 1 deletion internal/locate/region_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ func (s *testRegionCacheWithDelaySuite) TestInsertStaleRegion() {
s.NoError(err)
s.Equal([]byte("b"), r2.StartKey())

stale := s.cache.insertRegionToCache(fakeRegion, true, true)
stale := !s.cache.insertRegionToCache(fakeRegion, true, true)
s.True(stale)

rs, err := s.cache.scanRegionsFromCache(s.bo, []byte(""), []byte(""), 100)
Expand Down
2 changes: 1 addition & 1 deletion internal/locate/sorted_btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int)

// removeIntersecting removes all items that have intersection with the key range of given region.
// If the region itself is in the cache, it's not removed.
func (s *SortedRegions) removeIntersecting(r *Region, verID *RegionVerID) ([]*btreeItem, bool) {
func (s *SortedRegions) removeIntersecting(r *Region, verID RegionVerID) ([]*btreeItem, bool) {
var deleted []*btreeItem
var stale bool
s.b.AscendGreaterOrEqual(newBtreeSearchItem(r.StartKey()), func(item *btreeItem) bool {
Expand Down

0 comments on commit 713e6ec

Please sign in to comment.