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());
}
}
}
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;
}
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);
}
}
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);
}
}
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) {
}
}
Aggregations