Add metrics for callWithRetry & doBackOff (#308)

Signed-off-by: birdstorm <samuelwyf@hotmail.com>
This commit is contained in:
birdstorm 2021-11-03 19:00:29 +09:00 committed by GitHub
parent e1c27941b0
commit 57845fe4c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 54 deletions

View File

@ -33,6 +33,12 @@ public abstract class RetryPolicy<RespT> {
.help("grpc request latency.") .help("grpc request latency.")
.labelNames("type") .labelNames("type")
.register(); .register();
public static final Histogram CALL_WITH_RETRY_DURATION =
Histogram.build()
.name("client_java_call_with_retry_duration")
.help("callWithRetry duration.")
.labelNames("type")
.register();
public static final Counter GRPC_REQUEST_RETRY_NUM = public static final Counter GRPC_REQUEST_RETRY_NUM =
Counter.build() Counter.build()
.name("client_java_grpc_requests_retry_num") .name("client_java_grpc_requests_retry_num")
@ -62,11 +68,14 @@ public abstract class RetryPolicy<RespT> {
} }
public RespT callWithRetry(Callable<RespT> proc, String methodName) { public RespT callWithRetry(Callable<RespT> proc, String methodName) {
Histogram.Timer callWithRetryTimer = CALL_WITH_RETRY_DURATION.labels(methodName).startTimer();
try {
while (true) { while (true) {
RespT result = null; RespT result = null;
try { try {
// add single request duration histogram // add single request duration histogram
Histogram.Timer requestTimer = GRPC_SINGLE_REQUEST_LATENCY.labels(methodName).startTimer(); Histogram.Timer requestTimer =
GRPC_SINGLE_REQUEST_LATENCY.labels(methodName).startTimer();
try { try {
result = proc.call(); result = proc.call();
} finally { } finally {
@ -94,6 +103,9 @@ public abstract class RetryPolicy<RespT> {
} }
return result; return result;
} }
} finally {
callWithRetryTimer.observeDuration();
}
} }
public interface Builder<T> { public interface Builder<T> {

View File

@ -20,6 +20,7 @@ package org.tikv.common.util;
import static org.tikv.common.ConfigUtils.TIKV_BO_REGION_MISS_BASE_IN_MS; import static org.tikv.common.ConfigUtils.TIKV_BO_REGION_MISS_BASE_IN_MS;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.prometheus.client.Histogram;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -35,7 +36,14 @@ public class ConcreteBackOffer implements BackOffer {
private final Map<BackOffFunction.BackOffFuncType, BackOffFunction> backOffFunctionMap; private final Map<BackOffFunction.BackOffFuncType, BackOffFunction> backOffFunctionMap;
private final List<Exception> errors; private final List<Exception> errors;
private int totalSleep; private int totalSleep;
private long deadline; private final long deadline;
public static final Histogram BACKOFF_DURATION =
Histogram.build()
.name("client_java_backoff_duration")
.help("backoff duration.")
.labelNames("type")
.register();
private ConcreteBackOffer(int maxSleep, long deadline) { private ConcreteBackOffer(int maxSleep, long deadline) {
Preconditions.checkArgument( Preconditions.checkArgument(
@ -140,6 +148,8 @@ public class ConcreteBackOffer implements BackOffer {
@Override @Override
public void doBackOffWithMaxSleep( public void doBackOffWithMaxSleep(
BackOffFunction.BackOffFuncType funcType, long maxSleepMs, Exception err) { BackOffFunction.BackOffFuncType funcType, long maxSleepMs, Exception err) {
Histogram.Timer backOffTimer = BACKOFF_DURATION.labels(funcType.name()).startTimer();
try {
BackOffFunction backOffFunction = BackOffFunction backOffFunction =
backOffFunctionMap.computeIfAbsent(funcType, this::createBackOffFunc); backOffFunctionMap.computeIfAbsent(funcType, this::createBackOffFunc);
@ -170,6 +180,9 @@ public class ConcreteBackOffer implements BackOffer {
if (maxSleep > 0 && totalSleep >= maxSleep) { if (maxSleep > 0 && totalSleep >= maxSleep) {
logThrowError(String.format("BackOffer.maxSleep %dms is exceeded, errors:", maxSleep), err); logThrowError(String.format("BackOffer.maxSleep %dms is exceeded, errors:", maxSleep), err);
} }
} finally {
backOffTimer.observeDuration();
}
} }
private void logThrowError(String msg, Exception err) { private void logThrowError(String msg, Exception err) {