Skip to content
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 @@ -75,9 +75,13 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might add some comments about this update since the scan has some implicit side effects. In addition, this issue might occur in

TiRegion loadCurrentRegionToCache() throws GrpcException {
, the raw scan of client-java. We might add some mock tests for this interface, at least a TODO should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have add comment and fix it in RawScanIterator. I think test it is not easy, so a TODO is added.

return region;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/tikv/common/region/RegionStoreClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ public List<KvPair> scan(
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()
Expand All @@ -365,6 +362,10 @@ public List<KvPair> scan(
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);
}
Expand Down Expand Up @@ -1253,6 +1254,9 @@ public List<KvPair> rawScan(BackOffer backOffer, ByteString key, int limit, bool
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();
Expand Down