From 56d3b452e4355931ee2383b82826e75d94b3635e Mon Sep 17 00:00:00 2001 From: you06 Date: Mon, 6 Feb 2023 15:32:21 +0800 Subject: [PATCH] support non-block read region Signed-off-by: you06 --- internal/locate/region_cache.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index bf969b5a54..296a696066 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -969,6 +969,20 @@ func (c *RegionCache) LocateKey(bo *retry.Backoffer, key []byte) (*KeyLocation, }, nil } +// TryLocateKey searches for the region and range that the key is located, but return nil when region miss or invalid. +func (c *RegionCache) TryLocateKey(key []byte) *KeyLocation { + r := c.tryFindRegionByKey(key, false) + if r == nil { + return nil + } + return &KeyLocation{ + Region: r.VerID(), + StartKey: r.StartKey(), + EndKey: r.EndKey(), + Buckets: r.getStore().buckets, + } +} + // LocateEndKey searches for the region and range that the key is located. // Unlike LocateKey, start key of a region is exclusive and end key is inclusive. func (c *RegionCache) LocateEndKey(bo *retry.Backoffer, key []byte) (*KeyLocation, error) { @@ -1016,6 +1030,14 @@ func (c *RegionCache) findRegionByKey(bo *retry.Backoffer, key []byte, isEndKey return r, nil } +func (c *RegionCache) tryFindRegionByKey(key []byte, isEndKey bool) (r *Region) { + r = c.searchCachedRegion(key, isEndKey) + if r == nil || r.checkNeedReloadAndMarkUpdated() { + return nil + } + return r +} + // OnSendFailForTiFlash handles send request fail logic for tiflash. func (c *RegionCache) OnSendFailForTiFlash(bo *retry.Backoffer, store *Store, region RegionVerID, prev *metapb.Region, scheduleReload bool, err error, skipSwitchPeerLog bool) { r := c.GetCachedRegionWithRLock(region)