• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / rocketmq / 7926

pending completion
7926

push

travis-ci-com

GitHub
[ISSUE #5965] Fix lmqTopicQueueTable initialization (#5967)

10 of 10 new or added lines in 2 files covered. (100.0%)

22455 of 43079 relevant lines covered (52.13%)

1.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.67
/common/src/main/java/org/apache/rocketmq/common/stats/StatsItem.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package org.apache.rocketmq.common.stats;
19

20
import java.util.LinkedList;
21
import java.util.concurrent.ScheduledExecutorService;
22
import java.util.concurrent.TimeUnit;
23
import java.util.concurrent.atomic.LongAdder;
24

25
import org.apache.rocketmq.common.UtilAll;
26
import org.apache.rocketmq.logging.InternalLogger;
27

28
public class StatsItem {
29

30
    private final LongAdder value = new LongAdder();
2✔
31

32
    private final LongAdder times = new LongAdder();
2✔
33

34
    private final LinkedList<CallSnapshot> csListMinute = new LinkedList<CallSnapshot>();
2✔
35

36
    private final LinkedList<CallSnapshot> csListHour = new LinkedList<CallSnapshot>();
2✔
37

38
    private final LinkedList<CallSnapshot> csListDay = new LinkedList<CallSnapshot>();
2✔
39

40
    private final String statsName;
41
    private final String statsKey;
42
    private final ScheduledExecutorService scheduledExecutorService;
43
    private final InternalLogger log;
44

45
    public StatsItem(String statsName, String statsKey, ScheduledExecutorService scheduledExecutorService,
46
        InternalLogger log) {
2✔
47
        this.statsName = statsName;
2✔
48
        this.statsKey = statsKey;
2✔
49
        this.scheduledExecutorService = scheduledExecutorService;
2✔
50
        this.log = log;
2✔
51
    }
2✔
52

53
    private static StatsSnapshot computeStatsData(final LinkedList<CallSnapshot> csList) {
54
        StatsSnapshot statsSnapshot = new StatsSnapshot();
2✔
55
        synchronized (csList) {
2✔
56
            double tps = 0;
2✔
57
            double avgpt = 0;
2✔
58
            long sum = 0;
2✔
59
            long timesDiff = 0;
2✔
60
            if (!csList.isEmpty()) {
2✔
61
                CallSnapshot first = csList.getFirst();
2✔
62
                CallSnapshot last = csList.getLast();
2✔
63
                sum = last.getValue() - first.getValue();
2✔
64
                tps = (sum * 1000.0d) / (last.getTimestamp() - first.getTimestamp());
2✔
65

66
                timesDiff = last.getTimes() - first.getTimes();
2✔
67
                if (timesDiff > 0) {
2✔
68
                    avgpt = (sum * 1.0d) / timesDiff;
2✔
69
                }
70
            }
71

72
            statsSnapshot.setSum(sum);
2✔
73
            statsSnapshot.setTps(tps);
2✔
74
            statsSnapshot.setAvgpt(avgpt);
2✔
75
            statsSnapshot.setTimes(timesDiff);
2✔
76
        }
2✔
77

78
        return statsSnapshot;
2✔
79
    }
80

81
    public StatsSnapshot getStatsDataInMinute() {
82
        return computeStatsData(this.csListMinute);
2✔
83
    }
84

85
    public StatsSnapshot getStatsDataInHour() {
86
        return computeStatsData(this.csListHour);
2✔
87
    }
88

89
    public StatsSnapshot getStatsDataInDay() {
90
        return computeStatsData(this.csListDay);
2✔
91
    }
92

93
    public void init() {
94

95
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
96
            @Override
97
            public void run() {
98
                try {
99
                    samplingInSeconds();
×
100
                } catch (Throwable ignored) {
×
101
                }
×
102
            }
×
103
        }, 0, 10, TimeUnit.SECONDS);
104

105
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
106
            @Override
107
            public void run() {
108
                try {
109
                    samplingInMinutes();
×
110
                } catch (Throwable ignored) {
×
111
                }
×
112
            }
×
113
        }, 0, 10, TimeUnit.MINUTES);
114

115
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
116
            @Override
117
            public void run() {
118
                try {
119
                    samplingInHour();
×
120
                } catch (Throwable ignored) {
×
121
                }
×
122
            }
×
123
        }, 0, 1, TimeUnit.HOURS);
124

125
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
126
            @Override
127
            public void run() {
128
                try {
129
                    printAtMinutes();
×
130
                } catch (Throwable ignored) {
×
131
                }
×
132
            }
×
133
        }, Math.abs(UtilAll.computeNextMinutesTimeMillis() - System.currentTimeMillis()), 1000 * 60, TimeUnit.MILLISECONDS);
×
134

135
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
136
            @Override
137
            public void run() {
138
                try {
139
                    printAtHour();
×
140
                } catch (Throwable ignored) {
×
141
                }
×
142
            }
×
143
        }, Math.abs(UtilAll.computeNextHourTimeMillis() - System.currentTimeMillis()), 1000 * 60 * 60, TimeUnit.MILLISECONDS);
×
144

145
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
×
146
            @Override
147
            public void run() {
148
                try {
149
                    printAtDay();
×
150
                } catch (Throwable ignored) {
×
151
                }
×
152
            }
×
153
        }, Math.abs(UtilAll.computeNextMorningTimeMillis() - System.currentTimeMillis()) - 2000, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
×
154
    }
×
155

156
    public void samplingInSeconds() {
157
        synchronized (this.csListMinute) {
2✔
158
            if (this.csListMinute.size() == 0) {
2✔
159
                this.csListMinute.add(new CallSnapshot(System.currentTimeMillis() - 10 * 1000, 0, 0));
2✔
160
            }
161
            this.csListMinute.add(new CallSnapshot(System.currentTimeMillis(), this.times.sum(), this.value
2✔
162
                .sum()));
2✔
163
            if (this.csListMinute.size() > 7) {
2✔
164
                this.csListMinute.removeFirst();
×
165
            }
166
        }
2✔
167
    }
2✔
168

169
    public void samplingInMinutes() {
170
        synchronized (this.csListHour) {
2✔
171
            if (this.csListHour.size() == 0) {
2✔
172
                this.csListHour.add(new CallSnapshot(System.currentTimeMillis() - 10 * 60 * 1000, 0, 0));
2✔
173
            }
174
            this.csListHour.add(new CallSnapshot(System.currentTimeMillis(), this.times.sum(), this.value
2✔
175
                .sum()));
2✔
176
            if (this.csListHour.size() > 7) {
2✔
177
                this.csListHour.removeFirst();
×
178
            }
179
        }
2✔
180
    }
2✔
181

182
    public void samplingInHour() {
183
        synchronized (this.csListDay) {
2✔
184
            if (this.csListDay.size() == 0) {
2✔
185
                this.csListDay.add(new CallSnapshot(System.currentTimeMillis() - 1 * 60 * 60 * 1000, 0, 0));
2✔
186
            }
187
            this.csListDay.add(new CallSnapshot(System.currentTimeMillis(), this.times.sum(), this.value
2✔
188
                .sum()));
2✔
189
            if (this.csListDay.size() > 25) {
2✔
190
                this.csListDay.removeFirst();
×
191
            }
192
        }
2✔
193
    }
2✔
194

195
    public void printAtMinutes() {
196
        StatsSnapshot ss = computeStatsData(this.csListMinute);
×
197
        log.info(String.format("[%s] [%s] Stats In One Minute, ", this.statsName, this.statsKey) + statPrintDetail(ss));
×
198
    }
×
199

200
    public void printAtHour() {
201
        StatsSnapshot ss = computeStatsData(this.csListHour);
×
202
        log.info(String.format("[%s] [%s] Stats In One Hour, ", this.statsName, this.statsKey) + statPrintDetail(ss));
×
203

204
    }
×
205

206
    public void printAtDay() {
207
        StatsSnapshot ss = computeStatsData(this.csListDay);
×
208
        log.info(String.format("[%s] [%s] Stats In One Day, ", this.statsName, this.statsKey) + statPrintDetail(ss));
×
209
    }
×
210

211
    protected String statPrintDetail(StatsSnapshot ss) {
212
        return String.format("SUM: %d TPS: %.2f AVGPT: %.2f",
×
213
                ss.getSum(),
×
214
                ss.getTps(),
×
215
                ss.getAvgpt());
×
216
    }
217

218
    public LongAdder getValue() {
219
        return value;
2✔
220
    }
221

222
    public String getStatsKey() {
223
        return statsKey;
×
224
    }
225

226
    public String getStatsName() {
227
        return statsName;
×
228
    }
229

230
    public LongAdder getTimes() {
231
        return times;
2✔
232
    }
233
}
234

235
class CallSnapshot {
236
    private final long timestamp;
237
    private final long times;
238

239
    private final long value;
240

241
    public CallSnapshot(long timestamp, long times, long value) {
242
        super();
2✔
243
        this.timestamp = timestamp;
2✔
244
        this.times = times;
2✔
245
        this.value = value;
2✔
246
    }
2✔
247

248
    public long getTimestamp() {
249
        return timestamp;
2✔
250
    }
251

252
    public long getTimes() {
253
        return times;
2✔
254
    }
255

256
    public long getValue() {
257
        return value;
2✔
258
    }
259
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc