use of org.tikv.kvproto.Metapb.Store in project client-java by tikv.
the class PDClientMockTest method testGetStore.
@Test
public void testGetStore() throws Exception {
long storeId = 1;
String testAddress = "testAddress";
leader.addGetStoreListener(request -> GrpcUtils.makeGetStoreResponse(leader.getClusterId(), GrpcUtils.makeStore(storeId, testAddress, Metapb.StoreState.Up, GrpcUtils.makeStoreLabel("k1", "v1"), GrpcUtils.makeStoreLabel("k2", "v2"))));
try (PDClient client = session.getPDClient()) {
Store r = client.getStore(defaultBackOff(), storeId);
assertEquals(storeId, r.getId());
assertEquals(testAddress, r.getAddress());
assertEquals(Metapb.StoreState.Up, r.getState());
assertEquals("k1", r.getLabels(0).getKey());
assertEquals("k2", r.getLabels(1).getKey());
assertEquals("v1", r.getLabels(0).getValue());
assertEquals("v2", r.getLabels(1).getValue());
leader.addGetStoreListener(request -> GrpcUtils.makeGetStoreResponse(leader.getClusterId(), GrpcUtils.makeStore(storeId, testAddress, Metapb.StoreState.Tombstone)));
assertEquals(StoreState.Tombstone, client.getStore(defaultBackOff(), storeId).getState());
}
}
use of org.tikv.kvproto.Metapb.Store in project client-java by tikv.
the class PDClientMockTest method testRetryPolicy.
@Test
public void testRetryPolicy() throws Exception {
long storeId = 1024;
ExecutorService service = Executors.newCachedThreadPool();
AtomicInteger i = new AtomicInteger();
leader.addGetStoreListener(request -> {
if (i.getAndIncrement() < 2) {
return null;
} else {
return GrpcUtils.makeGetStoreResponse(leader.getClusterId(), GrpcUtils.makeStore(storeId, "", Metapb.StoreState.Up));
}
});
try (PDClient client = session.getPDClient()) {
Callable<Store> storeCallable = () -> client.getStore(ConcreteBackOffer.newCustomBackOff(5000), 0);
Future<Store> storeFuture = service.submit(storeCallable);
try {
Store r = storeFuture.get(50, TimeUnit.SECONDS);
assertEquals(r.getId(), storeId);
} catch (TimeoutException e) {
fail();
}
// Should fail
AtomicInteger j = new AtomicInteger();
leader.addGetStoreListener(request -> {
if (j.getAndIncrement() < 6) {
return null;
} else {
return GrpcUtils.makeGetStoreResponse(leader.getClusterId(), GrpcUtils.makeStore(storeId, "", Metapb.StoreState.Up));
}
});
try {
client.getStore(defaultBackOff(), 0);
} catch (GrpcException e) {
assertTrue(true);
return;
}
fail();
}
}
use of org.tikv.kvproto.Metapb.Store in project tispark by pingcap.
the class RegionManager method getRegionStorePairByKey.
public Pair<TiRegion, Store> getRegionStorePairByKey(ByteString key, TiStoreType storeType, BackOffer backOffer) {
TiRegion region = cache.getRegionByKey(key, backOffer);
if (region == null) {
throw new TiClientInternalException("Region not exist for key:" + formatBytesUTF8(key));
}
if (!region.isValid()) {
throw new TiClientInternalException("Region invalid: " + region.toString());
}
Store store = null;
if (storeType == TiStoreType.TiKV) {
Peer leader = region.getLeader();
store = cache.getStoreById(leader.getStoreId(), backOffer);
} else {
outerLoop: for (Peer peer : region.getLearnerList()) {
Store s = getStoreById(peer.getStoreId(), backOffer);
for (Metapb.StoreLabel label : s.getLabelsList()) {
if (label.getKey().equals(storeType.getLabelKey()) && label.getValue().equals(storeType.getLabelValue())) {
store = s;
break outerLoop;
}
}
}
if (store == null) {
// clear the region cache so we may get the learner peer next time
cache.invalidateRange(region.getStartKey(), region.getEndKey());
}
}
if (store == null) {
throw new TiClientInternalException("Cannot find valid store on " + storeType + " for region " + region.toString());
}
return Pair.create(region, store);
}
use of org.tikv.kvproto.Metapb.Store in project tispark by pingcap.
the class PDClientTest method testGetStore.
@Test
public void testGetStore() throws Exception {
long storeId = 1;
String testAddress = "testAddress";
pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), GrpcUtils.makeStore(storeId, testAddress, Metapb.StoreState.Up, GrpcUtils.makeStoreLabel("k1", "v1"), GrpcUtils.makeStoreLabel("k2", "v2"))));
try (PDClient client = session.getPDClient()) {
Store r = client.getStore(defaultBackOff(), 0);
assertEquals(r.getId(), storeId);
assertEquals(r.getAddress(), testAddress);
assertEquals(r.getState(), Metapb.StoreState.Up);
assertEquals(r.getLabels(0).getKey(), "k1");
assertEquals(r.getLabels(1).getKey(), "k2");
assertEquals(r.getLabels(0).getValue(), "v1");
assertEquals(r.getLabels(1).getValue(), "v2");
pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), GrpcUtils.makeStore(storeId, testAddress, Metapb.StoreState.Tombstone)));
assertEquals(StoreState.Tombstone, client.getStore(defaultBackOff(), 0).getState());
}
}
use of org.tikv.kvproto.Metapb.Store in project tispark by pingcap.
the class PDClientTest method testRetryPolicy.
@Test
public void testRetryPolicy() throws Exception {
long storeId = 1024;
ExecutorService service = Executors.newCachedThreadPool();
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), GrpcUtils.makeStore(storeId, "", Metapb.StoreState.Up)));
try (PDClient client = session.getPDClient()) {
Callable<Store> storeCallable = () -> client.getStore(ConcreteBackOffer.newCustomBackOff(5000), 0);
Future<Store> storeFuture = service.submit(storeCallable);
try {
Store r = storeFuture.get(50, TimeUnit.SECONDS);
assertEquals(r.getId(), storeId);
} catch (TimeoutException e) {
fail();
}
// Should fail
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(null);
pdServer.addGetStoreResp(GrpcUtils.makeGetStoreResponse(pdServer.getClusterId(), GrpcUtils.makeStore(storeId, "", Metapb.StoreState.Up)));
try {
client.getStore(defaultBackOff(), 0);
} catch (GrpcException e) {
assertTrue(true);
return;
}
fail();
}
}
Aggregations