11% %--------------------------------------------------------------------
2- % % Copyright (c) 2019-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
2+ % % Copyright (c) 2019-2026 EMQ Technologies Co., Ltd. All Rights Reserved.
33% %
44% % Licensed under the Apache License, Version 2.0 (the "License");
55% % you may not use this file except in compliance with the License.
4242-record (state , {
4343 partitions :: list (node ()),
4444 heartbeat :: undefined | reference (),
45- autoheal :: mria_autoheal :autoheal (),
46- autoclean :: mria_autoclean :autoclean ()
45+ autoheal :: mria_autoheal :autoheal ()
4746 }).
4847
4948-define (SERVER , ? MODULE ).
@@ -79,8 +78,7 @@ init([]) ->
7978 {ok , _ } = mnesia :subscribe (system ),
8079 lists :foreach (fun (N ) -> self () ! {nodeup , N , []} end , nodes () -- [node ()]),
8180 State = # state {partitions = [],
82- autoheal = mria_autoheal :init (),
83- autoclean = mria_autoclean :init ()
81+ autoheal = mria_autoheal :init ()
8482 },
8583 {ok , ensure_heartbeat (State )}.
8684
@@ -199,6 +197,7 @@ handle_info(heartbeat, State) ->
199197 true -> ok
200198 end
201199 end , mria_mnesia :cluster_nodes (all )),
200+ autoclean_cores (),
202201 {noreply , ensure_heartbeat (State # state {heartbeat = undefined })};
203202
204203handle_info (Msg = {'EXIT' , Pid , _Reason }, State = # state {autoheal = Autoheal }) ->
@@ -207,10 +206,6 @@ handle_info(Msg = {'EXIT', Pid, _Reason}, State = #state{autoheal = Autoheal}) -
207206 _ -> {noreply , State }
208207 end ;
209208
210- % % Autoclean Event.
211- handle_info (autoclean , State = # state {autoclean = AutoClean }) ->
212- {noreply , State # state {autoclean = mria_autoclean :check (AutoClean )}};
213-
214209handle_info (Info , State ) ->
215210 logger :error (" Unexpected info: ~p " , [Info ]),
216211 {noreply , State }.
@@ -237,3 +232,30 @@ ensure_heartbeat(State) ->
237232
238233autoheal_handle_msg (Msg , State = # state {autoheal = Autoheal }) ->
239234 State # state {autoheal = mria_autoheal :handle_msg (Msg , Autoheal )}.
235+
236+ % %--------------------------------------------------------------------
237+ % % Autoclean of stopped core nodes
238+ % %--------------------------------------------------------------------
239+
240+ autoclean_cores () ->
241+ maybe
242+ {ok , Expiry } ?= application :get_env (mria , cluster_autoclean ),
243+ [maybe_clean (Member , Expiry ) || Member <- mria_membership :members (down )]
244+ end ,
245+ ok .
246+
247+ maybe_clean (# member {node = Node , last_update = WentDownAt } = Member , MaxDownSecs ) ->
248+ Now = mria_membership :now_seconds (),
249+ case Now - WentDownAt > MaxDownSecs of
250+ true ->
251+ ? tp (notice , mria_autoclean_force_leave ,
252+ #{ node => Node
253+ , limit => MaxDownSecs
254+ , status => Member
255+ , last_update => WentDownAt
256+ , now => Now
257+ }),
258+ mria :force_leave (Node );
259+ false ->
260+ ok
261+ end .
0 commit comments