Search in sources :

Example 1 with TiStore

use of org.tikv.common.region.TiStore 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 TiStore

use of org.tikv.common.region.TiStore 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 TiStore

use of org.tikv.common.region.TiStore 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)

Example 4 with TiStore

use of org.tikv.common.region.TiStore in project client-java by tikv.

the class DAGIterator method processByStreaming.

private Iterator<SelectResponse> processByStreaming(RangeSplitter.RegionTask regionTask) {
    List<Coprocessor.KeyRange> ranges = regionTask.getRanges();
    TiRegion region = regionTask.getRegion();
    TiStore store = regionTask.getStore();
    RegionStoreClient client;
    try {
        client = session.getRegionStoreClientBuilder().build(region, store, storeType);
        Iterator<SelectResponse> responseIterator = client.coprocessStreaming(dagRequest, ranges, startTs);
        if (responseIterator == null) {
            eof = true;
            return null;
        }
        return responseIterator;
    } catch (Exception e) {
        // see:https://github.com/pingcap/tikv-client-lib-java/pull/149
        throw new TiClientInternalException("Error Closing Store client.", e);
    }
}
Also used : TiClientInternalException(org.tikv.common.exception.TiClientInternalException) TiRegion(org.tikv.common.region.TiRegion) TiStore(org.tikv.common.region.TiStore) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) RegionStoreClient(org.tikv.common.region.RegionStoreClient) RegionTaskException(org.tikv.common.exception.RegionTaskException) TiClientInternalException(org.tikv.common.exception.TiClientInternalException)

Example 5 with TiStore

use of org.tikv.common.region.TiStore in project client-java by tikv.

the class SwitchTiKVModeClient method doSwitchTiKVMode.

private void doSwitchTiKVMode(ImportSstpb.SwitchMode mode) {
    BackOffer bo = ConcreteBackOffer.newCustomBackOff(BackOffer.PD_INFO_BACKOFF, pdClient.getClusterId());
    List<Metapb.Store> allStores = pdClient.getAllStores(bo);
    for (Metapb.Store store : allStores) {
        ImporterStoreClient client = builder.build(new TiStore(store));
        client.switchMode(mode);
    }
}
Also used : TiStore(org.tikv.common.region.TiStore) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) BackOffer(org.tikv.common.util.BackOffer) TiStore(org.tikv.common.region.TiStore) Metapb(org.tikv.kvproto.Metapb)

Aggregations

TiStore (org.tikv.common.region.TiStore)19 TiRegion (org.tikv.common.region.TiRegion)15 ByteString (com.google.protobuf.ByteString)9 Metapb (org.tikv.kvproto.Metapb)7 BackOffer (org.tikv.common.util.BackOffer)6 ConcreteBackOffer (org.tikv.common.util.ConcreteBackOffer)6 GrpcException (org.tikv.common.exception.GrpcException)5 TiBatchWriteException (org.tikv.common.exception.TiBatchWriteException)5 Pair (org.tikv.common.util.Pair)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 RegionManager (org.tikv.common.region.RegionManager)4 RegionStoreClient (org.tikv.common.region.RegionStoreClient)4 ClientRPCResult (org.tikv.txn.type.ClientRPCResult)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2