Search in sources :

Example 1 with ConcreteBackOffer

use of org.tikv.common.util.ConcreteBackOffer in project client-java by tikv.

the class RawKVClient method compareAndSet.

@Override
public void compareAndSet(ByteString key, Optional<ByteString> prevValue, ByteString value, long ttl) throws RawCASConflictException {
    if (!atomicForCAS) {
        throw new IllegalArgumentException("To use compareAndSet or putIfAbsent, please enable the config tikv.enable_atomic_for_cas.");
    }
    String[] labels = withClusterId("client_raw_compare_and_set");
    Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(labels).startTimer();
    SlowLog slowLog = withClusterInfo(new SlowLogImpl(conf.getRawKVWriteSlowLogInMS()));
    SlowLogSpan span = slowLog.start("putIfAbsent");
    span.addProperty("key", KeyUtils.formatBytesUTF8(key));
    ConcreteBackOffer backOffer = ConcreteBackOffer.newDeadlineBackOff(conf.getRawKVWriteTimeoutInMS(), slowLog, clusterId);
    try {
        while (true) {
            try (RegionStoreClient client = clientBuilder.build(key, backOffer)) {
                span.addProperty("region", client.getRegion().toString());
                client.rawCompareAndSet(backOffer, key, prevValue, value, ttl);
                RAW_REQUEST_SUCCESS.labels(labels).inc();
                return;
            } catch (final TiKVException e) {
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
                logger.warn("Retry for putIfAbsent error", e);
            }
        }
    } catch (Exception e) {
        RAW_REQUEST_FAILURE.labels(labels).inc();
        slowLog.setError(e);
        throw e;
    } finally {
        requestTimer.observeDuration();
        span.end();
        slowLog.log();
    }
}
Also used : Histogram(io.prometheus.client.Histogram) TiKVException(org.tikv.common.exception.TiKVException) SlowLog(org.tikv.common.log.SlowLog) SlowLogImpl(org.tikv.common.log.SlowLogImpl) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) ByteString(com.google.protobuf.ByteString) SlowLogSpan(org.tikv.common.log.SlowLogSpan) RegionStoreClient(org.tikv.common.region.RegionStoreClient) RawCASConflictException(org.tikv.common.exception.RawCASConflictException) TiKVException(org.tikv.common.exception.TiKVException) GrpcException(org.tikv.common.exception.GrpcException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ConcreteBackOffer

use of org.tikv.common.util.ConcreteBackOffer in project client-java by tikv.

the class RawKVClient method batchGet.

@Override
public List<KvPair> batchGet(List<ByteString> keys) {
    String[] labels = withClusterId("client_raw_batch_get");
    Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(labels).startTimer();
    SlowLog slowLog = withClusterInfo(new SlowLogImpl(conf.getRawKVBatchReadSlowLogInMS()));
    SlowLogSpan span = slowLog.start("batchGet");
    span.addProperty("keySize", String.valueOf(keys.size()));
    ConcreteBackOffer backOffer = ConcreteBackOffer.newDeadlineBackOff(conf.getRawKVBatchReadTimeoutInMS(), slowLog, clusterId);
    try {
        long deadline = System.currentTimeMillis() + conf.getRawKVBatchReadTimeoutInMS();
        List<KvPair> result = doSendBatchGet(backOffer, keys, deadline);
        RAW_REQUEST_SUCCESS.labels(labels).inc();
        return result;
    } catch (Exception e) {
        RAW_REQUEST_FAILURE.labels(labels).inc();
        slowLog.setError(e);
        throw e;
    } finally {
        requestTimer.observeDuration();
        span.end();
        slowLog.log();
    }
}
Also used : Histogram(io.prometheus.client.Histogram) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) SlowLog(org.tikv.common.log.SlowLog) SlowLogImpl(org.tikv.common.log.SlowLogImpl) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) ByteString(com.google.protobuf.ByteString) SlowLogSpan(org.tikv.common.log.SlowLogSpan) RawCASConflictException(org.tikv.common.exception.RawCASConflictException) TiKVException(org.tikv.common.exception.TiKVException) GrpcException(org.tikv.common.exception.GrpcException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ConcreteBackOffer

use of org.tikv.common.util.ConcreteBackOffer in project client-java by tikv.

the class RawKVClient method put.

@Override
public void put(ByteString key, ByteString value, long ttl) {
    String[] labels = withClusterId("client_raw_put");
    Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(labels).startTimer();
    SlowLog slowLog = withClusterInfo(new SlowLogImpl(conf.getRawKVWriteSlowLogInMS()));
    SlowLogSpan span = slowLog.start("put");
    span.addProperty("key", KeyUtils.formatBytesUTF8(key));
    ConcreteBackOffer backOffer = ConcreteBackOffer.newDeadlineBackOff(conf.getRawKVWriteTimeoutInMS(), slowLog, clusterId);
    try {
        while (true) {
            try (RegionStoreClient client = clientBuilder.build(key, backOffer)) {
                span.addProperty("region", client.getRegion().toString());
                client.rawPut(backOffer, key, value, ttl, atomicForCAS);
                RAW_REQUEST_SUCCESS.labels(labels).inc();
                return;
            } catch (final TiKVException e) {
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
                logger.warn("Retry for put error", e);
            }
        }
    } catch (Exception e) {
        RAW_REQUEST_FAILURE.labels(labels).inc();
        slowLog.setError(e);
        throw e;
    } finally {
        requestTimer.observeDuration();
        span.end();
        slowLog.log();
    }
}
Also used : Histogram(io.prometheus.client.Histogram) TiKVException(org.tikv.common.exception.TiKVException) SlowLog(org.tikv.common.log.SlowLog) SlowLogImpl(org.tikv.common.log.SlowLogImpl) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) ByteString(com.google.protobuf.ByteString) SlowLogSpan(org.tikv.common.log.SlowLogSpan) RegionStoreClient(org.tikv.common.region.RegionStoreClient) RawCASConflictException(org.tikv.common.exception.RawCASConflictException) TiKVException(org.tikv.common.exception.TiKVException) GrpcException(org.tikv.common.exception.GrpcException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ConcreteBackOffer

use of org.tikv.common.util.ConcreteBackOffer in project client-java by tikv.

the class RawKVClient method scan.

@Override
public List<KvPair> scan(ByteString startKey, ByteString endKey, boolean keyOnly) {
    String[] labels = withClusterId("client_raw_scan_without_limit");
    Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(labels).startTimer();
    SlowLog slowLog = withClusterInfo(new SlowLogImpl(conf.getRawKVScanSlowLogInMS()));
    SlowLogSpan span = slowLog.start("scan");
    span.addProperty("startKey", KeyUtils.formatBytesUTF8(startKey));
    span.addProperty("endKey", KeyUtils.formatBytesUTF8(endKey));
    span.addProperty("keyOnly", String.valueOf(keyOnly));
    ConcreteBackOffer backOffer = ConcreteBackOffer.newDeadlineBackOff(conf.getRawKVScanTimeoutInMS(), slowLog, clusterId);
    try {
        ByteString newStartKey = startKey;
        List<KvPair> result = new ArrayList<>();
        while (true) {
            Iterator<KvPair> iterator = rawScanIterator(conf, clientBuilder, newStartKey, endKey, conf.getScanBatchSize(), keyOnly, backOffer);
            if (!iterator.hasNext()) {
                break;
            }
            iterator.forEachRemaining(result::add);
            newStartKey = Key.toRawKey(result.get(result.size() - 1).getKey()).next().toByteString();
        }
        RAW_REQUEST_SUCCESS.labels(labels).inc();
        return result;
    } catch (Exception e) {
        RAW_REQUEST_FAILURE.labels(labels).inc();
        slowLog.setError(e);
        throw e;
    } finally {
        requestTimer.observeDuration();
        span.end();
        slowLog.log();
    }
}
Also used : Histogram(io.prometheus.client.Histogram) KvPair(org.tikv.kvproto.Kvrpcpb.KvPair) ByteString(com.google.protobuf.ByteString) SlowLog(org.tikv.common.log.SlowLog) SlowLogImpl(org.tikv.common.log.SlowLogImpl) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) RawCASConflictException(org.tikv.common.exception.RawCASConflictException) TiKVException(org.tikv.common.exception.TiKVException) GrpcException(org.tikv.common.exception.GrpcException) ExecutionException(java.util.concurrent.ExecutionException) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) SlowLogSpan(org.tikv.common.log.SlowLogSpan)

Example 5 with ConcreteBackOffer

use of org.tikv.common.util.ConcreteBackOffer in project client-java by tikv.

the class RawKVClient method getKeyTTL.

@Override
public Optional<Long> getKeyTTL(ByteString key) {
    String[] labels = withClusterId("client_raw_get_key_ttl");
    Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(labels).startTimer();
    SlowLog slowLog = withClusterInfo(new SlowLogImpl(conf.getRawKVReadSlowLogInMS()));
    SlowLogSpan span = slowLog.start("getKeyTTL");
    span.addProperty("key", KeyUtils.formatBytesUTF8(key));
    ConcreteBackOffer backOffer = ConcreteBackOffer.newDeadlineBackOff(conf.getRawKVReadTimeoutInMS(), slowLog, clusterId);
    try {
        while (true) {
            try (RegionStoreClient client = clientBuilder.build(key, backOffer)) {
                span.addProperty("region", client.getRegion().toString());
                Optional<Long> result = client.rawGetKeyTTL(backOffer, key);
                RAW_REQUEST_SUCCESS.labels(labels).inc();
                return result;
            } catch (final TiKVException e) {
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
                logger.warn("Retry for getKeyTTL error", e);
            }
        }
    } catch (Exception e) {
        RAW_REQUEST_FAILURE.labels(labels).inc();
        slowLog.setError(e);
        throw e;
    } finally {
        requestTimer.observeDuration();
        span.end();
        slowLog.log();
    }
}
Also used : Histogram(io.prometheus.client.Histogram) TiKVException(org.tikv.common.exception.TiKVException) SlowLog(org.tikv.common.log.SlowLog) SlowLogImpl(org.tikv.common.log.SlowLogImpl) ByteString(com.google.protobuf.ByteString) RawCASConflictException(org.tikv.common.exception.RawCASConflictException) TiKVException(org.tikv.common.exception.TiKVException) GrpcException(org.tikv.common.exception.GrpcException) ExecutionException(java.util.concurrent.ExecutionException) ConcreteBackOffer(org.tikv.common.util.ConcreteBackOffer) SlowLogSpan(org.tikv.common.log.SlowLogSpan) RegionStoreClient(org.tikv.common.region.RegionStoreClient)

Aggregations

GrpcException (org.tikv.common.exception.GrpcException)13 ConcreteBackOffer (org.tikv.common.util.ConcreteBackOffer)13 ByteString (com.google.protobuf.ByteString)11 Histogram (io.prometheus.client.Histogram)11 ExecutionException (java.util.concurrent.ExecutionException)11 RawCASConflictException (org.tikv.common.exception.RawCASConflictException)11 TiKVException (org.tikv.common.exception.TiKVException)11 SlowLog (org.tikv.common.log.SlowLog)10 SlowLogImpl (org.tikv.common.log.SlowLogImpl)10 SlowLogSpan (org.tikv.common.log.SlowLogSpan)10 RegionStoreClient (org.tikv.common.region.RegionStoreClient)5 ArrayList (java.util.ArrayList)3 KvPair (org.tikv.kvproto.Kvrpcpb.KvPair)3 Test (org.junit.Test)2 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 BackOffFunction (org.tikv.common.util.BackOffFunction)1 BackOffFuncType (org.tikv.common.util.BackOffFunction.BackOffFuncType)1