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

rabbit_peer_discovery: Rewrite the core logic #9797

Merged
merged 8 commits into from
Dec 7, 2023
8 changes: 8 additions & 0 deletions deps/rabbit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,14 @@ rabbitmq_suite(
],
)

rabbitmq_suite(
name = "unit_cluster_formation_sort_nodes_SUITE",
size = "small",
deps = [
"@meck//:erlang_app",
],
)

rabbitmq_suite(
name = "unit_collections_SUITE",
size = "small",
Expand Down
8 changes: 8 additions & 0 deletions deps/rabbit/app.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,14 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
app_name = "rabbit",
erlc_opts = "//:test_erlc_opts",
)
erlang_bytecode(
name = "unit_cluster_formation_sort_nodes_SUITE_beam_files",
testonly = True,
srcs = ["test/unit_cluster_formation_sort_nodes_SUITE.erl"],
outs = ["test/unit_cluster_formation_sort_nodes_SUITE.beam"],
app_name = "rabbit",
erlc_opts = "//:test_erlc_opts",
)
erlang_bytecode(
name = "unit_collections_SUITE_beam_files",
testonly = True,
Expand Down
21 changes: 13 additions & 8 deletions deps/rabbit/src/rabbit_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@ init() ->
#{domain => ?RMQLOG_DOMAIN_DB}),

ensure_dir_exists(),
rabbit_peer_discovery:log_configured_backend(),
rabbit_peer_discovery:maybe_init(),

pre_init(IsVirgin),

case IsVirgin of
true ->
%% At this point, the database backend could change if the node
%% joins a cluster and that cluster uses a different database.
?LOG_INFO(
"DB: virgin node -> run peer discovery",
#{domain => ?RMQLOG_DOMAIN_DB}),
rabbit_peer_discovery:sync_desired_cluster();
false ->
ok
end,

Ret = case rabbit_khepri:is_enabled() of
true -> init_using_khepri();
false -> init_using_mnesia()
Expand All @@ -66,8 +77,8 @@ init() ->
"DB: initialization successeful",
#{domain => ?RMQLOG_DOMAIN_DB}),

rabbit_peer_discovery:maybe_register(),
init_finished(),
post_init(IsVirgin),

ok;
Error ->
Expand All @@ -82,12 +93,6 @@ pre_init(IsVirgin) ->
OtherMembers = rabbit_nodes:nodes_excl_me(Members),
rabbit_db_cluster:ensure_feature_flags_are_in_sync(OtherMembers, IsVirgin).

post_init(false = _IsVirgin) ->
rabbit_peer_discovery:maybe_register();
post_init(true = _IsVirgin) ->
%% Registration handled by rabbit_peer_discovery.
ok.

init_using_mnesia() ->
?LOG_DEBUG(
"DB: initialize Mnesia",
Expand Down
29 changes: 6 additions & 23 deletions deps/rabbit/src/rabbit_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@
init() ->
ensure_mnesia_running(),
ensure_mnesia_dir(),
%% Peer discovery may have been a no-op if it decided that all other nodes
%% should join this one. Therefore, we need to look at if this node is
%% still virgin and finish our init of Mnesia accordingly. In particular,
%% this second part crates all our Mnesia tables.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
%% this second part crates all our Mnesia tables.
%% this second part creates all our Mnesia tables.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #10073. Thanks!

case is_virgin_node() of
true ->
rabbit_log:info("Node database directory at ~ts is empty. "
"Assuming we need to join an existing cluster or initialise from scratch...",
[dir()]),
rabbit_peer_discovery:maybe_create_cluster(
fun create_cluster_callback/2);
true ->
init_db_and_upgrade([node()], disc, true, _Retry = true);
false ->
NodeType = node_type(),
case is_node_type_permitted(NodeType) of
Expand All @@ -141,23 +141,6 @@ init() ->
ok = rabbit_node_monitor:global_sync(),
ok.

create_cluster_callback(none, NodeType) ->
DiscNodes = [node()],
NodeType1 = case is_node_type_permitted(NodeType) of
false -> disc;
true -> NodeType
end,
init_db_and_upgrade(DiscNodes, NodeType1, true, _Retry = true),
ok;
create_cluster_callback(RemoteNode, NodeType) ->
{ok, {_, DiscNodes, _}} = discover_cluster0(RemoteNode),
NodeType1 = case is_node_type_permitted(NodeType) of
false -> disc;
true -> NodeType
end,
init_db_and_upgrade(DiscNodes, NodeType1, true, _Retry = true),
ok.

%% Make the node join a cluster. The node will be reset automatically
%% before we actually cluster it. The nodes provided will be used to
%% find out about the nodes in the cluster.
Expand Down
Loading
Loading