Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix race condition in BuiltinMetricsTracer #1320

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ 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 final AtomicBoolean serverLatencyTimerIsRunning = new AtomicBoolean();
private boolean serverLatencyTimerIsRunning = false;
private final Object timerLock = new Object();

private boolean flowControlIsDisabled = false;

Expand Down Expand Up @@ -117,8 +118,11 @@ public void attemptStarted(Object request, int attemptNumber) {
this.tableId = Util.extractTableId(request);
}
if (!flowControlIsDisabled) {
if (serverLatencyTimerIsRunning.compareAndSet(false, true)) {
serverLatencyTimer.start();
synchronized (timerLock) {
if (!serverLatencyTimerIsRunning) {
serverLatencyTimer.start();
serverLatencyTimerIsRunning = true;
}
}
}
}
Expand All @@ -144,8 +148,11 @@ public void onRequest(int requestCount) {
if (flowControlIsDisabled) {
// 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.
if (serverLatencyTimerIsRunning.compareAndSet(false, true)) {
serverLatencyTimer.start();
synchronized (timerLock) {
if (!serverLatencyTimerIsRunning) {
serverLatencyTimer.start();
serverLatencyTimerIsRunning = true;
}
}
}
}
Expand All @@ -159,9 +166,12 @@ public void responseReceived() {
// When auto flow control is disabled and application requested multiple responses, server
// latency is measured between afterResponse and responseReceived.
// In all the cases, we want to stop the serverLatencyTimer here.
if (serverLatencyTimerIsRunning.compareAndSet(true, false)) {
totalServerLatency.addAndGet(serverLatencyTimer.elapsed(TimeUnit.MILLISECONDS));
serverLatencyTimer.reset();
synchronized (timerLock) {
if (serverLatencyTimerIsRunning) {
totalServerLatency.addAndGet(serverLatencyTimer.elapsed(TimeUnit.MILLISECONDS));
serverLatencyTimer.reset();
serverLatencyTimerIsRunning = false;
}
}
}

Expand All @@ -172,8 +182,11 @@ public void afterResponse(long applicationLatency) {
// measured between after the last response is processed and before the next response is
// received. If flow control is disabled but requestLeft is greater than 0,
// also start the timer to count the time between afterResponse and responseReceived.
if (serverLatencyTimerIsRunning.compareAndSet(false, true)) {
serverLatencyTimer.start();
synchronized (timerLock) {
if (!serverLatencyTimerIsRunning) {
serverLatencyTimer.start();
serverLatencyTimerIsRunning = true;
}
}
}
}
Expand Down Expand Up @@ -235,10 +248,13 @@ private void recordOperationCompletion(@Nullable Throwable status) {
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.
if (serverLatencyTimerIsRunning.compareAndSet(true, false)) {
requestLeft.decrementAndGet();
totalServerLatency.addAndGet(serverLatencyTimer.elapsed(TimeUnit.MILLISECONDS));
serverLatencyTimer.reset();
synchronized (timerLock) {
if (serverLatencyTimerIsRunning) {
requestLeft.decrementAndGet();
totalServerLatency.addAndGet(serverLatencyTimer.elapsed(TimeUnit.MILLISECONDS));
serverLatencyTimer.reset();
serverLatencyTimerIsRunning = false;
}
}
recorder.putAttemptLatencies(attemptTimer.elapsed(TimeUnit.MILLISECONDS));
recorder.record(Util.extractStatus(status), tableId, zone, cluster);
Expand Down