Search in sources :

Example 11 with TiRegion

use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.

the class LockResolverTest method versionTest.

private void versionTest(boolean hasLock, boolean blockingRead) {
    for (int i = 0; i < 26; i++) {
        ByteString key = ByteString.copyFromUtf8(String.valueOf((char) ('a' + i)));
        TiRegion tiRegion = session.getRegionManager().getRegionByKey(key);
        RegionStoreClient client = builder.build(tiRegion);
        BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
        if (blockingRead) {
            try {
                ByteString v = client.get(backOffer, key, session.getTimestamp().getVersion());
                if (hasLock && i == 3) {
                    // key "d" should be locked
                    fail();
                } else {
                    assertEquals(String.valueOf((char) ('a' + i)), v.toStringUtf8());
                }
            } catch (GrpcException e) {
                assertEquals(e.getMessage(), "retry is exhausted.");
            }
        } else {
            ByteString v = client.get(backOffer, key, session.getTimestamp().getVersion());
            assertEquals(String.valueOf((char) ('a' + i)), v.toStringUtf8());
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) GrpcException(com.pingcap.tikv.exception.GrpcException) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient)

Example 12 with TiRegion

use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.

the class ClientUtils method groupKeysByRegion.

/**
 * Group by list of keys according to its region
 *
 * @param keys keys
 * @return a mapping of keys and their region
 */
public static Map<TiRegion, List<ByteString>> groupKeysByRegion(RegionManager regionManager, List<ByteString> keys, BackOffer backoffer, boolean sorted) {
    Map<TiRegion, List<ByteString>> groups = new HashMap<>();
    Stream<ByteString> keyStream = keys.stream();
    if (!sorted) {
        keyStream = keys.stream().sorted((k1, k2) -> FastByteComparisons.compareTo(k1.toByteArray(), k2.toByteArray()));
    }
    TiRegion lastRegion = null;
    for (ByteString key : keyStream.toArray(ByteString[]::new)) {
        if (lastRegion == null || !lastRegion.contains(key)) {
            lastRegion = regionManager.getRegionByKey(key, backoffer);
        }
        groups.computeIfAbsent(lastRegion, k -> new ArrayList<>()).add(key);
    }
    return groups;
}
Also used : Set(java.util.Set) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TiRegion(com.pingcap.tikv.region.TiRegion) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RegionManager(com.pingcap.tikv.region.RegionManager) List(java.util.List) TiKVException(com.pingcap.tikv.exception.TiKVException) Stream(java.util.stream.Stream) Map(java.util.Map) Queue(java.util.Queue) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with TiRegion

use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.

the class PDClientTest method testGetRegionByIdAsync.

@Test
public void testGetRegionByIdAsync() throws Exception {
    byte[] startKey = new byte[] { 1, 0, 2, 4 };
    byte[] endKey = new byte[] { 1, 0, 2, 5 };
    int confVer = 1026;
    int ver = 1027;
    pdServer.addGetRegionByIDResp(GrpcUtils.makeGetRegionResponse(pdServer.getClusterId(), GrpcUtils.makeRegion(1, encodeKey(startKey), encodeKey(endKey), GrpcUtils.makeRegionEpoch(confVer, ver), GrpcUtils.makePeer(1, 10), GrpcUtils.makePeer(2, 20))));
    try (PDClient client = session.getPDClient()) {
        TiRegion r = client.getRegionByIDAsync(defaultBackOff(), 0).get();
        assertEquals(r.getStartKey(), ByteString.copyFrom(startKey));
        assertEquals(r.getEndKey(), ByteString.copyFrom(endKey));
        assertEquals(r.getRegionEpoch().getConfVer(), confVer);
        assertEquals(r.getRegionEpoch().getVersion(), ver);
        assertEquals(r.getLeader().getId(), 1);
        assertEquals(r.getLeader().getStoreId(), 10);
    }
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) Test(org.junit.Test)

Example 14 with TiRegion

use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.

the class PDClientTest method testGetRegionByKey.

@Test
public void testGetRegionByKey() throws Exception {
    byte[] startKey = new byte[] { 1, 0, 2, 4 };
    byte[] endKey = new byte[] { 1, 0, 2, 5 };
    int confVer = 1026;
    int ver = 1027;
    pdServer.addGetRegionResp(GrpcUtils.makeGetRegionResponse(pdServer.getClusterId(), GrpcUtils.makeRegion(1, encodeKey(startKey), encodeKey(endKey), GrpcUtils.makeRegionEpoch(confVer, ver), GrpcUtils.makePeer(1, 10), GrpcUtils.makePeer(2, 20))));
    try (PDClient client = session.getPDClient()) {
        TiRegion r = client.getRegionByKey(defaultBackOff(), ByteString.EMPTY);
        assertEquals(r.getStartKey(), ByteString.copyFrom(startKey));
        assertEquals(r.getEndKey(), ByteString.copyFrom(endKey));
        assertEquals(r.getRegionEpoch().getConfVer(), confVer);
        assertEquals(r.getRegionEpoch().getVersion(), ver);
        assertEquals(r.getLeader().getId(), 1);
        assertEquals(r.getLeader().getStoreId(), 10);
    }
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) Test(org.junit.Test)

Example 15 with TiRegion

use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.

the class RegionManagerTest method getRegionByKey.

@Test
public void getRegionByKey() {
    ByteString startKey = ByteString.copyFrom(new byte[] { 1 });
    ByteString endKey = ByteString.copyFrom(new byte[] { 10 });
    ByteString searchKey = ByteString.copyFrom(new byte[] { 5 });
    ByteString searchKeyNotExists = ByteString.copyFrom(new byte[] { 11 });
    int confVer = 1026;
    int ver = 1027;
    long regionId = 233;
    pdServer.addGetRegionResp(GrpcUtils.makeGetRegionResponse(pdServer.getClusterId(), GrpcUtils.makeRegion(regionId, GrpcUtils.encodeKey(startKey.toByteArray()), GrpcUtils.encodeKey(endKey.toByteArray()), GrpcUtils.makeRegionEpoch(confVer, ver), GrpcUtils.makePeer(1, 10), GrpcUtils.makePeer(2, 20))));
    TiRegion region = mgr.getRegionByKey(startKey);
    assertEquals(region.getId(), regionId);
    TiRegion regionToSearch = mgr.getRegionByKey(searchKey);
    assertEquals(region, regionToSearch);
    // since we set just one rpc response
    try {
        mgr.getRegionByKey(searchKeyNotExists);
        fail();
    } catch (Exception ignored) {
    }
}
Also used : ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TiRegion (com.pingcap.tikv.region.TiRegion)43 ByteString (com.google.protobuf.ByteString)19 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)17 ArrayList (java.util.ArrayList)16 BackOffer (com.pingcap.tikv.util.BackOffer)10 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)10 List (java.util.List)10 Test (org.junit.Test)9 TiKVException (com.pingcap.tikv.exception.TiKVException)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)8 Metapb (org.tikv.kvproto.Metapb)8 GrpcException (com.pingcap.tikv.exception.GrpcException)7 KeyException (com.pingcap.tikv.exception.KeyException)7 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)6 RegionException (com.pingcap.tikv.exception.RegionException)4 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)4 Key (com.pingcap.tikv.key.Key)4 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)4