use of com.pingcap.tikv.exception.GrpcException in project tispark by pingcap.
the class TwoPhaseCommitter method doPrewritePrimaryKeyWithRetry.
private void doPrewritePrimaryKeyWithRetry(BackOffer backOffer, ByteString key, ByteString value) throws TiBatchWriteException {
Pair<TiRegion, Metapb.Store> pair = this.regionManager.getRegionStorePairByKey(key, backOffer);
TiRegion tiRegion = pair.first;
Kvrpcpb.Mutation mutation;
if (!value.isEmpty()) {
mutation = Kvrpcpb.Mutation.newBuilder().setKey(key).setValue(value).setOp(Op.Put).build();
} else {
mutation = Kvrpcpb.Mutation.newBuilder().setKey(key).setOp(Op.Del).build();
}
List<Kvrpcpb.Mutation> mutationList = Collections.singletonList(mutation);
// send rpc request to tikv server
long lockTTL = getTxnLockTTL(this.startTs);
ClientRPCResult prewriteResult = this.kvClient.prewrite(backOffer, mutationList, key, lockTTL, this.startTs, tiRegion);
if (!prewriteResult.isSuccess() && !prewriteResult.isRetry()) {
throw new TiBatchWriteException("prewrite primary key error", prewriteResult.getException());
}
if (prewriteResult.isRetry()) {
try {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(String.format("Txn prewrite primary key failed, regionId=%s", tiRegion.getId()), prewriteResult.getException()));
// re-split keys and commit again.
this.doPrewritePrimaryKeyWithRetry(backOffer, key, value);
} catch (GrpcException e) {
String errorMsg = String.format("Txn prewrite primary key error, re-split commit failed, regionId=%s, detail=%s", tiRegion.getId(), e.getMessage());
throw new TiBatchWriteException(errorMsg, e);
}
}
LOG.info("prewrite primary key {} successfully", LogDesensitization.hide(KeyUtils.formatBytes(key)));
}
use of com.pingcap.tikv.exception.GrpcException in project tispark by pingcap.
the class TwoPhaseCommitter method doCommitPrimaryKeyWithRetry.
private void doCommitPrimaryKeyWithRetry(BackOffer backOffer, ByteString key, long commitTs) throws TiBatchWriteException {
Pair<TiRegion, Metapb.Store> pair = this.regionManager.getRegionStorePairByKey(key, backOffer);
TiRegion tiRegion = pair.first;
List<ByteString> keys = new ArrayList<>();
keys.add(key);
// send rpc request to tikv server
ClientRPCResult commitResult = this.kvClient.commit(backOffer, keys, this.startTs, commitTs, tiRegion);
if (!commitResult.isSuccess()) {
if (!commitResult.isRetry()) {
throw new TiBatchWriteException("commit primary key error", commitResult.getException());
} else {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(String.format("Txn commit primary key failed, regionId=%s", tiRegion.getId()), commitResult.getException()));
// re-split keys and commit again.
this.doCommitPrimaryKeyWithRetry(backOffer, key, commitTs);
}
}
LOG.info("commit primary key {} successfully", LogDesensitization.hide(KeyUtils.formatBytes(key)));
}
use of com.pingcap.tikv.exception.GrpcException in project tispark by pingcap.
the class PDClient method waitScatterRegionFinish.
/**
* wait scatter region until finish
*
* @param region
*/
void waitScatterRegionFinish(TiRegion region, BackOffer backOffer) {
for (; ; ) {
GetOperatorResponse resp = getOperator(region.getId());
if (resp != null) {
if (isScatterRegionFinish(resp)) {
logger.info(String.format("wait scatter region on %d is finished", region.getId()));
return;
} else {
backOffer.doBackOff(BackOffFuncType.BoRegionMiss, new GrpcException("waiting scatter region"));
logger.info(// LogDesensitization: show region key range in log
String.format("wait scatter region %d at key %s is %s", region.getId(), KeyUtils.formatBytes(resp.getDesc().toByteArray()), resp.getStatus().toString()));
}
}
}
}
use of com.pingcap.tikv.exception.GrpcException in project tispark by pingcap.
the class TwoPhaseCommitter method doPrewriteSecondaryKeySingleBatchWithRetry.
private void doPrewriteSecondaryKeySingleBatchWithRetry(BackOffer backOffer, ByteString primaryKey, BatchKeys batchKeys, Map<ByteString, Kvrpcpb.Mutation> mutations) throws TiBatchWriteException {
LOG.info("start prewrite secondary key, row={}, size={}KB, regionId={}", batchKeys.getKeys().size(), batchKeys.getSizeInKB(), batchKeys.getRegion().getId());
List<ByteString> keyList = batchKeys.getKeys();
int batchSize = keyList.size();
List<Kvrpcpb.Mutation> mutationList = new ArrayList<>(batchSize);
for (ByteString key : keyList) {
mutationList.add(mutations.get(key));
}
// send rpc request to tikv server
int txnSize = batchKeys.getKeys().size();
long lockTTL = getTxnLockTTL(this.startTs, txnSize);
ClientRPCResult prewriteResult = this.kvClient.prewrite(backOffer, mutationList, primaryKey, lockTTL, this.startTs, batchKeys.getRegion());
if (!prewriteResult.isSuccess() && !prewriteResult.isRetry()) {
throw new TiBatchWriteException("prewrite secondary key error", prewriteResult.getException());
}
if (prewriteResult.isRetry()) {
LOG.info("prewrite secondary key fail, will backoff and retry");
try {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(String.format("Txn prewrite secondary key SingleBatch failed, regionId=%s", batchKeys.getRegion().getId()), prewriteResult.getException()));
// re-split keys and commit again.
retryPrewriteBatch(backOffer, primaryKey, batchKeys, mutations, 0);
} catch (GrpcException e) {
String errorMsg = String.format("Txn prewrite secondary key SingleBatch error, re-split commit failed, regionId=%s, detail=%s", batchKeys.getRegion().getId(), e.getMessage());
throw new TiBatchWriteException(errorMsg, e);
}
}
LOG.info("prewrite secondary key successfully, row={}, size={}KB, regionId={}", batchKeys.getKeys().size(), batchKeys.getSizeInKB(), batchKeys.getRegion().getId());
}
use of com.pingcap.tikv.exception.GrpcException in project tispark by pingcap.
the class KVErrorHandler method handleRequestError.
@Override
public boolean handleRequestError(BackOffer backOffer, Exception e) {
regionManager.onRequestFail(recv.getRegion());
notifyRegionStoreCacheInvalidate(recv.getRegion(), CacheInvalidateEvent.CacheType.REQ_FAILED);
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoTiKVRPC, new GrpcException("send tikv request error: " + e.getMessage() + ", try next peer later", e));
// should re-fetch the new leader from PD and send request to it
return false;
}
Aggregations