Search in sources :

Example 1 with RegionTask

use of com.pingcap.tikv.util.RangeSplitter.RegionTask in project tispark by pingcap.

the class RegionUtils method getStoreRegionIdDistribution.

public static Map<Long, List<Long>> getStoreRegionIdDistribution(TiSession session, String databaseName, String tableName) {
    List<RegionTask> tasks = getRegionTasks(session, databaseName, tableName);
    Map<Long, List<Long>> storeMap = new HashMap<>();
    for (RegionTask task : tasks) {
        long regionId = task.getRegion().getId();
        long storeId = task.getStore().getId();
        storeMap.putIfAbsent(storeId, new ArrayList<>());
        storeMap.get(storeId).add(regionId);
    }
    return storeMap;
}
Also used : RegionTask(com.pingcap.tikv.util.RangeSplitter.RegionTask) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with RegionTask

use of com.pingcap.tikv.util.RangeSplitter.RegionTask in project tispark by pingcap.

the class RegionUtils method getRegionDistribution.

public static Map<String, Integer> getRegionDistribution(TiSession session, String databaseName, String tableName) {
    List<RegionTask> tasks = getRegionTasks(session, databaseName, tableName);
    Map<String, Integer> regionMap = new HashMap<>();
    for (RegionTask task : tasks) {
        regionMap.merge(task.getHost() + "_" + task.getStore().getId(), 1, Integer::sum);
    }
    return regionMap;
}
Also used : RegionTask(com.pingcap.tikv.util.RangeSplitter.RegionTask) HashMap(java.util.HashMap)

Example 3 with RegionTask

use of com.pingcap.tikv.util.RangeSplitter.RegionTask 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();
}
Also used : RegionTask(com.pingcap.tikv.util.RangeSplitter.RegionTask) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ArrayList(java.util.ArrayList) TiSession(com.pingcap.tikv.TiSession) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) NoSuchElementException(java.util.NoSuchElementException) Handle(com.pingcap.tikv.key.Handle)

Example 4 with RegionTask

use of com.pingcap.tikv.util.RangeSplitter.RegionTask in project tispark by pingcap.

the class DAGIteratorTest method staleEpochTest.

@Test
public void staleEpochTest() {
    Metapb.Store store = Metapb.Store.newBuilder().setAddress(LOCAL_ADDR + ":" + port).setId(1).setState(Metapb.StoreState.Up).setVersion(Version.RESOLVE_LOCK_V4).build();
    TiTableInfo table = createTable();
    TiDAGRequest req = new TiDAGRequest(PushDownType.NORMAL);
    req.setTableInfo(table);
    req.addRequiredColumn(ColumnRef.create("c1", IntegerType.INT));
    req.addRequiredColumn(ColumnRef.create("c2", StringType.VARCHAR));
    req.setStartTs(new TiTimestamp(0, 1));
    List<KeyRange> keyRanges = ImmutableList.of(createByteStringRange(ByteString.copyFromUtf8("key1"), ByteString.copyFromUtf8("key4")));
    pdServer.addGetRegionResp(GrpcUtils.makeGetRegionResponse(pdServer.getClusterId(), region.getMeta()));
    pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), store));
    server.putError("key1", KVMockServer.STALE_EPOCH);
    CodecDataOutput cdo = new CodecDataOutput();
    IntegerCodec.writeLongFully(cdo, 666, false);
    BytesCodec.writeBytesFully(cdo, "value1".getBytes());
    server.put("key1", cdo.toByteString());
    List<RegionTask> tasks = ImmutableList.of(RegionTask.newInstance(region, store, keyRanges));
    CoprocessorIterator<Row> iter = CoprocessorIterator.getRowIterator(req, tasks, session);
    if (!iter.hasNext()) {
        assertEquals("iterator has next should be true", true, false);
    } else {
        Row r = iter.next();
        SchemaInfer infer = SchemaInfer.create(req);
        assertEquals(r.get(0, infer.getType(0)), 666L);
        assertEquals(r.get(1, infer.getType(1)), "value1");
    }
}
Also used : TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) TiDAGRequest(com.pingcap.tikv.meta.TiDAGRequest) RegionTask(com.pingcap.tikv.util.RangeSplitter.RegionTask) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) CodecDataOutput(com.pingcap.tikv.codec.CodecDataOutput) Row(com.pingcap.tikv.row.Row) SchemaInfer(com.pingcap.tikv.operation.SchemaInfer) Metapb(org.tikv.kvproto.Metapb) MockServerTest(com.pingcap.tikv.MockServerTest) Test(org.junit.Test)

Aggregations

RegionTask (com.pingcap.tikv.util.RangeSplitter.RegionTask)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImmutableList (com.google.common.collect.ImmutableList)1 MockServerTest (com.pingcap.tikv.MockServerTest)1 TiSession (com.pingcap.tikv.TiSession)1 CodecDataOutput (com.pingcap.tikv.codec.CodecDataOutput)1 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 Handle (com.pingcap.tikv.key.Handle)1 TiDAGRequest (com.pingcap.tikv.meta.TiDAGRequest)1 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)1 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)1 SchemaInfer (com.pingcap.tikv.operation.SchemaInfer)1 Row (com.pingcap.tikv.row.Row)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Test (org.junit.Test)1 KeyRange (org.tikv.kvproto.Coprocessor.KeyRange)1 Metapb (org.tikv.kvproto.Metapb)1