Skip to content

Commit e001889

Browse files
committed
feat(autoheal): Add autoheal callback
1 parent 790a964 commit e001889

5 files changed

Lines changed: 16 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,6 @@ They can be registered using `mria:register_callback/2` function.
171171

172172
- `stop`: This callback is executed when the DB stops or restarts.
173173
- `start`: This callback is executed when the DB starts or restarts.
174+
- `heal_partition`: This callback is executed after mria heals a network partition by rebooting the losing nodes
174175

175176
Note that the DB restarts when the node joins the cluster.

src/mria_autoheal.erl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -142,8 +142,10 @@ coordinator([Majority | _]) ->
142142
heal_partition([[_Majority]]) ->
143143
%% There are no partitions:
144144
ok;
145-
heal_partition([_Majority|Minorities]) ->
146-
reboot_minority(lists:append(Minorities)).
145+
heal_partition([Majority|Minorities]) ->
146+
Result = reboot_minority(lists:append(Minorities)),
147+
mria_lib:exec_callback(heal_partition, {Majority, Minorities}),
148+
Result.
147149

148150
reboot_minority(Minority) ->
149151
?tp(info, "Rebooting minority", #{nodes => Minority}),

src/mria_config.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
-type callback() :: start
7474
| stop
7575
| {start | stop, mria_rlog:shard()}
76+
| heal_partition
7677
| core_node_discovery
7778
| lb_custom_info
7879
| lb_custom_info_check.

test/mria_autoheal_SUITE.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ t_autoheal(Config) when is_list(Config) ->
3838
?assertMatch({[N3], [N1, N2, N4]}, view(N3)),
3939
?assertMatch({[N4], [N1, N2, N3]}, view(N4)),
4040
%% Wait for autoheal, it should happen automatically:
41+
?block_until(#{?snk_kind := mria_ct_heal_partition}),
4142
?retry(1000, 20,
4243
begin
4344
?assertMatch({Nodes, []}, view(N1)),
@@ -209,7 +210,7 @@ prop_callbacks(Trace0) ->
209210
))
210211
|| N <- Minority],
211212
%% Check that ONLY the minority nodes have been restarted:
212-
Restarted = lists:usort([Node || #{?snk_kind := mria_exec_callback, ?snk_meta := #{node := Node}} <- AfterHeal]),
213+
Restarted = lists:usort([Node || #{?snk_kind := mria_exec_callback, type := stop, ?snk_meta := #{node := Node}} <- AfterHeal]),
213214
?assertEqual(lists:sort(Minority),
214215
Restarted),
215216
true.

test/mria_ct.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ cluster(Specs0, CommonEnv, ClusterOpts) ->
8080
, env => [ {mria, core_nodes, CoreNodes}
8181
, {mria, node_role, Role}
8282
, {mria, rlog_replica_reconnect_interval, 100} % For faster response times
83+
, {mria, {callback, heal_partition}, fun heal_callback/1}
8384
, {gen_rpc, tcp_server_port, BaseGenRpcPort + Number}
8485
, {gen_rpc, client_config_per_node, {internal, GenRpcPorts}}
8586
| Env]
@@ -273,6 +274,12 @@ get_txid() ->
273274
TID
274275
end.
275276

277+
heal_callback({Majority, Minority}) ->
278+
?tp(mria_ct_heal_partition,
279+
#{ majority => Majority
280+
, minority => Minority
281+
}).
282+
276283
-if(?OTP_RELEASE >= 25).
277284
start_dist() ->
278285
ensure_epmd(),

0 commit comments

Comments
 (0)