@@ -3,16 +3,17 @@ use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
33use valkey_module:: alloc:: ValkeyAlloc ;
44use valkey_module:: server_events:: {
55 ClientChangeSubevent , ForkChildSubevent , KeyChangeSubevent , MasterLinkChangeSubevent ,
6- PersistenceSubevent , ReplicaChangeSubevent ,
6+ PersistenceSubevent , ReplAsyncLoadSubevent , ReplicaChangeSubevent ,
77} ;
88use valkey_module:: {
9- server_events:: FlushSubevent , valkey_module, Context , ValkeyResult , ValkeyString , ValkeyValue ,
9+ server_events:: FlushSubevent , valkey_module, Context , ModuleOptions , Status , ValkeyResult ,
10+ ValkeyString , ValkeyValue ,
1011} ;
1112use valkey_module_macros:: {
1213 client_changed_event_handler, config_changed_event_handler, cron_event_handler,
1314 flush_event_handler, fork_child_event_handler, key_event_handler,
14- master_link_change_event_handler, persistence_event_handler, replica_change_event_handler ,
15- shutdown_event_handler,
15+ master_link_change_event_handler, persistence_event_handler, repl_async_load_event_handler ,
16+ replica_change_event_handler , shutdown_event_handler,
1617} ;
1718
1819static NUM_FLUSHES : AtomicI64 = AtomicI64 :: new ( 0 ) ;
@@ -25,6 +26,7 @@ static NUM_MASTER_LINK_CHANGE_EVENTS: AtomicI64 = AtomicI64::new(0);
2526static IS_MASTER_LINK_UP : AtomicBool = AtomicBool :: new ( false ) ;
2627static NUM_FORK_CHILD_EVENTS : AtomicI64 = AtomicI64 :: new ( 0 ) ;
2728static NUM_REPLICA_CHANGE_EVENTS : AtomicI64 = AtomicI64 :: new ( 0 ) ;
29+ static NUM_REPL_ASYNC_LOAD_EVENTS : AtomicI64 = AtomicI64 :: new ( 0 ) ;
2830
2931#[ flush_event_handler]
3032fn flushed_event_handler ( _ctx : & Context , flush_event : FlushSubevent ) {
@@ -140,27 +142,41 @@ fn fork_child_event_handler(ctx: &Context, fork_child_subevent: ForkChildSubeven
140142 match fork_child_subevent {
141143 ForkChildSubevent :: Born => {
142144 ctx. log_warning ( "Fork child born" ) ;
143- NUM_FORK_CHILD_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
144145 }
145146 ForkChildSubevent :: Died => {
146147 ctx. log_warning ( "Fork child died" ) ;
147- NUM_FORK_CHILD_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
148148 }
149149 }
150+ NUM_FORK_CHILD_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
150151}
151152
152153#[ replica_change_event_handler]
153154fn replica_change_event_handler ( ctx : & Context , replica_change_subevent : ReplicaChangeSubevent ) {
154155 match replica_change_subevent {
155156 ReplicaChangeSubevent :: Online => {
156157 ctx. log_notice ( "Replica online" ) ;
157- NUM_REPLICA_CHANGE_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
158158 }
159159 ReplicaChangeSubevent :: Offline => {
160160 ctx. log_notice ( "Replica offline" ) ;
161- NUM_REPLICA_CHANGE_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
162161 }
163162 }
163+ NUM_REPLICA_CHANGE_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
164+ }
165+
166+ #[ repl_async_load_event_handler]
167+ fn repl_async_load_event_handler ( ctx : & Context , repl_async_load_subevent : ReplAsyncLoadSubevent ) {
168+ match repl_async_load_subevent {
169+ ReplAsyncLoadSubevent :: Started => {
170+ ctx. log_notice ( "Repl async load started" ) ;
171+ }
172+ ReplAsyncLoadSubevent :: Aborted => {
173+ ctx. log_notice ( "Repl async load aborted" ) ;
174+ }
175+ ReplAsyncLoadSubevent :: Completed => {
176+ ctx. log_notice ( "Repl async load completed" ) ;
177+ }
178+ }
179+ NUM_REPL_ASYNC_LOAD_EVENTS . fetch_add ( 1 , Ordering :: SeqCst ) ;
164180}
165181
166182fn num_flushed ( _ctx : & Context , _args : Vec < ValkeyString > ) -> ValkeyResult {
@@ -213,13 +229,28 @@ fn num_replica_change_events(_ctx: &Context, _args: Vec<ValkeyString>) -> Valkey
213229 ) )
214230}
215231
232+ fn num_repl_async_load_events ( _ctx : & Context , _args : Vec < ValkeyString > ) -> ValkeyResult {
233+ Ok ( ValkeyValue :: Integer (
234+ NUM_REPL_ASYNC_LOAD_EVENTS . load ( Ordering :: SeqCst ) ,
235+ ) )
236+ }
237+
238+ fn init ( ctx : & Context , args : & [ ValkeyString ] ) -> Status {
239+ // https://valkey.io/topics/modules-api-ref/#ValkeyModule_SetModuleOptions
240+ // otherwise you get: Skipping diskless-load because there are modules that are not aware of async replication.
241+ // needed for repl_async_load_event_handler
242+ ctx. set_module_options ( ModuleOptions :: HANDLE_REPL_ASYNC_LOAD ) ;
243+ Status :: Ok
244+ }
245+
216246//////////////////////////////////////////////////////
217247
218248valkey_module ! {
219249 name: "srv_events" ,
220250 version: 1 ,
221251 allocator: ( ValkeyAlloc , ValkeyAlloc ) ,
222252 data_types: [ ] ,
253+ init: init,
223254 commands: [
224255 [ "num_flushed" , num_flushed, "readonly" , 0 , 0 , 0 ] ,
225256 [ "num_max_memory_changes" , num_maxmemory_changes, "readonly" , 0 , 0 , 0 ] ,
@@ -231,5 +262,6 @@ valkey_module! {
231262 [ "is_master_link_up" , is_master_link_up, "readonly" , 0 , 0 , 0 ] ,
232263 [ "num_fork_child_events" , num_fork_child_events, "readonly" , 0 , 0 , 0 ] ,
233264 [ "num_replica_change_events" , num_replica_change_events, "readonly" , 0 , 0 , 0 ] ,
265+ [ "num_repl_async_load_events" , num_repl_async_load_events, "readonly" , 0 , 0 , 0 ]
234266 ]
235267}
0 commit comments