Search in sources :

Example 1 with ClientRPCResult

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;
}
Also used : ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) RegionException(com.pingcap.tikv.exception.RegionException) StatusRuntimeException(io.grpc.StatusRuntimeException) GrpcException(com.pingcap.tikv.exception.GrpcException) TiKVException(com.pingcap.tikv.exception.TiKVException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 2 with ClientRPCResult

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;
}
Also used : ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) RegionException(com.pingcap.tikv.exception.RegionException) StatusRuntimeException(io.grpc.StatusRuntimeException) GrpcException(com.pingcap.tikv.exception.GrpcException) TiKVException(com.pingcap.tikv.exception.TiKVException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 3 with ClientRPCResult

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);
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) GrpcException(com.pingcap.tikv.exception.GrpcException) ByteString(com.google.protobuf.ByteString) ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) Metapb(org.tikv.kvproto.Metapb) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Example 4 with ClientRPCResult

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)));
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TiRegion(com.pingcap.tikv.region.TiRegion) GrpcException(com.pingcap.tikv.exception.GrpcException) ByteString(com.google.protobuf.ByteString) ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Example 5 with ClientRPCResult

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)));
}
Also used : ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) GrpcException(com.pingcap.tikv.exception.GrpcException) ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Aggregations

ClientRPCResult (com.pingcap.tikv.txn.type.ClientRPCResult)8 GrpcException (com.pingcap.tikv.exception.GrpcException)7 ByteString (com.google.protobuf.ByteString)5 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)5 KeyException (com.pingcap.tikv.exception.KeyException)3 RegionException (com.pingcap.tikv.exception.RegionException)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)3 TiKVException (com.pingcap.tikv.exception.TiKVException)3 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)3 TiRegion (com.pingcap.tikv.region.TiRegion)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 ArrayList (java.util.ArrayList)2 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)1 Metapb (org.tikv.kvproto.Metapb)1