Search in sources :

Example 1 with KeyException

use of org.tikv.common.exception.KeyException in project client-java by tikv.

the class ConcreteScanIterator method resolveCurrentLock.

private ByteString resolveCurrentLock(Kvrpcpb.KvPair current) {
    logger.warn(String.format("resolve current key error %s", current.getError().toString()));
    Pair<TiRegion, TiStore> pair = builder.getRegionManager().getRegionStorePairByKey(current.getKey());
    TiRegion region = pair.first;
    TiStore store = pair.second;
    BackOffer backOffer = ConcreteBackOffer.newGetBackOff(builder.getRegionManager().getPDClient().getClusterId());
    try (RegionStoreClient client = builder.build(region, store)) {
        return client.get(backOffer, current.getKey(), version);
    } catch (Exception e) {
        throw new KeyException(current.getError());
    }
}
Also used : TiRegion(org.tikv.common.region.TiRegion) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) BackOffer(org.tikv.common.util.BackOffer) TiStore(org.tikv.common.region.TiStore) RegionStoreClient(org.tikv.common.region.RegionStoreClient) KeyException(org.tikv.common.exception.KeyException) GrpcException(org.tikv.common.exception.GrpcException) KeyException(org.tikv.common.exception.KeyException)

Example 2 with KeyException

use of org.tikv.common.exception.KeyException in project client-java by tikv.

the class KVErrorHandler method handleResponseError.

// Referenced from TiDB
// store/tikv/region_request.go - onRegionError
/**
 * @return true: client should retry
 */
@Override
public boolean handleResponseError(BackOffer backOffer, RespT resp) {
    if (resp == null) {
        String msg = String.format("Request Failed with unknown reason for [%s]", regionHandler.getRegion());
        logger.warn(msg);
        return handleRequestError(backOffer, new GrpcException(msg));
    }
    Errorpb.Error error = regionHandler.getRegionError(resp);
    if (error != null) {
        return regionHandler.handleRegionError(backOffer, error);
    }
    // Key error handling logic
    Kvrpcpb.KeyError keyError = getKeyError.apply(resp);
    if (keyError != null) {
        try {
            Lock lock = AbstractLockResolverClient.extractLockFromKeyErr(keyError);
            resolveLock(backOffer, lock);
            return true;
        } catch (KeyException e) {
            logger.warn("Unable to handle KeyExceptions other than LockException", e);
        }
    }
    return false;
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) GrpcException(org.tikv.common.exception.GrpcException) Errorpb(org.tikv.kvproto.Errorpb) KeyException(org.tikv.common.exception.KeyException) Lock(org.tikv.txn.Lock)

Example 3 with KeyException

use of org.tikv.common.exception.KeyException in project client-java by tikv.

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.getLeaderContext()).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(org.tikv.common.exception.TiClientInternalException) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) KVErrorHandler(org.tikv.common.operation.KVErrorHandler) RegionException(org.tikv.common.exception.RegionException) KeyException(org.tikv.common.exception.KeyException)

Example 4 with KeyException

use of org.tikv.common.exception.KeyException in project client-java by tikv.

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.getLeaderContext()).setStartVersion(lock.getTxnID()).setCommitVersion(txnStatus).build();
        } else {
            factory = () -> ResolveLockRequest.newBuilder().setContext(region.getLeaderContext()).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(org.tikv.common.exception.TiClientInternalException) ResolveLockRequest(org.tikv.kvproto.Kvrpcpb.ResolveLockRequest) KVErrorHandler(org.tikv.common.operation.KVErrorHandler) RegionException(org.tikv.common.exception.RegionException) ResolveLockResponse(org.tikv.kvproto.Kvrpcpb.ResolveLockResponse) KeyException(org.tikv.common.exception.KeyException)

Example 5 with KeyException

use of org.tikv.common.exception.KeyException in project client-java by tikv.

the class LockResolverClientV2 method getTxnStatus.

private Long getTxnStatus(BackOffer bo, Long txnID, ByteString primary) {
    Long status = getResolved(txnID);
    if (status != null) {
        return status;
    }
    while (true) {
        // refresh region
        region = regionManager.getRegionByKey(primary);
        Supplier<CleanupRequest> factory = () -> CleanupRequest.newBuilder().setContext(region.getLeaderContext()).setKey(primary).setStartVersion(txnID).build();
        KVErrorHandler<CleanupResponse> handler = new KVErrorHandler<>(regionManager, this, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, 0L, false);
        CleanupResponse resp = callWithRetry(bo, TikvGrpc.getKvCleanupMethod(), factory, handler);
        status = 0L;
        if (resp == null) {
            logger.error("getKvCleanupMethod failed without a cause");
            regionManager.onRequestFail(region);
            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()) {
            logger.error(String.format("unexpected cleanup err: %s, tid: %d", resp.getError(), txnID));
            throw new KeyException(resp.getError());
        }
        if (resp.getCommitVersion() != 0) {
            status = resp.getCommitVersion();
        }
        saveResolved(txnID, status);
        return status;
    }
}
Also used : CleanupResponse(org.tikv.kvproto.Kvrpcpb.CleanupResponse) TiClientInternalException(org.tikv.common.exception.TiClientInternalException) CleanupRequest(org.tikv.kvproto.Kvrpcpb.CleanupRequest) KVErrorHandler(org.tikv.common.operation.KVErrorHandler) RegionException(org.tikv.common.exception.RegionException) KeyException(org.tikv.common.exception.KeyException)

Aggregations

KeyException (org.tikv.common.exception.KeyException)12 RegionException (org.tikv.common.exception.RegionException)9 TiClientInternalException (org.tikv.common.exception.TiClientInternalException)9 KVErrorHandler (org.tikv.common.operation.KVErrorHandler)7 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)6 Lock (org.tikv.txn.Lock)3 ResolveLockResult (org.tikv.txn.ResolveLockResult)3 ArrayList (java.util.ArrayList)2 GrpcException (org.tikv.common.exception.GrpcException)2 BoTxnLock (org.tikv.common.util.BackOffFunction.BackOffFuncType.BoTxnLock)2 CleanupRequest (org.tikv.kvproto.Kvrpcpb.CleanupRequest)2 CleanupResponse (org.tikv.kvproto.Kvrpcpb.CleanupResponse)2 TiKVException (org.tikv.common.exception.TiKVException)1 RegionStoreClient (org.tikv.common.region.RegionStoreClient)1 TiRegion (org.tikv.common.region.TiRegion)1 TiStore (org.tikv.common.region.TiStore)1 BackOffer (org.tikv.common.util.BackOffer)1 ConcreteBackOffer (org.tikv.common.util.ConcreteBackOffer)1 Errorpb (org.tikv.kvproto.Errorpb)1 KeyError (org.tikv.kvproto.Kvrpcpb.KeyError)1