diff --git a/common/src/main/java/org/apache/rocketmq/common/stats/StatsItem.java b/common/src/main/java/org/apache/rocketmq/common/stats/StatsItem.java index bd575baba8caa52fa3df174a3cd04c9c6ccade74..6304ea2c4a37f5696f08e85614d003e363d4347a 100644 --- a/common/src/main/java/org/apache/rocketmq/common/stats/StatsItem.java +++ b/common/src/main/java/org/apache/rocketmq/common/stats/StatsItem.java @@ -152,6 +152,9 @@ public class StatsItem { public void samplingInSeconds() { synchronized (this.csListMinute) { + if (this.csListMinute.size() == 0) { + this.csListMinute.add(new CallSnapshot(System.currentTimeMillis() - 10 * 1000, 0, 0)); + } this.csListMinute.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value .get())); if (this.csListMinute.size() > 7) { @@ -162,6 +165,9 @@ public class StatsItem { public void samplingInMinutes() { synchronized (this.csListHour) { + if (this.csListHour.size() == 0) { + this.csListHour.add(new CallSnapshot(System.currentTimeMillis() - 10 * 60 * 1000, 0, 0)); + } this.csListHour.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value .get())); if (this.csListHour.size() > 7) { @@ -172,6 +178,9 @@ public class StatsItem { public void samplingInHour() { synchronized (this.csListDay) { + if (this.csListDay.size() == 0) { + this.csListDay.add(new CallSnapshot(System.currentTimeMillis() - 1 * 60 * 60 * 1000, 0, 0)); + } this.csListDay.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value .get())); if (this.csListDay.size() > 25) { diff --git a/common/src/test/java/org/apache/rocketmq/common/stats/StatsItemSetTest.java b/common/src/test/java/org/apache/rocketmq/common/stats/StatsItemSetTest.java index bd6550d7be7fced55f8d925ae4cb6efe84562d5d..4b4a86765b99aa4bba2437c3e1f2eab7de27facf 100644 --- a/common/src/test/java/org/apache/rocketmq/common/stats/StatsItemSetTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/stats/StatsItemSetTest.java @@ -44,6 +44,35 @@ public class StatsItemSetTest { assertEquals(10, test_unit_moment().longValue()); } + @Test + public void test_statsOfFirstStatisticsCycle() throws InterruptedException { + final StatsItemSet statsItemSet = new StatsItemSet("topicTest", scheduler, null); + executor = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS, + new ArrayBlockingQueue(100), new ThreadFactoryImpl("testMultiThread")); + for (int i = 0; i < 10; i++) { + executor.submit(new Runnable() { + @Override + public void run() { + statsItemSet.addValue("topicTest", 2, 1); + } + }); + } + while (true) { + if (executor.getCompletedTaskCount() == 10) { + break; + } + Thread.sleep(1000); + } + // simulate schedule task execution + statsItemSet.getStatsItem("topicTest").samplingInSeconds(); + statsItemSet.getStatsItem("topicTest").samplingInMinutes(); + statsItemSet.getStatsItem("topicTest").samplingInHour(); + + assertEquals(20L, statsItemSet.getStatsDataInMinute("topicTest").getSum()); + assertEquals(20L, statsItemSet.getStatsDataInHour("topicTest").getSum()); + assertEquals(20L, statsItemSet.getStatsDataInDay("topicTest").getSum()); + } + private AtomicLong test_unit() throws InterruptedException { final StatsItemSet statsItemSet = new StatsItemSet("topicTest", scheduler, null); executor = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS,