Search in sources :

Example 21 with TiRegion

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;
}
Also used : RowKey(com.pingcap.tikv.key.RowKey) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) RowKey(com.pingcap.tikv.key.RowKey) IndexKey(com.pingcap.tikv.key.IndexKey) Key(com.pingcap.tikv.key.Key)

Example 22 with TiRegion

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)));
}
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 23 with TiRegion

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);
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) ArrayList(java.util.ArrayList) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException) LinkedHashMap(java.util.LinkedHashMap) TiRegion(com.pingcap.tikv.region.TiRegion) BatchKeys(com.pingcap.tikv.txn.type.BatchKeys) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 24 with TiRegion

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)));
}
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 25 with TiRegion

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();
}
Also used : GetRegionResponse(org.tikv.kvproto.Pdpb.GetRegionResponse) PDErrorHandler(com.pingcap.tikv.operation.PDErrorHandler) TiRegion(com.pingcap.tikv.region.TiRegion) FutureObserver(com.pingcap.tikv.util.FutureObserver) GetRegionRequest(org.tikv.kvproto.Pdpb.GetRegionRequest)

Aggregations

TiRegion (com.pingcap.tikv.region.TiRegion)43 ByteString (com.google.protobuf.ByteString)19 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)17 ArrayList (java.util.ArrayList)16 BackOffer (com.pingcap.tikv.util.BackOffer)10 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)10 List (java.util.List)10 Test (org.junit.Test)9 TiKVException (com.pingcap.tikv.exception.TiKVException)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)8 Metapb (org.tikv.kvproto.Metapb)8 GrpcException (com.pingcap.tikv.exception.GrpcException)7 KeyException (com.pingcap.tikv.exception.KeyException)7 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)6 RegionException (com.pingcap.tikv.exception.RegionException)4 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)4 Key (com.pingcap.tikv.key.Key)4 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)4