use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.
the class RegionStoreClient method handleCopResponse.
// handleCopResponse checks coprocessor Response for region split and lock,
// returns more tasks when that happens, or handles the response if no error.
// if we're handling streaming coprocessor response, lastRange is the range of last
// successful response, otherwise it's nil.
private List<RangeSplitter.RegionTask> handleCopResponse(BackOffer backOffer, Coprocessor.Response response, List<Coprocessor.KeyRange> ranges, Queue<SelectResponse> responseQueue, long startTs) {
boolean forWrite = false;
if (response == null) {
// Send request failed, reasons may:
// 1. TiKV down
// 2. Network partition
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException("TiKV down or Network partition"));
logger.warn("Re-splitting region task due to region error: TiKV down or Network partition");
// Split ranges
return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
}
if (response.hasRegionError()) {
Errorpb.Error regionError = response.getRegionError();
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(regionError.toString()));
logger.warn("Re-splitting region task due to region error:" + regionError.getMessage());
// Split ranges
return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
}
if (response.hasLocked()) {
Lock lock = new Lock(response.getLocked());
logger.debug(String.format("coprocessor encounters locks: %s", lock));
ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, startTs, Collections.singletonList(lock), forWrite);
addResolvedLocks(startTs, resolveLockResult.getResolvedLocks());
long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
if (msBeforeExpired > 0) {
backOffer.doBackOffWithMaxSleep(BoTxnLockFast, msBeforeExpired, new LockException(lock));
}
// Split ranges
return RangeSplitter.newSplitter(this.regionManager).splitRangeByRegion(ranges, storeType);
}
String otherError = response.getOtherError();
if (!otherError.isEmpty()) {
logger.warn(String.format("Other error occurred, message: %s", otherError));
throw new GrpcException(otherError);
}
responseQueue.offer(doCoprocessor(response));
return null;
}
use of com.pingcap.tikv.txn.ResolveLockResult 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.txn.ResolveLockResult in project tispark by pingcap.
the class RegionStoreClient method handleBatchGetResponse.
private List<KvPair> handleBatchGetResponse(BackOffer backOffer, BatchGetResponse resp, long version) {
boolean forWrite = false;
if (resp == null) {
this.regionManager.onRequestFail(region);
throw new TiClientInternalException("BatchGetResponse failed without a cause");
}
if (resp.hasRegionError()) {
throw new RegionException(resp.getRegionError());
}
List<Lock> locks = new ArrayList<>();
for (KvPair pair : resp.getPairsList()) {
if (pair.hasError()) {
if (pair.getError().hasLocked()) {
Lock lock = new Lock(pair.getError().getLocked());
locks.add(lock);
} else {
throw new KeyException(pair.getError());
}
}
}
if (!locks.isEmpty()) {
ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, version, locks, forWrite);
addResolvedLocks(version, resolveLockResult.getResolvedLocks());
// resolveLocks already retried, just throw error to upper logic.
throw new TiKVException("locks not resolved, retry");
} else {
return resp.getPairsList();
}
}
use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.
the class RegionStoreClient method splitRegion.
/**
* Send SplitRegion request to tikv split a region at splitKey. splitKey must between current
* region's start key and end key.
*
* @param splitKeys is the split points for a specific region.
* @return a split region info.
*/
public List<TiRegion> splitRegion(Iterable<ByteString> splitKeys) {
Supplier<SplitRegionRequest> request = () -> SplitRegionRequest.newBuilder().setContext(region.getContext()).addAllSplitKeys(splitKeys).build();
KVErrorHandler<SplitRegionResponse> handler = new KVErrorHandler<>(regionManager, this, null, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> null, resolveLockResult -> null, 0L, false);
SplitRegionResponse resp = callWithRetry(ConcreteBackOffer.newGetBackOff(), TikvGrpc.getSplitRegionMethod(), request, handler);
if (resp == null) {
this.regionManager.onRequestFail(region);
throw new TiClientInternalException("SplitRegion Response failed without a cause");
}
if (resp.hasRegionError()) {
throw new TiClientInternalException(String.format("failed to split region %d because %s", region.getId(), resp.getRegionError().toString()));
}
return resp.getRegionsList().stream().map(region -> new TiRegion(region, null, conf.getIsolationLevel(), conf.getCommandPriority())).collect(Collectors.toList());
}
use of com.pingcap.tikv.txn.ResolveLockResult in project tispark by pingcap.
the class KVErrorHandler method resolveLock.
private void resolveLock(BackOffer backOffer, Lock lock) {
if (lockResolverClient != null) {
logger.warn("resolving lock");
ResolveLockResult resolveLockResult = lockResolverClient.resolveLocks(backOffer, callerStartTS, Collections.singletonList(lock), forWrite);
resolveLockResultCallback.apply(resolveLockResult);
long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
if (msBeforeExpired > 0) {
// if not resolve all locks, we wait and retry
backOffer.doBackOffWithMaxSleep(BoTxnLockFast, msBeforeExpired, new KeyException(lock.toString()));
}
}
}
Aggregations