Skip to content

Commit

Permalink
Clear intersecting regions in the cache when inserting a region
Browse files Browse the repository at this point in the history
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
  • Loading branch information
sticnarf committed Aug 11, 2022
1 parent 516cfcd commit e80022c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/locate/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,11 @@ func (c *RegionCache) insertRegionToCache(cachedRegion *Region) {
if !ok || latest.GetVer() < newVer.GetVer() || latest.GetConfVer() < newVer.GetConfVer() {
c.mu.latestVersions[cachedRegion.VerID().id] = newVer
}
// The intersecting regions in the cache are probably stale, clear them.
deleted := c.mu.sorted.removeIntersecting(cachedRegion)
for _, r := range deleted {
c.removeVersionFromCache(r.cachedRegion.VerID(), r.cachedRegion.GetID())
}
}

// searchCachedRegion finds a region from cache by key. Like `getCachedRegion`,
Expand Down
22 changes: 22 additions & 0 deletions internal/locate/sorted_btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int)
return regions
}

// 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) []*btreeItem {
var deleted []*btreeItem
s.b.AscendGreaterOrEqual(newBtreeSearchItem(r.StartKey()), func(item btree.Item) bool {
region := item.(*btreeItem).cachedRegion
// Skip the item that is equal to the given region.
if region.VerID() == r.VerID() {
return true
}
if len(r.EndKey()) > 0 && bytes.Compare(region.StartKey(), r.EndKey()) >= 0 {
return false
}
deleted = append(deleted, item.(*btreeItem))
return true
})
for _, item := range deleted {
s.b.Delete(item)
}
return deleted
}

// Clear removes all items from the btree.
func (s *SortedRegions) Clear() {
s.b.Clear(false)
Expand Down
21 changes: 21 additions & 0 deletions internal/locate/sorted_btree_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int)
return regions
}

// 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) []*btreeItem {
var deleted []*btreeItem
s.b.AscendGreaterOrEqual(newBtreeSearchItem(r.StartKey()), func(item *btreeItem) bool {
// Skip the item that is equal to the given region.
if item.cachedRegion.VerID() == r.VerID() {
return true
}
if len(r.EndKey()) > 0 && bytes.Compare(item.cachedRegion.StartKey(), r.EndKey()) >= 0 {
return false
}
deleted = append(deleted, item)
return true
})
for _, item := range deleted {
s.b.Delete(item)
}
return deleted
}

// Clear removes all items from the btree.
func (s *SortedRegions) Clear() {
s.b.Clear(false)
Expand Down

0 comments on commit e80022c

Please sign in to comment.