Search in sources :

Example 1 with Pair

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

the class TiKVScanAnalyzer method buildTableScanKeyRangePerId.

private Pair<Key, Key> buildTableScanKeyRangePerId(long id, IndexRange ir) {
    Key startKey;
    Key endKey;
    if (ir.hasAccessKey()) {
        checkArgument(!ir.hasRange(), "Table scan must have one and only one access condition / point");
        Key key = ir.getAccessKey();
        checkArgument(key instanceof TypedKey, "Table scan key range must be typed key");
        TypedKey typedKey = (TypedKey) key;
        startKey = RowKey.toRowKey(id, typedKey);
        endKey = startKey.next();
    } else if (ir.hasRange()) {
        checkArgument(!ir.hasAccessKey(), "Table scan must have one and only one access condition / point");
        Range<TypedKey> r = ir.getRange();
        if (!r.hasLowerBound()) {
            // -INF
            startKey = RowKey.createMin(id);
        } else {
            // Comparison with null should be filtered since it yields unknown always
            startKey = RowKey.toRowKey(id, r.lowerEndpoint());
            if (r.lowerBoundType().equals(BoundType.OPEN)) {
                startKey = startKey.next();
            }
        }
        if (!r.hasUpperBound()) {
            // INF
            endKey = RowKey.createBeyondMax(id);
        } else {
            endKey = RowKey.toRowKey(id, r.upperEndpoint());
            if (r.upperBoundType().equals(BoundType.CLOSED)) {
                endKey = endKey.next();
            }
        }
    } else {
        throw new TiClientInternalException("Empty access conditions");
    }
    return new Pair<>(startKey, endKey);
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) Range(com.google.common.collect.Range) KeyRangeUtils.makeCoprocRange(com.pingcap.tikv.util.KeyRangeUtils.makeCoprocRange) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) Key(com.pingcap.tikv.key.Key) RowKey(com.pingcap.tikv.key.RowKey) TypedKey(com.pingcap.tikv.key.TypedKey) Pair(com.pingcap.tikv.util.Pair)

Example 2 with Pair

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

the class KVClient method doSendBatchGet.

private List<KvPair> doSendBatchGet(BackOffer backOffer, List<ByteString> keys, long version) {
    ExecutorCompletionService<Pair<List<Batch>, List<KvPair>>> completionService = new ExecutorCompletionService<>(batchGetThreadPool);
    List<Batch> batches = getBatches(backOffer, keys, BATCH_GET_SIZE, MAX_BATCH_LIMIT, this.clientBuilder);
    // prevent stack overflow
    Queue<List<Batch>> taskQueue = new LinkedList<>();
    List<KvPair> result = new ArrayList<>();
    taskQueue.offer(batches);
    while (!taskQueue.isEmpty()) {
        List<Batch> task = taskQueue.poll();
        for (Batch batch : task) {
            completionService.submit(() -> doSendBatchGetInBatchesWithRetry(batch.getBackOffer(), batch, version));
        }
        result.addAll(getTasksWithOutput(completionService, taskQueue, task, BackOffer.RAWKV_MAX_BACKOFF));
    }
    return result;
}
Also used : Batch(com.pingcap.tikv.util.Batch) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) Pair(com.pingcap.tikv.util.Pair) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair)

Example 3 with Pair

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

the class CatalogTransaction method getTables.

List<TiTableInfo> getTables(long dbId) {
    ByteString dbKey = MetaCodec.encodeDatabaseID(dbId);
    List<Pair<ByteString, ByteString>> fields = MetaCodec.hashGetFields(dbKey, this.snapshot);
    ImmutableList.Builder<TiTableInfo> builder = ImmutableList.builder();
    for (Pair<ByteString, ByteString> pair : fields) {
        if (KeyUtils.hasPrefix(pair.first, ByteString.copyFromUtf8(MetaCodec.KEY_TABLE))) {
            try {
                TiTableInfo tableInfo = parseFromJson(pair.second, TiTableInfo.class);
                if (!tableInfo.isSequence() && !tableInfo.isView()) {
                    builder.add(tableInfo);
                }
            } catch (TiClientInternalException e) {
                logger.warn("fail to parse table from json!", e);
            }
        }
    }
    return builder.build();
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ByteString(com.google.protobuf.ByteString) ImmutableList(com.google.common.collect.ImmutableList) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) Pair(com.pingcap.tikv.util.Pair)

Example 4 with Pair

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

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

the class MetaCodec method hashGetFields.

public static List<Pair<ByteString, ByteString>> hashGetFields(ByteString key, Snapshot snapshot) {
    CodecDataOutput cdo = new CodecDataOutput();
    MetaCodec.encodeHashDataKeyPrefix(cdo, key.toByteArray());
    ByteString encodedKey = cdo.toByteString();
    Iterator<KvPair> iterator = snapshot.scanPrefix(encodedKey);
    List<Pair<ByteString, ByteString>> fields = new ArrayList<>();
    while (iterator.hasNext()) {
        Kvrpcpb.KvPair kv = iterator.next();
        if (kv == null || kv.getKey().isEmpty()) {
            continue;
        }
        fields.add(Pair.create(MetaCodec.decodeHashDataKey(kv.getKey()).second, kv.getValue()));
    }
    return fields;
}
Also used : KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) ByteString(com.google.protobuf.ByteString) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) ArrayList(java.util.ArrayList) Pair(com.pingcap.tikv.util.Pair) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair)

Aggregations

Pair (com.pingcap.tikv.util.Pair)5 ByteString (com.google.protobuf.ByteString)3 ArrayList (java.util.ArrayList)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Key (com.pingcap.tikv.key.Key)2 List (java.util.List)2 KvPair (org.tikv.kvproto.Kvrpcpb.KvPair)2 ImmutableList (com.google.common.collect.ImmutableList)1 Range (com.google.common.collect.Range)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Catalog (com.pingcap.tikv.catalog.Catalog)1 CacheInvalidateEvent (com.pingcap.tikv.event.CacheInvalidateEvent)1 TiKVException (com.pingcap.tikv.exception.TiKVException)1 RowKey (com.pingcap.tikv.key.RowKey)1 TypedKey (com.pingcap.tikv.key.TypedKey)1 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)1 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)1 RegionManager (com.pingcap.tikv.region.RegionManager)1 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)1 TiRegion (com.pingcap.tikv.region.TiRegion)1