Skip to content

Commit 4effe3c

Browse files
authored
[ISSUE #6541]✨StatsItem to use TimeUtils.current_millis for timestamp generation (#6576)
use TimeUtils::current_millis in stats_item
1 parent 2cc1652 commit 4effe3c

File tree

1 file changed

+12
-77
lines changed

1 file changed

+12
-77
lines changed

rocketmq-common/src/common/stats/stats_item.rs

Lines changed: 12 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::sync::atomic::AtomicU64;
1818
use std::sync::atomic::Ordering;
1919
use std::time::SystemTime;
2020

21+
use crate::TimeUtils::current_millis;
2122
use parking_lot::Mutex;
2223
use tracing::info;
2324

@@ -37,17 +38,13 @@ pub struct StatsItem {
3738

3839
impl StatsItem {
3940
pub fn new(stats_name: &str, stats_key: &str) -> Self {
40-
let now_ms = SystemTime::now()
41-
.duration_since(SystemTime::UNIX_EPOCH)
42-
.unwrap_or_default()
43-
.as_millis() as u64;
4441
StatsItem {
4542
value: AtomicU64::new(0),
4643
times: AtomicU64::new(0),
4744
cs_list_minute: Mutex::new(LinkedList::new()),
4845
cs_list_hour: Mutex::new(LinkedList::new()),
4946
cs_list_day: Mutex::new(LinkedList::new()),
50-
last_update_timestamp: AtomicU64::new(now_ms),
47+
last_update_timestamp: AtomicU64::new(current_millis()),
5148
stats_name: stats_name.to_string(),
5249
stats_key: stats_key.to_string(),
5350
}
@@ -67,13 +64,7 @@ impl StatsItem {
6764
pub fn add(&self, value_delta: u64, times_delta: u64) {
6865
self.value.fetch_add(value_delta, Ordering::Relaxed);
6966
self.times.fetch_add(times_delta, Ordering::Relaxed);
70-
self.last_update_timestamp.store(
71-
SystemTime::now()
72-
.duration_since(SystemTime::UNIX_EPOCH)
73-
.unwrap_or_default()
74-
.as_millis() as u64,
75-
Ordering::Relaxed,
76-
);
67+
self.last_update_timestamp.store(current_millis(), Ordering::Relaxed);
7768
}
7869

7970
/// Returns the current accumulated value.
@@ -163,24 +154,9 @@ impl StatsItem {
163154
) {
164155
let mut cs_list = cs_list.lock();
165156
if cs_list.is_empty() {
166-
cs_list.push_back(CallSnapshot::new(
167-
SystemTime::now()
168-
.duration_since(SystemTime::UNIX_EPOCH)
169-
.unwrap()
170-
.as_millis() as u64
171-
- 10 * 1000,
172-
0,
173-
0,
174-
));
157+
cs_list.push_back(CallSnapshot::new(current_millis() - 10 * 1000, 0, 0));
175158
}
176-
cs_list.push_back(CallSnapshot::new(
177-
SystemTime::now()
178-
.duration_since(SystemTime::UNIX_EPOCH)
179-
.unwrap()
180-
.as_millis() as u64,
181-
current_times,
182-
current_value,
183-
));
159+
cs_list.push_back(CallSnapshot::new(current_millis(), current_times, current_value));
184160
if cs_list.len() > 7 {
185161
cs_list.pop_front();
186162
}
@@ -189,24 +165,9 @@ impl StatsItem {
189165
pub fn sampling_in_minutes(cs_list: &Mutex<LinkedList<CallSnapshot>>, current_value: u64, current_times: u64) {
190166
let mut cs_list = cs_list.lock();
191167
if cs_list.is_empty() {
192-
cs_list.push_back(CallSnapshot::new(
193-
SystemTime::now()
194-
.duration_since(SystemTime::UNIX_EPOCH)
195-
.unwrap()
196-
.as_millis() as u64
197-
- 10 * 60 * 1000,
198-
0,
199-
0,
200-
));
168+
cs_list.push_back(CallSnapshot::new(current_millis() - 10 * 60 * 1000, 0, 0));
201169
}
202-
cs_list.push_back(CallSnapshot::new(
203-
SystemTime::now()
204-
.duration_since(SystemTime::UNIX_EPOCH)
205-
.unwrap()
206-
.as_millis() as u64,
207-
current_times,
208-
current_value,
209-
));
170+
cs_list.push_back(CallSnapshot::new(current_millis(), current_times, current_value));
210171
if cs_list.len() > 7 {
211172
cs_list.pop_front();
212173
}
@@ -215,24 +176,9 @@ impl StatsItem {
215176
pub fn sampling_in_hour(cs_list: &Mutex<LinkedList<CallSnapshot>>, current_value: u64, current_times: u64) {
216177
let mut cs_list = cs_list.lock();
217178
if cs_list.is_empty() {
218-
cs_list.push_back(CallSnapshot::new(
219-
SystemTime::now()
220-
.duration_since(SystemTime::UNIX_EPOCH)
221-
.unwrap()
222-
.as_millis() as u64
223-
- 60 * 60 * 1000,
224-
0,
225-
0,
226-
));
179+
cs_list.push_back(CallSnapshot::new(current_millis() - 60 * 60 * 1000, 0, 0));
227180
}
228-
cs_list.push_back(CallSnapshot::new(
229-
SystemTime::now()
230-
.duration_since(SystemTime::UNIX_EPOCH)
231-
.unwrap()
232-
.as_millis() as u64,
233-
current_times,
234-
current_value,
235-
));
181+
cs_list.push_back(CallSnapshot::new(current_millis(), current_times, current_value));
236182
if cs_list.len() > 25 {
237183
cs_list.pop_front();
238184
}
@@ -278,26 +224,15 @@ impl StatsItem {
278224
}
279225

280226
pub fn compute_next_minutes_time_millis() -> u64 {
281-
let now = SystemTime::now()
282-
.duration_since(SystemTime::UNIX_EPOCH)
283-
.unwrap()
284-
.as_millis() as u64;
285-
(now / 60000 + 1) * 60000
227+
(current_millis() / 60000 + 1) * 60000
286228
}
287229

288230
pub fn compute_next_hour_time_millis() -> u64 {
289-
let now = SystemTime::now()
290-
.duration_since(SystemTime::UNIX_EPOCH)
291-
.unwrap()
292-
.as_millis() as u64;
293-
(now / 3600000 + 1) * 3600000
231+
(current_millis() / 3600000 + 1) * 3600000
294232
}
295233

296234
pub fn compute_next_morning_time_millis() -> u64 {
297-
let now = SystemTime::now()
298-
.duration_since(SystemTime::UNIX_EPOCH)
299-
.unwrap()
300-
.as_millis() as u64;
235+
let now = current_millis();
301236
let current_day = now / (24 * 3600000);
302237
(current_day + 1) * 24 * 3600000
303238
}

0 commit comments

Comments
 (0)