Search in sources :

Example 1 with BackOffer

use of com.pingcap.tikv.util.BackOffer 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 2 with BackOffer

use of com.pingcap.tikv.util.BackOffer 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 3 with BackOffer

use of com.pingcap.tikv.util.BackOffer 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)

Example 4 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class LockResolverSITest method SITestNonBlocking.

@Test
public void SITestNonBlocking() {
    if (!init) {
        skipTestInit();
        return;
    }
    if (!isLockResolverClientV4()) {
        skipTestTiDBV4();
        return;
    }
    TiTimestamp startTs = session.getTimestamp();
    TiTimestamp endTs = session.getTimestamp();
    // Put <a, a> into kv
    putKV("a", "a", startTs.getVersion(), endTs.getVersion());
    startTs = session.getTimestamp();
    endTs = session.getTimestamp();
    // Prewrite <a, aa> as primary without committing it
    assertTrue(lockKey("a", "aa", "a", "aa", false, startTs.getVersion(), endTs.getVersion(), LARGE_LOCK_TTL));
    TiRegion tiRegion = session.getRegionManager().getRegionByKey(ByteString.copyFromUtf8("a"));
    RegionStoreClient client = builder.build(tiRegion);
    {
        BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
        Long callerTS = session.getTimestamp().getVersion();
        System.out.println("callerTS1= " + callerTS);
        ByteString v = client.get(backOffer, ByteString.copyFromUtf8("a"), callerTS);
        assertEquals(v.toStringUtf8(), String.valueOf('a'));
    }
    try {
        // Trying to continue the commitString phase of <a, aa> will fail because CommitTS <
        // MinCommitTS
        commitString(Collections.singletonList("a"), startTs.getVersion(), endTs.getVersion());
        fail();
    } catch (KeyException e) {
        assertTrue(e.getMessage().startsWith("Key exception occurred and the reason is commit_ts_expired"));
    }
    // Trying to continue the commitString phase of <a, aa> will success because CommitTS >
    // MinCommitTS
    endTs = session.getTimestamp();
    assertTrue(commitString(Collections.singletonList("a"), startTs.getVersion(), endTs.getVersion()));
}
Also used : TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) KeyException(com.pingcap.tikv.exception.KeyException) Test(org.junit.Test)

Example 5 with BackOffer

use of com.pingcap.tikv.util.BackOffer in project tispark by pingcap.

the class LockResolverSITest method SITestBlocking.

@Test
public void SITestBlocking() {
    if (!init) {
        skipTestInit();
        return;
    }
    if (isLockResolverClientV4()) {
        logger.warn("Test skipped due to version of TiDB/TiKV should be 2.x or 3.x.");
        return;
    }
    TiTimestamp startTs = session.getTimestamp();
    TiTimestamp endTs = session.getTimestamp();
    // Put <a, a> into kv
    putKV("a", "a", startTs.getVersion(), endTs.getVersion());
    startTs = session.getTimestamp();
    endTs = session.getTimestamp();
    // Prewrite <a, aa> as primary without committing it
    assertTrue(lockKey("a", "aa", "a", "aa", false, startTs.getVersion(), endTs.getVersion()));
    TiRegion tiRegion = session.getRegionManager().getRegionByKey(ByteString.copyFromUtf8("a"));
    RegionStoreClient client = builder.build(tiRegion);
    {
        BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
        // With TTL set to 10, after 10 milliseconds <a, aa> is resolved.
        // We should be able to read <a, a> instead.
        ByteString v = client.get(backOffer, ByteString.copyFromUtf8("a"), session.getTimestamp().getVersion());
        assertEquals(v.toStringUtf8(), String.valueOf('a'));
    }
    try {
        // Trying to continue the commitString phase of <a, aa> will fail because TxnLockNotFound
        commitString(Collections.singletonList("a"), startTs.getVersion(), endTs.getVersion());
        fail();
    } catch (KeyException e) {
        assertFalse(e.getKeyError().getRetryable().isEmpty());
    }
}
Also used : TiTimestamp(com.pingcap.tikv.meta.TiTimestamp) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) ConcreteBackOffer(com.pingcap.tikv.util.ConcreteBackOffer) BackOffer(com.pingcap.tikv.util.BackOffer) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) KeyException(com.pingcap.tikv.exception.KeyException) Test(org.junit.Test)

Aggregations

BackOffer (com.pingcap.tikv.util.BackOffer)19 ConcreteBackOffer (com.pingcap.tikv.util.ConcreteBackOffer)19 RegionStoreClient (com.pingcap.tikv.region.RegionStoreClient)13 TiRegion (com.pingcap.tikv.region.TiRegion)10 ByteString (com.google.protobuf.ByteString)9 TiTimestamp (com.pingcap.tikv.meta.TiTimestamp)6 ArrayList (java.util.ArrayList)6 GrpcException (com.pingcap.tikv.exception.GrpcException)5 TiKVException (com.pingcap.tikv.exception.TiKVException)4 Metapb (org.tikv.kvproto.Metapb)4 KeyException (com.pingcap.tikv.exception.KeyException)3 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)3 Test (org.junit.Test)3 Chunk (com.pingcap.tidb.tipb.Chunk)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2 RegionException (com.pingcap.tikv.exception.RegionException)2 ArrayDeque (java.util.ArrayDeque)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)2