From e4358bf4d32215a3fe6d6fbed7001feba873d00a Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 6 Dec 2022 14:12:03 +0800 Subject: [PATCH] Optimize region not find (#2575) (#2596) --- .../com/pingcap/tikv/region/RegionManager.java | 15 +++++++++++++-- .../pingcap/tikv/region/RegionStoreClient.java | 10 ++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tikv-client/src/main/java/com/pingcap/tikv/region/RegionManager.java b/tikv-client/src/main/java/com/pingcap/tikv/region/RegionManager.java index d6fd12803e..e24f5e06a7 100644 --- a/tikv-client/src/main/java/com/pingcap/tikv/region/RegionManager.java +++ b/tikv-client/src/main/java/com/pingcap/tikv/region/RegionManager.java @@ -189,6 +189,10 @@ public void invalidateRegion(TiRegion region) { cache.invalidateRegion(region); } + public void invalidateRange(ByteString startKey, ByteString endKey) { + cache.invalidateRange(startKey,endKey); + } + public static class RegionCache { // private final Map regionCache; private final Map storeCache; @@ -253,19 +257,26 @@ private synchronized TiRegion getRegionFromCache(Key key) { private synchronized void invalidateRange(ByteString startKey, ByteString endKey) { regionCache.remove(makeRange(startKey, endKey)); + if (logger.isDebugEnabled()) { + logger.debug(String.format("invalidateRange success, startKey[%s], endKey[%s]", startKey, endKey)); + } } /** Removes region associated with regionId from regionCache. */ public synchronized void invalidateRegion(TiRegion region) { try { if (logger.isDebugEnabled()) { - logger.debug(String.format("invalidateRegion ID[%s]", region.getId())); + logger.debug(String.format("invalidateRegion ID[%s] start", region.getId())); } TiRegion oldRegion = regionCache.get(getEncodedKey(region.getStartKey())); if (oldRegion != null && oldRegion.equals(region)) { regionCache.remove(makeRange(region.getStartKey(), region.getEndKey())); + if (logger.isDebugEnabled()) { + logger.debug(String.format("invalidateRegion ID[%s] success", region.getId())); + } } - } catch (Exception ignore) { + } catch (Exception e) { + logger.warn("invalidateRegion failed", e); } } diff --git a/tikv-client/src/main/java/com/pingcap/tikv/region/RegionStoreClient.java b/tikv-client/src/main/java/com/pingcap/tikv/region/RegionStoreClient.java index bb38279ce8..238cc6f918 100644 --- a/tikv-client/src/main/java/com/pingcap/tikv/region/RegionStoreClient.java +++ b/tikv-client/src/main/java/com/pingcap/tikv/region/RegionStoreClient.java @@ -673,7 +673,7 @@ public List coprocess( forWrite); Coprocessor.Response resp = callWithRetry(backOffer, TikvGrpc.getCoprocessorMethod(), reqToSend, handler); - return handleCopResponse(backOffer, resp, ranges, responseQueue, startTs); + return handleCopResponse(backOffer, resp, ranges, responseQueue, startTs, region); } // handleCopResponse checks coprocessor Response for region split and lock, @@ -685,7 +685,8 @@ private List handleCopResponse( Coprocessor.Response response, List ranges, Queue responseQueue, - long startTs) { + long startTs, + TiRegion region) { boolean forWrite = false; if (response == null) { // Send request failed, reasons may: @@ -704,6 +705,11 @@ private List handleCopResponse( backOffer.doBackOff( BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(regionError.toString())); logger.warn("Re-splitting region task due to region error:" + regionError.getMessage()); + // we need to invalidate cache when region not find + if (regionError.hasRegionNotFound()) { + logger.info("invalidateRange when Re-splitting region task because of region not find."); + this.regionManager.invalidateRange(region.getStartKey(),region.getEndKey()); + } // Split ranges return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType); }