@@ -24,9 +24,13 @@ use std::{
24
24
time:: Duration ,
25
25
} ;
26
26
27
- use rocketmq_common:: common:: {
28
- broker:: broker_config:: BrokerConfig , config:: TopicConfig , config_manager:: ConfigManager ,
29
- constant:: PermName , server:: config:: ServerConfig ,
27
+ use rocketmq_common:: {
28
+ common:: {
29
+ broker:: broker_config:: BrokerConfig , config:: TopicConfig , config_manager:: ConfigManager ,
30
+ constant:: PermName , server:: config:: ServerConfig ,
31
+ } ,
32
+ TimeUtils :: get_current_millis,
33
+ UtilAll :: compute_next_morning_time_millis,
30
34
} ;
31
35
use rocketmq_remoting:: {
32
36
protocol:: {
@@ -37,9 +41,12 @@ use rocketmq_remoting::{
37
41
} ;
38
42
use rocketmq_runtime:: RocketMQRuntime ;
39
43
use rocketmq_store:: {
40
- base:: store_enum:: StoreType , config:: message_store_config:: MessageStoreConfig ,
41
- log_file:: MessageStore , message_store:: default_message_store:: DefaultMessageStore ,
42
- stats:: broker_stats_manager:: BrokerStatsManager , timer:: timer_message_store:: TimerMessageStore ,
44
+ base:: store_enum:: StoreType ,
45
+ config:: message_store_config:: MessageStoreConfig ,
46
+ log_file:: MessageStore ,
47
+ message_store:: default_message_store:: DefaultMessageStore ,
48
+ stats:: { broker_stats:: BrokerStats , broker_stats_manager:: BrokerStatsManager } ,
49
+ timer:: timer_message_store:: TimerMessageStore ,
43
50
} ;
44
51
use tracing:: { info, warn} ;
45
52
@@ -83,6 +90,8 @@ pub(crate) struct BrokerRuntime {
83
90
consumer_order_info_manager : Arc < ConsumerOrderInfoManager > ,
84
91
#[ cfg( feature = "local_file_store" ) ]
85
92
message_store : Option < DefaultMessageStore > ,
93
+ #[ cfg( feature = "local_file_store" ) ]
94
+ broker_stats : Option < Arc < BrokerStats < DefaultMessageStore > > > ,
86
95
//message_store: Option<Arc<Mutex<LocalFileMessageStore>>>,
87
96
schedule_message_service : ScheduleMessageService ,
88
97
timer_message_store : Option < TimerMessageStore > ,
@@ -96,6 +105,7 @@ pub(crate) struct BrokerRuntime {
96
105
shutdown_hook : Option < BrokerShutdownHook > ,
97
106
broker_stats_manager : Arc < BrokerStatsManager > ,
98
107
topic_queue_mapping_clean_service : Option < Arc < TopicQueueMappingCleanService > > ,
108
+ update_master_haserver_addr_periodically : bool ,
99
109
}
100
110
101
111
impl Clone for BrokerRuntime {
@@ -111,6 +121,7 @@ impl Clone for BrokerRuntime {
111
121
consumer_filter_manager : Arc :: new ( Default :: default ( ) ) ,
112
122
consumer_order_info_manager : Arc :: new ( Default :: default ( ) ) ,
113
123
message_store : self . message_store . clone ( ) ,
124
+ broker_stats : self . broker_stats . clone ( ) ,
114
125
schedule_message_service : Default :: default ( ) ,
115
126
timer_message_store : self . timer_message_store . clone ( ) ,
116
127
broker_out_api : self . broker_out_api . clone ( ) ,
@@ -121,6 +132,7 @@ impl Clone for BrokerRuntime {
121
132
shutdown_hook : self . shutdown_hook . clone ( ) ,
122
133
broker_stats_manager : self . broker_stats_manager . clone ( ) ,
123
134
topic_queue_mapping_clean_service : self . topic_queue_mapping_clean_service . clone ( ) ,
135
+ update_master_haserver_addr_periodically : self . update_master_haserver_addr_periodically ,
124
136
}
125
137
}
126
138
}
@@ -161,6 +173,7 @@ impl BrokerRuntime {
161
173
consumer_filter_manager : Arc :: new ( Default :: default ( ) ) ,
162
174
consumer_order_info_manager : Arc :: new ( Default :: default ( ) ) ,
163
175
message_store : None ,
176
+ broker_stats : None ,
164
177
schedule_message_service : Default :: default ( ) ,
165
178
timer_message_store : None ,
166
179
broker_out_api : broker_outer_api,
@@ -171,6 +184,7 @@ impl BrokerRuntime {
171
184
shutdown_hook : None ,
172
185
broker_stats_manager,
173
186
topic_queue_mapping_clean_service : None ,
187
+ update_master_haserver_addr_periodically : false ,
174
188
}
175
189
}
176
190
@@ -256,6 +270,7 @@ impl BrokerRuntime {
256
270
) ;
257
271
self . topic_config_manager
258
272
. set_message_store ( Some ( message_store. clone ( ) ) ) ;
273
+ self . broker_stats = Some ( Arc :: new ( BrokerStats :: new ( Arc :: new ( message_store. clone ( ) ) ) ) ) ;
259
274
self . message_store = Some ( message_store) ;
260
275
} else if self . message_store_config . store_type == StoreType :: RocksDB {
261
276
info ! ( "Use RocksDB as message store" ) ;
@@ -342,7 +357,110 @@ impl BrokerRuntime {
342
357
}
343
358
}
344
359
345
- fn initialize_scheduled_tasks ( & mut self ) { }
360
+ fn initialize_scheduled_tasks ( & mut self ) {
361
+ let initial_delay = compute_next_morning_time_millis ( ) - get_current_millis ( ) ;
362
+ let period = Duration :: from_days ( 1 ) . as_millis ( ) as u64 ;
363
+ let broker_stats = self . broker_stats . clone ( ) ;
364
+ self . broker_runtime
365
+ . as_ref ( )
366
+ . unwrap ( )
367
+ . get_handle ( )
368
+ . spawn ( async move {
369
+ info ! ( "BrokerStats Start scheduled task" ) ;
370
+ tokio:: time:: sleep ( Duration :: from_millis ( initial_delay) ) . await ;
371
+ loop {
372
+ let current_execution_time = tokio:: time:: Instant :: now ( ) ;
373
+ broker_stats. as_ref ( ) . unwrap ( ) . record ( ) ;
374
+ let next_execution_time =
375
+ current_execution_time + Duration :: from_millis ( period) ;
376
+ let delay =
377
+ next_execution_time. saturating_duration_since ( tokio:: time:: Instant :: now ( ) ) ;
378
+ tokio:: time:: sleep ( delay) . await ;
379
+ }
380
+ } ) ;
381
+
382
+ let consumer_offset_manager = self . consumer_offset_manager . clone ( ) ;
383
+ let flush_consumer_offset_interval = self . broker_config . flush_consumer_offset_interval ;
384
+ self . broker_runtime
385
+ . as_ref ( )
386
+ . unwrap ( )
387
+ . get_handle ( )
388
+ . spawn ( async move {
389
+ info ! ( "Consumer offset manager Start scheduled task" ) ;
390
+ tokio:: time:: sleep ( Duration :: from_millis ( 1000 * 10 ) ) . await ;
391
+ loop {
392
+ let current_execution_time = tokio:: time:: Instant :: now ( ) ;
393
+ consumer_offset_manager. persist ( ) ;
394
+ let next_execution_time = current_execution_time
395
+ + Duration :: from_millis ( flush_consumer_offset_interval) ;
396
+ let delay =
397
+ next_execution_time. saturating_duration_since ( tokio:: time:: Instant :: now ( ) ) ;
398
+ tokio:: time:: sleep ( delay) . await ;
399
+ }
400
+ } ) ;
401
+
402
+ let consumer_filter_manager = self . consumer_filter_manager . clone ( ) ;
403
+ let consumer_order_info_manager = self . consumer_order_info_manager . clone ( ) ;
404
+ self . broker_runtime
405
+ . as_ref ( )
406
+ . unwrap ( )
407
+ . get_handle ( )
408
+ . spawn ( async move {
409
+ info ! ( "consumer filter manager Start scheduled task" ) ;
410
+ info ! ( "consumer order info manager Start scheduled task" ) ;
411
+ tokio:: time:: sleep ( Duration :: from_millis ( 1000 * 10 ) ) . await ;
412
+ loop {
413
+ let current_execution_time = tokio:: time:: Instant :: now ( ) ;
414
+ consumer_filter_manager. persist ( ) ;
415
+ consumer_order_info_manager. persist ( ) ;
416
+ let next_execution_time =
417
+ current_execution_time + Duration :: from_millis ( 1000 * 10 ) ;
418
+ let delay =
419
+ next_execution_time. saturating_duration_since ( tokio:: time:: Instant :: now ( ) ) ;
420
+ tokio:: time:: sleep ( delay) . await ;
421
+ }
422
+ } ) ;
423
+
424
+ let mut runtime = self . clone ( ) ;
425
+ self . broker_runtime
426
+ . as_ref ( )
427
+ . unwrap ( )
428
+ . get_handle ( )
429
+ . spawn ( async move {
430
+ info ! ( "Protect broker Start scheduled task" ) ;
431
+ tokio:: time:: sleep ( Duration :: from_mins ( 3 ) ) . await ;
432
+ loop {
433
+ let current_execution_time = tokio:: time:: Instant :: now ( ) ;
434
+ runtime. protect_broker ( ) ;
435
+ let next_execution_time = current_execution_time + Duration :: from_mins ( 3 ) ;
436
+ let delay =
437
+ next_execution_time. saturating_duration_since ( tokio:: time:: Instant :: now ( ) ) ;
438
+ tokio:: time:: sleep ( delay) . await ;
439
+ }
440
+ } ) ;
441
+
442
+ let message_store = self . message_store . clone ( ) ;
443
+ self . broker_runtime
444
+ . as_ref ( )
445
+ . unwrap ( )
446
+ . get_handle ( )
447
+ . spawn ( async move {
448
+ info ! ( "Message store dispatch_behind_bytes Start scheduled task" ) ;
449
+ tokio:: time:: sleep ( Duration :: from_secs ( 10 ) ) . await ;
450
+ loop {
451
+ let current_execution_time = tokio:: time:: Instant :: now ( ) ;
452
+ message_store. as_ref ( ) . unwrap ( ) . dispatch_behind_bytes ( ) ;
453
+ let next_execution_time = current_execution_time + Duration :: from_secs ( 60 ) ;
454
+ let delay =
455
+ next_execution_time. saturating_duration_since ( tokio:: time:: Instant :: now ( ) ) ;
456
+ tokio:: time:: sleep ( delay) . await ;
457
+ }
458
+ } ) ;
459
+
460
+ if self . broker_config . enable_controller_mode {
461
+ self . update_master_haserver_addr_periodically = true ;
462
+ }
463
+ }
346
464
347
465
fn initial_transaction ( & mut self ) { }
348
466
@@ -351,6 +469,8 @@ impl BrokerRuntime {
351
469
fn initial_rpc_hooks ( & mut self ) { }
352
470
fn initial_request_pipeline ( & mut self ) { }
353
471
472
+ fn protect_broker ( & mut self ) { }
473
+
354
474
pub async fn start ( & mut self ) {
355
475
self . message_store
356
476
. as_mut ( )
0 commit comments