forked from mxsm/rocketmq-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker_stats_manager.rs
More file actions
1419 lines (1256 loc) · 51.9 KB
/
broker_stats_manager.rs
File metadata and controls
1419 lines (1256 loc) · 51.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::Arc;
use cheetah_string::CheetahString;
use dashmap::DashMap;
use parking_lot::Mutex;
use rocketmq_common::common::broker::broker_config::BrokerConfig;
use rocketmq_common::common::statistics::state_getter::StateGetter;
use rocketmq_common::common::statistics::statistics_item::StatisticsItem;
use rocketmq_common::common::statistics::statistics_item_formatter::StatisticsItemFormatter;
use rocketmq_common::common::statistics::statistics_item_printer::StatisticsItemPrinter;
use rocketmq_common::common::statistics::statistics_item_scheduled_printer::StatisticsItemScheduledPrinter;
use rocketmq_common::common::statistics::statistics_item_state_getter::StatisticsItemStateGetter;
use rocketmq_common::common::statistics::statistics_kind_meta::StatisticsKindMeta;
use rocketmq_common::common::statistics::statistics_manager::StatisticsManager;
use rocketmq_common::common::stats::moment_stats_item_set::MomentStatsItemSet;
use rocketmq_common::common::stats::stats_item::StatsItem;
use rocketmq_common::common::stats::stats_item_set::StatsItemSet;
use rocketmq_common::common::stats::Stats;
use rocketmq_common::common::topic::TopicValidator;
use rocketmq_common::TimeUtils::get_current_millis;
use rocketmq_rust::schedule::simple_scheduler::ScheduledTaskManager;
use tokio::time::Duration;
use tracing::info;
use tracing::warn;
type TaskId = u64;
pub struct BrokerStatsManager {
stats_table: Arc<DashMap<String, StatsItemSet>>,
cluster_name: String,
enable_queue_stat: bool,
moment_stats_item_set_fall_size: Option<Arc<MomentStatsItemSet>>,
moment_stats_item_set_fall_time: Option<Arc<MomentStatsItemSet>>,
account_stat_manager: StatisticsManager,
producer_state_getter: Option<Arc<dyn StateGetter>>,
consumer_state_getter: Option<Arc<dyn StateGetter>>,
broker_config: Option<Arc<BrokerConfig>>,
scheduler: Option<Arc<ScheduledTaskManager>>,
task_ids: Arc<Mutex<Vec<TaskId>>>,
}
impl BrokerStatsManager {
pub const ACCOUNT_AUTH_FAILED: &'static str = "AUTH_FAILED";
pub const ACCOUNT_AUTH_TYPE: &'static str = "AUTH_TYPE";
pub const ACCOUNT_OWNER_PARENT: &'static str = "OWNER_PARENT";
pub const ACCOUNT_OWNER_SELF: &'static str = "OWNER_SELF";
pub const ACCOUNT_RCV: &'static str = "RCV";
pub const ACCOUNT_REV_REJ: &'static str = "RCV_REJ";
pub const ACCOUNT_SEND: &'static str = "SEND";
pub const ACCOUNT_SEND_BACK: &'static str = "SEND_BACK";
pub const ACCOUNT_SEND_BACK_TO_DLQ: &'static str = "SEND_BACK_TO_DLQ";
pub const ACCOUNT_SEND_REJ: &'static str = "SEND_REJ";
pub const ACCOUNT_STAT_INVERTAL: u64 = 60 * 1000;
pub const BROKER_ACK_NUMS: &'static str = "BROKER_ACK_NUMS";
pub const BROKER_CK_NUMS: &'static str = "BROKER_CK_NUMS";
pub const BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC: &'static str = "BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC";
pub const BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC: &'static str = "BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC";
pub const CHANNEL_ACTIVITY: &'static str = "CHANNEL_ACTIVITY";
pub const CHANNEL_ACTIVITY_CLOSE: &'static str = "CLOSE";
pub const CHANNEL_ACTIVITY_CONNECT: &'static str = "CONNECT";
pub const CHANNEL_ACTIVITY_EXCEPTION: &'static str = "EXCEPTION";
pub const CHANNEL_ACTIVITY_IDLE: &'static str = "IDLE";
pub const COMMERCIAL_MSG_NUM: &'static str = "COMMERCIAL_MSG_NUM";
pub const COMMERCIAL_OWNER: &'static str = "Owner";
// Consumer Register Time
pub const CONSUMER_REGISTER_TIME: &'static str = "CONSUMER_REGISTER_TIME";
pub const DLQ_PUT_NUMS: &'static str = "DLQ_PUT_NUMS";
pub const FAILURE_MSG_NUM: &'static str = "FAILURE_MSG_NUM";
pub const FAILURE_MSG_SIZE: &'static str = "FAILURE_MSG_SIZE";
pub const FAILURE_REQ_NUM: &'static str = "FAILURE_REQ_NUM";
pub const GROUP_ACK_NUMS: &'static str = "GROUP_ACK_NUMS";
pub const GROUP_CK_NUMS: &'static str = "GROUP_CK_NUMS";
#[deprecated]
pub const GROUP_GET_FALL_SIZE: &'static str = "GROUP_GET_FALL_SIZE";
#[deprecated]
pub const GROUP_GET_FALL_TIME: &'static str = "GROUP_GET_FALL_TIME";
// Pull Message Latency
#[deprecated]
pub const GROUP_GET_LATENCY: &'static str = "GROUP_GET_LATENCY";
pub const INNER_RT: &'static str = "INNER_RT";
pub const MSG_NUM: &'static str = "MSG_NUM";
pub const MSG_SIZE: &'static str = "MSG_SIZE";
// Producer Register Time
pub const PRODUCER_REGISTER_TIME: &'static str = "PRODUCER_REGISTER_TIME";
pub const RT: &'static str = "RT";
pub const SNDBCK2DLQ_TIMES: &'static str = "SNDBCK2DLQ_TIMES";
pub const SUCCESS_MSG_NUM: &'static str = "SUCCESS_MSG_NUM";
pub const SUCCESS_MSG_SIZE: &'static str = "SUCCESS_MSG_SIZE";
pub const SUCCESS_REQ_NUM: &'static str = "SUCCESS_REQ_NUM";
pub const TOPIC_PUT_LATENCY: &'static str = "TOPIC_PUT_LATENCY";
}
impl BrokerStatsManager {
#[inline]
pub fn start(&self) {
if let Some(scheduler) = &self.scheduler {
self.start_sampling_tasks(scheduler);
info!("BrokerStatsManager started with scheduled tasks");
} else {
warn!("ScheduledTaskManager not provided, sampling tasks not started");
}
}
/// Start all periodic sampling tasks
fn start_sampling_tasks(&self, scheduler: &Arc<ScheduledTaskManager>) {
// Task 1: Sample every 10 seconds for minute-level statistics
let stats_table = Arc::clone(&self.stats_table);
let task_id = scheduler.add_fixed_rate_task(Duration::from_secs(10), Duration::from_secs(10), move |_cancel| {
let stats_table = Arc::clone(&stats_table);
async move {
for entry in stats_table.iter() {
entry.value().sampling_in_minutes();
}
Ok(())
}
});
self.task_ids.lock().push(task_id);
// Task 2: Sample every minute (aligned to minute boundary)
let stats_table = Arc::clone(&self.stats_table);
let initial_delay = Self::compute_initial_delay_to_next_minute();
let task_id = scheduler.add_fixed_rate_task(initial_delay, Duration::from_secs(60), move |_cancel| {
let stats_table = Arc::clone(&stats_table);
async move {
info!("Executing minute-level sampling for all stats");
for entry in stats_table.iter() {
entry.value().sampling_in_minutes();
}
Ok(())
}
});
self.task_ids.lock().push(task_id);
// Task 3: Clean up expired stats every 10 minutes
let stats_table = Arc::clone(&self.stats_table);
let task_id =
scheduler.add_fixed_rate_task(Duration::from_secs(600), Duration::from_secs(600), move |_cancel| {
let stats_table = Arc::clone(&stats_table);
async move {
info!("Cleaning expired statistics items");
// TODO: Implement cleanup logic based on last access time
Ok(())
}
});
self.task_ids.lock().push(task_id);
info!(
"Started {} scheduled tasks for BrokerStatsManager",
self.task_ids.lock().len()
);
}
/// Compute delay to next minute boundary
fn compute_initial_delay_to_next_minute() -> Duration {
let now = get_current_millis();
let next_minute = ((now / 60000) + 1) * 60000;
let delay_ms = next_minute - now;
Duration::from_millis(delay_ms)
}
#[inline]
pub fn new(broker_config: Arc<BrokerConfig>) -> Self {
Self::new_with_scheduler(broker_config, None)
}
#[inline]
pub fn new_with_scheduler(broker_config: Arc<BrokerConfig>, scheduler: Option<Arc<ScheduledTaskManager>>) -> Self {
let stats_table = Arc::new(DashMap::new());
let enable_queue_stat = broker_config.enable_detail_stat;
let cluster_name = broker_config.broker_identity.broker_cluster_name.to_string();
let mut broker_stats_manager = BrokerStatsManager {
stats_table,
cluster_name,
enable_queue_stat,
moment_stats_item_set_fall_size: None,
moment_stats_item_set_fall_time: None,
account_stat_manager: Default::default(),
producer_state_getter: None,
consumer_state_getter: None,
broker_config: Some(broker_config),
scheduler,
task_ids: Arc::new(Mutex::new(Vec::new())),
};
broker_stats_manager.init();
broker_stats_manager
}
#[inline]
pub fn new_with_name(broker_config: Arc<BrokerConfig>, cluster_name: String, enable_queue_stat: bool) -> Self {
let stats_table = Arc::new(DashMap::new());
let mut broker_stats_manager = BrokerStatsManager {
stats_table,
cluster_name,
enable_queue_stat,
moment_stats_item_set_fall_size: None,
moment_stats_item_set_fall_time: None,
account_stat_manager: Default::default(),
producer_state_getter: None,
consumer_state_getter: None,
broker_config: Some(broker_config),
scheduler: None,
task_ids: Arc::new(Mutex::new(Vec::new())),
};
broker_stats_manager.init();
broker_stats_manager
}
#[inline]
pub fn init(&mut self) {
self.moment_stats_item_set_fall_size = Some(Arc::new(MomentStatsItemSet::new(
Stats::GROUP_GET_FALL_SIZE.to_string(),
)));
self.moment_stats_item_set_fall_time = Some(Arc::new(MomentStatsItemSet::new(
Stats::GROUP_GET_FALL_TIME.to_string(),
)));
let enable_queue_stat = self.enable_queue_stat;
if enable_queue_stat {
self.stats_table.insert(
Stats::QUEUE_PUT_NUMS.to_string(),
StatsItemSet::new(Stats::QUEUE_PUT_NUMS.to_string()),
);
self.stats_table.insert(
Stats::QUEUE_PUT_SIZE.to_string(),
StatsItemSet::new(Stats::QUEUE_PUT_SIZE.to_string()),
);
self.stats_table.insert(
Stats::QUEUE_GET_NUMS.to_string(),
StatsItemSet::new(Stats::QUEUE_GET_NUMS.to_string()),
);
self.stats_table.insert(
Stats::QUEUE_GET_SIZE.to_string(),
StatsItemSet::new(Stats::QUEUE_GET_SIZE.to_string()),
);
}
self.stats_table.insert(
Stats::TOPIC_PUT_NUMS.to_string(),
StatsItemSet::new(Stats::TOPIC_PUT_NUMS.to_string()),
);
self.stats_table.insert(
Stats::TOPIC_PUT_SIZE.to_string(),
StatsItemSet::new(Stats::TOPIC_PUT_SIZE.to_string()),
);
self.stats_table.insert(
Stats::GROUP_GET_NUMS.to_string(),
StatsItemSet::new(Stats::GROUP_GET_NUMS.to_string()),
);
self.stats_table.insert(
Stats::GROUP_GET_SIZE.to_string(),
StatsItemSet::new(Stats::GROUP_GET_SIZE.to_string()),
);
self.stats_table.insert(
Self::GROUP_ACK_NUMS.to_string(),
StatsItemSet::new(Self::GROUP_ACK_NUMS.to_string()),
);
self.stats_table.insert(
Self::GROUP_CK_NUMS.to_string(),
StatsItemSet::new(Self::GROUP_CK_NUMS.to_string()),
);
self.stats_table.insert(
Stats::GROUP_GET_LATENCY.to_string(),
StatsItemSet::new(Stats::GROUP_GET_LATENCY.to_string()),
);
self.stats_table.insert(
Self::TOPIC_PUT_LATENCY.to_string(),
StatsItemSet::new(Self::TOPIC_PUT_LATENCY.to_string()),
);
self.stats_table.insert(
Stats::SNDBCK_PUT_NUMS.to_string(),
StatsItemSet::new(Stats::SNDBCK_PUT_NUMS.to_string()),
);
self.stats_table.insert(
Self::DLQ_PUT_NUMS.to_string(),
StatsItemSet::new(Self::DLQ_PUT_NUMS.to_string()),
);
self.stats_table.insert(
Stats::BROKER_PUT_NUMS.to_string(),
StatsItemSet::new(Stats::BROKER_PUT_NUMS.to_string()),
);
self.stats_table.insert(
Stats::BROKER_GET_NUMS.to_string(),
StatsItemSet::new(Stats::BROKER_GET_NUMS.to_string()),
);
self.stats_table.insert(
Self::BROKER_ACK_NUMS.to_string(),
StatsItemSet::new(Self::BROKER_ACK_NUMS.to_string()),
);
self.stats_table.insert(
Self::BROKER_CK_NUMS.to_string(),
StatsItemSet::new(Self::BROKER_CK_NUMS.to_string()),
);
self.stats_table.insert(
Self::BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC.to_string(),
StatsItemSet::new(Self::BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC.to_string()),
);
self.stats_table.insert(
Self::BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC.to_string(),
StatsItemSet::new(Self::BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC.to_string()),
);
self.stats_table.insert(
Stats::GROUP_GET_FROM_DISK_NUMS.to_string(),
StatsItemSet::new(Stats::GROUP_GET_FROM_DISK_NUMS.to_string()),
);
self.stats_table.insert(
Stats::GROUP_GET_FROM_DISK_SIZE.to_string(),
StatsItemSet::new(Stats::GROUP_GET_FROM_DISK_SIZE.to_string()),
);
self.stats_table.insert(
Stats::BROKER_GET_FROM_DISK_NUMS.to_string(),
StatsItemSet::new(Stats::BROKER_GET_FROM_DISK_NUMS.to_string()),
);
self.stats_table.insert(
Stats::BROKER_GET_FROM_DISK_SIZE.to_string(),
StatsItemSet::new(Stats::BROKER_GET_FROM_DISK_SIZE.to_string()),
);
self.stats_table.insert(
Self::SNDBCK2DLQ_TIMES.to_string(),
StatsItemSet::new(Self::SNDBCK2DLQ_TIMES.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_SEND_TIMES.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_SEND_TIMES.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_RCV_TIMES.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_RCV_TIMES.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_SEND_SIZE.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_SEND_SIZE.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_RCV_SIZE.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_RCV_SIZE.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_RCV_EPOLLS.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_RCV_EPOLLS.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_SNDBCK_TIMES.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_SNDBCK_TIMES.to_string()),
);
self.stats_table.insert(
Stats::COMMERCIAL_PERM_FAILURES.to_string(),
StatsItemSet::new(Stats::COMMERCIAL_PERM_FAILURES.to_string()),
);
self.stats_table.insert(
Self::CONSUMER_REGISTER_TIME.to_string(),
StatsItemSet::new(Self::CONSUMER_REGISTER_TIME.to_string()),
);
self.stats_table.insert(
Self::PRODUCER_REGISTER_TIME.to_string(),
StatsItemSet::new(Self::PRODUCER_REGISTER_TIME.to_string()),
);
self.stats_table.insert(
Self::CHANNEL_ACTIVITY.to_string(),
StatsItemSet::new(Self::CHANNEL_ACTIVITY.to_string()),
);
let formatter = StatisticsItemFormatter;
self.account_stat_manager.set_brief_meta(vec![
(Self::RT.to_string(), vec![vec![50, 50], vec![100, 10], vec![1000, 10]]),
(
Self::INNER_RT.to_string(),
vec![vec![50, 50], vec![100, 10], vec![1000, 10]],
),
]);
let item_names = vec![
Self::MSG_NUM,
Self::SUCCESS_MSG_NUM,
Self::FAILURE_MSG_NUM,
Self::COMMERCIAL_MSG_NUM,
Self::SUCCESS_REQ_NUM,
Self::FAILURE_REQ_NUM,
Self::MSG_SIZE,
Self::SUCCESS_MSG_SIZE,
Self::FAILURE_MSG_SIZE,
Self::RT,
Self::INNER_RT,
];
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_SEND,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_RCV,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_SEND_BACK,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_SEND_BACK_TO_DLQ,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_SEND_REJ,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
self.account_stat_manager
.add_statistics_kind_meta(create_statistics_kind_meta(
Self::ACCOUNT_REV_REJ,
item_names.clone(),
&formatter,
Self::ACCOUNT_STAT_INVERTAL,
self.broker_config.as_ref().expect("Broker config must be initialized"),
));
struct DefaultStatisticsItemStateGetter {
producer_state_getter: Option<Arc<dyn StateGetter>>,
consumer_state_getter: Option<Arc<dyn StateGetter>>,
}
impl StatisticsItemStateGetter for DefaultStatisticsItemStateGetter {
#[inline]
fn online(&self, item: &StatisticsItem) -> bool {
let vec = split_account_stat_key(item.stat_object());
if vec.is_empty() || vec.len() < 4 {
return false;
}
let instance_id = CheetahString::from_slice(vec[1]);
let topic = CheetahString::from_slice(vec[2]);
let group = CheetahString::from_slice(vec[3]);
let kind = item.stat_kind();
if BrokerStatsManager::ACCOUNT_SEND == kind || BrokerStatsManager::ACCOUNT_SEND_REJ == kind {
self.producer_state_getter
.as_ref()
.unwrap()
.online(&instance_id, &group, &topic);
} else if BrokerStatsManager::ACCOUNT_RCV == kind
|| BrokerStatsManager::ACCOUNT_SEND_BACK == kind
|| BrokerStatsManager::ACCOUNT_SEND_BACK_TO_DLQ == kind
|| BrokerStatsManager::ACCOUNT_REV_REJ == kind
{
self.consumer_state_getter
.as_ref()
.unwrap()
.online(&instance_id, &group, &topic);
}
false
}
}
self.account_stat_manager
.set_statistics_item_state_getter(Arc::new(DefaultStatisticsItemStateGetter {
producer_state_getter: self.producer_state_getter.clone(),
consumer_state_getter: self.consumer_state_getter.clone(),
}));
}
#[inline]
pub fn set_producer_state_getter(&mut self, state_getter: Arc<dyn StateGetter>) {
self.producer_state_getter = Some(state_getter);
}
#[inline]
pub fn set_consumer_state_getter(&mut self, state_getter: Arc<dyn StateGetter>) {
self.consumer_state_getter = Some(state_getter);
}
#[inline]
pub fn get_stats_table(&self) -> Arc<DashMap<String, StatsItemSet>> {
Arc::clone(&self.stats_table)
}
#[inline]
pub fn get_cluster_name(&self) -> &str {
&self.cluster_name
}
#[inline]
pub fn get_enable_queue_stat(&self) -> bool {
self.enable_queue_stat
}
#[inline]
pub fn get_moment_stats_item_set_fall_size(&self) -> Option<Arc<MomentStatsItemSet>> {
self.moment_stats_item_set_fall_size.clone()
}
#[inline]
pub fn get_moment_stats_item_set_fall_time(&self) -> Option<Arc<MomentStatsItemSet>> {
self.moment_stats_item_set_fall_time.clone()
}
#[inline]
pub fn get_broker_puts_num_without_system_topic(&self) -> u64 {
if let Some(stats) = self.stats_table.get(Self::BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC) {
stats.get_stats_data_in_minute(&self.cluster_name).get_sum()
} else {
0
}
}
#[inline]
pub fn get_broker_gets_num_without_system_topic(&self) -> u64 {
if let Some(stats) = self.stats_table.get(Self::BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC) {
stats.get_stats_data_in_minute(&self.cluster_name).get_sum()
} else {
0
}
}
#[inline]
pub fn get_broker_put_nums(&self) -> u64 {
if let Some(stats) = self.stats_table.get(Stats::BROKER_PUT_NUMS) {
stats.get_stats_data_in_minute(&self.cluster_name).get_sum()
} else {
0
}
}
#[inline]
pub fn get_broker_get_nums(&self) -> u64 {
if let Some(stats) = self.stats_table.get(Stats::BROKER_GET_NUMS) {
stats.get_stats_data_in_minute(&self.cluster_name).get_sum()
} else {
0
}
}
#[inline]
pub fn get_stats_item(&self, stats_name: &str, stats_key: &str) -> Option<Arc<StatsItem>> {
self.stats_table
.get(stats_name)
.and_then(|stats_set| stats_set.get_stats_item(stats_key))
}
#[inline]
pub fn record_disk_fall_behind_size(&self, group: &str, topic: &str, queue_id: i32, fall_behind: i64) {
if let Some(fall_size_set) = &self.moment_stats_item_set_fall_size {
let stats_key = format!("{}@{}@{}", queue_id, topic, group);
let item = fall_size_set.get_and_create_stats_item(stats_key);
item.get_value()
.store(fall_behind, std::sync::atomic::Ordering::Relaxed);
}
}
#[inline]
pub fn inc_topic_put_nums(&self, topic: &str, num: i32, times: i32) {
if let Some(stats) = self.stats_table.get(Stats::TOPIC_PUT_NUMS) {
stats.add_value(topic, num, times);
}
}
#[inline]
pub fn inc_topic_put_size(&self, topic: &str, size: i32) {
if let Some(stats) = self.stats_table.get(Stats::TOPIC_PUT_SIZE) {
stats.add_value(topic, size, 1);
}
}
#[inline]
pub fn inc_group_get_nums(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_NUMS) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_get_size(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_SIZE) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_ck_nums(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Self::GROUP_CK_NUMS) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_ack_nums(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Self::GROUP_ACK_NUMS) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_get_from_disk_nums(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_FROM_DISK_NUMS) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_get_from_disk_size(&self, group: &str, topic: &str, inc_value: i32) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_FROM_DISK_SIZE) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_broker_get_nums(&self, topic: &str, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Stats::BROKER_GET_NUMS) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
if !TopicValidator::is_system_topic(topic) {
if let Some(stats) = self.stats_table.get(Self::BROKER_GET_NUMS_WITHOUT_SYSTEM_TOPIC) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
}
#[inline]
pub fn inc_broker_put_nums(&self, topic: &str, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Stats::BROKER_PUT_NUMS) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
if !TopicValidator::is_system_topic(topic) {
if let Some(stats) = self.stats_table.get(Self::BROKER_PUT_NUMS_WITHOUT_SYSTEM_TOPIC) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
}
#[inline]
pub fn on_topic_deleted(&self, topic: &CheetahString) {
let topic_str = topic.as_str();
if let Some(stats) = self.stats_table.get(Stats::TOPIC_PUT_NUMS) {
stats.del_value(topic_str);
}
if let Some(stats) = self.stats_table.get(Stats::TOPIC_PUT_SIZE) {
stats.del_value(topic_str);
}
if self.enable_queue_stat {
if let Some(stats) = self.stats_table.get(Stats::QUEUE_PUT_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::QUEUE_PUT_SIZE) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::QUEUE_GET_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::QUEUE_GET_SIZE) {
stats.del_value_by_prefix_key(topic_str, "@");
}
}
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_SIZE) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Self::GROUP_ACK_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Self::GROUP_CK_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::SNDBCK_PUT_NUMS) {
stats.del_value_by_prefix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_LATENCY) {
stats.del_value_by_infix_key(topic_str, "@");
}
if let Some(stats) = self.stats_table.get(Self::TOPIC_PUT_LATENCY) {
stats.del_value_by_suffix_key(topic_str, "@");
}
if let Some(fall_size) = &self.moment_stats_item_set_fall_size {
fall_size.del_value_by_infix_key(topic_str, "@");
}
if let Some(fall_time) = &self.moment_stats_item_set_fall_time {
fall_time.del_value_by_infix_key(topic_str, "@");
}
info!("Deleted all stats for topic: {}", topic_str);
}
#[inline]
pub fn on_group_deleted(&self, group: &CheetahString) {
let group_str = group.as_str();
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_NUMS) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_SIZE) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(stats) = self.stats_table.get(Self::GROUP_ACK_NUMS) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(stats) = self.stats_table.get(Self::GROUP_CK_NUMS) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::SNDBCK_PUT_NUMS) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_LATENCY) {
stats.del_value_by_suffix_key(group_str, "@");
}
if let Some(fall_size) = &self.moment_stats_item_set_fall_size {
fall_size.del_value_by_suffix_key(group_str, "@");
}
if let Some(fall_time) = &self.moment_stats_item_set_fall_time {
fall_time.del_value_by_suffix_key(group_str, "@");
}
info!("Deleted all stats for group: {}", group_str);
}
#[inline]
pub fn inc_queue_put_nums(&self, topic: &str, queue_id: i32, num: i32, times: i32) {
if self.enable_queue_stat {
let stats_key = format!("{}@{}", topic, queue_id);
if let Some(stats) = self.stats_table.get(Stats::QUEUE_PUT_NUMS) {
stats.add_value(&stats_key, num, times);
}
}
}
#[inline]
pub fn inc_queue_put_size(&self, topic: &str, queue_id: i32, size: i32) {
if self.enable_queue_stat {
let stats_key = format!("{}@{}", topic, queue_id);
if let Some(stats) = self.stats_table.get(Stats::QUEUE_PUT_SIZE) {
stats.add_value(&stats_key, size, 1);
}
}
}
#[inline]
pub fn inc_queue_get_nums(&self, topic: &str, queue_id: i32, num: i32, times: i32) {
if self.enable_queue_stat {
let stats_key = format!("{}@{}", topic, queue_id);
if let Some(stats) = self.stats_table.get(Stats::QUEUE_GET_NUMS) {
stats.add_value(&stats_key, num, times);
}
}
}
#[inline]
pub fn inc_queue_get_size(&self, topic: &str, queue_id: i32, size: i32) {
if self.enable_queue_stat {
let stats_key = format!("{}@{}", topic, queue_id);
if let Some(stats) = self.stats_table.get(Stats::QUEUE_GET_SIZE) {
stats.add_value(&stats_key, size, 1);
}
}
}
#[inline]
pub fn inc_topic_put_latency(&self, topic: &str, queue_id: i32, inc_value: i32) {
let stats_key = format!("{}@{}", queue_id, topic);
if let Some(stats) = self.stats_table.get(Self::TOPIC_PUT_LATENCY) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_group_get_latency(&self, group: &str, topic: &str, queue_id: i32, inc_value: i32) {
let stats_key = format!("{}@{}@{}", queue_id, topic, group);
if let Some(stats) = self.stats_table.get(Stats::GROUP_GET_LATENCY) {
stats.add_rt_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn record_disk_fall_behind_time(&self, group: &str, topic: &str, queue_id: i32, fall_behind: i64) {
if let Some(fall_time_set) = &self.moment_stats_item_set_fall_time {
let stats_key = format!("{}@{}@{}", queue_id, topic, group);
let item = fall_time_set.get_and_create_stats_item(stats_key);
item.get_value()
.store(fall_behind, std::sync::atomic::Ordering::Relaxed);
}
}
#[inline]
pub fn tps_group_get_nums(&self, group: &str, topic: &str) -> f64 {
let stats_key = build_stats_key(Some(topic), Some(group));
match self.stats_table.get(Stats::GROUP_GET_NUMS) {
Some(stats) => stats.get_stats_data_in_minute(&stats_key).get_tps(),
None => 0.0,
}
}
#[inline]
pub fn inc_broker_ack_nums(&self, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Self::BROKER_ACK_NUMS) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
#[inline]
pub fn inc_broker_get_from_disk_nums(&self, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Stats::BROKER_GET_FROM_DISK_NUMS) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
#[inline]
pub fn inc_broker_get_from_disk_size(&self, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Stats::BROKER_GET_FROM_DISK_SIZE) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
#[inline]
pub fn inc_send_back_nums(&self, group: &str, topic: &str) {
let stats_key = build_stats_key(Some(topic), Some(group));
if let Some(stats) = self.stats_table.get(Stats::SNDBCK_PUT_NUMS) {
stats.add_value(&stats_key, 1, 1);
}
}
#[inline]
pub fn inc_dlq_stat_value(&self, key: &str, owner: &str, group: &str, topic: &str, msg_type: &str, inc_value: i32) {
let stats_key = build_commercial_stats_key(owner, topic, group, msg_type);
if let Some(stats) = self.stats_table.get(key) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_commercial_value(
&self,
key: &str,
owner: &str,
group: &str,
topic: &str,
msg_type: &str,
inc_value: i32,
) {
let stats_key = build_commercial_stats_key(owner, topic, group, msg_type);
if let Some(stats) = self.stats_table.get(key) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_account_value(
&self,
key: &str,
account_owner_parent: &str,
account_owner_self: &str,
instance_id: &str,
topic: &str,
group: &str,
msg_type: &str,
inc_value: i32,
) {
let stats_key = build_account_stats_key(
account_owner_parent,
account_owner_self,
instance_id,
topic,
group,
msg_type,
);
if let Some(stats) = self.stats_table.get(key) {
stats.add_value(&stats_key, inc_value, 1);
}
}
#[inline]
pub fn inc_account_value_with_flow_limit(
&self,
key: &str,
account_owner_parent: &str,
account_owner_self: &str,
instance_id: &str,
topic: &str,
group: &str,
msg_type: &str,
flow_limit_threshold: &str,
inc_value: i32,
) {
let stats_key = build_account_stats_key_with_flowlimit(
account_owner_parent,
account_owner_self,
instance_id,
topic,
group,
msg_type,
flow_limit_threshold,
);
if let Some(stats) = self.stats_table.get(key) {
stats.add_value(&stats_key, inc_value, 1);
}
}
pub fn shutdown(&self) {
info!("Shutting down BrokerStatsManager...");
if let Some(scheduler) = &self.scheduler {
for task_id in self.task_ids.lock().drain(..) {
scheduler.cancel_task(task_id);
info!("Cancelled task {}", task_id);
}
}
info!("BrokerStatsManager shutdown complete");
}
pub fn inc_consumer_register_time(&self, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Self::CONSUMER_REGISTER_TIME) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
pub fn inc_producer_register_time(&self, inc_value: i32) {
if let Some(stats) = self.stats_table.get(Self::PRODUCER_REGISTER_TIME) {
stats.add_value(&self.cluster_name, inc_value, 1);
}
}
pub fn inc_channel_idle_num(&self) {
if let Some(stats) = self.stats_table.get(Self::CHANNEL_ACTIVITY) {
stats.add_value(Self::CHANNEL_ACTIVITY_IDLE, 1, 1);
}
}
pub fn inc_channel_exception_num(&self) {
if let Some(stats) = self.stats_table.get(Self::CHANNEL_ACTIVITY) {
stats.add_value(Self::CHANNEL_ACTIVITY_EXCEPTION, 1, 1);
}
}
pub fn inc_channel_close_num(&self) {
if let Some(stats) = self.stats_table.get(Self::CHANNEL_ACTIVITY) {
stats.add_value(Self::CHANNEL_ACTIVITY_CLOSE, 1, 1);
}
}
pub fn inc_channel_connect_num(&self) {
if let Some(stats) = self.stats_table.get(Self::CHANNEL_ACTIVITY) {
stats.add_value(Self::CHANNEL_ACTIVITY_CONNECT, 1, 1);
}
}
}
#[inline]
pub fn build_stats_key(topic: Option<&str>, group: Option<&str>) -> String {
let mut str_builder = String::new();
if let Some(t) = topic {
str_builder.push_str(t);
}
str_builder.push('@');
if let Some(g) = group {
str_builder.push_str(g);
}