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