Search in sources :

Example 6 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class KVMockServer method kvScan.

@Override
public void kvScan(org.tikv.kvproto.Kvrpcpb.ScanRequest request, io.grpc.stub.StreamObserver<org.tikv.kvproto.Kvrpcpb.ScanResponse> responseObserver) {
    try {
        verifyContext(request.getContext());
        if (request.getVersion() == 0) {
            throw new Exception();
        }
        ByteString key = request.getStartKey();
        Kvrpcpb.ScanResponse.Builder builder = Kvrpcpb.ScanResponse.newBuilder();
        Error.Builder errBuilder = Error.newBuilder();
        Integer errorCode = errorMap.remove(key);
        if (errorCode != null) {
            if (errorCode == ABORT) {
                errBuilder.setServerIsBusy(Errorpb.ServerIsBusy.getDefaultInstance());
            }
            builder.setRegionError(errBuilder.build());
        } else {
            ByteString startKey = request.getStartKey();
            SortedMap<Key, ByteString> kvs = dataMap.tailMap(toRawKey(startKey));
            builder.addAllPairs(kvs.entrySet().stream().limit(request.getLimit()).map(kv -> Kvrpcpb.KvPair.newBuilder().setKey(kv.getKey().toByteString()).setValue(kv.getValue()).build()).collect(Collectors.toList()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Error(org.tikv.kvproto.Errorpb.Error) IOException(java.io.IOException) Key(com.pingcap.tikv.key.Key) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey)

Example 7 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class TiBatchWriteUtils method getRecordRegions.

public static List<TiRegion> getRecordRegions(TiSession session, TiTableInfo table) {
    ArrayList<TiRegion> regionList = new ArrayList<>();
    Key key = RowKey.createMin(table.getId());
    RowKey endRowKey = RowKey.createBeyondMax(table.getId());
    while (key.compareTo(endRowKey) < 0) {
        TiRegion region = session.getRegionManager().getRegionByKey(key.toByteString());
        regionList.add(region);
        key = Key.toRawKey(region.getEndKey());
    }
    return regionList;
}
Also used : RowKey(com.pingcap.tikv.key.RowKey) 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 8 with Key

use of com.pingcap.tikv.key.Key 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 9 with Key

use of com.pingcap.tikv.key.Key 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 10 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class PredicateUtils method expressionToIndexRanges.

/**
 * Build index ranges from access points and access conditions
 *
 * @param pointPredicates conditions converting to a single point access
 * @param rangePredicate conditions converting to a range
 * @return Index Range for scan
 */
public static List<IndexRange> expressionToIndexRanges(List<Expression> pointPredicates, Optional<Expression> rangePredicate, TiTableInfo table, TiIndexInfo index) {
    requireNonNull(pointPredicates, "pointPredicates is null");
    requireNonNull(rangePredicate, "rangePredicate is null");
    ImmutableList.Builder<IndexRange> builder = ImmutableList.builder();
    IndexRangeSetBuilder indexRangeBuilder = new IndexRangeSetBuilder(table, index);
    if (pointPredicates.size() != 0) {
        List<Key> pointKeys = expressionToPoints(pointPredicates, table, index);
        for (Key key : pointKeys) {
            if (rangePredicate.isPresent()) {
                Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
                for (Range<TypedKey> range : ranges) {
                    builder.add(new IndexRange(key, range));
                }
            } else {
                // no predicates with point keys leads to empty range encoding
                builder.add(new IndexRange(key, null));
            }
        }
    } else {
        if (rangePredicate.isPresent()) {
            Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
            for (Range<TypedKey> range : ranges) {
                builder.add(new IndexRange(null, range));
            }
        } else {
            // no filter at all means full range
            builder.add(new IndexRange(null, Range.all()));
        }
    }
    return builder.build();
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) ImmutableList(com.google.common.collect.ImmutableList) IndexRangeSetBuilder(com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder) Range(com.google.common.collect.Range) CompoundKey(com.pingcap.tikv.key.CompoundKey) Key(com.pingcap.tikv.key.Key) TypedKey(com.pingcap.tikv.key.TypedKey)

Aggregations

Key (com.pingcap.tikv.key.Key)15 TypedKey (com.pingcap.tikv.key.TypedKey)6 ImmutableList (com.google.common.collect.ImmutableList)5 TiRegion (com.pingcap.tikv.region.TiRegion)5 ArrayList (java.util.ArrayList)5 Range (com.google.common.collect.Range)4 ByteString (com.google.protobuf.ByteString)4 CompoundKey (com.pingcap.tikv.key.CompoundKey)4 RowKey (com.pingcap.tikv.key.RowKey)4 Key.toRawKey (com.pingcap.tikv.key.Key.toRawKey)3 HashMap (java.util.HashMap)3 List (java.util.List)3 KeyRange (org.tikv.kvproto.Coprocessor.KeyRange)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Expression (com.pingcap.tikv.expression.Expression)2 IndexRangeSetBuilder (com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder)2 IndexKey (com.pingcap.tikv.key.IndexKey)2 Pair (com.pingcap.tikv.util.Pair)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2