Search in sources :

Example 26 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler 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 27 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler 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

KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)27 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)10 KeyException (com.pingcap.tikv.exception.KeyException)9 RegionException (com.pingcap.tikv.exception.RegionException)8 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)7 RawBatchGetRequest (org.tikv.kvproto.Kvrpcpb.RawBatchGetRequest)5 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)4 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)4 TiRegion (com.pingcap.tikv.region.TiRegion)4 ArrayList (java.util.ArrayList)4 BatchGetRequest (org.tikv.kvproto.Kvrpcpb.BatchGetRequest)4 RawBatchGetResponse (org.tikv.kvproto.Kvrpcpb.RawBatchGetResponse)4 RawGetRequest (org.tikv.kvproto.Kvrpcpb.RawGetRequest)4 RawScanRequest (org.tikv.kvproto.Kvrpcpb.RawScanRequest)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 BatchGetResponse (org.tikv.kvproto.Kvrpcpb.BatchGetResponse)3 GetRequest (org.tikv.kvproto.Kvrpcpb.GetRequest)3 PrewriteRequest (org.tikv.kvproto.Kvrpcpb.PrewriteRequest)3