Search in sources :

Example 1 with RegionManager

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;
}
Also used : Set(java.util.Set) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TiRegion(com.pingcap.tikv.region.TiRegion) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RegionManager(com.pingcap.tikv.region.RegionManager) List(java.util.List) TiKVException(com.pingcap.tikv.exception.TiKVException) Stream(java.util.stream.Stream) Map(java.util.Map) Queue(java.util.Queue) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with RegionManager

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;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ChannelFactory(com.pingcap.tikv.util.ChannelFactory) Metapb(org.tikv.kvproto.Metapb) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) Key(com.pingcap.tikv.key.Key) Pair(com.pingcap.tikv.util.Pair) ArrayList(java.util.ArrayList) Map(java.util.Map) BackOffer(com.pingcap.tikv.util.BackOffer) ExecutorService(java.util.concurrent.ExecutorService) ClientUtils.groupKeysByRegion(com.pingcap.tikv.util.ClientUtils.groupKeysByRegion) BackOffFunction(com.pingcap.tikv.util.BackOffFunction) Logger(org.slf4j.Logger) TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) TiRegion(com.pingcap.tikv.region.TiRegion) Collectors(java.util.stream.Collectors) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) Executors(java.util.concurrent.Executors) ByteString(com.google.protobuf.ByteString) CacheInvalidateEvent(com.pingcap.tikv.event.CacheInvalidateEvent) RegionManager(com.pingcap.tikv.region.RegionManager) List(java.util.List) TiKVException(com.pingcap.tikv.exception.TiKVException) TxnKVClient(com.pingcap.tikv.txn.TxnKVClient) Catalog(com.pingcap.tikv.catalog.Catalog) ByteString(com.google.protobuf.ByteString) TiKVException(com.pingcap.tikv.exception.TiKVException) ArrayList(java.util.ArrayList) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Metapb(org.tikv.kvproto.Metapb)

Aggregations

ByteString (com.google.protobuf.ByteString)2 TiKVException (com.pingcap.tikv.exception.TiKVException)2 RegionManager (com.pingcap.tikv.region.RegionManager)2 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)2 TiRegion (com.pingcap.tikv.region.TiRegion)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Catalog (com.pingcap.tikv.catalog.Catalog)1 CacheInvalidateEvent (com.pingcap.tikv.event.CacheInvalidateEvent)1 Key (com.pingcap.tikv.key.Key)1 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)1 TxnKVClient (com.pingcap.tikv.txn.TxnKVClient)1 BackOffFunction (com.pingcap.tikv.util.BackOffFunction)1 BackOffer (com.pingcap.tikv.util.BackOffer)1 ChannelFactory (com.pingcap.tikv.util.ChannelFactory)1 ClientUtils.groupKeysByRegion (com.pingcap.tikv.util.ClientUtils.groupKeysByRegion)1 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)1