Skip to content

Commit

Permalink
Optimize region not find (#2575) (#2596)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 6, 2022
1 parent babd8c9 commit e4358bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, TiRegion> regionCache;
private final Map<Long, Store> storeCache;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public List<RangeSplitter.RegionTask> 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,
Expand All @@ -685,7 +685,8 @@ private List<RangeSplitter.RegionTask> handleCopResponse(
Coprocessor.Response response,
List<Coprocessor.KeyRange> ranges,
Queue<SelectResponse> responseQueue,
long startTs) {
long startTs,
TiRegion region) {
boolean forWrite = false;
if (response == null) {
// Send request failed, reasons may:
Expand All @@ -704,6 +705,11 @@ private List<RangeSplitter.RegionTask> 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);
}
Expand Down

0 comments on commit e4358bf

Please sign in to comment.