Skip to content

Commit aaf677b

Browse files
authored
[Dataflow] Fix thread safety of HotKey logger (#38816)
1 parent c01ceea commit aaf677b

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

  • runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker

runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/HotKeyLogger.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.beam.runners.dataflow.worker;
1919

2020
import com.google.api.client.util.Clock;
21+
import javax.annotation.concurrent.GuardedBy;
2122
import org.apache.beam.runners.dataflow.util.TimeUtil;
2223
import org.joda.time.Duration;
2324
import org.slf4j.Logger;
@@ -33,6 +34,7 @@ public class HotKeyLogger {
3334
* The previous time the HotKeyDetection was logged. This is used to throttle logging to every 5
3435
* minutes.
3536
*/
37+
@GuardedBy("this")
3638
private long prevHotKeyDetectionLogMs = 0;
3739

3840
/** Throttles logging the detection to every loggingPeriod */
@@ -83,10 +85,12 @@ public void logHotKeyDetection(String userStepName, Duration hotKeyAge, Object h
8385
protected boolean isThrottled() {
8486
// Throttle logging the HotKeyDetection to every 5 minutes.
8587
long nowMs = clock.currentTimeMillis();
86-
if (nowMs - prevHotKeyDetectionLogMs < loggingPeriod.getMillis()) {
87-
return true;
88+
synchronized (this) {
89+
if (nowMs - prevHotKeyDetectionLogMs < loggingPeriod.getMillis()) {
90+
return true;
91+
}
92+
prevHotKeyDetectionLogMs = nowMs;
8893
}
89-
prevHotKeyDetectionLogMs = nowMs;
9094
return false;
9195
}
9296
}

0 commit comments

Comments
 (0)