Skip to content
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
9 changes: 1 addition & 8 deletions deps/rabbit/src/rabbit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,10 @@
{enables, external_infrastructure}]}).

-rabbit_boot_step({database,
[{mfa, {rabbit_mnesia, init, []}},
[{mfa, {rabbit_db, init, []}},
{requires, file_handle_cache},
{enables, external_infrastructure}]}).

-rabbit_boot_step({database_sync,
[{description, "database sync"},
{mfa, {rabbit_sup, start_child, [mnesia_sync]}},
{requires, database},
{enables, external_infrastructure}]}).

-rabbit_boot_step({networking_metadata_store,
[{description, "networking infrastructure"},
{mfa, {rabbit_sup, start_child, [rabbit_networking_store]}},
Expand Down Expand Up @@ -1113,7 +1107,6 @@ get_default_data_param(Param) ->

data_dir() ->
{ok, DataDir} = application:get_env(rabbit, data_dir),
?assertEqual(DataDir, rabbit_mnesia:dir()),
DataDir.

%%---------------------------------------------------------------------------
Expand Down
106 changes: 106 additions & 0 deletions deps/rabbit/src/rabbit_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,114 @@
-include_lib("kernel/include/logger.hrl").
-include_lib("stdlib/include/assert.hrl").

-include_lib("rabbit_common/include/logging.hrl").

-export([init/0,
is_virgin_node/0, is_virgin_node/1,
dir/0,
ensure_dir_exists/0]).
-export([run/1]).

%% Default timeout for operations on remote nodes.
-define(TIMEOUT, 60000).

%% -------------------------------------------------------------------
%% DB initialization.
%% -------------------------------------------------------------------

-spec init() -> Ret when
Ret :: ok | {error, any()}.
%% @doc Initializes the DB layer.

init() ->
IsVirgin = is_virgin_node(),
?LOG_DEBUG(
"DB: this node is virgin: ~ts", [IsVirgin],
#{domain => ?RMQLOG_DOMAIN_DB}),
ensure_dir_exists(),
case init_mnesia() of
ok ->
?LOG_DEBUG(
"DB: initialization successeful",
#{domain => ?RMQLOG_DOMAIN_DB}),
ok;
Error ->
?LOG_DEBUG(
"DB: initialization failed: ~0p", [Error],
#{domain => ?RMQLOG_DOMAIN_DB}),
Error
end.

init_mnesia() ->
?LOG_DEBUG(
"DB: initialize Mnesia",
#{domain => ?RMQLOG_DOMAIN_DB}),
ok = rabbit_mnesia:init(),
?assertEqual(rabbit:data_dir(), mnesia_dir()),
rabbit_sup:start_child(mnesia_sync).

-spec is_virgin_node() -> IsVirgin when
IsVirgin :: boolean().
%% @doc Indicates if this RabbitMQ node is virgin.
%%
%% @returns `true' if the node is virgin, `false' if it is not.
%%
%% @see is_virgin_node/1.

is_virgin_node() ->
ThisNode = node(),
is_virgin_node(ThisNode).

-spec is_virgin_node(Node) -> IsVirgin | undefined when
Node :: node(),
IsVirgin :: boolean().
%% @doc Indicates if the given RabbitMQ node is virgin.
%%
%% A virgin node is a node starting for the first time. It could be a brand
%% new node or a node having been reset.
%%
%% @returns `true' if the node is virgin, `false' if it is not, or `undefined'
%% if the given node is remote and we couldn't determine it.

is_virgin_node(Node) when is_atom(Node) ->
is_virgin_node_with_mnesia(Node).

is_virgin_node_with_mnesia(Node) when Node =:= node() ->
rabbit_mnesia:is_virgin_node();
is_virgin_node_with_mnesia(Node) ->
try
erpc:call(Node, rabbit_mnesia, is_virgin_node, [], ?TIMEOUT)
catch
_:_ ->
undefined
end.

-spec dir() -> DBDir when
DBDir :: file:filename().
%% @doc Returns the directory where the database stores its data.
%%
%% @returns the directory path.

dir() ->
mnesia_dir().

mnesia_dir() ->
rabbit_mnesia:dir().

-spec ensure_dir_exists() -> ok | no_return().
%% @doc Ensures the database directory exists.
%%
%% @returns `ok' if it exists or throws an exception if it does not.

ensure_dir_exists() ->
DBDir = dir() ++ "/",
case filelib:ensure_dir(DBDir) of
ok ->
ok;
{error, Reason} ->
throw({error, {cannot_create_db_dir, DBDir, Reason}})
end.

%% -------------------------------------------------------------------
%% run().
%% -------------------------------------------------------------------
Expand Down
10 changes: 1 addition & 9 deletions deps/rabbit/src/rabbit_ff_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ check_required_and_enable(
VirginNodesWhereDisabled =
lists:filter(
fun(Node) ->
case is_virgin_node(Node) of
case rabbit_db:is_virgin_node(Node) of
IsVirgin when is_boolean(IsVirgin) ->
IsVirgin;
undefined ->
Expand Down Expand Up @@ -853,14 +853,6 @@ update_feature_state_and_enable(
Error
end.

is_virgin_node(Node) ->
case rpc_call(Node, rabbit_mnesia, is_virgin_node, [], ?TIMEOUT) of
IsVirgin when is_boolean(IsVirgin) ->
IsVirgin;
{error, _} ->
undefined
end.

restore_feature_flag_state(
Nodes, NodesWhereDisabled, Inventory, FeatureName) ->
NodesWhereEnabled = Nodes -- NodesWhereDisabled,
Expand Down
6 changes: 3 additions & 3 deletions deps/rabbit/src/rabbit_node_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
-spec running_nodes_filename() -> string().

running_nodes_filename() ->
filename:join(rabbit_mnesia:dir(), "nodes_running_at_shutdown").
filename:join(rabbit:data_dir(), "nodes_running_at_shutdown").

-spec cluster_status_filename() -> string().

cluster_status_filename() ->
filename:join(rabbit_mnesia:dir(), "cluster_nodes.config").
filename:join(rabbit:data_dir(), "cluster_nodes.config").

coordination_filename() ->
filename:join(rabbit:data_dir(), "coordination").
Expand All @@ -81,7 +81,7 @@ default_quorum_filename() ->
-spec prepare_cluster_status_files() -> 'ok' | no_return().

prepare_cluster_status_files() ->
rabbit_mnesia:ensure_mnesia_dir(),
rabbit_db:ensure_dir_exists(),
RunningNodes1 = case try_read_file(running_nodes_filename()) of
{ok, [Nodes]} when is_list(Nodes) -> Nodes;
{ok, Other} -> corrupt_cluster_status_files(Other);
Expand Down
8 changes: 4 additions & 4 deletions deps/rabbit/test/feature_flags_v2_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1272,11 +1272,11 @@ have_required_feature_flag_in_cluster_and_add_member_without_it(
?assert(rabbit_feature_flags:is_supported(FeatureName)),
?assertNot(rabbit_feature_flags:is_enabled(FeatureName)),

MnesiaDir = rabbit_mnesia:dir(),
ok = filelib:ensure_path(MnesiaDir),
SomeFile = filename:join(MnesiaDir, "some-mnesia-file.db"),
DBDir = rabbit_db:dir(),
ok = filelib:ensure_path(DBDir),
SomeFile = filename:join(DBDir, "some-file.db"),
ok = file:write_file(SomeFile, <<>>),
?assertNot(rabbit_mnesia:is_virgin_node()),
?assertNot(rabbit_db:is_virgin_node()),
ok
end,
[]),
Expand Down
2 changes: 1 addition & 1 deletion deps/rabbit/test/per_vhost_msg_store_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ folder_size(Dir) ->
fun(F,Acc) -> filelib:file_size(F) + Acc end, 0).

get_global_folder_size(Config) ->
BaseDir = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_mnesia, dir, []),
BaseDir = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit, data_dir, []),
folder_size(BaseDir).

vhost_dir(Vhost, Config) ->
Expand Down
1 change: 1 addition & 0 deletions deps/rabbit_common/include/logging.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

-define(RMQLOG_DOMAIN_CHAN, ?DEFINE_RMQLOG_DOMAIN(channel)).
-define(RMQLOG_DOMAIN_CONN, ?DEFINE_RMQLOG_DOMAIN(connection)).
-define(RMQLOG_DOMAIN_DB, ?DEFINE_RMQLOG_DOMAIN(db)).
-define(RMQLOG_DOMAIN_FEAT_FLAGS, ?DEFINE_RMQLOG_DOMAIN(feature_flags)).
-define(RMQLOG_DOMAIN_MIRRORING, ?DEFINE_RMQLOG_DOMAIN(mirroring)).
-define(RMQLOG_DOMAIN_PRELAUNCH, ?DEFINE_RMQLOG_DOMAIN(prelaunch)).
Expand Down
2 changes: 1 addition & 1 deletion deps/rabbitmq_cli/test/ctl/force_boot_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule ForceBootCommandTest do
stop_rabbitmq_app()
on_exit(fn -> start_rabbitmq_app() end)
assert @command.run([], context[:opts]) == :ok
data_dir = :rpc.call(get_rabbit_hostname(), :rabbit_mnesia, :dir, [])
data_dir = :rpc.call(get_rabbit_hostname(), :rabbit, :data_dir, [])

path = Path.join(data_dir, "force_load")
assert File.exists?(path)
Expand Down
22 changes: 11 additions & 11 deletions deps/rabbitmq_cli/test/ctl/forget_cluster_node_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule ForgetClusterNodeCommandTest do
:rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir])

rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
data_dir = :rabbit_misc.rpc_call(node, :rabbit_mnesia, :dir, [])
data_dir = :rabbit_misc.rpc_call(node, :rabbit, :data_dir, [])

feature_flags_file =
:rabbit_misc.rpc_call(node, :rabbit_feature_flags, :enabled_feature_flags_list_file, [])
Expand Down Expand Up @@ -63,34 +63,34 @@ defmodule ForgetClusterNodeCommandTest do
)
end

test "validate_execution_environment: offline forget without mnesia dir fails", context do
test "validate_execution_environment: offline forget without data dir fails", context do
offline_opts =
Map.merge(
context[:opts],
%{offline: true, node: :non_exist@localhost}
)

opts_without_mnesia = Map.delete(offline_opts, :data_dir)
Application.put_env(:mnesia, :dir, "/tmp")
on_exit(fn -> Application.delete_env(:mnesia, :dir) end)
opts_without_data_dir = Map.delete(offline_opts, :data_dir)
Application.put_env(:rabbit, :data_dir, "/tmp")
on_exit(fn -> Application.delete_env(:rabbit, :data_dir) end)

assert match?(
:ok,
@command.validate_execution_environment(
["other_node@localhost"],
opts_without_mnesia
opts_without_data_dir
)
)

Application.delete_env(:mnesia, :dir)
Application.delete_env(:rabbit, :data_dir)
System.put_env("RABBITMQ_MNESIA_DIR", "/tmp")
on_exit(fn -> System.delete_env("RABBITMQ_MNESIA_DIR") end)

assert match?(
:ok,
@command.validate_execution_environment(
["other_node@localhost"],
opts_without_mnesia
opts_without_data_dir
)
)

Expand All @@ -102,15 +102,15 @@ defmodule ForgetClusterNodeCommandTest do
)
end

test "validate_execution_environment: online mode does not fail is mnesia is not loaded",
test "validate_execution_environment: online mode does not fail if database is not loaded",
context do
opts_without_mnesia = Map.delete(context[:opts], :data_dir)
opts_without_data_dir = Map.delete(context[:opts], :data_dir)

assert match?(
:ok,
@command.validate_execution_environment(
["other_node@localhost"],
opts_without_mnesia
opts_without_data_dir
)
)
end
Expand Down
16 changes: 8 additions & 8 deletions deps/rabbitmq_cli/test/ctl/rename_cluster_node_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule RenameClusterNodeCommandTest do
:rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir])

rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
data_dir = :rabbit_misc.rpc_call(node, :rabbit_mnesia, :dir, [])
data_dir = :rabbit_misc.rpc_call(node, :rabbit, :data_dir, [])

on_exit([], fn ->
start_rabbitmq_app()
Expand Down Expand Up @@ -67,26 +67,26 @@ defmodule RenameClusterNodeCommandTest do
)
end

test "validate_execution_environment: not providing node mnesia dir fails validation",
test "validate_execution_environment: not providing node data dir fails validation",
context do
opts_without_mnesia = Map.delete(context[:opts], :data_dir)
Application.put_env(:mnesia, :dir, "/tmp")
on_exit(fn -> Application.delete_env(:mnesia, :dir) end)
opts_without_data_dir = Map.delete(context[:opts], :data_dir)
Application.put_env(:rabbit, :data_dir, "/tmp")
on_exit(fn -> Application.delete_env(:rabbit, :data_dir) end)

assert :ok ==
@command.validate(
["some_node@localhost", "other_node@localhost"],
opts_without_mnesia
opts_without_data_dir
)

Application.delete_env(:mnesia, :dir)
Application.delete_env(:rabbit, :data_dir)
System.put_env("RABBITMQ_MNESIA_DIR", "/tmp")
on_exit(fn -> System.delete_env("RABBITMQ_MNESIA_DIR") end)

assert :ok ==
@command.validate(
["some_node@localhost", "other_node@localhost"],
opts_without_mnesia
opts_without_data_dir
)

System.delete_env("RABBITMQ_MNESIA_DIR")
Expand Down