Skip to content

Commit b9e3990

Browse files
committed
Address feedback - part 2
1 parent 9115bdc commit b9e3990

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

metadata/src/main/java/org/apache/kafka/controller/metrics/QuorumControllerMetrics.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Optional;
34-
import java.util.OptionalLong;
3534
import java.util.concurrent.ConcurrentHashMap;
3635
import java.util.concurrent.atomic.AtomicLong;
3736
import java.util.function.Consumer;
@@ -82,15 +81,15 @@ public class QuorumControllerMetrics implements AutoCloseable {
8281
private final AtomicLong lastAppliedRecordTimestamp = new AtomicLong(0);
8382
private final Consumer<Long> eventQueueTimeUpdater;
8483
private final Consumer<Long> eventQueueProcessingTimeUpdater;
85-
public final TimeRatio avgIdleTimeRatio;
84+
private final TimeRatio avgIdleTimeRatio;
8685

8786
private final AtomicLong timedOutHeartbeats = new AtomicLong(0);
8887
private final AtomicLong operationsStarted = new AtomicLong(0);
8988
private final AtomicLong operationsTimedOut = new AtomicLong(0);
9089
private final AtomicLong newActiveControllers = new AtomicLong(0);
9190
private final Map<Integer, Long> brokerContactTimesMs = new ConcurrentHashMap<>();
9291
private final int sessionTimeoutMs;
93-
private volatile OptionalLong idleStartTime = OptionalLong.empty();
92+
private final AtomicLong idleStartTime = new AtomicLong(-1);
9493

9594
private Consumer<Long> newHistogram(MetricName name, boolean biased) {
9695
if (registry.isPresent()) {
@@ -176,18 +175,17 @@ public Double value() {
176175
}
177176

178177
public void updateIdleStartTime() {
179-
if (idleStartTime.isEmpty()) {
180-
idleStartTime = OptionalLong.of(time.milliseconds());
181-
}
178+
idleStartTime.compareAndExchange(-1, time.milliseconds());
182179
}
183180

184181
public void updateIdleEndTime() {
185-
if (this.idleStartTime.isPresent()) {
186-
long idleDurationMs = Math.max(time.milliseconds() - idleStartTime.getAsLong(), 0);
182+
long startTime = idleStartTime.getAndSet(-1);
183+
if (startTime != -1) {
184+
long idleDurationMs = Math.max(time.milliseconds() - startTime, 0);
187185
avgIdleTimeRatio.record(METRIC_CONFIG, (double) idleDurationMs, time.milliseconds());
188-
idleStartTime = OptionalLong.empty();
189186
}
190187
}
188+
191189
public void addTimeSinceLastHeartbeatMetric(int brokerId) {
192190
brokerContactTimesMs.put(brokerId, time.milliseconds());
193191
registry.ifPresent(r -> r.newGauge(

0 commit comments

Comments
 (0)