[fix #623] region may be missed (#624)

This commit is contained in:
Trafalgar Ricardo Lu 2022-06-30 17:34:23 +08:00 committed by GitHub
parent 3724e87df6
commit 5a757d95e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View File

@ -75,9 +75,13 @@ public class ConcreteScanIterator extends ScanIterator {
TiRegion region;
try (RegionStoreClient client = builder.build(startKey)) {
client.setTimeout(conf.getScanTimeout());
region = client.getRegion();
BackOffer backOffer = ConcreteBackOffer.newScannerNextMaxBackOff();
currentCache = client.scan(backOffer, startKey, version);
// If we get region before scan, we will use region from cache which
// may have wrong end key. This may miss some regions that split from old region.
// Client will get the newest region during scan. So we need to
// update region after scan.
region = client.getRegion();
return region;
}
}

View File

@ -57,6 +57,9 @@ public class RawScanIterator extends ScanIterator {
} else {
try {
currentCache = client.rawScan(backOffer, startKey, limit, keyOnly);
// Client will get the newest region during scan. So we need to
// update region after scan.
region = client.getRegion();
} catch (final TiKVException e) {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
continue;

View File

@ -65,6 +65,7 @@ public abstract class ScanIterator implements Iterator<Kvrpcpb.KvPair> {
*
* @return TiRegion of current data loaded to cache
* @throws GrpcException if scan still fails after backoff
* <p>TODO : Add test to check it correctness
*/
abstract TiRegion loadCurrentRegionToCache() throws GrpcException;

View File

@ -339,9 +339,6 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
BackOffer backOffer, ByteString startKey, long version, boolean keyOnly) {
boolean forWrite = false;
while (true) {
// we should refresh region
region = regionManager.getRegionByKey(startKey, backOffer);
Supplier<ScanRequest> request =
() ->
ScanRequest.newBuilder()
@ -365,6 +362,10 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
version,
forWrite);
ScanResponse resp = callWithRetry(backOffer, TikvGrpc.getKvScanMethod(), request, handler);
// retry may refresh region info
// we need to update region after retry
region = regionManager.getRegionByKey(startKey, backOffer);
if (isScanSuccess(backOffer, resp)) {
return doScan(resp);
}
@ -1253,6 +1254,9 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
regionManager, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null);
RawScanResponse resp =
callWithRetry(backOffer, TikvGrpc.getRawScanMethod(), factory, handler);
// RegionErrorHandler may refresh region cache due to outdated region info,
// This region need to get newest info from cache.
region = regionManager.getRegionByKey(key, backOffer);
return rawScanHelper(resp);
} finally {
requestTimer.observeDuration();