Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EMQX-11826 prevent left node from rejoining #233

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{deps, [{jiffy, {git, "https://github.com/emqx/jiffy", {tag, "1.0.5"}}},
{eetcd, {git, "https://github.com/zhongwencool/eetcd", {tag, "v0.3.4"}}},
{snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe", {tag, "1.0.0"}}},
{mria, {git, "https://github.com/emqx/mria", {tag, "0.8.4"}}}
{mria, {git, "https://github.com/emqx/mria", {tag, "0.8.5"}}}
]}.

{erl_opts, [warn_unused_vars,
Expand Down
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
33 changes: 26 additions & 7 deletions 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 Expand Up @@ -248,12 +263,16 @@ join_with(false) ->
join_with(Node) when Node =:= node() ->
ignore;
join_with(Node) ->
Res = ekka_cluster:join(Node),
%% Wait for ekka to be restarted after join to avoid noproc error
%% that can occur if underlying cluster implementation (e.g. ekka_cluster_etcd)
%% uses some processes started under ekka supervision tree
_ = wait_application_ready(ekka, 10),
Res.
case ekka_cluster:join(Node) of
{error, {already_in_cluster, Node}} ->
ignore;
Res ->
%% Wait for ekka to be restarted after join to avoid noproc error
%% that can occur if underlying cluster implementation (e.g. ekka_cluster_etcd)
%% uses some processes started under ekka supervision tree
_ = wait_application_ready(ekka, 10),
Res
end.

find_oldest_node([Node]) ->
Node;
Expand Down
Loading