Search in sources :

Example 31 with TiRegion

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

the class DAGIterator method processByStreaming.

private Iterator<SelectResponse> processByStreaming(RangeSplitter.RegionTask regionTask) {
    List<Coprocessor.KeyRange> ranges = regionTask.getRanges();
    TiRegion region = regionTask.getRegion();
    Metapb.Store store = regionTask.getStore();
    RegionStoreClient client;
    try {
        client = session.getRegionStoreClientBuilder().build(region, store, storeType);
        Iterator<SelectResponse> responseIterator = client.coprocessStreaming(dagRequest, ranges, startTs);
        if (responseIterator == null) {
            eof = true;
            return null;
        }
        return responseIterator;
    } catch (Exception e) {
        // see:https://github.com/pingcap/tikv-client-lib-java/pull/149
        throw new TiClientInternalException("Error Closing Store client.", e);
    }
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) Metapb(org.tikv.kvproto.Metapb) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) RegionTaskException(com.pingcap.tikv.exception.RegionTaskException)

Example 32 with TiRegion

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

the class ScanIterator method cacheLoadFails.

// return true if current cache is not loaded or empty
boolean cacheLoadFails() {
    if (endOfScan || processingLastBatch) {
        return true;
    }
    if (startKey == null) {
        return true;
    }
    try {
        TiRegion region = loadCurrentRegionToCache();
        ByteString curRegionEndKey = region.getEndKey();
        // See https://github.com/pingcap/tispark/issues/393 for details
        if (currentCache == null) {
            return true;
        }
        index = 0;
        Key lastKey;
        // of a transaction. Otherwise below code might lose data
        if (currentCache.size() < limit) {
            startKey = curRegionEndKey;
            lastKey = Key.toRawKey(curRegionEndKey);
        } else if (currentCache.size() > limit) {
            throw new IndexOutOfBoundsException("current cache size = " + currentCache.size() + ", larger than " + limit);
        } else {
            // Start new scan from exact next key in current region
            lastKey = Key.toRawKey(currentCache.get(currentCache.size() - 1).getKey());
            startKey = lastKey.next().toByteString();
        }
        // if startKey is empty, it indicates +∞
        if (hasEndKey && lastKey.compareTo(endKey) >= 0 || startKey.isEmpty()) {
            processingLastBatch = true;
            startKey = null;
        }
    } catch (Exception e) {
        throw new TiClientInternalException("Error scanning data from region.", e);
    }
    return false;
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) Key(com.pingcap.tikv.key.Key) GrpcException(com.pingcap.tikv.exception.GrpcException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException)

Example 33 with TiRegion

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

the class LockResolverClientV3 method getTxnStatus.

private TxnStatus getTxnStatus(BackOffer bo, Long txnID, ByteString primary, Long currentTS) {
    TxnStatus status = getResolved(txnID);
    if (status != null) {
        return status;
    }
    Supplier<CleanupRequest> factory = () -> {
        TiRegion primaryKeyRegion = regionManager.getRegionByKey(primary);
        return CleanupRequest.newBuilder().setContext(primaryKeyRegion.getContext()).setKey(primary).setStartVersion(txnID).setCurrentTs(currentTS).build();
    };
    status = new TxnStatus();
    while (true) {
        // new RegionStoreClient for PrimaryKey
        RegionStoreClient primaryKeyRegionStoreClient = clientBuilder.build(primary);
        TiRegion primaryKeyRegion = primaryKeyRegionStoreClient.getRegion();
        KVErrorHandler<CleanupResponse> handler = new KVErrorHandler<>(regionManager, primaryKeyRegionStoreClient, primaryKeyRegionStoreClient.lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, 0L, false);
        CleanupResponse resp = primaryKeyRegionStoreClient.callWithRetry(bo, TikvGrpc.getKvCleanupMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKvCleanupMethod failed without a cause");
            regionManager.onRequestFail(primaryKeyRegion);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCleanupMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.hasError()) {
            Kvrpcpb.KeyError keyError = resp.getError();
            // the TTL.
            if (keyError.hasLocked()) {
                Kvrpcpb.LockInfo lockInfo = keyError.getLocked();
                return new TxnStatus(lockInfo.getLockTtl(), 0L);
            }
            logger.error(String.format("unexpected cleanup err: %s, tid: %d", keyError, txnID));
            throw new KeyException(keyError);
        }
        if (resp.getCommitVersion() != 0) {
            status = new TxnStatus(0L, resp.getCommitVersion());
        }
        saveResolved(txnID, status);
        return status;
    }
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException) CleanupResponse(org.tikv.kvproto.Kvrpcpb.CleanupResponse) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) CleanupRequest(org.tikv.kvproto.Kvrpcpb.CleanupRequest) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 34 with TiRegion

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

the class LockResolverClientV4 method getTxnStatus.

/**
 * getTxnStatus sends the CheckTxnStatus request to the TiKV server. When rollbackIfNotExist is
 * false, the caller should be careful with the TxnNotFoundException error.
 */
private TxnStatus getTxnStatus(BackOffer bo, Long txnID, ByteString primary, Long callerStartTS, Long currentTS, boolean rollbackIfNotExist, Lock lock) {
    TxnStatus status = getResolved(txnID);
    if (status != null) {
        return status;
    }
    // CheckTxnStatus may meet the following cases:
    // 1. LOCK
    // 1.1 Lock expired -- orphan lock, fail to update TTL, crash recovery etc.
    // 1.2 Lock TTL -- active transaction holding the lock.
    // 2. NO LOCK
    // 2.1 Txn Committed
    // 2.2 Txn Rollbacked -- rollback itself, rollback by others, GC tomb etc.
    // 2.3 No lock -- pessimistic lock rollback, concurrence prewrite.
    Supplier<Kvrpcpb.CheckTxnStatusRequest> factory = () -> {
        TiRegion primaryKeyRegion = regionManager.getRegionByKey(primary);
        return Kvrpcpb.CheckTxnStatusRequest.newBuilder().setContext(primaryKeyRegion.getContext()).setPrimaryKey(primary).setLockTs(txnID).setCallerStartTs(callerStartTS).setCurrentTs(currentTS).setRollbackIfNotExist(rollbackIfNotExist).build();
    };
    while (true) {
        // new RegionStoreClient for PrimaryKey
        RegionStoreClient primaryKeyRegionStoreClient = clientBuilder.build(primary);
        TiRegion primaryKeyRegion = primaryKeyRegionStoreClient.getRegion();
        KVErrorHandler<Kvrpcpb.CheckTxnStatusResponse> handler = new KVErrorHandler<>(regionManager, primaryKeyRegionStoreClient, primaryKeyRegionStoreClient.lockResolverClient, resp -> resp.hasRegionError() ? resp.getRegionError() : null, resp -> resp.hasError() ? resp.getError() : null, resolveLockResult -> null, callerStartTS, false);
        Kvrpcpb.CheckTxnStatusResponse resp = primaryKeyRegionStoreClient.callWithRetry(bo, TikvGrpc.getKvCheckTxnStatusMethod(), factory, handler);
        if (resp == null) {
            logger.error("getKvCheckTxnStatusMethod failed without a cause");
            regionManager.onRequestFail(primaryKeyRegion);
            bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCheckTxnStatusMethod failed without a cause"));
            continue;
        }
        if (resp.hasRegionError()) {
            bo.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
            continue;
        }
        if (resp.hasError()) {
            Kvrpcpb.KeyError keyError = resp.getError();
            if (keyError.hasTxnNotFound()) {
                throw new TxnNotFoundException();
            }
            logger.error(String.format("unexpected cleanup err: %s, tid: %d", keyError, txnID));
            throw new KeyException(keyError);
        }
        status = new TxnStatus();
        status.setAction(resp.getAction());
        status.setPrimaryLock(resp.getLockInfo());
        if (status.getPrimaryLock() != null && status.getPrimaryLock().getUseAsyncCommit()) {
            if (!TsoUtils.isExpired(txnID, resp.getLockTtl())) {
                status.setTtl(resp.getLockTtl());
            }
        } else if (resp.getLockTtl() != 0) {
            status.setTtl(resp.getLockTtl());
        } else {
            status.setCommitTS(resp.getCommitVersion());
            // change.
            if (status.isCommitted() || (lock != null && lock.getLockType() != org.tikv.kvproto.Kvrpcpb.Op.PessimisticLock)) {
                saveResolved(txnID, status);
            }
        }
        return status;
    }
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TxnNotFoundException(com.pingcap.tikv.exception.TxnNotFoundException) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) RegionException(com.pingcap.tikv.exception.RegionException) KeyException(com.pingcap.tikv.exception.KeyException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

Example 35 with TiRegion

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

the class LockResolverClientV4 method checkSecondaries.

private boolean checkSecondaries(BackOffer bo, long txnID, List<ByteString> curKeys, TiRegion tiRegion, AsyncResolveData shared) {
    RegionStoreClient regionStoreClient = clientBuilder.build(tiRegion);
    Supplier<Kvrpcpb.CheckSecondaryLocksRequest> factory = () -> Kvrpcpb.CheckSecondaryLocksRequest.newBuilder().setContext(tiRegion.getContext()).setStartVersion(txnID).addAllKeys(curKeys).build();
    KVErrorHandler<Kvrpcpb.CheckSecondaryLocksResponse> handler = new KVErrorHandler<>(regionManager, regionStoreClient, regionStoreClient.lockResolverClient, resp -> null, resp -> null, resolveLockResult -> null, 0L, false);
    Kvrpcpb.CheckSecondaryLocksResponse resp = regionStoreClient.callWithRetry(bo, TikvGrpc.getKvCheckSecondaryLocksMethod(), factory, handler);
    if (resp == null) {
        logger.error("getKvCheckSecondaryLocksMethod failed without a cause");
        regionManager.onRequestFail(tiRegion);
        bo.doBackOff(BoRegionMiss, new TiClientInternalException("getKvCheckSecondaryLocksMethod failed without a cause"));
        logger.debug(String.format("checkSecondaries: region error, regrouping, txnID=%d, regionId=%d", txnID, tiRegion.getId()));
        // If regions have changed, then we might need to regroup the keys. Since this should be rare
        // and for the sake of simplicity, we will resolve regions sequentially.
        Map<TiRegion, List<ByteString>> groupResult = groupKeysByRegion(this.regionManager, curKeys, bo);
        for (Map.Entry<TiRegion, List<ByteString>> entry : groupResult.entrySet()) {
            TiRegion region = entry.getKey();
            List<ByteString> keys = entry.getValue();
            checkSecondaries(bo, txnID, keys, region, shared);
        }
    }
    shared.addKeys(resp.getLocksList(), curKeys.size(), txnID, resp.getCommitTs());
    return true;
}
Also used : Kvrpcpb(org.tikv.kvproto.Kvrpcpb) ByteString(com.google.protobuf.ByteString) KVErrorHandler(com.pingcap.tikv.operation.KVErrorHandler) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RegionStoreClient(com.pingcap.tikv.region.RegionStoreClient) AbstractRegionStoreClient(com.pingcap.tikv.region.AbstractRegionStoreClient)

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