• 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

49.25
/common/src/main/java/org/apache/rocketmq/common/stats/StatsItemSet.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.Iterator;
21
import java.util.Map.Entry;
22
import java.util.concurrent.ConcurrentHashMap;
23
import java.util.concurrent.ConcurrentMap;
24
import java.util.concurrent.ScheduledExecutorService;
25
import java.util.concurrent.TimeUnit;
26
import org.apache.rocketmq.common.UtilAll;
27
import org.apache.rocketmq.logging.InternalLogger;
28

29
public class StatsItemSet {
30
    private final ConcurrentMap<String/* key */, StatsItem> statsItemTable =
2✔
31
        new ConcurrentHashMap<String, StatsItem>(128);
32

33
    private final String statsName;
34
    private final ScheduledExecutorService scheduledExecutorService;
35
    private final InternalLogger log;
36

37
    public StatsItemSet(String statsName, ScheduledExecutorService scheduledExecutorService, InternalLogger log) {
2✔
38
        this.statsName = statsName;
2✔
39
        this.scheduledExecutorService = scheduledExecutorService;
2✔
40
        this.log = log;
2✔
41
        this.init();
2✔
42
    }
2✔
43

44
    public void init() {
45

46
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
47
            @Override
48
            public void run() {
49
                try {
50
                    samplingInSeconds();
2✔
51
                } catch (Throwable ignored) {
×
52
                }
2✔
53
            }
2✔
54
        }, 0, 10, TimeUnit.SECONDS);
55

56
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
57
            @Override
58
            public void run() {
59
                try {
60
                    samplingInMinutes();
2✔
61
                } catch (Throwable ignored) {
×
62
                }
2✔
63
            }
2✔
64
        }, 0, 10, TimeUnit.MINUTES);
65

66
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
67
            @Override
68
            public void run() {
69
                try {
70
                    samplingInHour();
2✔
71
                } catch (Throwable ignored) {
×
72
                }
2✔
73
            }
2✔
74
        }, 0, 1, TimeUnit.HOURS);
75

76
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
77
            @Override
78
            public void run() {
79
                try {
80
                    printAtMinutes();
×
81
                } catch (Throwable ignored) {
×
82
                }
×
83
            }
×
84
        }, Math.abs(UtilAll.computeNextMinutesTimeMillis() - System.currentTimeMillis()), 1000 * 60, TimeUnit.MILLISECONDS);
2✔
85

86
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
87
            @Override
88
            public void run() {
89
                try {
90
                    printAtHour();
×
91
                } catch (Throwable ignored) {
×
92
                }
×
93
            }
×
94
        }, Math.abs(UtilAll.computeNextHourTimeMillis() - System.currentTimeMillis()), 1000 * 60 * 60, TimeUnit.MILLISECONDS);
2✔
95

96
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
2✔
97
            @Override
98
            public void run() {
99
                try {
100
                    printAtDay();
×
101
                } catch (Throwable ignored) {
×
102
                }
×
103
            }
×
104
        }, Math.abs(UtilAll.computeNextMorningTimeMillis() - System.currentTimeMillis()), 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
2✔
105
    }
2✔
106

107
    private void samplingInSeconds() {
108
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
2✔
109
        while (it.hasNext()) {
2✔
110
            Entry<String, StatsItem> next = it.next();
2✔
111
            next.getValue().samplingInSeconds();
2✔
112
        }
2✔
113
    }
2✔
114

115
    private void samplingInMinutes() {
116
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
2✔
117
        while (it.hasNext()) {
2✔
118
            Entry<String, StatsItem> next = it.next();
×
119
            next.getValue().samplingInMinutes();
×
120
        }
×
121
    }
2✔
122

123
    private void samplingInHour() {
124
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
2✔
125
        while (it.hasNext()) {
2✔
126
            Entry<String, StatsItem> next = it.next();
×
127
            next.getValue().samplingInHour();
×
128
        }
×
129
    }
2✔
130

131
    private void printAtMinutes() {
132
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
133
        while (it.hasNext()) {
×
134
            Entry<String, StatsItem> next = it.next();
×
135
            next.getValue().printAtMinutes();
×
136
        }
×
137
    }
×
138

139
    private void printAtHour() {
140
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
141
        while (it.hasNext()) {
×
142
            Entry<String, StatsItem> next = it.next();
×
143
            next.getValue().printAtHour();
×
144
        }
×
145
    }
×
146

147
    private void printAtDay() {
148
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
149
        while (it.hasNext()) {
×
150
            Entry<String, StatsItem> next = it.next();
×
151
            next.getValue().printAtDay();
×
152
        }
×
153
    }
×
154

155
    public void addValue(final String statsKey, final int incValue, final int incTimes) {
156
        StatsItem statsItem = this.getAndCreateStatsItem(statsKey);
2✔
157
        statsItem.getValue().add(incValue);
2✔
158
        statsItem.getTimes().add(incTimes);
2✔
159
    }
2✔
160

161
    public void addRTValue(final String statsKey, final int incValue, final int incTimes) {
162
        StatsItem statsItem = this.getAndCreateRTStatsItem(statsKey);
2✔
163
        statsItem.getValue().add(incValue);
2✔
164
        statsItem.getTimes().add(incTimes);
2✔
165
    }
2✔
166

167
    public void delValue(final String statsKey) {
168
        StatsItem statsItem = this.statsItemTable.get(statsKey);
×
169
        if (null != statsItem) {
×
170
            this.statsItemTable.remove(statsKey);
×
171
        }
172
    }
×
173

174
    public void delValueByPrefixKey(final String statsKey, String separator) {
175
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
176
        while (it.hasNext()) {
×
177
            Entry<String, StatsItem> next = it.next();
×
178
            if (next.getKey().startsWith(statsKey + separator)) {
×
179
                it.remove();
×
180
            }
181
        }
×
182
    }
×
183

184
    public void delValueByInfixKey(final String statsKey, String separator) {
185
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
186
        while (it.hasNext()) {
×
187
            Entry<String, StatsItem> next = it.next();
×
188
            if (next.getKey().contains(separator + statsKey + separator)) {
×
189
                it.remove();
×
190
            }
191
        }
×
192
    }
×
193

194
    public void delValueBySuffixKey(final String statsKey, String separator) {
195
        Iterator<Entry<String, StatsItem>> it = this.statsItemTable.entrySet().iterator();
×
196
        while (it.hasNext()) {
×
197
            Entry<String, StatsItem> next = it.next();
×
198
            if (next.getKey().endsWith(separator + statsKey)) {
×
199
                it.remove();
×
200
            }
201
        }
×
202
    }
×
203

204
    public StatsItem getAndCreateStatsItem(final String statsKey) {
205
        return getAndCreateItem(statsKey, false);
2✔
206
    }
207

208
    public StatsItem getAndCreateRTStatsItem(final String statsKey) {
209
        return getAndCreateItem(statsKey, true);
2✔
210
    }
211

212
    public StatsItem getAndCreateItem(final String statsKey, boolean rtItem) {
213
        StatsItem statsItem = this.statsItemTable.get(statsKey);
2✔
214
        if (null == statsItem) {
2✔
215
            if (rtItem) {
2✔
216
                statsItem = new RTStatsItem(this.statsName, statsKey, this.scheduledExecutorService, this.log);
2✔
217
            } else {
218
                statsItem = new StatsItem(this.statsName, statsKey, this.scheduledExecutorService, this.log);
2✔
219
            }
220
            StatsItem prev = this.statsItemTable.putIfAbsent(statsKey, statsItem);
2✔
221

222
            if (null != prev) {
2✔
223
                statsItem = prev;
×
224
                // statsItem.init();
225
            }
226
        }
227

228
        return statsItem;
2✔
229
    }
230

231
    public StatsSnapshot getStatsDataInMinute(final String statsKey) {
232
        StatsItem statsItem = this.statsItemTable.get(statsKey);
2✔
233
        if (null != statsItem) {
2✔
234
            return statsItem.getStatsDataInMinute();
2✔
235
        }
236
        return new StatsSnapshot();
×
237
    }
238

239
    public StatsSnapshot getStatsDataInHour(final String statsKey) {
240
        StatsItem statsItem = this.statsItemTable.get(statsKey);
2✔
241
        if (null != statsItem) {
2✔
242
            return statsItem.getStatsDataInHour();
2✔
243
        }
244
        return new StatsSnapshot();
×
245
    }
246

247
    public StatsSnapshot getStatsDataInDay(final String statsKey) {
248
        StatsItem statsItem = this.statsItemTable.get(statsKey);
2✔
249
        if (null != statsItem) {
2✔
250
            return statsItem.getStatsDataInDay();
2✔
251
        }
252
        return new StatsSnapshot();
×
253
    }
254

255
    public StatsItem getStatsItem(final String statsKey) {
256
        return this.statsItemTable.get(statsKey);
2✔
257
    }
258
}
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