Browse Source
The raft idle ratio is currently computed as the average of all recorded poll durations. This tends to underestimate the actual idle ratio since it treats all measurements equally regardless how much time was spent. For example, say we poll twice with the following durations: Poll 1: 2s Poll 2: 0s Assume that the busy time is negligible, so 2s passes overall. In the first measurement, 2s is spent waiting, so we compute and record a ratio of 1.0. In the second measurement, no time passes, and we record 0.0. The idle ratio is then computed as the average of these two values (1.0 + 0.0 / 2 = 0.5), which suggests that the process was busy for 1s, which overestimates the true busy time. In this patch, we create a new `TimeRatio` class which tracks the total duration of a periodic event over a full interval of time measurement. Reviewers: José Armando García Sancio <jsancio@apache.org>pull/13192/head
Jason Gustafson
2 years ago
committed by
GitHub
5 changed files with 231 additions and 31 deletions
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.kafka.raft.internals; |
||||
|
||||
import org.apache.kafka.common.metrics.MeasurableStat; |
||||
import org.apache.kafka.common.metrics.MetricConfig; |
||||
|
||||
/** |
||||
* Maintains an approximate ratio of the duration of a specific event |
||||
* over all time. For example, this can be used to compute the ratio of |
||||
* time that a thread is busy or idle. The value is approximate since the |
||||
* measurement and recording intervals may not be aligned. |
||||
* |
||||
* Note that the duration of the event is assumed to be small relative to |
||||
* the interval of measurement. |
||||
* |
||||
*/ |
||||
public class TimeRatio implements MeasurableStat { |
||||
private long intervalStartTimestampMs = -1; |
||||
private long lastRecordedTimestampMs = -1; |
||||
private double totalRecordedDurationMs = 0; |
||||
|
||||
private final double defaultRatio; |
||||
|
||||
public TimeRatio(double defaultRatio) { |
||||
if (defaultRatio < 0.0 || defaultRatio > 1.0) { |
||||
throw new IllegalArgumentException("Invalid ratio: value " + defaultRatio + " is not between 0 and 1."); |
||||
} |
||||
|
||||
this.defaultRatio = defaultRatio; |
||||
} |
||||
|
||||
@Override |
||||
public double measure(MetricConfig config, long currentTimestampMs) { |
||||
if (lastRecordedTimestampMs < 0) { |
||||
// Return the default value if no recordings have been captured.
|
||||
return defaultRatio; |
||||
} else { |
||||
// We measure the ratio over the
|
||||
double intervalDurationMs = Math.max(lastRecordedTimestampMs - intervalStartTimestampMs, 0); |
||||
final double ratio; |
||||
if (intervalDurationMs == 0) { |
||||
ratio = defaultRatio; |
||||
} else if (totalRecordedDurationMs > intervalDurationMs) { |
||||
ratio = 1.0; |
||||
} else { |
||||
ratio = totalRecordedDurationMs / intervalDurationMs; |
||||
} |
||||
|
||||
// The next interval begins at the
|
||||
intervalStartTimestampMs = lastRecordedTimestampMs; |
||||
totalRecordedDurationMs = 0; |
||||
return ratio; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void record(MetricConfig config, double value, long currentTimestampMs) { |
||||
if (intervalStartTimestampMs < 0) { |
||||
// Discard the initial value since the value occurred prior to the interval start
|
||||
intervalStartTimestampMs = currentTimestampMs; |
||||
} else { |
||||
totalRecordedDurationMs += value; |
||||
lastRecordedTimestampMs = currentTimestampMs; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.kafka.raft.internals; |
||||
|
||||
import org.apache.kafka.common.metrics.MetricConfig; |
||||
import org.apache.kafka.common.utils.MockTime; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
||||
class TimeRatioTest { |
||||
|
||||
@Test |
||||
public void testRatio() { |
||||
MetricConfig config = new MetricConfig(); |
||||
MockTime time = new MockTime(); |
||||
TimeRatio ratio = new TimeRatio(1.0); |
||||
|
||||
ratio.record(config, 0.0, time.milliseconds()); |
||||
time.sleep(10); |
||||
ratio.record(config, 10, time.milliseconds()); |
||||
time.sleep(10); |
||||
ratio.record(config, 0, time.milliseconds()); |
||||
assertEquals(0.5, ratio.measure(config, time.milliseconds())); |
||||
|
||||
time.sleep(10); |
||||
ratio.record(config, 10, time.milliseconds()); |
||||
time.sleep(40); |
||||
ratio.record(config, 0, time.milliseconds()); |
||||
assertEquals(0.2, ratio.measure(config, time.milliseconds())); |
||||
} |
||||
|
||||
@Test |
||||
public void testRatioMisalignedWindow() { |
||||
MetricConfig config = new MetricConfig(); |
||||
MockTime time = new MockTime(); |
||||
TimeRatio ratio = new TimeRatio(1.0); |
||||
|
||||
ratio.record(config, 0.0, time.milliseconds()); |
||||
time.sleep(10); |
||||
ratio.record(config, 10, time.milliseconds()); |
||||
time.sleep(10); |
||||
|
||||
// No recordings, so the last 10ms are not counted.
|
||||
assertEquals(1.0, ratio.measure(config, time.milliseconds())); |
||||
|
||||
// Now the measurement of 5ms arrives. We measure the time since the last
|
||||
// recording, so 5ms/10ms = 0.5.
|
||||
ratio.record(config, 5, time.milliseconds()); |
||||
assertEquals(0.5, ratio.measure(config, time.milliseconds())); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue