use of com.pingcap.tikv.exception.RegionException in project tispark by pingcap.
the class RegionStoreClient method isPrewriteSuccess.
/**
* @param backOffer backOffer
* @param resp response
* @return Return true means the rpc call success. Return false means the rpc call fail,
* RegionStoreClient should retry. Throw an Exception means the rpc call fail,
* RegionStoreClient cannot handle this kind of error
* @throws TiClientInternalException
* @throws RegionException
* @throws KeyException
*/
private boolean isPrewriteSuccess(BackOffer backOffer, PrewriteResponse resp, long startTs) throws TiClientInternalException, KeyException, RegionException {
boolean forWrite = true;
if (resp == null) {
this.regionManager.onRequestFail(region);
throw new TiClientInternalException("Prewrite Response failed without a cause");
}
if (resp.hasRegionError()) {
throw new RegionException(resp.getRegionError());
}
boolean isSuccess = true;
List<Lock> locks = new ArrayList<>();
for (KeyError err : resp.getErrorsList()) {
if (err.hasLocked()) {
isSuccess = false;
Lock lock = new Lock(err.getLocked());
locks.add(lock);
} else {
throw new KeyException(err.toString());
}
}
if (isSuccess) {
return true;
}
ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, startTs, locks, forWrite);
addResolvedLocks(startTs, resolveLockResult.getResolvedLocks());
long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
if (msBeforeExpired > 0) {
backOffer.doBackOffWithMaxSleep(BoTxnLock, msBeforeExpired, new KeyException(resp.getErrorsList().get(0)));
}
return false;
}
use of com.pingcap.tikv.exception.RegionException 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.exception.RegionException 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.exception.RegionException in project tispark by pingcap.
the class LockResolverTest method commit.
boolean commit(List<ByteString> keys, long startTS, long commitTS) {
if (keys.size() == 0)
return true;
BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
for (ByteString byteStringK : keys) {
while (true) {
try {
TiRegion tiRegion = session.getRegionManager().getRegionByKey(byteStringK);
RegionStoreClient client = builder.build(tiRegion);
client.commit(backOffer, Collections.singletonList(byteStringK), startTS, commitTS);
break;
} catch (RegionException e) {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
}
}
}
return true;
}
use of com.pingcap.tikv.exception.RegionException in project tispark by pingcap.
the class LockResolverTest method prewrite.
boolean prewrite(List<Mutation> mutations, long startTS, ByteString primary, long ttl, boolean useAsyncCommit, Iterable<ByteString> secondaries) {
if (mutations.size() == 0)
return true;
BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
for (Mutation m : mutations) {
while (true) {
try {
TiRegion region = session.getRegionManager().getRegionByKey(m.getKey());
RegionStoreClient client = builder.build(region);
client.prewrite(backOffer, primary, Collections.singletonList(m), startTS, ttl, false, useAsyncCommit, secondaries);
break;
} catch (RegionException e) {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
}
}
}
return true;
}
Aggregations