Search in sources :

Example 11 with TiClientInternalException

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

the class PDClient method scatterRegion.

/**
 * Sends request to pd to scatter region.
 *
 * @param region represents a region info
 */
void scatterRegion(TiRegion region, BackOffer backOffer) {
    Supplier<ScatterRegionRequest> request = () -> ScatterRegionRequest.newBuilder().setHeader(header).setRegionId(region.getId()).build();
    PDErrorHandler<ScatterRegionResponse> handler = new PDErrorHandler<>(r -> r.getHeader().hasError() ? buildFromPdpbError(r.getHeader().getError()) : null, this);
    ScatterRegionResponse resp = callWithRetry(backOffer, PDGrpc.getScatterRegionMethod(), request, handler);
    // TODO: maybe we should retry here, need dig into pd's codebase.
    if (resp.hasHeader() && resp.getHeader().hasError()) {
        throw new TiClientInternalException(String.format("failed to scatter region because %s", resp.getHeader().getError()));
    }
}
Also used : ScatterRegionRequest(org.tikv.kvproto.Pdpb.ScatterRegionRequest) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ScatterRegionResponse(org.tikv.kvproto.Pdpb.ScatterRegionResponse) PDErrorHandler(com.pingcap.tikv.operation.PDErrorHandler)

Example 12 with TiClientInternalException

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

the class CatalogTransaction method parseFromJson.

public static <T> T parseFromJson(ByteString json, Class<T> cls) {
    Objects.requireNonNull(json, "json is null");
    Objects.requireNonNull(cls, "cls is null");
    logger.debug(String.format("Parse Json %s : %s", cls.getSimpleName(), json.toStringUtf8()));
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.readValue(json.toStringUtf8(), cls);
    } catch (JsonParseException | JsonMappingException e) {
        String errMsg = String.format("Invalid JSON value for Type %s: %s\n", cls.getSimpleName(), json.toStringUtf8());
        throw new TiClientInternalException(errMsg, e);
    } catch (Exception e1) {
        throw new TiClientInternalException("Error parsing Json", e1);
    }
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ByteString(com.google.protobuf.ByteString) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 13 with TiClientInternalException

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

the class RegionStoreClient method handleBatchGetResponse.

private List<KvPair> handleBatchGetResponse(BackOffer backOffer, BatchGetResponse resp, long version) {
    boolean forWrite = false;
    if (resp == null) {
        this.regionManager.onRequestFail(region);
        throw new TiClientInternalException("BatchGetResponse failed without a cause");
    }
    if (resp.hasRegionError()) {
        throw new RegionException(resp.getRegionError());
    }
    List<Lock> locks = new ArrayList<>();
    for (KvPair pair : resp.getPairsList()) {
        if (pair.hasError()) {
            if (pair.getError().hasLocked()) {
                Lock lock = new Lock(pair.getError().getLocked());
                locks.add(lock);
            } else {
                throw new KeyException(pair.getError());
            }
        }
    }
    if (!locks.isEmpty()) {
        ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, version, locks, forWrite);
        addResolvedLocks(version, resolveLockResult.getResolvedLocks());
        // resolveLocks already retried, just throw error to upper logic.
        throw new TiKVException("locks not resolved, retry");
    } else {
        return resp.getPairsList();
    }
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) TiKVException(com.pingcap.tikv.exception.TiKVException) ArrayList(java.util.ArrayList) ResolveLockResult(com.pingcap.tikv.txn.ResolveLockResult) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException) Lock(com.pingcap.tikv.txn.Lock) BoTxnLock(com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)

Example 14 with TiClientInternalException

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

the class RegionStoreClient method splitRegion.

/**
 * Send SplitRegion request to tikv split a region at splitKey. splitKey must between current
 * region's start key and end key.
 *
 * @param splitKeys is the split points for a specific region.
 * @return a split region info.
 */
public List<TiRegion> splitRegion(Iterable<ByteString> splitKeys) {
    Supplier<SplitRegionRequest> request = () -> SplitRegionRequest.newBuilder().setContext(region.getContext()).addAllSplitKeys(splitKeys).build();
    KVErrorHandler<SplitRegionResponse> handler = new KVErrorHandler<>(regionManager, this, null, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> null, resolveLockResult -> null, 0L, false);
    SplitRegionResponse resp = callWithRetry(ConcreteBackOffer.newGetBackOff(), TikvGrpc.getSplitRegionMethod(), request, handler);
    if (resp == null) {
        this.regionManager.onRequestFail(region);
        throw new TiClientInternalException("SplitRegion Response failed without a cause");
    }
    if (resp.hasRegionError()) {
        throw new TiClientInternalException(String.format("failed to split region %d because %s", region.getId(), resp.getRegionError().toString()));
    }
    return resp.getRegionsList().stream().map(region -> new TiRegion(region, null, conf.getIsolationLevel(), conf.getCommandPriority())).collect(Collectors.toList());
}
Also used : Batch(com.pingcap.tikv.util.Batch) CommitResponse(org.tikv.kvproto.Kvrpcpb.CommitResponse) RawPutRequest(org.tikv.kvproto.Kvrpcpb.RawPutRequest) ScanRequest(org.tikv.kvproto.Kvrpcpb.ScanRequest) ChannelFactory(com.pingcap.tikv.util.ChannelFactory) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) ManagedChannel(io.grpc.ManagedChannel) RawScanRequest(org.tikv.kvproto.Kvrpcpb.RawScanRequest) Errorpb(org.tikv.kvproto.Errorpb) RawBatchGetRequest(org.tikv.kvproto.Kvrpcpb.RawBatchGetRequest) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) RawGetKeyTTLRequest(org.tikv.kvproto.Kvrpcpb.RawGetKeyTTLRequest) LoggerFactory(org.slf4j.LoggerFactory) PDClient(com.pingcap.tikv.PDClient) RawBatchGetResponse(org.tikv.kvproto.Kvrpcpb.RawBatchGetResponse) RegionException(com.pingcap.tikv.exception.RegionException) Pair(com.pingcap.tikv.util.Pair) REQ_TYPE_DAG(com.pingcap.tikv.region.RegionStoreClient.RequestTypes.REQ_TYPE_DAG) RawDeleteRangeResponse(org.tikv.kvproto.Kvrpcpb.RawDeleteRangeResponse) Map(java.util.Map) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) StreamingResponse(com.pingcap.tikv.streaming.StreamingResponse) BackOffer(com.pingcap.tikv.util.BackOffer) Coprocessor(org.tikv.kvproto.Coprocessor) TxnHeartBeatResponse(org.tikv.kvproto.Kvrpcpb.TxnHeartBeatResponse) TikvBlockingStub(org.tikv.kvproto.TikvGrpc.TikvBlockingStub) RawDeleteRangeRequest(org.tikv.kvproto.Kvrpcpb.RawDeleteRangeRequest) TikvStub(org.tikv.kvproto.TikvGrpc.TikvStub) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RawBatchPutResponse(org.tikv.kvproto.Kvrpcpb.RawBatchPutResponse) RawDeleteRequest(org.tikv.kvproto.Kvrpcpb.RawDeleteRequest) BackOffFunction(com.pingcap.tikv.util.BackOffFunction) BoRegionMiss(com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoRegionMiss) RawCASResponse(org.tikv.kvproto.Kvrpcpb.RawCASResponse) RangeSplitter(com.pingcap.tikv.util.RangeSplitter) Set(java.util.Set) SelectException(com.pingcap.tikv.exception.SelectException) Collectors(java.util.stream.Collectors) RawGetKeyTTLResponse(org.tikv.kvproto.Kvrpcpb.RawGetKeyTTLResponse) ByteString(com.google.protobuf.ByteString) Objects(java.util.Objects) List(java.util.List) TiKVException(com.pingcap.tikv.exception.TiKVException) Lock(com.pingcap.tikv.txn.Lock) GetRequest(org.tikv.kvproto.Kvrpcpb.GetRequest) TiConfiguration(com.pingcap.tikv.TiConfiguration) ResolveLockResult(com.pingcap.tikv.txn.ResolveLockResult) Queue(java.util.Queue) Mutation(org.tikv.kvproto.Kvrpcpb.Mutation) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) RawPutResponse(org.tikv.kvproto.Kvrpcpb.RawPutResponse) LockException(com.pingcap.tikv.exception.LockException) BatchGetResponse(org.tikv.kvproto.Kvrpcpb.BatchGetResponse) RawBatchDeleteRequest(org.tikv.kvproto.Kvrpcpb.RawBatchDeleteRequest) Store(org.tikv.kvproto.Metapb.Store) Version(com.pingcap.tikv.Version) HashMap(java.util.HashMap) RawDeleteResponse(org.tikv.kvproto.Kvrpcpb.RawDeleteResponse) PrewriteResponse(org.tikv.kvproto.Kvrpcpb.PrewriteResponse) SplitRegionResponse(org.tikv.kvproto.Kvrpcpb.SplitRegionResponse) Supplier(java.util.function.Supplier) ScanResponse(org.tikv.kvproto.Kvrpcpb.ScanResponse) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BoTxnLockFast(com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLockFast) GrpcException(com.pingcap.tikv.exception.GrpcException) GetResponse(org.tikv.kvproto.Kvrpcpb.GetResponse) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) KeyError(org.tikv.kvproto.Kvrpcpb.KeyError) TxnHeartBeatRequest(org.tikv.kvproto.Kvrpcpb.TxnHeartBeatRequest) BoTxnLock(com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock) RawGetResponse(org.tikv.kvproto.Kvrpcpb.RawGetResponse) KeyException(com.pingcap.tikv.exception.KeyException) RawGetRequest(org.tikv.kvproto.Kvrpcpb.RawGetRequest) RawCASRequest(org.tikv.kvproto.Kvrpcpb.RawCASRequest) Logger(org.slf4j.Logger) PrewriteRequest(org.tikv.kvproto.Kvrpcpb.PrewriteRequest) StoreVersion(com.pingcap.tikv.StoreVersion) Iterator(java.util.Iterator) CommitRequest(org.tikv.kvproto.Kvrpcpb.CommitRequest) TikvGrpc(org.tikv.kvproto.TikvGrpc) BatchGetRequest(org.tikv.kvproto.Kvrpcpb.BatchGetRequest) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) RawScanResponse(org.tikv.kvproto.Kvrpcpb.RawScanResponse) RawBatchPutRequest(org.tikv.kvproto.Kvrpcpb.RawBatchPutRequest) RawBatchDeleteResponse(org.tikv.kvproto.Kvrpcpb.RawBatchDeleteResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AbstractLockResolverClient(com.pingcap.tikv.txn.AbstractLockResolverClient) Collections(java.util.Collections) SplitRegionRequest(org.tikv.kvproto.Kvrpcpb.SplitRegionRequest) SplitRegionResponse(org.tikv.kvproto.Kvrpcpb.SplitRegionResponse) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) SplitRegionRequest(org.tikv.kvproto.Kvrpcpb.SplitRegionRequest) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler)

Example 15 with TiClientInternalException

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

the class DAGIterator method processByStreaming.

private Iterator<SelectResponse> processByStreaming(RangeSplitter.RegionTask regionTask) {
    List<Coprocessor.KeyRange> ranges = regionTask.getRanges();
    TiRegion region = regionTask.getRegion();
    Metapb.Store 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(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) Metapb(org.tikv.kvproto.Metapb) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) RegionTaskException(com.pingcap.tikv.exception.RegionTaskException)

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