diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java index d4e27ff2c..14a112b27 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/BuiltinMetricsTracer.java @@ -67,7 +67,6 @@ class BuiltinMetricsTracer extends BigtableTracer { // Stopwatch is not thread safe so this is a workaround to check if the stopwatch changes is // flushed to memory. private final Stopwatch serverLatencyTimer = Stopwatch.createUnstarted(); - private boolean serverLatencyTimerIsRunning = false; private final Object timerLock = new Object(); private boolean flowControlIsDisabled = false; @@ -163,9 +162,8 @@ public void attemptStarted(Object request, int attemptNumber) { } if (!flowControlIsDisabled) { synchronized (timerLock) { - if (!serverLatencyTimerIsRunning) { + if (!serverLatencyTimer.isRunning()) { serverLatencyTimer.start(); - serverLatencyTimerIsRunning = true; } } } @@ -198,9 +196,8 @@ public void onRequest(int requestCount) { // On request is only called when auto flow control is disabled. When auto flow control is // disabled, server latency is measured between onRequest and onResponse. synchronized (timerLock) { - if (!serverLatencyTimerIsRunning) { + if (!serverLatencyTimer.isRunning()) { serverLatencyTimer.start(); - serverLatencyTimerIsRunning = true; } } } @@ -219,10 +216,9 @@ public void responseReceived() { // latency is measured between afterResponse and responseReceived. // In all the cases, we want to stop the serverLatencyTimer here. synchronized (timerLock) { - if (serverLatencyTimerIsRunning) { + if (serverLatencyTimer.isRunning()) { totalServerLatencyNano.addAndGet(serverLatencyTimer.elapsed(TimeUnit.NANOSECONDS)); serverLatencyTimer.reset(); - serverLatencyTimerIsRunning = false; } } } @@ -235,9 +231,8 @@ public void afterResponse(long applicationLatency) { // received. If flow control is disabled but requestLeft is greater than 0, // also start the timer to count the time between afterResponse and responseReceived. synchronized (timerLock) { - if (!serverLatencyTimerIsRunning) { + if (!serverLatencyTimer.isRunning()) { serverLatencyTimer.start(); - serverLatencyTimerIsRunning = true; } } } @@ -324,11 +319,10 @@ private void recordAttemptCompletion(@Nullable Throwable status) { // If the attempt failed, the time spent in retry should be counted in application latency. // Stop the stopwatch and decrement requestLeft. synchronized (timerLock) { - if (serverLatencyTimerIsRunning) { + if (serverLatencyTimer.isRunning()) { requestLeft.decrementAndGet(); totalServerLatencyNano.addAndGet(serverLatencyTimer.elapsed(TimeUnit.NANOSECONDS)); serverLatencyTimer.reset(); - serverLatencyTimerIsRunning = false; } }