Search in sources :

Example 41 with TiRegion

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

the class PDClientTest method testGetRegionById.

@Test
public void testGetRegionById() 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.getRegionByID(defaultBackOff(), 0);
        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 42 with TiRegion

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

the class RegionManagerTest method getStoreByKey.

@Test
public void getStoreByKey() {
    ByteString startKey = ByteString.copyFrom(new byte[] { 1 });
    ByteString endKey = ByteString.copyFrom(new byte[] { 10 });
    ByteString searchKey = ByteString.copyFrom(new byte[] { 5 });
    String testAddress = "testAddress";
    long storeId = 233;
    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(storeId, 10), GrpcUtils.makePeer(storeId + 1, 20))));
    pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), GrpcUtils.makeStore(storeId, testAddress, Metapb.StoreState.Up, GrpcUtils.makeStoreLabel("k1", "v1"), GrpcUtils.makeStoreLabel("k2", "v2"))));
    Pair<TiRegion, Store> pair = mgr.getRegionStorePairByKey(searchKey);
    assertEquals(pair.first.getId(), regionId);
    assertEquals(pair.first.getId(), storeId);
}
Also used : ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) Store(org.tikv.kvproto.Metapb.Store) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 43 with TiRegion

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

the class LockResolverSITest method cleanLockTest.

@Test
public void cleanLockTest() {
    if (!init) {
        skipTestInit();
        return;
    }
    for (int i = 0; i < 26; i++) {
        String k = String.valueOf((char) ('a' + i));
        TiTimestamp startTs = session.getTimestamp();
        TiTimestamp endTs = session.getTimestamp();
        assertTrue(lockKey(k, k, k, k, false, startTs.getVersion(), endTs.getVersion()));
    }
    List<Mutation> mutations = new ArrayList<>();
    List<String> keys = new ArrayList<>();
    for (int i = 0; i < 26; i++) {
        String k = String.valueOf((char) ('a' + i));
        String v = String.valueOf((char) ('a' + i + 1));
        Mutation m = Mutation.newBuilder().setKey(ByteString.copyFromUtf8(k)).setOp(Op.Put).setValue(ByteString.copyFromUtf8(v)).build();
        mutations.add(m);
        keys.add(k);
    }
    TiTimestamp startTs = session.getTimestamp();
    TiTimestamp endTs = session.getTimestamp();
    boolean res = prewriteString(mutations, startTs.getVersion(), mutations.get(0).getKey().toStringUtf8(), DEFAULT_TTL);
    assertTrue(res);
    res = commitString(keys, startTs.getVersion(), endTs.getVersion());
    assertTrue(res);
    for (int i = 0; i < 26; i++) {
        TiRegion tiRegion = session.getRegionManager().getRegionByKey(ByteString.copyFromUtf8(String.valueOf((char) ('a' + i))));
        RegionStoreClient client = builder.build(tiRegion);
        BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
        ByteString v = client.get(backOffer, ByteString.copyFromUtf8(String.valueOf((char) ('a' + i))), session.getTimestamp().getVersion());
        assertEquals(v.toStringUtf8(), String.valueOf((char) ('a' + i + 1)));
    }
}
Also used : TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) ByteString(com.google.protobuf.ByteString) Mutation(org.tikv.kvproto.Kvrpcpb.Mutation) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) 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