Search in sources :

Example 1 with Peer

use of org.tikv.kvproto.Metapb.Peer in project client-java by tikv.

the class RegionManager method getRegionStorePairByKey.

public Pair<TiRegion, TiStore> getRegionStorePairByKey(ByteString key, TiStoreType storeType, BackOffer backOffer) {
    TiRegion region = getRegionByKey(key, backOffer);
    if (!region.isValid()) {
        throw new TiClientInternalException("Region invalid: " + region);
    }
    TiStore store = null;
    if (storeType == TiStoreType.TiKV) {
        Peer peer = region.getCurrentReplica();
        store = getStoreById(peer.getStoreId(), backOffer);
    } else {
        outerLoop: for (Peer peer : region.getLearnerList()) {
            TiStore s = getStoreById(peer.getStoreId(), backOffer);
            for (Metapb.StoreLabel label : s.getStore().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.invalidateRegion(region);
        }
    }
    return Pair.create(region, store);
}
Also used : TiClientInternalException(org.tikv.common.exception.TiClientInternalException) Peer(org.tikv.kvproto.Metapb.Peer)

Example 2 with Peer

use of org.tikv.kvproto.Metapb.Peer 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 3 with Peer

use of org.tikv.kvproto.Metapb.Peer in project client-java by tikv.

the class TiRegion method getReplicaContext.

public Kvrpcpb.Context getReplicaContext(Set<Long> resolvedLocks, TiStoreType storeType) {
    Peer currentPeer = getCurrentReplica();
    boolean replicaRead = !isLeader(currentPeer) && TiStoreType.TiKV.equals(storeType);
    return getContext(currentPeer, resolvedLocks, replicaRead);
}
Also used : Peer(org.tikv.kvproto.Metapb.Peer)

Example 4 with Peer

use of org.tikv.kvproto.Metapb.Peer in project client-java by tikv.

the class RegionManager method createRegion.

private TiRegion createRegion(Metapb.Region region, Metapb.Peer leader, BackOffer backOffer) {
    List<Metapb.Peer> peers = new ArrayList<>();
    List<TiStore> stores = new ArrayList<>();
    for (Metapb.Peer peer : region.getPeersList()) {
        try {
            stores.add(getStoreById(peer.getStoreId(), backOffer));
            peers.add(peer);
        } catch (Exception e) {
            logger.warn("Store {} not found: {}", peer.getStoreId(), e.toString());
        }
    }
    Metapb.Region newRegion = Metapb.Region.newBuilder().mergeFrom(region).clearPeers().addAllPeers(peers).build();
    return new TiRegion(conf, newRegion, leader, peers, stores);
}
Also used : Peer(org.tikv.kvproto.Metapb.Peer) Peer(org.tikv.kvproto.Metapb.Peer) ArrayList(java.util.ArrayList) Metapb(org.tikv.kvproto.Metapb) InvalidStoreException(org.tikv.common.exception.InvalidStoreException) GrpcException(org.tikv.common.exception.GrpcException) TiClientInternalException(org.tikv.common.exception.TiClientInternalException)

Aggregations

Peer (org.tikv.kvproto.Metapb.Peer)4 TiClientInternalException (org.tikv.common.exception.TiClientInternalException)2 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 ArrayList (java.util.ArrayList)1 GrpcException (org.tikv.common.exception.GrpcException)1 InvalidStoreException (org.tikv.common.exception.InvalidStoreException)1 Metapb (org.tikv.kvproto.Metapb)1 Store (org.tikv.kvproto.Metapb.Store)1