Search in sources :

Example 1 with TxnNotFoundException

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

Aggregations

KeyException (com.pingcap.tikv.exception.KeyException)1 RegionException (com.pingcap.tikv.exception.RegionException)1 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 TxnNotFoundException (com.pingcap.tikv.exception.TxnNotFoundException)1 KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)1 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)1 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)1 TiRegion (com.pingcap.tikv.region.TiRegion)1 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)1