ahuang98 commented on PR #17139:
URL: https://github.com/apache/kafka/pull/17139#issuecomment-2590913611

   > Complete the poll at t=30
   Call measure at t=31.
   > We now have 20 ms of polling time inside an 11 ms measurement window. This 
causes the metric to [report 
1.0](https://github.com/apache/kafka/blob/da0c3beffa96944a899ea949a7ab5705f67be882/raft/src/main/java/org/apache/kafka/raft/internals/TimeRatio.java#L59)
 and reset the counters to 0.
   
   This doesn't seem to be correct - it's quite convoluted but the measurement 
window (lastRecordedTimestampMs - intervalStartTimestampMs) would encompass the 
start of that poll. `testRatioMisalignedWindow` seems to do this check, I also 
wrote out a more detailed test to confirm my own understanding -
   
   ```
       @Test
       public void testPartialPoll() {
           MetricConfig config = new MetricConfig();
           MockTime time = new MockTime();
           TimeRatio ratio = new TimeRatio(1.0);
   
           ratio.record(config, 0.0, time.milliseconds());
           time.sleep(20);
           ratio.record(config, 10, time.milliseconds());
   
           time.sleep(10);
           // pretend a poll() starts at this point
           time.sleep(10);
   
           // We ignore the last 20ms that lapsed after the last record() was 
called. Cumulative value captured by calls
           // to record since last call to measure is 10, 
lastRecordedTimestampMs - intervalStartTimestampMs = 20s - 0s.
           assertEquals(0.5, ratio.measure(config, time.milliseconds()));
   
           // Now the current poll() finishes.
           ratio.record(config, 10, time.milliseconds());
           // Cumulative value captured by calls to record since last call to 
measure is 10,
           // lastRecordedTimestampMs - intervalStartTimestampMs = 40s - 20s.
           assertEquals(0.5, ratio.measure(config, time.milliseconds()));
   
           // If we had calculated the ratio over the entire window of the test 
(40s), the ratio would have been
           // (10 + 10) / 40 = 0.5.
       }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to