use of com.pingcap.tikv.region.RegionManager in project tispark by pingcap.
the class ClientUtils method groupKeysByRegion.
/**
* Group by list of keys according to its region
*
* @param keys keys
* @return a mapping of keys and their region
*/
public static Map<TiRegion, List<ByteString>> groupKeysByRegion(RegionManager regionManager, List<ByteString> keys, BackOffer backoffer, boolean sorted) {
Map<TiRegion, List<ByteString>> groups = new HashMap<>();
Stream<ByteString> keyStream = keys.stream();
if (!sorted) {
keyStream = keys.stream().sorted((k1, k2) -> FastByteComparisons.compareTo(k1.toByteArray(), k2.toByteArray()));
}
TiRegion lastRegion = null;
for (ByteString key : keyStream.toArray(ByteString[]::new)) {
if (lastRegion == null || !lastRegion.contains(key)) {
lastRegion = regionManager.getRegionByKey(key, backoffer);
}
groups.computeIfAbsent(lastRegion, k -> new ArrayList<>()).add(key);
}
return groups;
}
use of com.pingcap.tikv.region.RegionManager 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;
}
Aggregations