use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class TiBatchWriteUtils method getRecordRegions.
public static List<TiRegion> getRecordRegions(TiSession session, TiTableInfo table) {
ArrayList<TiRegion> regionList = new ArrayList<>();
Key key = RowKey.createMin(table.getId());
RowKey endRowKey = RowKey.createBeyondMax(table.getId());
while (key.compareTo(endRowKey) < 0) {
TiRegion region = session.getRegionManager().getRegionByKey(key.toByteString());
regionList.add(region);
key = Key.toRawKey(region.getEndKey());
}
return regionList;
}
use of com.pingcap.tikv.region.TiRegion 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.region.TiRegion in project tispark by pingcap.
the class TwoPhaseCommitter method doPrewriteSecondaryKeysInBatchesWithRetry.
private void doPrewriteSecondaryKeysInBatchesWithRetry(BackOffer backOffer, ByteString primaryKey, List<ByteString> keys, List<ByteString> values, int size, int level) throws TiBatchWriteException {
if (keys == null || keys.isEmpty() || values == null || values.isEmpty() || size <= 0) {
// return success
return;
}
Map<ByteString, Kvrpcpb.Mutation> mutations = new LinkedHashMap<>();
for (int i = 0; i < size; i++) {
ByteString key = keys.get(i);
ByteString value = values.get(i);
Kvrpcpb.Mutation mutation;
if (!value.isEmpty()) {
mutation = Kvrpcpb.Mutation.newBuilder().setKey(key).setValue(value).setOp(Kvrpcpb.Op.Put).build();
} else {
// value can be null (table with one primary key integer column, data is encoded in key)
mutation = Kvrpcpb.Mutation.newBuilder().setKey(key).setOp(Kvrpcpb.Op.Del).build();
}
mutations.put(key, mutation);
}
// groups keys by region
Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, keys, backOffer);
List<BatchKeys> batchKeyList = new ArrayList<>();
for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
TiRegion tiRegion = entry.getKey();
this.appendBatchBySize(batchKeyList, tiRegion, entry.getValue(), true, mutations);
}
// For prewrite, stop sending other requests after receiving first error.
for (BatchKeys batchKeys : batchKeyList) {
TiRegion oldRegion = batchKeys.getRegion();
TiRegion currentRegion = this.regionManager.getRegionByKey(oldRegion.getStartKey(), backOffer);
if (oldRegion.equals(currentRegion)) {
doPrewriteSecondaryKeySingleBatchWithRetry(backOffer, primaryKey, batchKeys, mutations);
} else {
if (level > prewriteMaxRetryTimes) {
throw new TiBatchWriteException(String.format("> max retry number %s, oldRegion=%s, currentRegion=%s", prewriteMaxRetryTimes, oldRegion, currentRegion));
}
LOG.info(String.format("oldRegion=%s != currentRegion=%s, will re-fetch region info and retry", oldRegion, currentRegion));
retryPrewriteBatch(backOffer, primaryKey, batchKeys, mutations, level <= 0 ? 1 : level + 1);
}
}
}
use of com.pingcap.tikv.region.TiRegion 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)));
}
use of com.pingcap.tikv.region.TiRegion in project tispark by pingcap.
the class PDClient method getRegionByKeyAsync.
@Override
public Future<TiRegion> getRegionByKeyAsync(BackOffer backOffer, ByteString key) {
FutureObserver<TiRegion, GetRegionResponse> responseObserver = new FutureObserver<>(resp -> new TiRegion(resp.getRegion(), resp.getLeader(), conf.getIsolationLevel(), conf.getCommandPriority()));
Supplier<GetRegionRequest> request = () -> GetRegionRequest.newBuilder().setHeader(header).setRegionKey(key).build();
PDErrorHandler<GetRegionResponse> handler = new PDErrorHandler<>(getRegionResponseErrorExtractor, this);
callAsyncWithRetry(backOffer, PDGrpc.getGetRegionMethod(), request, responseObserver, handler);
return responseObserver.getFuture();
}
Aggregations