Search in sources :

Example 1 with ResolveLockResult

use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.

the class RegionStoreClient method handleCopResponse.

// handleCopResponse checks coprocessor Response for region split and lock,
// returns more tasks when that happens, or handles the response if no error.
// if we're handling streaming coprocessor response, lastRange is the range of last
// successful response, otherwise it's nil.
private List<RangeSplitter.RegionTask> handleCopResponse(BackOffer backOffer, Coprocessor.Response response, List<Coprocessor.KeyRange> ranges, Queue<SelectResponse> responseQueue, long startTs) {
    boolean forWrite = false;
    if (response == null) {
        // Send request failed, reasons may:
        // 1. TiKV down
        // 2. Network partition
        backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException("TiKV down or Network partition"));
        logger.warn("Re-splitting region task due to region error: TiKV down or Network partition");
        // Split ranges
        return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
    }
    if (response.hasRegionError()) {
        Errorpb.Error regionError = response.getRegionError();
        backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(regionError.toString()));
        logger.warn("Re-splitting region task due to region error:" + regionError.getMessage());
        // Split ranges
        return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
    }
    if (response.hasLocked()) {
        Lock lock = new Lock(response.getLocked());
        logger.debug(String.format("coprocessor encounters locks: %s", lock));
        ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, startTs, Collections.singletonList(lock), forWrite);
        addResolvedLocks(startTs, resolveLockResult.getResolvedLocks());
        long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
        if (msBeforeExpired > 0) {
            backOffer.doBackOffWithMaxSleep(BoTxnLockFast, msBeforeExpired, new LockException(lock));
        }
        // Split ranges
        return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
    }
    String otherError = response.getOtherError();
    if (!otherError.isEmpty()) {
        logger.warn(String.format("Other error occurred, message: %s", otherError));
        throw new GrpcException(otherError);
    }
    responseQueue.offer(doCoprocessor(response));
    return null;
}
Also used : LockException(com.pingcap.tikv.exception.LockException) GrpcException(com.pingcap.tikv.exception.GrpcException) Errorpb(org.tikv.kvproto.Errorpb) ResolveLockResult(com.pingcap.tikv.txn.ResolveLockResult) ByteString(com.google.protobuf.ByteString) Lock(com.pingcap.tikv.txn.Lock) BoTxnLock(com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)

Example 2 with ResolveLockResult

use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.

the class RegionStoreClient method isPrewriteSuccess.

/**
 * @param backOffer backOffer
 * @param resp response
 * @return Return true means the rpc call success. Return false means the rpc call fail,
 *     RegionStoreClient should retry. Throw an Exception means the rpc call fail,
 *     RegionStoreClient cannot handle this kind of error
 * @throws TiClientInternalException
 * @throws RegionException
 * @throws KeyException
 */
private boolean isPrewriteSuccess(BackOffer backOffer, PrewriteResponse resp, long startTs) throws TiClientInternalException, KeyException, RegionException {
    boolean forWrite = true;
    if (resp == null) {
        this.regionManager.onRequestFail(region);
        throw new TiClientInternalException("Prewrite Response failed without a cause");
    }
    if (resp.hasRegionError()) {
        throw new RegionException(resp.getRegionError());
    }
    boolean isSuccess = true;
    List<Lock> locks = new ArrayList<>();
    for (KeyError err : resp.getErrorsList()) {
        if (err.hasLocked()) {
            isSuccess = false;
            Lock lock = new Lock(err.getLocked());
            locks.add(lock);
        } else {
            throw new KeyException(err.toString());
        }
    }
    if (isSuccess) {
        return true;
    }
    ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, startTs, locks, forWrite);
    addResolvedLocks(startTs, resolveLockResult.getResolvedLocks());
    long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
    if (msBeforeExpired > 0) {
        backOffer.doBackOffWithMaxSleep(BoTxnLock, msBeforeExpired, new KeyException(resp.getErrorsList().get(0)));
    }
    return false;
}
Also used : KeyError(org.tikv.kvproto.Kvrpcpb.KeyError) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) 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 3 with ResolveLockResult

use of com.pingcap.tikv.txn.ResolveLockResult 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 4 with ResolveLockResult

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

use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.

the class KVErrorHandler method resolveLock.

private void resolveLock(BackOffer backOffer, Lock lock) {
    if (lockResolverClient != null) {
        logger.warn("resolving lock");
        ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, callerStartTS, Collections.singletonList(lock), forWrite);
        resolveLockResultCallback.apply(resolveLockResult);
        long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
        if (msBeforeExpired > 0) {
            // if not resolve all locks, we wait and retry
            backOffer.doBackOffWithMaxSleep(BoTxnLockFast, msBeforeExpired, new KeyException(lock.toString()));
        }
    }
}
Also used : ResolveLockResult(com.pingcap.tikv.txn.ResolveLockResult) KeyException(com.pingcap.tikv.exception.KeyException)

Aggregations

ResolveLockResult (com.pingcap.tikv.txn.ResolveLockResult)5 KeyException (com.pingcap.tikv.exception.KeyException)4 Lock (com.pingcap.tikv.txn.Lock)4 BoTxnLock (com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)4 RegionException (com.pingcap.tikv.exception.RegionException)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)3 ArrayList (java.util.ArrayList)3 ByteString (com.google.protobuf.ByteString)2 GrpcException (com.pingcap.tikv.exception.GrpcException)2 LockException (com.pingcap.tikv.exception.LockException)2 TiKVException (com.pingcap.tikv.exception.TiKVException)2 KvPair (org.tikv.kvproto.Kvrpcpb.KvPair)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 DAGRequest (com.pingcap.tidb.tipb.DAGRequest)1 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)1 PDClient (com.pingcap.tikv.PDClient)1 StoreVersion (com.pingcap.tikv.StoreVersion)1 TiConfiguration (com.pingcap.tikv.TiConfiguration)1 Version (com.pingcap.tikv.Version)1