rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinrongt...@apache.org
Subject [rocketmq] 21/22: [ISSUE #1656] Fix StatsItem bug (#1704)
Date Mon, 20 Jan 2020 13:25:54 GMT
This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch test-release
in repository https://gitbox.apache.org/repos/asf/rocketmq.git

commit a655070a8960ed844ad8d857b42408ad5271f118
Author: Chengyang He <Aaron-He@users.noreply.github.com>
AuthorDate: Mon Jan 13 17:11:55 2020 +0800

    [ISSUE #1656] Fix StatsItem bug (#1704)
    
    Fix bug: The stats data could be inaccurate of first minute/hour/day
---
 .../apache/rocketmq/common/stats/StatsItem.java    |  9 +++++++
 .../rocketmq/common/stats/StatsItemSetTest.java    | 29 ++++++++++++++++++++++
 2 files changed, 38 insertions(+)

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 bd575ba..6304ea2 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 bd6550d..4b4a867 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<Runnable>(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,


Mime
View raw message