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);
}
}
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);
}
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;
}
}
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;
}
}
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;
}
}
}
Aggregations