use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class PDClient method getRegionByKey.
@Override
public TiRegion getRegionByKey(BackOffer backOffer, ByteString key) {
CodecDataOutput cdo = new CodecDataOutput();
BytesCodec.writeBytes(cdo, key.toByteArray());
ByteString encodedKey = cdo.toByteString();
Supplier<GetRegionRequest> request = () -> GetRegionRequest.newBuilder().setHeader(header).setRegionKey(encodedKey).build();
PDErrorHandler<GetRegionResponse> handler = new PDErrorHandler<>(getRegionResponseErrorExtractor, this);
GetRegionResponse resp = callWithRetry(backOffer, PDGrpc.getGetRegionMethod(), request, handler);
return new TiRegion(resp.getRegion(), resp.getLeader(), conf.getIsolationLevel(), conf.getCommandPriority());
}
use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class TiBatchWriteUtils method getRegionByIndex.
public static List<TiRegion> getRegionByIndex(TiSession session, TiTableInfo table, TiIndexInfo index) {
ArrayList<TiRegion> regionList = new ArrayList<>();
Key min = IndexKey.toIndexKey(table.getId(), index.getId());
Key max = min.nextPrefix();
while (min.compareTo(max) < 0) {
TiRegion region = session.getRegionManager().getRegionByKey(min.toByteString());
regionList.add(region);
min = Key.toRawKey(region.getEndKey());
}
return regionList;
}
use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class TiSession method splitRegionAndScatter.
/**
* split region and scatter
*
* @param splitKeys
*/
public void splitRegionAndScatter(List<byte[]> splitKeys, int splitRegionBackoffMS, int scatterRegionBackoffMS, int scatterWaitMS) {
logger.info(String.format("split key's size is %d", splitKeys.size()));
long startMS = System.currentTimeMillis();
// split region
List<TiRegion> newRegions = splitRegion(splitKeys.stream().map(k -> Key.toRawKey(k).toByteString()).collect(Collectors.toList()), ConcreteBackOffer.newCustomBackOff(splitRegionBackoffMS));
// scatter region
for (TiRegion newRegion : newRegions) {
try {
getPDClient().scatterRegion(newRegion, ConcreteBackOffer.newCustomBackOff(scatterRegionBackoffMS));
} catch (Exception e) {
logger.warn(String.format("failed to scatter region: %d", newRegion.getId()), e);
}
}
// wait scatter region finish
if (scatterWaitMS > 0) {
logger.info("start to wait scatter region finish");
long scatterRegionStartMS = System.currentTimeMillis();
for (TiRegion newRegion : newRegions) {
long remainMS = (scatterRegionStartMS + scatterWaitMS) - System.currentTimeMillis();
if (remainMS <= 0) {
logger.warn("wait scatter region timeout");
return;
}
getPDClient().waitScatterRegionFinish(newRegion, ConcreteBackOffer.newCustomBackOff((int) remainMS));
}
} else {
logger.info("skip to wait scatter region finish");
}
long endMS = System.currentTimeMillis();
logger.info("splitRegionAndScatter cost {} seconds", (endMS - startMS) / 1000);
}
use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class TiSession method splitRegion.
private List<TiRegion> splitRegion(List<ByteString> splitKeys, BackOffer backOffer) {
List<TiRegion> regions = new ArrayList<>();
Map<TiRegion, List<ByteString>> groupKeys = groupKeysByRegion(regionManager, splitKeys, backOffer);
for (Map.Entry<TiRegion, List<ByteString>> entry : groupKeys.entrySet()) {
Pair<TiRegion, Metapb.Store> pair = getRegionManager().getRegionStorePairByKey(entry.getKey().getStartKey());
TiRegion region = pair.first;
Metapb.Store store = pair.second;
List<ByteString> splits = entry.getValue().stream().filter(k -> !k.equals(region.getStartKey()) && !k.equals(region.getEndKey())).collect(Collectors.toList());
if (splits.isEmpty()) {
logger.warn("split key equal to region start key or end key. Region splitting is not needed.");
} else {
logger.info("start to split region id={}, split size={}", region.getId(), splits.size());
List<TiRegion> newRegions;
try {
newRegions = getRegionStoreClientBuilder().build(region, store).splitRegion(splits);
} catch (final TiKVException e) {
// retry
logger.warn("ReSplitting ranges for splitRegion", e);
clientBuilder.getRegionManager().invalidateRegion(region);
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
newRegions = splitRegion(splits, backOffer);
}
logger.info("region id={}, new region size={}", region.getId(), newRegions.size());
regions.addAll(newRegions);
}
}
logger.info("splitRegion: return region size={}", regions.size());
return regions;
}
use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class TwoPhaseCommitter method doCommitSecondaryKeysWithRetry.
private void doCommitSecondaryKeysWithRetry(BackOffer backOffer, List<ByteString> keys, int size, long commitTs) throws TiBatchWriteException {
if (keys == null || keys.isEmpty() || size <= 0) {
return;
}
// groups keys by region
Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, keys, backOffer);
List<BatchKeys> batchKeyList = new ArrayList<>();
for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
TiRegion tiRegion = entry.getKey();
this.appendBatchBySize(batchKeyList, tiRegion, entry.getValue(), false, null);
}
for (BatchKeys batchKeys : batchKeyList) {
doCommitSecondaryKeySingleBatchWithRetry(backOffer, batchKeys, commitTs);
}
}
Aggregations