Thanks to visit codestin.com
Credit goes to github.com

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
6 changes: 5 additions & 1 deletion deps/rabbit/src/rabbit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,16 @@
{requires, prevent_startup_if_node_was_reset},
{enables, routing_ready}]}).


-rabbit_boot_step({cluster_tags,
[{description, "Set cluster tags"},
{mfa, {?MODULE, update_cluster_tags, []}},
{requires, core_initialized}]}).

-rabbit_boot_step({logger_exchange,
[{description, "Declare exchange for rabbit_logger_exchange_h"},
{mfa, {rabbit_logger_exchange_h, declare_exchange, []}},
{requires, [core_initialized, recovery]}]}).

-rabbit_boot_step({routing_ready,
[{description, "message delivery logic ready"},
{requires, [core_initialized, recovery]}]}).
Expand Down
11 changes: 8 additions & 3 deletions deps/rabbit/src/rabbit_definitions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,14 @@ args(L) -> rabbit_misc:to_amqp_table(L).
%%

list_exchanges() ->
%% exclude internal exchanges, they are not meant to be declared or used by
%% applications
[exchange_definition(X) || X <- lists:filter(fun(#exchange{internal = true}) -> false;
%% Exclude internal exchanges, they are not meant to be declared or used by
%% applications, except for the exchange used by `rabbit_logger_exchange_h' ('amq.rabbitmq.log').
%% Its virtual host depends on the configurable default virtual host: we don't
%% want the exported definitions to depend on the configuration, that's why
%% we include it in the exported data.
LoggerExchange = rabbit_logger_exchange_h:exchange(),
[exchange_definition(X) || X <- lists:filter(fun(#exchange{name = Name}) when Name =:= LoggerExchange -> true;
(#exchange{internal = true}) -> false;
(#exchange{name = #resource{name = <<>>}}) -> false;
(#exchange{name = #resource{name = <<"amq.", _Rest/binary>>}}) -> false;
(_) -> true
Expand Down
98 changes: 33 additions & 65 deletions deps/rabbit/src/rabbit_logger_exchange_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom”
%% refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

-module(rabbit_logger_exchange_h).
Expand All @@ -15,8 +16,10 @@
%% logger callbacks
-export([log/2, adding_handler/1, removing_handler/1, changing_config/3,
filter_config/1]).
%% Boot step callback.
-export([declare_exchange/0]).
-export([exchange/0]).

-define(DECL_EXCHANGE_INTERVAL_SECS, 5).
-define(LOG_EXCH_NAME, <<"amq.rabbitmq.log">>).
-define(DEFAULT_FORMATTER, logger_formatter).
-define(DEFAULT_FORMATTER_CONFIG, #{}).
Expand Down Expand Up @@ -78,7 +81,7 @@ do_log(
Content = rabbit_basic:build_content(PBasic, Body),
case mc_amqpl:message(Exchange, RoutingKey, Content) of
{ok, Msg} ->
%% Publishing a message might involve a Erlang process, like a Ra
%% Publishing a message might involve an Erlang process, like a Ra
%% server process, to log something and call itself. We need to
%% publish the message asynchronously from a separate process and
%% ignore the fate of that publish, to not block an Erlang
Expand Down Expand Up @@ -159,51 +162,17 @@ start_setup_proc(#{config := InternalConfig} = Config) ->
Exchange = rabbit_misc:r(DefaultVHost, exchange, ?LOG_EXCH_NAME),
InternalConfig1 = InternalConfig#{exchange => Exchange},
Pid = spawn(fun() ->
wait_for_initial_pass(60),
setup_proc(Config#{config => InternalConfig1})
end),
InternalConfig2 = InternalConfig1#{setup_proc => Pid},
Config#{config => InternalConfig2}.

%% Declaring an exchange requires the metadata store to be ready
%% which happens on a boot step after the second phase of the prelaunch.
%% This function waits for the store initialisation.
wait_for_initial_pass(0) ->
ok;
wait_for_initial_pass(N) ->
case rabbit_db:is_init_finished() of
false ->
timer:sleep(1000),
wait_for_initial_pass(N - 1);
true ->
ok
end.

setup_proc(
#{id := Id,
config := #{exchange := Exchange}} = Config) ->
setup_proc(#{id := Id} = Config) ->
%% We register this process using the logger handler ID. It makes
%% debugging convenient but it's not critical. That's why we catch any
%% exceptions and ignore the return value.
_ = catch erlang:register(Id, self()),

case declare_exchange(Config) of
ok ->
?LOG_INFO(
"Logging to ~ts ready", [rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
loop(Config);
error ->
?LOG_DEBUG(
"Logging to ~ts not ready, trying again in ~b second(s)",
[rabbit_misc:rs(Exchange), ?DECL_EXCHANGE_INTERVAL_SECS],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
receive
stop -> ok
after ?DECL_EXCHANGE_INTERVAL_SECS * 1000 ->
setup_proc(Config)
end
end.
loop(Config).

loop(#{config := #{exchange := Exchange}} = Config) ->
receive
Expand All @@ -214,44 +183,43 @@ loop(#{config := #{exchange := Exchange}} = Config) ->
ok
end.

declare_exchange(#{config := #{exchange := Exchange}}) ->
try rabbit_exchange:declare(
Exchange, topic, true, false, true, [], ?INTERNAL_USER) of
{ok, #exchange{}} ->
?LOG_DEBUG(
"Declared ~ts",
[rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
ok;
{error, timeout} ->
?LOG_DEBUG(
"Could not declare ~ts because the operation timed out",
[rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
error
exchange() ->
{ok, DefaultVHost} = application:get_env(rabbit, default_vhost),
Exchange = rabbit_misc:r(DefaultVHost, exchange, ?LOG_EXCH_NAME),
Exchange.

declare_exchange() ->
Exchange = exchange(),
try
Ret = rabbit_exchange:declare(
Exchange, topic, true, false, true, [], ?INTERNAL_USER),
case Ret of
{ok, #exchange{}} ->
?LOG_DEBUG(
"Declared ~ts",
[rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
ok;
{error, timeout} ->
?LOG_DEBUG(
"Could not declare ~ts because the operation timed out",
[rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
ok
end
catch
Class:Reason ->
?LOG_DEBUG(
"Could not declare ~ts, reason: ~0p:~0p",
[rabbit_misc:rs(Exchange), Class, Reason],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
error
ok
end.

unconfigure_exchange(
#{config := #{exchange := Exchange,
setup_proc := Pid}}) ->
Pid ! stop,
case rabbit_exchange:ensure_deleted(Exchange, false, ?INTERNAL_USER) of
ok ->
ok;
{error, timeout} ->
?LOG_ERROR(
"Could not delete ~ts due to a timeout",
[rabbit_misc:rs(Exchange)],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
ok
end,
?LOG_INFO(
"Logging to ~ts disabled",
[rabbit_misc:rs(Exchange)],
Expand Down
1 change: 1 addition & 0 deletions deps/rabbit/test/exchanges_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ from_mnesia_to_khepri(Config) ->
rabbit_misc:r(<<"/">>, exchange, <<"amq.fanout">>),
rabbit_misc:r(<<"/">>, exchange, <<"amq.headers">>),
rabbit_misc:r(<<"/">>, exchange, <<"amq.match">>),
rabbit_misc:r(<<"/">>, exchange, <<"amq.rabbitmq.log">>),
rabbit_misc:r(<<"/">>, exchange, <<"amq.rabbitmq.trace">>),
rabbit_misc:r(<<"/">>, exchange, <<"amq.topic">>),
rabbit_misc:r(<<"/">>, exchange, X)]),
Expand Down
2 changes: 1 addition & 1 deletion deps/rabbit/test/metadata_store_migration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ from_mnesia_to_khepri(Config) ->
Queues = lists:sort(rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_amqqueue, list, [])),
?assertMatch([_, _], Queues),
Exchanges = lists:sort(rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_exchange, list, [])),
?assertEqual(14, length(Exchanges)),
?assertEqual(15, length(Exchanges)),
Bindings = lists:sort(rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_binding, list, [<<"/">>])),
?assertEqual(4, length(Bindings)),
Server = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename),
Expand Down
4 changes: 2 additions & 2 deletions deps/rabbit/test/unit_connection_tracking_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ end_per_testcase(Testcase, Config) ->
%% ---------------------------------------------------------------------------

exchange_count(Config) ->
%% Default exchanges == 7
?assertEqual(7, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_exchange, count, [])).
%% Default exchanges == 8
?assertEqual(8, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_exchange, count, [])).

queue_count(Config) ->
Conn = rabbit_ct_client_helpers:open_connection(Config, 0),
Expand Down
Loading