use of com.pingcap.tikv.txn.type.ClientRPCResult in project tispark by pingcap.
the class TxnKVClient method prewrite.
/**
* when encountered region error,ErrBodyMissing, and other errors
*/
public ClientRPCResult prewrite(BackOffer backOffer, List<Kvrpcpb.Mutation> mutations, ByteString primary, long lockTTL, long startTs, TiRegion tiRegion) {
ClientRPCResult result = new ClientRPCResult(true, false, null);
// send request
RegionStoreClient client = clientBuilder.build(tiRegion);
try {
client.prewrite(backOffer, primary, mutations, startTs, lockTTL);
} catch (Exception e) {
result.setSuccess(false);
// mark retryable, region error, should retry prewrite again
result.setRetry(retryableException(e));
result.setException(e);
}
return result;
}
use of com.pingcap.tikv.txn.type.ClientRPCResult in project tispark by pingcap.
the class TxnKVClient method commit.
/**
* Commit request of 2pc, add backoff logic when encountered region error, ErrBodyMissing, and
* other errors
*
* @param backOffer
* @param keys
* @param startTs
* @param commitTs
* @param tiRegion
* @return
*/
public ClientRPCResult commit(BackOffer backOffer, List<ByteString> keys, long startTs, long commitTs, TiRegion tiRegion) {
ClientRPCResult result = new ClientRPCResult(true, false, null);
// send request
RegionStoreClient client = clientBuilder.build(tiRegion);
try {
client.commit(backOffer, keys, startTs, commitTs);
} catch (Exception e) {
result.setSuccess(false);
// mark retryable, region error, should retry prewrite again
result.setRetry(retryableException(e));
result.setException(e);
}
return result;
}
use of com.pingcap.tikv.txn.type.ClientRPCResult in project tispark by pingcap.
the class TTLManager method sendTxnHeartBeat.
private void sendTxnHeartBeat(BackOffer bo, long ttl) {
Pair<TiRegion, Metapb.Store> pair = regionManager.getRegionStorePairByKey(primaryLock);
TiRegion tiRegion = pair.first;
Metapb.Store store = pair.second;
ClientRPCResult result = kvClient.txnHeartBeat(bo, primaryLock, startTS, ttl, tiRegion, store);
if (!result.isSuccess() && !result.isRetry()) {
throw new TiBatchWriteException("sendTxnHeartBeat error", result.getException());
}
if (result.isRetry()) {
try {
bo.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(String.format("sendTxnHeartBeat failed, regionId=%s", tiRegion.getId()), result.getException()));
this.regionManager.invalidateStore(store.getId());
this.regionManager.invalidateRegion(tiRegion);
// re-split keys and commit again.
sendTxnHeartBeat(bo, ttl);
} catch (GrpcException e) {
String errorMsg = String.format("sendTxnHeartBeat error, regionId=%s, detail=%s", tiRegion.getId(), e.getMessage());
throw new TiBatchWriteException(errorMsg, e);
}
}
LOG.debug("sendTxnHeartBeat success key={} ttl={} success", LogDesensitization.hide(KeyUtils.formatBytes(primaryLock)), ttl);
}
use of com.pingcap.tikv.txn.type.ClientRPCResult 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.txn.type.ClientRPCResult 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)));
}
Aggregations