Search in sources :

Example 11 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class LockResolverTest method checkTTLNotExpired.

void checkTTLNotExpired(String key) {
    try {
        RegionStoreClient client = getRegionStoreClient(key);
        BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(CHECK_TTL_BACKOFF);
        // In SI mode, a lock <key, value2> is read. Try resolve it, but failed, cause TTL not
        // expires.
        client.get(backOffer, ByteString.copyFromUtf8(key), session.getTimestamp().getVersion());
        fail();
    } catch (GrpcException e) {
        assertEquals(e.getMessage(), "retry is exhausted.");
    }
}
Also used : GrpcException(com.pingcap.tikv.exception.GrpcException) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient)

Example 12 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class TTLManager method doKeepAlive.

private void doKeepAlive() {
    BackOffer bo = ConcreteBackOffer.newCustomBackOff(MANAGED_LOCK_TTL);
    long uptime = calculateUptime(kvClient, startTS);
    long ttl = uptime + MANAGED_LOCK_TTL;
    LOG.info("doKeepAlive key={} uptime={} ttl={}", LogDesensitization.hide(KeyUtils.formatBytes(primaryLock)), uptime, ttl);
    try {
        sendTxnHeartBeat(bo, ttl);
        LOG.info("doKeepAlive success");
    } catch (Exception e) {
        LOG.warn("doKeepAlive error", e);
    }
}
Also used : ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException) GrpcException(com.pingcap.tikv.exception.GrpcException)

Example 13 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class StoreVersion method minTiKVVersion.

public static boolean minTiKVVersion(String version, PDClient pdClient) {
    StoreVersion storeVersion = new StoreVersion(version);
    BackOffer bo = ConcreteBackOffer.newCustomBackOff(BackOffer.PD_INFO_BACKOFF);
    List<Metapb.Store> storeList = pdClient.getAllStores(bo).stream().filter(store -> !isTiFlash(store) && (store.getState() == Metapb.StoreState.Up || store.getState() == Metapb.StoreState.Offline)).collect(Collectors.toList());
    for (Metapb.Store store : storeList) {
        if (storeVersion.greatThan(new StoreVersion(store.getVersion()))) {
            return false;
        }
    }
    return true;
}
Also used : TiStoreType(com.pingcap.tikv.region.TiStoreType) List(java.util.List) Logger(org.slf4j.Logger) Metapb(org.tikv.kvproto.Metapb) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) LoggerFactory(org.slf4j.LoggerFactory) BackOffer(com.pingcap.tikv.util.BackOffer) Collectors(java.util.stream.Collectors) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) Metapb(org.tikv.kvproto.Metapb)

Example 14 with BackOffer

use of com.pingcap.tikv.util.BackOffer 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 15 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class TwoPhaseCommitter method doPrewriteSecondaryKeys.

private void doPrewriteSecondaryKeys(ByteString primaryKey, Iterator<Pair<ByteString, ByteString>> pairs, int maxBackOfferMS) throws TiBatchWriteException {
    try {
        int taskBufferSize = writeThreadPerTask * 2;
        int totalSize = 0, cnt = 0;
        Pair<ByteString, ByteString> pair;
        ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<>(executorService);
        while (pairs.hasNext()) {
            List<ByteString> keyBytes = new ArrayList<>(writeBufferSize);
            List<ByteString> valueBytes = new ArrayList<>(writeBufferSize);
            while (keyBytes.size() < writeBufferSize && pairs.hasNext()) {
                pair = pairs.next();
                keyBytes.add(pair.first);
                valueBytes.add(pair.second);
            }
            int curSize = keyBytes.size();
            cnt++;
            if (cnt > taskBufferSize) {
                // consume one task if reaches task limit
                completionService.take().get();
            }
            BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(maxBackOfferMS);
            completionService.submit(() -> {
                doPrewriteSecondaryKeysInBatchesWithRetry(backOffer, primaryKey, keyBytes, valueBytes, curSize, 0);
                return null;
            });
            totalSize = totalSize + keyBytes.size();
        }
        for (int i = 0; i < Math.min(taskBufferSize, cnt); i++) {
            completionService.take().get();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new TiBatchWriteException("Current thread interrupted.", e);
    } catch (ExecutionException e) {
        throw new TiBatchWriteException("Execution exception met.", e);
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) ExecutionException(java.util.concurrent.ExecutionException) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Aggregations

BackOffer (com.pingcap.tikv.util.BackOffer)19 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)19 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)13 TiRegion (com.pingcap.tikv.region.TiRegion)10 ByteString (com.google.protobuf.ByteString)9 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)6 ArrayList (java.util.ArrayList)6 GrpcException (com.pingcap.tikv.exception.GrpcException)5 TiKVException (com.pingcap.tikv.exception.TiKVException)4 Metapb (org.tikv.kvproto.Metapb)4 KeyException (com.pingcap.tikv.exception.KeyException)3 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)3 Test (org.junit.Test)3 Chunk (com.pingcap.tidb.tipb.Chunk)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2 RegionException (com.pingcap.tikv.exception.RegionException)2 ArrayDeque (java.util.ArrayDeque)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)2