Search in sources :

Example 1 with BackOffer

use of org.tikv.common.util.BackOffer in project client-java by tikv.

the class TiSession method warmUp.

private synchronized void warmUp() {
    long warmUpStartTime = System.nanoTime();
    BackOffer backOffer = ConcreteBackOffer.newRawKVBackOff(getPDClient().getClusterId());
    try {
        // let JVM ClassLoader load gRPC error related classes
        // this operation may cost 100ms
        Errorpb.Error.newBuilder().setNotLeader(Errorpb.NotLeader.newBuilder().build()).build().toString();
        this.client = getPDClient();
        this.regionManager = getRegionManager();
        List<Metapb.Store> stores = this.client.getAllStores(backOffer);
        // warm up store cache
        for (Metapb.Store store : stores) {
            this.regionManager.updateStore(null, new TiStore(this.client.getStore(backOffer, store.getId())));
        }
        // use scan region to load region cache with limit
        ByteString startKey = ByteString.EMPTY;
        do {
            List<Pdpb.Region> regions = regionManager.scanRegions(backOffer, startKey, ByteString.EMPTY, conf.getScanRegionsLimit());
            if (regions == null || regions.isEmpty()) {
                // something went wrong, but the warm-up process could continue
                break;
            }
            for (Pdpb.Region region : regions) {
                regionManager.insertRegionToCache(regionManager.createRegion(region.getRegion(), backOffer));
            }
            startKey = regions.get(regions.size() - 1).getRegion().getEndKey();
        } while (!startKey.isEmpty());
        try (RawKVClient rawKVClient = createRawClient()) {
            ByteString exampleKey = ByteString.EMPTY;
            Optional<ByteString> prev = rawKVClient.get(exampleKey);
            if (prev.isPresent()) {
                rawKVClient.delete(exampleKey);
                rawKVClient.putIfAbsent(exampleKey, prev.get());
                rawKVClient.put(exampleKey, prev.get());
            } else {
                rawKVClient.putIfAbsent(exampleKey, ByteString.EMPTY);
                rawKVClient.put(exampleKey, ByteString.EMPTY);
                rawKVClient.delete(exampleKey);
            }
        }
    } catch (Exception e) {
        // ignore error
        logger.info("warm up fails, ignored ", e);
    } finally {
        logger.info(String.format("warm up duration %d ms", (System.nanoTime() - warmUpStartTime) / 1_000_000));
    }
}
Also used : Pdpb(org.tikv.kvproto.Pdpb) ByteString(com.google.protobuf.ByteString) TiStore(org.tikv.common.region.TiStore) BackOffer(org.tikv.common.util.BackOffer) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) TiStore(org.tikv.common.region.TiStore) TiKVException(org.tikv.common.exception.TiKVException) SmartRawKVClient(org.tikv.raw.SmartRawKVClient) RawKVClient(org.tikv.raw.RawKVClient) TiRegion(org.tikv.common.region.TiRegion) ClientUtils.groupKeysByRegion(org.tikv.common.util.ClientUtils.groupKeysByRegion) Metapb(org.tikv.kvproto.Metapb)

Example 2 with BackOffer

use of org.tikv.common.util.BackOffer in project client-java by tikv.

the class TiSession method splitRegion.

private List<Metapb.Region> splitRegion(List<ByteString> splitKeys, BackOffer backOffer, int depth) {
    List<Metapb.Region> regions = new ArrayList<>();
    Map<TiRegion, List<ByteString>> groupKeys = groupKeysByRegion(getRegionManager(), splitKeys, backOffer);
    for (Map.Entry<TiRegion, List<ByteString>> entry : groupKeys.entrySet()) {
        Pair<TiRegion, TiStore> pair = getRegionManager().getRegionStorePairByKey(entry.getKey().getStartKey());
        TiRegion region = pair.first;
        TiStore 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<Metapb.Region> newRegions;
            try {
                newRegions = getRegionStoreClientBuilder().build(region, store).splitRegion(splits);
                // invalidate old region
                getRegionManager().invalidateRegion(region);
            } catch (final TiKVException e) {
                // retry
                logger.warn("ReSplitting ranges for splitRegion", e);
                getRegionManager().invalidateRegion(region);
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
                if (depth >= MAX_SPLIT_REGION_STACK_DEPTH) {
                    logger.warn(String.format("Skip split region because MAX_SPLIT_REGION_STACK_DEPTH(%d) reached!", MAX_SPLIT_REGION_STACK_DEPTH));
                    newRegions = new ArrayList<>();
                } else {
                    newRegions = splitRegion(splits, backOffer, depth + 1);
                }
            }
            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) Catalog(org.tikv.common.catalog.Catalog) CircuitBreaker(org.tikv.service.failsafe.CircuitBreaker) Metapb(org.tikv.kvproto.Metapb) Errorpb(org.tikv.kvproto.Errorpb) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TiStore(org.tikv.common.region.TiStore) ImporterStoreClient(org.tikv.common.importer.ImporterStoreClient) RegionManager(org.tikv.common.region.RegionManager) TiRegion(org.tikv.common.region.TiRegion) CircuitBreakerImpl(org.tikv.service.failsafe.CircuitBreakerImpl) TiKVException(org.tikv.common.exception.TiKVException) Map(java.util.Map) TxnKVClient(org.tikv.txn.TxnKVClient) KVClient(org.tikv.txn.KVClient) TiTimestamp(org.tikv.common.meta.TiTimestamp) BackOffer(org.tikv.common.util.BackOffer) BackOffFunction(org.tikv.common.util.BackOffFunction) ExecutorService(java.util.concurrent.ExecutorService) SmartRawKVClient(org.tikv.raw.SmartRawKVClient) ClientUtils.groupKeysByRegion(org.tikv.common.util.ClientUtils.groupKeysByRegion) SwitchTiKVModeClient(org.tikv.common.importer.SwitchTiKVModeClient) Properties(java.util.Properties) Logger(org.slf4j.Logger) Pair(org.tikv.common.util.Pair) ImportSstpb(org.tikv.kvproto.ImportSstpb) RawKVClient(org.tikv.raw.RawKVClient) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ByteString(com.google.protobuf.ByteString) List(java.util.List) Key(org.tikv.common.key.Key) RegionStoreClient(org.tikv.common.region.RegionStoreClient) Optional(java.util.Optional) Pdpb(org.tikv.kvproto.Pdpb) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ChannelFactory(org.tikv.common.util.ChannelFactory) ByteString(com.google.protobuf.ByteString) TiKVException(org.tikv.common.exception.TiKVException) ArrayList(java.util.ArrayList) TiStore(org.tikv.common.region.TiStore) TiRegion(org.tikv.common.region.TiRegion) TiRegion(org.tikv.common.region.TiRegion) ClientUtils.groupKeysByRegion(org.tikv.common.util.ClientUtils.groupKeysByRegion) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with BackOffer

use of org.tikv.common.util.BackOffer in project client-java by tikv.

the class ImporterClient method ingest.

private void ingest() throws GrpcException {
    List<ImporterStoreClient> workingClients = new ArrayList<>(clientList);
    while (!workingClients.isEmpty()) {
        Iterator<ImporterStoreClient> itor = workingClients.iterator();
        while (itor.hasNext()) {
            ImporterStoreClient client = itor.next();
            if (client.isWriteResponseReceived()) {
                itor.remove();
            } else if (client.hasWriteResponseError()) {
                throw new GrpcException(client.getWriteError());
            }
        }
        if (!workingClients.isEmpty()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    Object writeResponse = clientLeader.getWriteResponse();
    BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(BackOffer.INGEST_BACKOFF, tiSession.getPDClient().getClusterId());
    ingestWithRetry(writeResponse, backOffer);
}
Also used : ArrayList(java.util.ArrayList) GrpcException(org.tikv.common.exception.GrpcException) BackOffer(org.tikv.common.util.BackOffer) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer)

Example 4 with BackOffer

use of org.tikv.common.util.BackOffer in project client-java by tikv.

the class RegionManager method getRegionById.

@Deprecated
public // This request is unrecoverable.
TiRegion getRegionById(long regionId) {
    BackOffer backOffer = defaultBackOff();
    TiRegion region = cache.getRegionById(regionId);
    if (region == null) {
        Pair<Metapb.Region, Metapb.Peer> regionAndLeader = pdClient.getRegionByID(backOffer, regionId);
        region = createRegion(regionAndLeader.first, regionAndLeader.second, backOffer);
        return cache.putRegion(region);
    }
    return region;
}
Also used : Peer(org.tikv.kvproto.Metapb.Peer) BackOffer(org.tikv.common.util.BackOffer) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer)

Example 5 with BackOffer

use of org.tikv.common.util.BackOffer in project client-java by tikv.

the class ConcreteScanIterator method resolveCurrentLock.

private ByteString resolveCurrentLock(Kvrpcpb.KvPair current) {
    logger.warn(String.format("resolve current key error %s", current.getError().toString()));
    Pair<TiRegion, TiStore> pair = builder.getRegionManager().getRegionStorePairByKey(current.getKey());
    TiRegion region = pair.first;
    TiStore store = pair.second;
    BackOffer backOffer = ConcreteBackOffer.newGetBackOff(builder.getRegionManager().getPDClient().getClusterId());
    try (RegionStoreClient client = builder.build(region, store)) {
        return client.get(backOffer, current.getKey(), version);
    } catch (Exception e) {
        throw new KeyException(current.getError());
    }
}
Also used : TiRegion(org.tikv.common.region.TiRegion) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) BackOffer(org.tikv.common.util.BackOffer) TiStore(org.tikv.common.region.TiStore) RegionStoreClient(org.tikv.common.region.RegionStoreClient) KeyException(org.tikv.common.exception.KeyException) GrpcException(org.tikv.common.exception.GrpcException) KeyException(org.tikv.common.exception.KeyException)

Aggregations

BackOffer (org.tikv.common.util.BackOffer)20 ConcreteBackOffer (org.tikv.common.util.ConcreteBackOffer)19 TiRegion (org.tikv.common.region.TiRegion)9 TiKVException (org.tikv.common.exception.TiKVException)7 RegionStoreClient (org.tikv.common.region.RegionStoreClient)7 ByteString (com.google.protobuf.ByteString)6 ExecutionException (java.util.concurrent.ExecutionException)6 TiStore (org.tikv.common.region.TiStore)6 GrpcException (org.tikv.common.exception.GrpcException)5 TiBatchWriteException (org.tikv.common.exception.TiBatchWriteException)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)3 TimeoutException (java.util.concurrent.TimeoutException)3 Test (org.junit.Test)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 BaseRawKVTest (org.tikv.BaseRawKVTest)3 Metapb (org.tikv.kvproto.Metapb)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2