Search in sources :

Example 6 with TiClientInternalException

use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.

the class ChunkIterator method getRawBytesChunkIterator.

public static ChunkIterator<ByteString> getRawBytesChunkIterator(List<Chunk> chunks) {
    return new ChunkIterator<ByteString>(chunks) {

        @Override
        public ByteString next() {
            Chunk c = chunks.get(chunkIndex);
            long endOffset = c.getRowsMeta(metaIndex).getLength() + bufOffset;
            if (endOffset > Integer.MAX_VALUE) {
                throw new TiClientInternalException("Offset exceeded MAX_INT.");
            }
            ByteString result = c.getRowsData().substring(bufOffset, (int) endOffset);
            advance();
            return result;
        }
    };
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ByteString(com.google.protobuf.ByteString) Chunk(com.pingcap.tidb.tipb.Chunk)

Example 7 with TiClientInternalException

use of com.pingcap.tikv.exception.TiClientInternalException 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 8 with TiClientInternalException

use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.

the class TiKVScanAnalyzer method buildTiDAGReq.

// Build scan plan picking access path with lowest cost by estimation
public TiDAGRequest buildTiDAGReq(boolean allowIndexScan, boolean useIndexScanFirst, boolean canUseTiKV, boolean canUseTiFlash, List<TiColumnInfo> columnList, List<Expression> conditions, TiTableInfo table, TableStatistics tableStatistics, TiTimestamp ts, TiDAGRequest dagRequest) {
    TiKVScanPlan minPlan = null;
    if (canUseTiKV) {
        minPlan = buildTableScan(conditions, table, tableStatistics);
    }
    if (canUseTiFlash) {
        // it is possible that only TiFlash plan exists due to isolation read.
        TiKVScanPlan plan = buildTiFlashScan(columnList, conditions, table, tableStatistics);
        if (minPlan == null || plan.getCost() < minPlan.getCost()) {
            minPlan = plan;
        }
    } else if (canUseTiKV && allowIndexScan) {
        minPlan.getFilters().forEach(dagRequest::addDowngradeFilter);
        if (table.isPartitionEnabled()) {
        // disable index scan
        } else {
            TiKVScanPlan minIndexPlan = null;
            double minIndexCost = Double.MAX_VALUE;
            for (TiIndexInfo index : table.getIndices()) {
                if (table.isCommonHandle() && table.getPrimaryKey().equals(index)) {
                    continue;
                }
                if (supportIndexScan(index, table)) {
                    TiKVScanPlan plan = buildIndexScan(columnList, conditions, index, table, tableStatistics, false);
                    if (plan.getCost() < minIndexCost) {
                        minIndexPlan = plan;
                        minIndexCost = plan.getCost();
                    }
                }
            }
            if (minIndexPlan != null && (minIndexCost < minPlan.getCost() || useIndexScanFirst)) {
                minPlan = minIndexPlan;
            }
        }
    }
    if (minPlan == null) {
        throw new TiClientInternalException("No valid plan found for table '" + table.getName() + "'");
    }
    TiStoreType minPlanStoreType = minPlan.getStoreType();
    // TiKV should not use CHBlock as Encode Type.
    if (minPlanStoreType == TiStoreType.TiKV && dagRequest.getEncodeType() == EncodeType.TypeCHBlock) {
        dagRequest.setEncodeType(EncodeType.TypeChunk);
    }
    // Set DAG Request's store type as minPlan's store type.
    dagRequest.setStoreType(minPlanStoreType);
    dagRequest.addRanges(minPlan.getKeyRanges());
    dagRequest.setPrunedParts(minPlan.getPrunedParts());
    dagRequest.addFilters(new ArrayList<>(minPlan.getFilters()));
    if (minPlan.isIndexScan()) {
        dagRequest.setIndexInfo(minPlan.getIndex());
        // need to set isDoubleRead to true for dagRequest in case of double read
        dagRequest.setIsDoubleRead(minPlan.isDoubleRead());
    }
    dagRequest.setTableInfo(table);
    dagRequest.setStartTs(ts);
    dagRequest.setEstimatedCount(minPlan.getEstimatedRowCount());
    return dagRequest;
}
Also used : TiStoreType(com.pingcap.tikv.region.TiStoreType) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo)

Example 9 with TiClientInternalException

use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.

the class LockResolverClientV2 method resolveLock.

private void resolveLock(BackOffer bo, Lock lock, long txnStatus, Set<RegionVerID> cleanRegion) {
    while (true) {
        region = regionManager.getRegionByKey(lock.getKey());
        if (cleanRegion.contains(region.getVerID())) {
            return;
        }
        Supplier<ResolveLockRequest> factory;
        if (txnStatus > 0) {
            // txn is committed with commitTS txnStatus
            factory = () -> ResolveLockRequest.newBuilder().setContext(region.getContext()).setStartVersion(lock.getTxnID()).setCommitVersion(txnStatus).build();
        } else {
            factory = () -> ResolveLockRequest.newBuilder().setContext(region.getContext()).setStartVersion(lock.getTxnID()).build();
        }
        KVErrorHandler<ResolveLockResponse> handler = new KVErrorHandler<>(regionManager, this, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, 0L, false);
        ResolveLockResponse resp = callWithRetry(bo, TikvGrpc.getKvResolveLockMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKvResolveLockMethod failed without a cause");
            regionManager.onRequestFail(region);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvResolveLockMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.hasError()) {
            logger.error(String.format("unexpected resolveLock err: %s, lock: %s", resp.getError(), lock));
            throw new KeyException(resp.getError());
        }
        cleanRegion.add(region.getVerID());
        return;
    }
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ResolveLockRequest(org.tikv.kvproto.Kvrpcpb.ResolveLockRequest) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) ResolveLockResponse(org.tikv.kvproto.Kvrpcpb.ResolveLockResponse) KeyException(com.pingcap.tikv.exception.KeyException)

Example 10 with TiClientInternalException

use of com.pingcap.tikv.exception.TiClientInternalException 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)

Aggregations

TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)25 KeyException (com.pingcap.tikv.exception.KeyException)11 RegionException (com.pingcap.tikv.exception.RegionException)10 KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)10 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)7 ByteString (com.google.protobuf.ByteString)6 TiRegion (com.pingcap.tikv.region.TiRegion)6 ArrayList (java.util.ArrayList)6 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)5 GrpcException (com.pingcap.tikv.exception.GrpcException)4 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)4 HashMap (java.util.HashMap)4 TiKVException (com.pingcap.tikv.exception.TiKVException)3 Lock (com.pingcap.tikv.txn.Lock)3 ResolveLockResult (com.pingcap.tikv.txn.ResolveLockResult)3 BoTxnLock (com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)3 List (java.util.List)3 Map (java.util.Map)3 DAGRequest (com.pingcap.tidb.tipb.DAGRequest)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2