Search in sources :

Example 21 with TiClientInternalException

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

Example 22 with TiClientInternalException

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

the class LockResolverClientV4 method checkSecondaries.

private boolean checkSecondaries(BackOffer bo, long txnID, List<ByteString> curKeys, TiRegion tiRegion, AsyncResolveData shared) {
    RegionStoreClient regionStoreClient = clientBuilder.build(tiRegion);
    Supplier<Kvrpcpb.CheckSecondaryLocksRequest> factory = () -> Kvrpcpb.CheckSecondaryLocksRequest.newBuilder().setContext(tiRegion.getContext()).setStartVersion(txnID).addAllKeys(curKeys).build();
    KVErrorHandler<Kvrpcpb.CheckSecondaryLocksResponse> handler = new KVErrorHandler<>(regionManager, regionStoreClient, regionStoreClient.lockResolverClient, resp -> null, resp -> null, resolveLockResult -> null, 0L, false);
    Kvrpcpb.CheckSecondaryLocksResponse resp = regionStoreClient.callWithRetry(bo, TikvGrpc.getKvCheckSecondaryLocksMethod(), factory, handler);
    if (resp == null) {
        logger.error("getKvCheckSecondaryLocksMethod failed without a cause");
        regionManager.onRequestFail(tiRegion);
        bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCheckSecondaryLocksMethod failed without a cause"));
        logger.debug(String.format("checkSecondaries: region error, regrouping, txnID=%d, regionId=%d", txnID, tiRegion.getId()));
        // If regions have changed, then we might need to regroup the keys. Since this should be rare
        // and for the sake of simplicity, we will resolve regions sequentially.
        Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, curKeys, bo);
        for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
            TiRegion region = entry.getKey();
            List<ByteString> keys = entry.getValue();
            checkSecondaries(bo, txnID, keys, region, shared);
        }
    }
    shared.addKeys(resp.getLocksList(), curKeys.size(), txnID, resp.getCommitTs());
    return true;
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) ByteString(com.google.protobuf.ByteString) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 23 with TiClientInternalException

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

the class LockResolverClientV4 method resolveRegionLocks.

/**
 * resolveRegionLocks is essentially the same as resolveLock, but we resolve all keys in the same
 * region at the same time.
 */
private boolean resolveRegionLocks(BackOffer bo, Lock lock, TiRegion tiRegion, List<ByteString> keys, TxnStatus status) {
    RegionStoreClient regionStoreClient = clientBuilder.build(tiRegion);
    Supplier<Kvrpcpb.ResolveLockRequest> factory = () -> Kvrpcpb.ResolveLockRequest.newBuilder().setContext(tiRegion.getContext()).setStartVersion(lock.getTxnID()).setCommitVersion(status.getCommitTS()).addAllKeys(keys).build();
    KVErrorHandler<Kvrpcpb.ResolveLockResponse> handler = new KVErrorHandler<>(regionManager, regionStoreClient, regionStoreClient.lockResolverClient, resp -> null, resp -> null, resolveLockResult -> null, 0L, false);
    Kvrpcpb.ResolveLockResponse resp = regionStoreClient.callWithRetry(bo, TikvGrpc.getKvResolveLockMethod(), factory, handler);
    if (resp == null || resp.hasRegionError()) {
        logger.error("getKvResolveLockMethod failed without a cause");
        regionManager.onRequestFail(tiRegion);
        bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvResolveLockMethod failed without a cause"));
        logger.debug(String.format("resolveRegionLocks region error, regrouping lock=%s region=%d", lock, tiRegion.getId()));
        // Regroup locks.
        Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, keys, bo);
        for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
            TiRegion region = entry.getKey();
            resolveRegionLocks(bo, lock, region, entry.getValue(), status);
        }
    } else if (resp.hasError()) {
        logger.error(String.format("unexpected resolveLock err: %s, lock: %s", resp.getError(), lock));
        throw new KeyException(resp.getError());
    }
    return true;
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) KeyException(com.pingcap.tikv.exception.KeyException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 24 with TiClientInternalException

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

the class TxnKVClient method getTimestamp.

public TiTimestamp getTimestamp() {
    BackOffer bo = ConcreteBackOffer.newTsoBackOff();
    TiTimestamp timestamp = new TiTimestamp(0, 0);
    try {
        while (true) {
            try {
                timestamp = pdClient.getTimestamp(bo);
                break;
            } catch (final TiKVException | TiClientInternalException e) {
                // retry is exhausted
                bo.doBackOff(BackOffFunction.BackOffFuncType.BoPDRPC, e);
            }
        }
    } catch (GrpcException e1) {
        LOG.error("Get tso from pd failed,", e1);
    }
    return timestamp;
}
Also used : TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiKVException(com.pingcap.tikv.exception.TiKVException) GrpcException(com.pingcap.tikv.exception.GrpcException) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer)

Example 25 with TiClientInternalException

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

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.getContext()).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(com.pingcap.tikv.exception.TiClientInternalException) CleanupRequest(org.tikv.kvproto.Kvrpcpb.CleanupRequest) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException)

Aggregations

TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)25 KeyException (com.pingcap.tikv.exception.KeyException)11 RegionException (com.pingcap.tikv.exception.RegionException)10 KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)10 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)7 ByteString (com.google.protobuf.ByteString)6 TiRegion (com.pingcap.tikv.region.TiRegion)6 ArrayList (java.util.ArrayList)6 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)5 GrpcException (com.pingcap.tikv.exception.GrpcException)4 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)4 HashMap (java.util.HashMap)4 TiKVException (com.pingcap.tikv.exception.TiKVException)3 Lock (com.pingcap.tikv.txn.Lock)3 ResolveLockResult (com.pingcap.tikv.txn.ResolveLockResult)3 BoTxnLock (com.pingcap.tikv.util.BackOffFunction.BackOffFuncType.BoTxnLock)3 List (java.util.List)3 Map (java.util.Map)3 DAGRequest (com.pingcap.tidb.tipb.DAGRequest)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2