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