Search in sources :

Example 11 with GrpcException

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)));
}
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 12 with GrpcException

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)));
}
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)

Example 13 with GrpcException

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()));
            }
        }
    }
}
Also used : GrpcException(com.pingcap.tikv.exception.GrpcException) GetOperatorResponse(org.tikv.kvproto.Pdpb.GetOperatorResponse)

Example 14 with GrpcException

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());
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) 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 15 with GrpcException

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;
}
Also used : GrpcException(com.pingcap.tikv.exception.GrpcException)

Aggregations

GrpcException (com.pingcap.tikv.exception.GrpcException)16 ByteString (com.google.protobuf.ByteString)7 TiRegion (com.pingcap.tikv.region.TiRegion)5 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)4 ClientRPCResult (com.pingcap.tikv.txn.type.ClientRPCResult)4 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)3 BackOffer (com.pingcap.tikv.util.BackOffer)3 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)3 ArrayList (java.util.ArrayList)3 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)2 Lock (com.pingcap.tikv.txn.Lock)2 Errorpb (org.tikv.kvproto.Errorpb)2 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)2 KeyException (com.pingcap.tikv.exception.KeyException)1 LockException (com.pingcap.tikv.exception.LockException)1 TiKVException (com.pingcap.tikv.exception.TiKVException)1 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)1 PDError (com.pingcap.tikv.pd.PDError)1 ResolveLockResult (com.pingcap.tikv.txn.ResolveLockResult)1 BackOffFunction (com.pingcap.tikv.util.BackOffFunction)1