Skip to content

Commit f1aeb35

Browse files
committed
txn: fix scan missing endKey in V2 API
- Add scan(backOffer, startKey, endKey, version, keyOnly) overload - Use codec.encodeRange() to set both startKey and endKey - Decode response KvPairs via codec.decodeKvPairs() - Pass rangeEndKey through ScanIterator Signed-off-by: lilei <lilei592@jd.com>
1 parent c12047a commit f1aeb35

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/main/java/org/tikv/common/operation/iterator/ConcreteScanIterator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
7676
try (RegionStoreClient client = builder.build(startKey)) {
7777
client.setTimeout(conf.getScanTimeout());
7878
BackOffer backOffer = ConcreteBackOffer.newScannerNextMaxBackOff();
79-
currentCache = client.scan(backOffer, startKey, version);
79+
currentCache = client.scan(backOffer, startKey, rangeEndKey, version, keyOnly);
8080
// If we get region before scan, we will use region from cache which
8181
// may have wrong end key. This may miss some regions that split from old region.
8282
// Client will get the newest region during scan. So we need to
@@ -115,7 +115,8 @@ public boolean hasNext() {
115115
// for last batch to be processed, we have to check if
116116
return !processingLastBatch
117117
|| current == null
118-
|| (hasEndKey && Key.toRawKey(current.getKey()).compareTo(endKey) < 0);
118+
|| !hasEndKey
119+
|| (Key.toRawKey(current.getKey()).compareTo(endKey) < 0);
119120
}
120121

121122
@Override

src/main/java/org/tikv/common/operation/iterator/ScanIterator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public abstract class ScanIterator implements Iterator<Kvrpcpb.KvPair> {
3535
protected final RegionStoreClientBuilder builder;
3636
protected List<Kvrpcpb.KvPair> currentCache;
3737
protected ByteString startKey;
38+
protected ByteString rangeEndKey;
3839
protected int index = -1;
3940
protected int limit;
4041
protected boolean keyOnly;
@@ -52,7 +53,8 @@ public abstract class ScanIterator implements Iterator<Kvrpcpb.KvPair> {
5253
int limit,
5354
boolean keyOnly) {
5455
this.startKey = requireNonNull(startKey, "start key is null");
55-
this.endKey = Key.toRawKey(requireNonNull(endKey, "end key is null"));
56+
this.rangeEndKey = requireNonNull(endKey, "end key is null");
57+
this.endKey = Key.toRawKey(this.rangeEndKey);
5658
this.hasEndKey = !endKey.isEmpty();
5759
this.limit = limit;
5860
this.keyOnly = keyOnly;
@@ -106,9 +108,11 @@ boolean cacheLoadFails() {
106108
}
107109
// notify last batch if lastKey is greater than or equal to endKey
108110
// if startKey is empty, it indicates +∞
109-
if (hasEndKey && lastKey.compareTo(endKey) >= 0 || startKey.isEmpty()) {
111+
if (hasEndKey && lastKey.compareTo(endKey) >= 0) {
110112
processingLastBatch = true;
111113
startKey = null;
114+
} else if (startKey.isEmpty()) {
115+
processingLastBatch = true;
112116
}
113117
} catch (Exception e) {
114118
throw new TiClientInternalException("Error scanning data from region.", e);

src/main/java/org/tikv/common/region/RegionStoreClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,23 @@ private List<KvPair> handleBatchGetResponse(
337337

338338
public List<KvPair> scan(
339339
BackOffer backOffer, ByteString startKey, long version, boolean keyOnly) {
340+
return scan(backOffer, startKey, ByteString.EMPTY, version, keyOnly);
341+
}
342+
343+
public List<KvPair> scan(
344+
BackOffer backOffer, ByteString startKey, ByteString endKey, long version, boolean keyOnly) {
340345
boolean forWrite = false;
341346
while (true) {
347+
Pair<ByteString, ByteString> range = codec.encodeRange(startKey, endKey);
348+
342349
Supplier<ScanRequest> request =
343350
() ->
344351
ScanRequest.newBuilder()
345352
.setContext(
346353
makeContext(
347354
getResolvedLocks(version), this.storeType, backOffer.getSlowLog()))
348-
.setStartKey(codec.encodeKey(startKey))
355+
.setStartKey(range.first)
356+
.setEndKey(range.second)
349357
.setVersion(version)
350358
.setKeyOnly(keyOnly)
351359
.setLimit(getConf().getScanBatchSize())
@@ -367,7 +375,7 @@ public List<KvPair> scan(
367375
region = regionManager.getRegionByKey(startKey, backOffer);
368376

369377
if (handleScanResponse(backOffer, resp, version, forWrite)) {
370-
return resp.getPairsList();
378+
return codec.decodeKvPairs(resp.getPairsList());
371379
}
372380
}
373381
}

0 commit comments

Comments
 (0)