Skip to content

Commit 47e318a

Browse files
iosmanthusti-srebot
authored andcommitted
cherry pick tikv#541 to release-3.1
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
1 parent fdb67bc commit 47e318a

3 files changed

Lines changed: 66 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.tikv.kvproto.Kvrpcpb;
2929

3030
public class RawScanIterator extends ScanIterator {
31+
3132
private final BackOffer scanBackOffer;
3233

3334
public RawScanIterator(
@@ -65,11 +66,12 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
6566
}
6667
}
6768

68-
private boolean notEndOfScan() {
69-
return limit > 0
70-
&& !(processingLastBatch
71-
&& (index >= currentCache.size()
72-
|| Key.toRawKey(currentCache.get(index).getKey()).compareTo(endKey) >= 0));
69+
private boolean endOfScan() {
70+
if (!processingLastBatch) {
71+
return false;
72+
}
73+
ByteString lastKey = currentCache.get(index).getKey();
74+
return !lastKey.isEmpty() && Key.toRawKey(lastKey).compareTo(endKey) >= 0;
7375
}
7476

7577
boolean isCacheDrained() {
@@ -88,7 +90,7 @@ public boolean hasNext() {
8890
return false;
8991
}
9092
}
91-
return notEndOfScan();
93+
return !endOfScan();
9294
}
9395

9496
private Kvrpcpb.KvPair getCurrent() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ boolean cacheLoadFails() {
8888
Key lastKey = Key.EMPTY;
8989
// Session should be single-threaded itself
9090
// so that we don't worry about conf change in the middle
91-
// of a transaction. Otherwise below code might lose data
91+
// of a transaction. Otherwise, below code might lose data
9292
if (currentCache.size() < limit) {
9393
startKey = curRegionEndKey;
9494
lastKey = Key.toRawKey(curRegionEndKey);

src/test/java/org/tikv/raw/RawKVClientTest.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
package org.tikv.raw;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
46

7+
import com.google.common.collect.ImmutableList;
58
import com.google.protobuf.ByteString;
9+
<<<<<<< HEAD
610
import java.io.IOException;
711
import java.util.*;
812
import java.util.concurrent.*;
13+
=======
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.Comparator;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Locale;
20+
import java.util.Map;
21+
import java.util.Optional;
22+
import java.util.Random;
23+
import java.util.TreeMap;
24+
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.ExecutorCompletionService;
26+
import java.util.concurrent.ExecutorService;
27+
import java.util.concurrent.Executors;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
>>>>>>> 97983823c... [close #540] rawkv: fix scan return empty set while exist empty key (#541)
931
import java.util.stream.Collectors;
1032
import org.apache.commons.lang3.RandomStringUtils;
1133
import org.junit.After;
34+
import org.junit.Assert;
1235
import org.junit.Before;
1336
import org.junit.Test;
1437
import org.slf4j.Logger;
@@ -28,6 +51,7 @@
2851
import org.tikv.kvproto.Kvrpcpb;
2952

3053
public class RawKVClientTest extends BaseRawKVTest {
54+
3155
private static final String RAW_PREFIX = "raw_\u0001_";
3256
private static final int KEY_POOL_SIZE = 1000000;
3357
private static final int TEST_CASES = 10000;
@@ -360,6 +384,34 @@ private List<Kvrpcpb.KvPair> rawKeys() {
360384
return client.scan(RAW_START_KEY, RAW_END_KEY);
361385
}
362386

387+
@Test
388+
public void scanTestForIssue540() {
389+
ByteString splitKeyA = ByteString.copyFromUtf8("splitKeyA");
390+
ByteString splitKeyB = ByteString.copyFromUtf8("splitKeyB");
391+
session.splitRegionAndScatter(
392+
ImmutableList.of(splitKeyA.toByteArray(), splitKeyB.toByteArray()));
393+
client.deleteRange(ByteString.EMPTY, ByteString.EMPTY);
394+
395+
client.put(ByteString.EMPTY, ByteString.EMPTY);
396+
client.put(splitKeyA, ByteString.EMPTY);
397+
Assert.assertEquals(0, client.scan(ByteString.EMPTY, 0).size());
398+
Assert.assertEquals(1, client.scan(ByteString.EMPTY, 1).size());
399+
Assert.assertEquals(2, client.scan(ByteString.EMPTY, 2).size());
400+
Assert.assertEquals(2, client.scan(ByteString.EMPTY, 3).size());
401+
402+
client.deleteRange(ByteString.EMPTY, ByteString.EMPTY);
403+
404+
client.put(ByteString.EMPTY, ByteString.EMPTY);
405+
client.put(splitKeyA, ByteString.EMPTY);
406+
client.put(splitKeyA.concat(ByteString.copyFromUtf8("1")), ByteString.EMPTY);
407+
client.put(splitKeyA.concat(ByteString.copyFromUtf8("2")), ByteString.EMPTY);
408+
client.put(splitKeyA.concat(ByteString.copyFromUtf8("3")), ByteString.EMPTY);
409+
client.put(splitKeyB.concat(ByteString.copyFromUtf8("1")), ByteString.EMPTY);
410+
Assert.assertEquals(6, client.scan(ByteString.EMPTY, 7).size());
411+
Assert.assertEquals(0, client.scan(ByteString.EMPTY, -1).size());
412+
client.deleteRange(ByteString.EMPTY, ByteString.EMPTY);
413+
}
414+
363415
@Test
364416
public void validate() {
365417
baseTest(100, 100, 100, 100, false, false, false, false, false);
@@ -449,7 +501,9 @@ private void prepare() {
449501
int i = cnt;
450502
completionService.submit(
451503
() -> {
452-
for (int j = 0; j < base; j++) checkDelete(remainingKeys.get(i * base + j).getKey());
504+
for (int j = 0; j < base; j++) {
505+
checkDelete(remainingKeys.get(i * base + j).getKey());
506+
}
453507
return null;
454508
});
455509
}
@@ -942,6 +996,7 @@ private static ByteString rawValue(String value) {
942996
}
943997

944998
private static class ByteStringComparator implements Comparator<ByteString> {
999+
9451000
@Override
9461001
public int compare(ByteString startKey, ByteString endKey) {
9471002
return FastByteComparisons.compareTo(startKey.toByteArray(), endKey.toByteArray());

0 commit comments

Comments
 (0)