Search in sources :

Example 1 with RegionStoreClient

use of com.pingcap.tikv.region.RegionStoreClient in project tispark by pingcap.

the class TxnKVClient method prewrite.

/**
 * when encountered region error,ErrBodyMissing, and other errors
 */
public ClientRPCResult prewrite(BackOffer backOffer, List<Kvrpcpb.Mutation> mutations, ByteString primary, long lockTTL, long startTs, TiRegion tiRegion) {
    ClientRPCResult result = new ClientRPCResult(true, false, null);
    // send request
    RegionStoreClient client = clientBuilder.build(tiRegion);
    try {
        client.prewrite(backOffer, primary, mutations, startTs, lockTTL);
    } catch (Exception e) {
        result.setSuccess(false);
        // mark retryable, region error, should retry prewrite again
        result.setRetry(retryableException(e));
        result.setException(e);
    }
    return result;
}
Also used : ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) RegionException(com.pingcap.tikv.exception.RegionException) StatusRuntimeException(io.grpc.StatusRuntimeException) GrpcException(com.pingcap.tikv.exception.GrpcException) TiKVException(com.pingcap.tikv.exception.TiKVException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 2 with RegionStoreClient

use of com.pingcap.tikv.region.RegionStoreClient in project tispark by pingcap.

the class TxnKVClient method commit.

/**
 * Commit request of 2pc, add backoff logic when encountered region error, ErrBodyMissing, and
 * other errors
 *
 * @param backOffer
 * @param keys
 * @param startTs
 * @param commitTs
 * @param tiRegion
 * @return
 */
public ClientRPCResult commit(BackOffer backOffer, List<ByteString> keys, long startTs, long commitTs, TiRegion tiRegion) {
    ClientRPCResult result = new ClientRPCResult(true, false, null);
    // send request
    RegionStoreClient client = clientBuilder.build(tiRegion);
    try {
        client.commit(backOffer, keys, startTs, commitTs);
    } catch (Exception e) {
        result.setSuccess(false);
        // mark retryable, region error, should retry prewrite again
        result.setRetry(retryableException(e));
        result.setException(e);
    }
    return result;
}
Also used : ClientRPCResult(com.pingcap.tikv.txn.type.ClientRPCResult) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) RegionException(com.pingcap.tikv.exception.RegionException) StatusRuntimeException(io.grpc.StatusRuntimeException) GrpcException(com.pingcap.tikv.exception.GrpcException) TiKVException(com.pingcap.tikv.exception.TiKVException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 3 with RegionStoreClient

use of com.pingcap.tikv.region.RegionStoreClient in project tispark by pingcap.

the class ConcreteScanIterator method loadCurrentRegionToCache.

@Override
TiRegion loadCurrentRegionToCache() throws GrpcException {
    BackOffer backOffer = ConcreteBackOffer.newScannerNextMaxBackOff();
    while (true) {
        try (RegionStoreClient client = builder.build(startKey)) {
            TiRegion region = client.getRegion();
            if (limit <= 0) {
                currentCache = null;
            } else {
                try {
                    int scanSize = Math.min(limit, conf.getScanBatchSize());
                    currentCache = client.scan(backOffer, startKey, scanSize, version);
                } catch (final TiKVException e) {
                    backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
                    continue;
                }
            }
            return region;
        }
    }
}
Also used : TiKVException(com.pingcap.tikv.exception.TiKVException) TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient)

Example 4 with RegionStoreClient

use of com.pingcap.tikv.region.RegionStoreClient in project tispark by pingcap.

the class ConcreteScanIterator method resolveCurrentLock.

private ByteString resolveCurrentLock(Kvrpcpb.KvPair current) {
    logger.warn(String.format("resolve current key error %s", current.getError().toString()));
    Pair<TiRegion, Metapb.Store> pair = builder.getRegionManager().getRegionStorePairByKey(current.getKey());
    TiRegion region = pair.first;
    Metapb.Store store = pair.second;
    BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
    try (RegionStoreClient client = builder.build(region, store)) {
        return client.get(backOffer, current.getKey(), version);
    } catch (Exception e) {
        throw new KeyException(current.getError());
    }
}
Also used : TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) Metapb(org.tikv.kvproto.Metapb) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) GrpcException(com.pingcap.tikv.exception.GrpcException) TiKVException(com.pingcap.tikv.exception.TiKVException) KeyException(com.pingcap.tikv.exception.KeyException) KeyException(com.pingcap.tikv.exception.KeyException)

Example 5 with RegionStoreClient

use of com.pingcap.tikv.region.RegionStoreClient in project tispark by pingcap.

the class DAGIterator method process.

private SelectResponse process(RangeSplitter.RegionTask regionTask) {
    Queue<RangeSplitter.RegionTask> remainTasks = new ArrayDeque<>();
    Queue<SelectResponse> responseQueue = new ArrayDeque<>();
    remainTasks.add(regionTask);
    BackOffer backOffer = ConcreteBackOffer.newCopNextMaxBackOff();
    HashSet<Long> resolvedLocks = new HashSet<>();
    // the remaining tasks.
    while (!remainTasks.isEmpty()) {
        RangeSplitter.RegionTask task = remainTasks.poll();
        if (task == null) {
            continue;
        }
        List<Coprocessor.KeyRange> ranges = task.getRanges();
        TiRegion region = task.getRegion();
        Metapb.Store store = task.getStore();
        try {
            RegionStoreClient client = session.getRegionStoreClientBuilder().build(region, store, storeType);
            client.addResolvedLocks(startTs, resolvedLocks);
            Collection<RangeSplitter.RegionTask> tasks = client.coprocess(backOffer, dagRequest, region, ranges, responseQueue, startTs);
            if (tasks != null) {
                remainTasks.addAll(tasks);
            }
            resolvedLocks.addAll(client.getResolvedLocks(startTs));
        } catch (Throwable e) {
            // Handle region task failed
            logger.error("Process region tasks failed, remain " + remainTasks.size() + " tasks not executed due to", e);
            // Rethrow to upper levels
            throw new RegionTaskException("Handle region task failed:", e);
        }
    }
    // Add all chunks to the final result
    List<Chunk> resultChunk = new ArrayList<>();
    EncodeType encodeType = null;
    while (!responseQueue.isEmpty()) {
        SelectResponse response = responseQueue.poll();
        if (response != null) {
            encodeType = response.getEncodeType();
            resultChunk.addAll(response.getChunksList());
        }
    }
    return SelectResponse.newBuilder().addAllChunks(resultChunk).setEncodeType(encodeType).build();
}
Also used : EncodeType(com.pingcap.tidb.tipb.EncodeType) ArrayList(java.util.ArrayList) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) Chunk(com.pingcap.tidb.tipb.Chunk) RegionTaskException(com.pingcap.tikv.exception.RegionTaskException) ArrayDeque(java.util.ArrayDeque) TiRegion(com.pingcap.tikv.region.TiRegion) RangeSplitter(com.pingcap.tikv.util.RangeSplitter) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Metapb(org.tikv.kvproto.Metapb) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) HashSet(java.util.HashSet)

Aggregations

RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)21 TiRegion (com.pingcap.tikv.region.TiRegion)15 BackOffer (com.pingcap.tikv.util.BackOffer)12 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)12 KeyException (com.pingcap.tikv.exception.KeyException)9 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)8 ByteString (com.google.protobuf.ByteString)7 RegionException (com.pingcap.tikv.exception.RegionException)7 GrpcException (com.pingcap.tikv.exception.GrpcException)6 TiKVException (com.pingcap.tikv.exception.TiKVException)6 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)4 KVErrorHandler (com.pingcap.tikv.operation.KVErrorHandler)4 AbstractRegionStoreClient (com.pingcap.tikv.region.AbstractRegionStoreClient)4 ArrayList (java.util.ArrayList)4 Kvrpcpb (org.tikv.kvproto.Kvrpcpb)4 ClientRPCResult (com.pingcap.tikv.txn.type.ClientRPCResult)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 Test (org.junit.Test)3 Metapb (org.tikv.kvproto.Metapb)3 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2