Skip to content

Commit

Permalink
feat: add API to disable/enable auto clustering
Browse files Browse the repository at this point in the history
The API can be used to prevent a node from re-joining the same cluster after it has (manually) left the cluster.
  • Loading branch information
SergeTupchiy committed Mar 27, 2024
1 parent 5b27db0 commit add5a89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/ekka.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
%% Autocluster API
-export([ autocluster/0
, autocluster/1
, autocluster_enabled/0
, enable_autocluster/0
, disable_autocluster/0
]).

%% Callbacks
Expand Down Expand Up @@ -169,6 +172,18 @@ autocluster(App) ->
false -> ok
end.

autocluster_enabled() ->
env(cluster_enable, true) andalso ekka_autocluster:configured().

disable_autocluster() ->
ok = application:set_env(ekka, cluster_enable, false),
ok = mria:disable_core_node_discovery(),
ok = ekka_autocluster:stop(disabled).

enable_autocluster() ->
ok = application:set_env(ekka, cluster_enable, true),
ok = mria:enable_core_node_discovery().

%%--------------------------------------------------------------------
%% Callbacks
%%--------------------------------------------------------------------
Expand Down
17 changes: 16 additions & 1 deletion src/ekka_autocluster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

%% API
-export([ enabled/0
, configured/0
, run/1
, unregister_node/0
, core_node_discovery_callback/0
, stop/1
]).

%% gen_server callbacks
Expand All @@ -49,9 +51,13 @@

-spec enabled() -> boolean().
enabled() ->
configured() andalso mria_config:role() =:= core.

-spec configured() -> boolean().
configured() ->
case ekka:env(cluster_discovery) of
{ok, {manual, _}} -> false;
{ok, _Strategy} -> mria_config:role() =:= core;
{ok, _Strategy} -> true;
undefined -> false
end.

Expand All @@ -74,6 +80,15 @@ unregister_node() ->
log_error("Unregister", ekka_cluster_strategy:unregister(Mod, Options))
end).

-spec stop(term()) -> ok.
stop(Reason) ->
try
gen_server:stop(?SERVER, {shutdown, Reason}, 5000)
catch
_:noproc ->
ok
end.

%% @doc Core node discovery used by mria by replicant nodes to find
%% the core nodes.
-spec core_node_discovery_callback() -> [node()].
Expand Down

0 comments on commit add5a89

Please sign in to comment.