use of com.pingcap.tikv.key.Handle in project tispark by pingcap.
the class RangeSplitter method createTask.
private void createTask(int startPos, int endPos, long tableId, List<Handle> handles, Pair<TiRegion, Metapb.Store> regionStorePair, ImmutableList.Builder<RegionTask> regionTasks) {
List<KeyRange> newKeyRanges = new ArrayList<>(endPos - startPos + 1);
Handle startHandle = handles.get(startPos);
Handle endHandle = startHandle;
for (int i = startPos + 1; i < endPos; i++) {
Handle curHandle = handles.get(i);
if (endHandle.next().equals(curHandle)) {
endHandle = curHandle;
} else {
newKeyRanges.add(makeCoprocRange(RowKey.toRowKey(tableId, startHandle).toByteString(), RowKey.toRowKey(tableId, endHandle.next()).toByteString()));
startHandle = curHandle;
endHandle = startHandle;
}
}
newKeyRanges.add(makeCoprocRange(RowKey.toRowKey(tableId, startHandle).toByteString(), RowKey.toRowKey(tableId, endHandle.next()).toByteString()));
regionTasks.add(new RegionTask(regionStorePair.first, regionStorePair.second, newKeyRanges));
}
use of com.pingcap.tikv.key.Handle in project tispark by pingcap.
the class RangeSplitterTest method groupByAndSortHandlesByRegionIdTest.
@Test
public void groupByAndSortHandlesByRegionIdTest() {
final long tableId = 1;
List<Handle> handles = new ArrayList<>();
handles.add(new IntHandle(1));
handles.add(new IntHandle(5));
handles.add(new IntHandle(4));
handles.add(new IntHandle(3));
handles.add(new IntHandle(10));
handles.add(new IntHandle(11));
handles.add(new IntHandle(12));
handles.add(new IntHandle(2));
handles.add(new IntHandle(100));
handles.add(new IntHandle(101));
handles.add(new IntHandle(99));
handles.add(new IntHandle(88));
handles.add(new IntHandle(-1));
handles.add(new IntHandle(-255));
handles.add(new IntHandle(-100));
handles.add(new IntHandle(-99));
handles.add(new IntHandle(-98));
handles.add(new IntHandle(Long.MIN_VALUE));
handles.add(new IntHandle(8960));
handles.add(new IntHandle(8959));
handles.add(new IntHandle(19999));
handles.add(new IntHandle(15001));
handles.add(new IntHandle(99999999999L));
handles.add(new IntHandle(Long.MAX_VALUE));
MockRegionManager mgr = new MockRegionManager(ImmutableList.of(keyRangeByHandle(tableId, null, Status.EQUAL, -100L, Status.EQUAL), keyRangeByHandle(tableId, -100L, Status.EQUAL, 10L, Status.GREATER), keyRangeByHandle(tableId, 10L, Status.GREATER, 50L, Status.EQUAL), keyRangeByHandle(tableId, 50L, Status.EQUAL, 100L, Status.GREATER), keyRangeByHandle(tableId, 100L, Status.GREATER, 9000L, Status.LESS), keyRangeByHandle(tableId, 0x2300L, /*8960*/
Status.LESS, 16000L, Status.EQUAL), keyRangeByHandle(tableId, 16000L, Status.EQUAL, null, Status.EQUAL)));
TLongObjectHashMap<List<Handle>> result = new TLongObjectHashMap<>();
RangeSplitter.newSplitter(mgr).groupByAndSortHandlesByRegionId(tableId, handles).forEach((k, v) -> result.put(k.first.getId(), v));
assertEquals(2, result.get(0).size());
assertEquals(10, result.get(1).size());
assertEquals(2, result.get(2).size());
assertEquals(3, result.get(3).size());
assertEquals(2, result.get(4).size());
assertEquals(2, result.get(5).size());
assertEquals(3, result.get(6).size());
}
use of com.pingcap.tikv.key.Handle in project tispark by pingcap.
the class RangeSplitterTest method splitAndSortHandlesByRegionTest.
@Test
public void splitAndSortHandlesByRegionTest() {
final long tableId = 1;
List<Handle> handles = new ArrayList<>();
handles.add(new IntHandle(1));
handles.add(new IntHandle(5));
handles.add(new IntHandle(4));
handles.add(new IntHandle(3));
handles.add(new IntHandle(10));
handles.add(new IntHandle(2));
handles.add(new IntHandle(100));
handles.add(new IntHandle(101));
handles.add(new IntHandle(99));
handles.add(new IntHandle(88));
handles.add(new IntHandle(-1));
handles.add(new IntHandle(-255));
handles.add(new IntHandle(-100));
handles.add(new IntHandle(-99));
handles.add(new IntHandle(-98));
handles.add(new IntHandle(Long.MIN_VALUE));
handles.add(new IntHandle(8960));
handles.add(new IntHandle(8959));
handles.add(new IntHandle(19999));
handles.add(new IntHandle(15001));
MockRegionManager mgr = new MockRegionManager(ImmutableList.of(keyRangeByHandle(tableId, null, Status.EQUAL, -100L, Status.EQUAL), keyRangeByHandle(tableId, -100L, Status.EQUAL, 10L, Status.GREATER), keyRangeByHandle(tableId, 10L, Status.GREATER, 50L, Status.EQUAL), keyRangeByHandle(tableId, 50L, Status.EQUAL, 100L, Status.GREATER), keyRangeByHandle(tableId, 100L, Status.GREATER, 9000L, Status.LESS), keyRangeByHandle(tableId, 0x2300L, /*8960*/
Status.LESS, 16000L, Status.EQUAL), keyRangeByHandle(tableId, 16000L, Status.EQUAL, null, Status.EQUAL)));
RangeSplitter s = RangeSplitter.newSplitter(mgr);
List<RangeSplitter.RegionTask> tasks = new ArrayList<>(s.splitAndSortHandlesByRegion(ImmutableList.of(tableId), handles));
tasks.sort((l, r) -> {
Long regionIdLeft = l.getRegion().getId();
Long regionIdRight = r.getRegion().getId();
return regionIdLeft.compareTo(regionIdRight);
});
// [-INF, -100): [Long.MIN_VALUE, Long.MIN_VALUE + 1), [-255, -254)
assertEquals(tasks.get(0).getRegion().getId(), 0);
assertEquals(tasks.get(0).getRanges().size(), 2);
assertEquals(tasks.get(0).getRanges().get(0), keyRangeByHandle(tableId, Long.MIN_VALUE, Long.MIN_VALUE + 1));
assertEquals(tasks.get(0).getRanges().get(1), keyRangeByHandle(tableId, -255L, -254L));
// [-100, 10.x): [-100, -97), [-1, 0), [1, 6), [10, 11)
assertEquals(tasks.get(1).getRegion().getId(), 1);
assertEquals(tasks.get(1).getRanges().size(), 4);
assertEquals(tasks.get(1).getRanges().get(0), keyRangeByHandle(tableId, -100L, -97L));
assertEquals(tasks.get(1).getRanges().get(1), keyRangeByHandle(tableId, -1L, 0L));
assertEquals(tasks.get(1).getRanges().get(2), keyRangeByHandle(tableId, 1L, 6L));
assertEquals(tasks.get(1).getRanges().get(3), keyRangeByHandle(tableId, 10L, 11L));
// [10.x, 50): empty
// [50, 100.x): [88, 89) [99, 101)
assertEquals(tasks.get(2).getRegion().getId(), 3);
assertEquals(tasks.get(2).getRanges().size(), 2);
assertEquals(tasks.get(2).getRanges().get(0), keyRangeByHandle(tableId, 88L, 89L));
assertEquals(tasks.get(2).getRanges().get(1), keyRangeByHandle(tableId, 99L, 101L));
// [100.x, less than 8960): [101, 102) [8959, 8960)
assertEquals(tasks.get(3).getRegion().getId(), 4);
assertEquals(tasks.get(3).getRanges().size(), 2);
assertEquals(tasks.get(3).getRanges().get(0), keyRangeByHandle(tableId, 101L, 102L));
assertEquals(tasks.get(3).getRanges().get(1), keyRangeByHandle(tableId, 8959L, 8960L));
// [less than 8960, 16000): [9000, 9001), [15001, 15002)
assertEquals(tasks.get(4).getRegion().getId(), 5);
assertEquals(tasks.get(4).getRanges().size(), 2);
assertEquals(tasks.get(4).getRanges().get(0), keyRangeByHandle(tableId, 8960L, 8961L));
assertEquals(tasks.get(4).getRanges().get(1), keyRangeByHandle(tableId, 15001L, 15002L));
// [16000, INF): [19999, 20000)
assertEquals(tasks.get(5).getRegion().getId(), 6);
assertEquals(tasks.get(5).getRanges().size(), 1);
assertEquals(tasks.get(5).getRanges().get(0), keyRangeByHandle(tableId, 19999L, 20000L));
}
use of com.pingcap.tikv.key.Handle in project tispark by pingcap.
the class IndexScanIterator method hasNext.
@Override
public boolean hasNext() {
try {
if (rowIterator == null) {
TiSession session = snapshot.getSession();
while (handleIterator.hasNext()) {
List<Handle> handles = feedBatch();
batchCount++;
completionService.submit(() -> {
List<RegionTask> tasks = new ArrayList<>();
List<Long> ids = dagReq.getPrunedPhysicalIds();
tasks.addAll(RangeSplitter.newSplitter(session.getRegionManager()).splitAndSortHandlesByRegion(ids, handles));
return CoprocessorIterator.getRowIterator(dagReq, tasks, session);
});
}
while (batchCount > 0) {
rowIterator = completionService.take().get();
batchCount--;
if (rowIterator.hasNext()) {
return true;
}
}
}
if (rowIterator == null) {
return false;
}
} catch (Exception e) {
throw new TiClientInternalException("Error reading rows from handle", e);
}
return rowIterator.hasNext();
}
use of com.pingcap.tikv.key.Handle in project tispark by pingcap.
the class RangeSplitter method groupByAndSortHandlesByRegionId.
/**
* Group by a list of handles by the handles' region, handles will be sorted.
*
* @param tableId Table id used for the handle
* @param handles Handle list
* @return <Region, HandleList> map
*/
public Map<Pair<TiRegion, Metapb.Store>, List<Handle>> groupByAndSortHandlesByRegionId(long tableId, List<Handle> handles) {
TLongObjectHashMap<List<Handle>> regionHandles = new TLongObjectHashMap<>();
TLongObjectHashMap<Pair<TiRegion, Metapb.Store>> idToRegionStorePair = new TLongObjectHashMap<>();
Map<Pair<TiRegion, Metapb.Store>, List<Handle>> result = new HashMap<>();
handles.sort(Handle::compare);
byte[] endKey = null;
TiRegion curRegion = null;
List<Handle> handlesInCurRegion = new ArrayList<>();
for (Handle curHandle : handles) {
RowKey key = RowKey.toRowKey(tableId, curHandle);
if (endKey == null || (endKey.length != 0 && FastByteComparisons.compareTo(key.getBytes(), endKey) >= 0)) {
if (curRegion != null) {
regionHandles.put(curRegion.getId(), handlesInCurRegion);
handlesInCurRegion = new ArrayList<>();
}
Pair<TiRegion, Metapb.Store> regionStorePair = regionManager.getRegionStorePairByKey(ByteString.copyFrom(key.getBytes()));
curRegion = regionStorePair.first;
idToRegionStorePair.put(curRegion.getId(), regionStorePair);
endKey = curRegion.getEndKey().toByteArray();
}
handlesInCurRegion.add(curHandle);
}
if (!handlesInCurRegion.isEmpty()) {
regionHandles.put(curRegion.getId(), handlesInCurRegion);
}
regionHandles.forEachEntry((k, v) -> {
Pair<TiRegion, Metapb.Store> regionStorePair = idToRegionStorePair.get(k);
result.put(regionStorePair, v);
return true;
});
return result;
}
Aggregations