Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize region not find (#2575) #2596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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