Search in sources :

Example 6 with TiBatchWriteException

use of com.pingcap.tikv.exception.TiBatchWriteException 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 7 with TiBatchWriteException

use of com.pingcap.tikv.exception.TiBatchWriteException in project tispark by pingcap.

the class TwoPhaseCommitter method doCommitSecondaryKeys.

private void doCommitSecondaryKeys(Iterator<ByteString> keys, long commitTs, int commitBackOfferMS) throws TiBatchWriteException {
    try {
        int taskBufferSize = writeThreadPerTask * 2;
        int totalSize = 0, cnt = 0;
        ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<>(executorService);
        while (keys.hasNext()) {
            List<ByteString> keyBytes = new ArrayList<>(writeBufferSize);
            while (keyBytes.size() < writeBufferSize && keys.hasNext()) {
                keyBytes.add(keys.next());
            }
            int curSize = keyBytes.size();
            cnt++;
            if (cnt > taskBufferSize) {
                // consume one task if reaches task limit
                completionService.take().get();
            }
            BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(commitBackOfferMS);
            completionService.submit(() -> {
                doCommitSecondaryKeysWithRetry(backOffer, keyBytes, curSize, commitTs);
                return null;
            });
            totalSize = totalSize + keyBytes.size();
        }
        for (int i = 0; i < Math.min(taskBufferSize, cnt); i++) {
            completionService.take().get();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new TiBatchWriteException("Current thread interrupted.", e);
    } catch (ExecutionException e) {
        throw new TiBatchWriteException("Execution exception met.", e);
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) ExecutionException(java.util.concurrent.ExecutionException) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Example 8 with TiBatchWriteException

use of com.pingcap.tikv.exception.TiBatchWriteException in project tispark by pingcap.

the class TwoPhaseCommitter method doCommitSecondaryKeySingleBatchWithRetry.

private void doCommitSecondaryKeySingleBatchWithRetry(BackOffer backOffer, BatchKeys batchKeys, long commitTs) throws TiBatchWriteException {
    LOG.info("start commit secondary key, row={}, size={}KB, regionId={}", batchKeys.getKeys().size(), batchKeys.getSizeInKB(), batchKeys.getRegion().getId());
    List<ByteString> keysCommit = batchKeys.getKeys();
    // send rpc request to tikv server
    ClientRPCResult commitResult = this.kvClient.commit(backOffer, keysCommit, this.startTs, commitTs, batchKeys.getRegion());
    if (retryCommitSecondaryKeys && commitResult.isRetry()) {
        doCommitSecondaryKeysWithRetry(backOffer, keysCommit, keysCommit.size(), commitTs);
    } else if (!commitResult.isSuccess()) {
        String error = String.format("Txn commit secondary key error, regionId=%s", batchKeys.getRegion());
        LOG.warn(error);
        throw new TiBatchWriteException("commit secondary key error", commitResult.getException());
    }
    LOG.info("commit {} rows successfully, size={}KB, regionId={}", batchKeys.getKeys().size(), batchKeys.getSizeInKB(), batchKeys.getRegion().getId());
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException)

Aggregations

ByteString (com.google.protobuf.ByteString)8 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)8 ClientRPCResult (com.pingcap.tikv.txn.type.ClientRPCResult)5 ArrayList (java.util.ArrayList)5 GrpcException (com.pingcap.tikv.exception.GrpcException)4 TiRegion (com.pingcap.tikv.region.TiRegion)4 BackOffer (com.pingcap.tikv.util.BackOffer)2 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)2 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)2 BatchKeys (com.pingcap.tikv.txn.type.BatchKeys)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Metapb (org.tikv.kvproto.Metapb)1