Search in sources :

Example 26 with TiRegion

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());
}
Also used : GetRegionResponse(org.tikv.kvproto.Pdpb.GetRegionResponse) ByteString(com.google.protobuf.ByteString) PDErrorHandler(com.pingcap.tikv.operation.PDErrorHandler) TiRegion(com.pingcap.tikv.region.TiRegion) CodecDataOutput(com.pingcap.tikv.codec.CodecDataOutput) GetRegionRequest(org.tikv.kvproto.Pdpb.GetRegionRequest)

Example 27 with TiRegion

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;
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) RowKey(com.pingcap.tikv.key.RowKey) IndexKey(com.pingcap.tikv.key.IndexKey) Key(com.pingcap.tikv.key.Key)

Example 28 with TiRegion

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);
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) TiKVException(com.pingcap.tikv.exception.TiKVException)

Example 29 with TiRegion

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

Example 30 with TiRegion

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);
    }
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) BatchKeys(com.pingcap.tikv.txn.type.BatchKeys) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TiRegion (com.pingcap.tikv.region.TiRegion)43 ByteString (com.google.protobuf.ByteString)19 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)17 ArrayList (java.util.ArrayList)16 BackOffer (com.pingcap.tikv.util.BackOffer)10 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)10 List (java.util.List)10 Test (org.junit.Test)9 TiKVException (com.pingcap.tikv.exception.TiKVException)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)8 Metapb (org.tikv.kvproto.Metapb)8 GrpcException (com.pingcap.tikv.exception.GrpcException)7 KeyException (com.pingcap.tikv.exception.KeyException)7 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)6 RegionException (com.pingcap.tikv.exception.RegionException)4 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)4 Key (com.pingcap.tikv.key.Key)4 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)4