Search in sources :

Example 6 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler in project tispark by pingcap.

the class RegionStoreClient method scan.

public List<KvPair> scan(BackOffer backOffer, ByteString startKey, int limit, long version, boolean keyOnly) {
    boolean forWrite = false;
    while (true) {
        Supplier<ScanRequest> request = () -> ScanRequest.newBuilder().setContext(region.getContext(getResolvedLocks(version))).setStartKey(startKey).setVersion(version).setKeyOnly(keyOnly).setLimit(limit).build();
        KVErrorHandler<ScanResponse> handler = new KVErrorHandler<>(regionManager, this, lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> null, resolveLockResult -> addResolvedLocks(version, resolveLockResult.getResolvedLocks()), version, forWrite);
        ScanResponse resp = callWithRetry(backOffer, TikvGrpc.getKvScanMethod(), request, handler);
        if (isScanSuccess(backOffer, resp)) {
            return doScan(resp);
        }
        // we should refresh region
        region = regionManager.getRegionByKey(startKey);
    }
}
Also used : ScanRequest(org.tikv.kvproto.Kvrpcpb.ScanRequest) RawScanRequest(org.tikv.kvproto.Kvrpcpb.RawScanRequest) ScanResponse(org.tikv.kvproto.Kvrpcpb.ScanResponse) RawScanResponse(org.tikv.kvproto.Kvrpcpb.RawScanResponse) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler)

Example 7 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler in project tispark by pingcap.

the class RegionStoreClient method rawBatchDelete.

public void rawBatchDelete(BackOffer backoffer, List<ByteString> keys, boolean atomic) {
    if (keys.isEmpty()) {
        return;
    }
    Supplier<RawBatchDeleteRequest> factory = () -> RawBatchDeleteRequest.newBuilder().setContext(region.getContext()).addAllKeys(keys).setForCas(atomic).build();
    KVErrorHandler<RawBatchDeleteResponse> handler = new KVErrorHandler<>(regionManager, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null);
    RawBatchDeleteResponse resp = callWithRetry(backoffer, TikvGrpc.getRawBatchDeleteMethod(), factory, handler);
    handleRawBatchDelete(resp);
}
Also used : RawBatchDeleteRequest(org.tikv.kvproto.Kvrpcpb.RawBatchDeleteRequest) RawBatchDeleteResponse(org.tikv.kvproto.Kvrpcpb.RawBatchDeleteResponse) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler)

Example 8 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler in project tispark by pingcap.

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.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 9 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler in project tispark by pingcap.

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

Example 10 with KVErrorHandler

use of com.pingcap.tikv.operation.KVErrorHandler in project tispark by pingcap.

the class RegionStoreClient method txnHeartBeat.

/**
 * TXN Heart Beat: update primary key ttl
 */
public void txnHeartBeat(BackOffer bo, ByteString primaryLock, long startTs, long ttl) {
    boolean forWrite = false;
    while (true) {
        Supplier<TxnHeartBeatRequest> factory = () -> TxnHeartBeatRequest.newBuilder().setContext(region.getContext()).setStartVersion(startTs).setPrimaryLock(primaryLock).setAdviseLockTtl(ttl).build();
        KVErrorHandler<TxnHeartBeatResponse> handler = new KVErrorHandler<>(regionManager, this, lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, startTs, forWrite);
        TxnHeartBeatResponse resp = callWithRetry(bo, TikvGrpc.getKvTxnHeartBeatMethod(), factory, handler);
        if (isTxnHeartBeatSuccess(resp)) {
            return;
        }
    }
}
Also used : TxnHeartBeatRequest(org.tikv.kvproto.Kvrpcpb.TxnHeartBeatRequest) TxnHeartBeatResponse(org.tikv.kvproto.Kvrpcpb.TxnHeartBeatResponse) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler)

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