jeffkbkim commented on code in PR #17221: URL: https://github.com/apache/kafka/pull/17221#discussion_r1772963066
########## coordinator-common/src/test/java/org/apache/kafka/coordinator/common/runtime/HdrHistogramTest.java: ########## @@ -172,4 +177,39 @@ public void testHistogramDataReset() { assertEquals(numEventsInFirstCycle, hdrHistogram.count(now + maxSnapshotAgeMs)); assertEquals(numEventsInSecondCycle, hdrHistogram.count(now + 1 + maxSnapshotAgeMs)); } + + @Test + public void testLatestHistogramRace() throws InterruptedException, ExecutionException { + long maxSnapshotAgeMs = 10L; + long now = System.currentTimeMillis(); + HdrHistogram hdrHistogram = new HdrHistogram(maxSnapshotAgeMs, MAX_VALUE, 1); + ExecutorService countExecutor = Executors.newFixedThreadPool(2); + for (int i = 1; i < 10000; i++) { + int numEvents = 2; + for (int j = 0; j < numEvents; j++) { + hdrHistogram.record(i); + } + final long moreThanMaxAge = now + maxSnapshotAgeMs + 1; + now = moreThanMaxAge; + CountDownLatch latch = new CountDownLatch(1); + Callable<Long> countTask = () -> { + try { + assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); + return hdrHistogram.count(moreThanMaxAge); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }; + Future<Long> t1Future = countExecutor.submit(countTask); + Future<Long> t2Future = countExecutor.submit(countTask); + latch.countDown(); + long t1Count = t1Future.get(); + long t2Count = t2Future.get(); + assertTrue( + numEvents == t1Count && numEvents == t2Count, Review Comment: ah, i understand where i got confused - with this PR a call to `latestHistogram()` will always return a MAX_AGE_SNAPSHOT worth of data (as it should). thanks for the explanation! -- 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