use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.
the class LockResolverClientV4 method resolvePessimisticLock.
private void resolvePessimisticLock(BackOffer bo, Lock lock, Set<RegionVerID> cleanRegion) {
while (true) {
region = regionManager.getRegionByKey(lock.getKey());
if (cleanRegion.contains(region.getVerID())) {
return;
}
final long forUpdateTS = lock.getLockForUpdateTs() == 0L ? Long.MAX_VALUE : lock.getLockForUpdateTs();
Supplier<Kvrpcpb.PessimisticRollbackRequest> factory = () -> Kvrpcpb.PessimisticRollbackRequest.newBuilder().setContext(region.getContext()).setStartVersion(lock.getTxnID()).setForUpdateTs(forUpdateTS).addKeys(lock.getKey()).build();
KVErrorHandler<Kvrpcpb.PessimisticRollbackResponse> handler = new KVErrorHandler<>(regionManager, this, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.getErrorsCount() > 0 ? resp.getErrorsList().get(0) : null, resolveLockResult -> null, 0L, false);
Kvrpcpb.PessimisticRollbackResponse resp = callWithRetry(bo, TikvGrpc.getKVPessimisticRollbackMethod(), factory, handler);
if (resp == null) {
logger.error("getKVPessimisticRollbackMethod failed without a cause");
regionManager.onRequestFail(region);
bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKVPessimisticRollbackMethod failed without a cause"));
continue;
}
if (resp.hasRegionError()) {
bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
continue;
}
if (resp.getErrorsCount() > 0) {
logger.error(String.format("unexpected resolveLock err: %s, lock: %s", resp.getErrorsList().get(0), lock));
throw new KeyException(resp.getErrorsList().get(0));
}
}
}
use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.
the class LockResolverClientV4 method checkSecondaries.
private boolean checkSecondaries(BackOffer bo, long txnID, List<ByteString> curKeys, TiRegion tiRegion, AsyncResolveData shared) {
RegionStoreClient regionStoreClient = clientBuilder.build(tiRegion);
Supplier<Kvrpcpb.CheckSecondaryLocksRequest> factory = () -> Kvrpcpb.CheckSecondaryLocksRequest.newBuilder().setContext(tiRegion.getContext()).setStartVersion(txnID).addAllKeys(curKeys).build();
KVErrorHandler<Kvrpcpb.CheckSecondaryLocksResponse> handler = new KVErrorHandler<>(regionManager, regionStoreClient, regionStoreClient.lockResolverClient, resp -> null, resp -> null, resolveLockResult -> null, 0L, false);
Kvrpcpb.CheckSecondaryLocksResponse resp = regionStoreClient.callWithRetry(bo, TikvGrpc.getKvCheckSecondaryLocksMethod(), factory, handler);
if (resp == null) {
logger.error("getKvCheckSecondaryLocksMethod failed without a cause");
regionManager.onRequestFail(tiRegion);
bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCheckSecondaryLocksMethod failed without a cause"));
logger.debug(String.format("checkSecondaries: region error, regrouping, txnID=%d, regionId=%d", txnID, tiRegion.getId()));
// If regions have changed, then we might need to regroup the keys. Since this should be rare
// and for the sake of simplicity, we will resolve regions sequentially.
Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, curKeys, bo);
for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
TiRegion region = entry.getKey();
List<ByteString> keys = entry.getValue();
checkSecondaries(bo, txnID, keys, region, shared);
}
}
shared.addKeys(resp.getLocksList(), curKeys.size(), txnID, resp.getCommitTs());
return true;
}
use of com.pingcap.tikv.exception.TiClientInternalException 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;
}
use of com.pingcap.tikv.exception.TiClientInternalException in project tispark by pingcap.
the class TxnKVClient method getTimestamp.
public TiTimestamp getTimestamp() {
BackOffer bo = ConcreteBackOffer.newTsoBackOff();
TiTimestamp timestamp = new TiTimestamp(0, 0);
try {
while (true) {
try {
timestamp = pdClient.getTimestamp(bo);
break;
} catch (final TiKVException | TiClientInternalException e) {
// retry is exhausted
bo.doBackOff(BackOffFunction.BackOffFuncType.BoPDRPC, e);
}
}
} catch (GrpcException e1) {
LOG.error("Get tso from pd failed,", e1);
}
return timestamp;
}
use of com.pingcap.tikv.exception.TiClientInternalException 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;
}
}
Aggregations