Search in sources :

Example 1 with Store

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());
    }
}
Also used : Store(org.tikv.kvproto.Metapb.Store) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with Store

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();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) GrpcException(org.tikv.common.exception.GrpcException) Store(org.tikv.kvproto.Metapb.Store) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 3 with Store

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);
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) Peer(org.tikv.kvproto.Metapb.Peer) Store(org.tikv.kvproto.Metapb.Store)

Example 4 with 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());
    }
}
Also used : Store(org.tikv.kvproto.Metapb.Store) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with Store

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();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) GrpcException(com.pingcap.tikv.exception.GrpcException) Store(org.tikv.kvproto.Metapb.Store) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

Store (org.tikv.kvproto.Metapb.Store)8 Test (org.junit.Test)7 ByteString (com.google.protobuf.ByteString)5 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 GrpcException (com.pingcap.tikv.exception.GrpcException)1 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 TiRegion (com.pingcap.tikv.region.TiRegion)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GrpcException (org.tikv.common.exception.GrpcException)1 Peer (org.tikv.kvproto.Metapb.Peer)1