Search in sources :

Example 1 with RegionException

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

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

the class LockResolverClientV4 method resolveLock.

private void resolveLock(BackOffer bo, Lock lock, TxnStatus txnStatus, Set<RegionVerID> cleanRegion) {
    boolean cleanWholeRegion = lock.getTxnSize() >= BIG_TXN_THRESHOLD;
    while (true) {
        region = regionManager.getRegionByKey(lock.getKey());
        if (cleanRegion.contains(region.getVerID())) {
            return;
        }
        Kvrpcpb.ResolveLockRequest.Builder builder = Kvrpcpb.ResolveLockRequest.newBuilder().setContext(region.getContext()).setStartVersion(lock.getTxnID());
        if (txnStatus.isCommitted()) {
            // txn is committed with commitTS txnStatus
            builder.setCommitVersion(txnStatus.getCommitTS());
        }
        if (lock.getTxnSize() < BIG_TXN_THRESHOLD) {
            // Only resolve specified keys when it is a small transaction,
            // prevent from scanning the whole region in this case.
            builder.addKeys(lock.getKey());
        }
        Supplier<Kvrpcpb.ResolveLockRequest> factory = builder::build;
        KVErrorHandler<Kvrpcpb.ResolveLockResponse> handler = new KVErrorHandler<>(regionManager, this, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, 0L, false);
        Kvrpcpb.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());
        }
        if (cleanWholeRegion) {
            cleanRegion.add(region.getVerID());
        }
        return;
    }
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 3 with RegionException

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

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

the class LockResolverTest method commit.

boolean commit(List<ByteString> keys, long startTS, long commitTS) {
    if (keys.size() == 0)
        return true;
    BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
    for (ByteString byteStringK : keys) {
        while (true) {
            try {
                TiRegion tiRegion = session.getRegionManager().getRegionByKey(byteStringK);
                RegionStoreClient client = builder.build(tiRegion);
                client.commit(backOffer, Collections.singletonList(byteStringK), startTS, commitTS);
                break;
            } catch (RegionException e) {
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
            }
        }
    }
    return true;
}
Also used : ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionException(com.pingcap.tikv.exception.RegionException) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient)

Example 5 with RegionException

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

the class LockResolverTest method prewrite.

boolean prewrite(List<Mutation> mutations, long startTS, ByteString primary, long ttl, boolean useAsyncCommit, Iterable<ByteString> secondaries) {
    if (mutations.size() == 0)
        return true;
    BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
    for (Mutation m : mutations) {
        while (true) {
            try {
                TiRegion region = session.getRegionManager().getRegionByKey(m.getKey());
                RegionStoreClient client = builder.build(region);
                client.prewrite(backOffer, primary, Collections.singletonList(m), startTS, ttl, false, useAsyncCommit, secondaries);
                break;
            } catch (RegionException e) {
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
            }
        }
    }
    return true;
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) Mutation(org.tikv.kvproto.Kvrpcpb.Mutation) RegionException(com.pingcap.tikv.exception.RegionException) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient)

Aggregations

RegionException (com.pingcap.tikv.exception.RegionException)11 KeyException (com.pingcap.tikv.exception.KeyException)9 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)9 KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)7 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)5 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)4 TiRegion (com.pingcap.tikv.region.TiRegion)4 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)2 Lock (com.pingcap.tikv.txn.Lock)2 ResolveLockResult (com.pingcap.tikv.txn.ResolveLockResult)2 BoTxnLock (com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)2 BackOffer (com.pingcap.tikv.util.BackOffer)2 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)2 ArrayList (java.util.ArrayList)2 CleanupRequest (org.tikv.kvproto.Kvrpcpb.CleanupRequest)2 CleanupResponse (org.tikv.kvproto.Kvrpcpb.CleanupResponse)2 ByteString (com.google.protobuf.ByteString)1 TiKVException (com.pingcap.tikv.exception.TiKVException)1 TxnNotFoundException (com.pingcap.tikv.exception.TxnNotFoundException)1 KeyError (org.tikv.kvproto.Kvrpcpb.KeyError)1