Search in sources :

Example 6 with RegionException

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

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

the class LockResolverClientV3 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 8 with RegionException

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

the class LockResolverClientV3 method getTxnStatus.

private TxnStatus getTxnStatus(BackOffer bo, Long txnID, ByteString primary, Long currentTS) {
    TxnStatus status = getResolved(txnID);
    if (status != null) {
        return status;
    }
    Supplier<CleanupRequest> factory = () -> {
        TiRegion primaryKeyRegion = regionManager.getRegionByKey(primary);
        return CleanupRequest.newBuilder().setContext(primaryKeyRegion.getContext()).setKey(primary).setStartVersion(txnID).setCurrentTs(currentTS).build();
    };
    status = new TxnStatus();
    while (true) {
        // new RegionStoreClient for PrimaryKey
        RegionStoreClient primaryKeyRegionStoreClient = clientBuilder.build(primary);
        TiRegion primaryKeyRegion = primaryKeyRegionStoreClient.getRegion();
        KVErrorHandler<CleanupResponse> handler = new KVErrorHandler<>(regionManager, primaryKeyRegionStoreClient, primaryKeyRegionStoreClient.lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, 0L, false);
        CleanupResponse resp = primaryKeyRegionStoreClient.callWithRetry(bo, TikvGrpc.getKvCleanupMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKvCleanupMethod failed without a cause");
            regionManager.onRequestFail(primaryKeyRegion);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCleanupMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.hasError()) {
            Kvrpcpb.KeyError keyError = resp.getError();
            // the TTL.
            if (keyError.hasLocked()) {
                Kvrpcpb.LockInfo lockInfo = keyError.getLocked();
                return new TxnStatus(lockInfo.getLockTtl(), 0L);
            }
            logger.error(String.format("unexpected cleanup err: %s, tid: %d", keyError, txnID));
            throw new KeyException(keyError);
        }
        if (resp.getCommitVersion() != 0) {
            status = new TxnStatus(0L, resp.getCommitVersion());
        }
        saveResolved(txnID, status);
        return status;
    }
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException) CleanupResponse(org.tikv.kvproto.Kvrpcpb.CleanupResponse) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) CleanupRequest(org.tikv.kvproto.Kvrpcpb.CleanupRequest) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 9 with RegionException

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

the class LockResolverClientV4 method getTxnStatus.

/**
 * getTxnStatus sends the CheckTxnStatus request to the TiKV server. When rollbackIfNotExist is
 * false, the caller should be careful with the TxnNotFoundException error.
 */
private TxnStatus getTxnStatus(BackOffer bo, Long txnID, ByteString primary, Long callerStartTS, Long currentTS, boolean rollbackIfNotExist, Lock lock) {
    TxnStatus status = getResolved(txnID);
    if (status != null) {
        return status;
    }
    // CheckTxnStatus may meet the following cases:
    // 1. LOCK
    // 1.1 Lock expired -- orphan lock, fail to update TTL, crash recovery etc.
    // 1.2 Lock TTL -- active transaction holding the lock.
    // 2. NO LOCK
    // 2.1 Txn Committed
    // 2.2 Txn Rollbacked -- rollback itself, rollback by others, GC tomb etc.
    // 2.3 No lock -- pessimistic lock rollback, concurrence prewrite.
    Supplier<Kvrpcpb.CheckTxnStatusRequest> factory = () -> {
        TiRegion primaryKeyRegion = regionManager.getRegionByKey(primary);
        return Kvrpcpb.CheckTxnStatusRequest.newBuilder().setContext(primaryKeyRegion.getContext()).setPrimaryKey(primary).setLockTs(txnID).setCallerStartTs(callerStartTS).setCurrentTs(currentTS).setRollbackIfNotExist(rollbackIfNotExist).build();
    };
    while (true) {
        // new RegionStoreClient for PrimaryKey
        RegionStoreClient primaryKeyRegionStoreClient = clientBuilder.build(primary);
        TiRegion primaryKeyRegion = primaryKeyRegionStoreClient.getRegion();
        KVErrorHandler<Kvrpcpb.CheckTxnStatusResponse> handler = new KVErrorHandler<>(regionManager, primaryKeyRegionStoreClient, primaryKeyRegionStoreClient.lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, callerStartTS, false);
        Kvrpcpb.CheckTxnStatusResponse resp = primaryKeyRegionStoreClient.callWithRetry(bo, TikvGrpc.getKvCheckTxnStatusMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKvCheckTxnStatusMethod failed without a cause");
            regionManager.onRequestFail(primaryKeyRegion);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCheckTxnStatusMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.hasError()) {
            Kvrpcpb.KeyError keyError = resp.getError();
            if (keyError.hasTxnNotFound()) {
                throw new TxnNotFoundException();
            }
            logger.error(String.format("unexpected cleanup err: %s, tid: %d", keyError, txnID));
            throw new KeyException(keyError);
        }
        status = new TxnStatus();
        status.setAction(resp.getAction());
        status.setPrimaryLock(resp.getLockInfo());
        if (status.getPrimaryLock() != null && status.getPrimaryLock().getUseAsyncCommit()) {
            if (!TsoUtils.isExpired(txnID, resp.getLockTtl())) {
                status.setTtl(resp.getLockTtl());
            }
        } else if (resp.getLockTtl() != 0) {
            status.setTtl(resp.getLockTtl());
        } else {
            status.setCommitTS(resp.getCommitVersion());
            // change.
            if (status.isCommitted() || (lock != null && lock.getLockType() != org.tikv.kvproto.Kvrpcpb.Op.PessimisticLock)) {
                saveResolved(txnID, status);
            }
        }
        return status;
    }
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TxnNotFoundException(com.pingcap.tikv.exception.TxnNotFoundException) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 10 with RegionException

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

the class LockResolverClientV4 method resolvePessimisticLock.

private void resolvePessimisticLock(BackOffer bo, Lock lock, Set<RegionVerID> cleanRegion) {
    while (true) {
        region = regionManager.getRegionByKey(lock.getKey());
        if (cleanRegion.contains(region.getVerID())) {
            return;
        }
        final long forUpdateTS = lock.getLockForUpdateTs() == 0L ? Long.MAX_VALUE : lock.getLockForUpdateTs();
        Supplier<Kvrpcpb.PessimisticRollbackRequest> factory = () -> Kvrpcpb.PessimisticRollbackRequest.newBuilder().setContext(region.getContext()).setStartVersion(lock.getTxnID()).setForUpdateTs(forUpdateTS).addKeys(lock.getKey()).build();
        KVErrorHandler<Kvrpcpb.PessimisticRollbackResponse> handler = new KVErrorHandler<>(regionManager, this, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.getErrorsCount() > 0 ? resp.getErrorsList().get(0) : null, resolveLockResult -> null, 0L, false);
        Kvrpcpb.PessimisticRollbackResponse resp = callWithRetry(bo, TikvGrpc.getKVPessimisticRollbackMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKVPessimisticRollbackMethod failed without a cause");
            regionManager.onRequestFail(region);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKVPessimisticRollbackMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.getErrorsCount() > 0) {
            logger.error(String.format("unexpected resolveLock err: %s, lock: %s", resp.getErrorsList().get(0), lock));
            throw new KeyException(resp.getErrorsList().get(0));
        }
    }
}
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)

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