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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations