Skip to content

OTP 24 compat: switch management and management_agent to use pg #2850

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

Merged
merged 5 commits into from
Feb 26, 2021
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
3 changes: 3 additions & 0 deletions deps/rabbitmq_management/include/rabbit_mgmt.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
-define(AUTH_REALM, "Basic realm=\"RabbitMQ Management\"").

-define(HEALTH_CHECK_FAILURE_STATUS, 503).

-define(MANAGEMENT_PG_SCOPE, rabbit_mgmt).
-define(MANAGEMENT_PG_GROUP, management_db).
7 changes: 3 additions & 4 deletions deps/rabbitmq_management/src/rabbit_mgmt_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

-module(rabbit_mgmt_db).

%% pg2 is deprecated in OTP 23.
-compile(nowarn_deprecated_function).

-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_metrics.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").
-include_lib("rabbit_common/include/rabbit_core_metrics.hrl").

-include("rabbit_mgmt.hrl").

-behaviour(gen_server2).

-export([start_link/0]).
Expand Down Expand Up @@ -733,7 +732,7 @@ created_stats_delegated(Type) ->

-spec delegate_invoke(mfargs()) -> [any()].
delegate_invoke(FunOrMFA) ->
MemberPids = [P || P <- pg2:get_members(management_db)],
MemberPids = [P || P <- pg:get_members(?MANAGEMENT_PG_SCOPE, ?MANAGEMENT_PG_GROUP)],
{Results, Errors} = delegate:invoke(MemberPids, ?DELEGATE_PREFIX, FunOrMFA),
case Errors of
[] -> ok;
Expand Down
37 changes: 31 additions & 6 deletions deps/rabbitmq_management/src/rabbit_mgmt_sup_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,47 @@

-module(rabbit_mgmt_sup_sup).

-behaviour(supervisor2).
-behaviour(supervisor).

-export([init/1]).
-export([start_link/0, start_child/0]).

-include_lib("rabbit_common/include/rabbit.hrl").
-include("rabbit_mgmt.hrl").

start_child() ->
supervisor2:start_child(?MODULE, sup()).
supervisor:start_child(?MODULE, sup()).

sup() ->
{rabbit_mgmt_sup, {rabbit_mgmt_sup, start_link, []},
temporary, ?SUPERVISOR_WAIT, supervisor, [rabbit_mgmt_sup]}.
#{
id => rabbit_mgmt_sup,
start => {rabbit_mgmt_sup, start_link, []},
restart => temporary,
shutdown => ?SUPERVISOR_WAIT,
type => supervisor,
modules => [rabbit_mgmt_sup]
}.

init([]) ->
{ok, {{one_for_one, 0, 1}, [sup()]}}.
%% This scope is used in the child process, so start it
%% early. We don't attach it to the supervision tree because
%%
%% * rabbitmq_management and rabbitmq_management_agent share a scope
%% * start an already running scope results in an "already started" error returned
%% * such errors wreck supervision tree startup
%%
%% So we expect management agent to start the scope as part of its
%% supervision tree and only start it here for environments
%% such as tests that may be testing parts of this plugin in isolation.
_ = pg:start_link(?MANAGEMENT_PG_SCOPE),

Flags = #{
strategy => one_for_one,
intensity => 0,
period => 1
},
Specs = [sup()],
{ok, {Flags, Specs}}.

start_link() ->
supervisor2:start_link({local, ?MODULE}, ?MODULE, []).
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
18 changes: 18 additions & 0 deletions deps/rabbitmq_management_agent/include/rabbit_mgmt_agent.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License at
%% https://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
%% License for the specific language governing rights and limitations
%% under the License.
%%
%% The Original Code is RabbitMQ Management Console.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2020-2021 VMware, Inc. or its affiliates. All rights reserved.
%%

-define(MANAGEMENT_PG_SCOPE, rabbit_mgmt).
-define(MANAGEMENT_PG_GROUP, management_db).
14 changes: 8 additions & 6 deletions deps/rabbitmq_management_agent/src/rabbit_mgmt_agent_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

-module(rabbit_mgmt_agent_sup).

%% pg2 is deprecated in OTP 23.
-compile(nowarn_deprecated_function).

-behaviour(supervisor).

-include_lib("rabbit_common/include/rabbit.hrl").
-include_lib("rabbit_common/include/rabbit_core_metrics.hrl").
-include("rabbit_mgmt_metrics.hrl").
-include("rabbit_mgmt_agent.hrl").

-export([init/1]).
-export([start_link/0]).
Expand All @@ -24,7 +22,12 @@ init([]) ->
ExternalStats = {rabbit_mgmt_external_stats,
{rabbit_mgmt_external_stats, start_link, []},
permanent, 5000, worker, [rabbit_mgmt_external_stats]},
{ok, {{one_for_one, 100, 10}, [ExternalStats] ++ MCs}}.
Flags = #{
strategy => one_for_one,
intensity => 0,
period => 1
},
{ok, {Flags, [ExternalStats] ++ MCs}}.

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
Expand All @@ -33,8 +36,7 @@ start_link() ->
maybe_enable_metrics_collector() ->
case application:get_env(rabbitmq_management_agent, disable_metrics_collector, false) of
false ->
pg2:create(management_db),
ok = pg2:join(management_db, self()),
ok = pg:join(?MANAGEMENT_PG_SCOPE, ?MANAGEMENT_PG_GROUP, self()),
ST = {rabbit_mgmt_storage, {rabbit_mgmt_storage, start_link, []},
permanent, ?WORKER_WAIT, worker, [rabbit_mgmt_storage]},
MD = {delegate_management_sup, {delegate_sup, start_link, [5, ?DELEGATE_PREFIX]},
Expand Down
36 changes: 30 additions & 6 deletions deps/rabbitmq_management_agent/src/rabbit_mgmt_agent_sup_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,46 @@

-module(rabbit_mgmt_agent_sup_sup).

-behaviour(supervisor2).
-behaviour(supervisor).

-export([init/1]).
-export([start_link/0, start_child/0]).

-include_lib("rabbit_common/include/rabbit.hrl").
-include("rabbit_mgmt_agent.hrl").

start_child() ->
supervisor2:start_child(?MODULE, sup()).
supervisor:start_child(?MODULE, sup()).

sup() ->
{rabbit_mgmt_agent_sup, {rabbit_mgmt_agent_sup, start_link, []},
temporary, ?SUPERVISOR_WAIT, supervisor, [rabbit_mgmt_agent_sup]}.
#{
id => rabbit_mgmt_agent_sup,
start => {rabbit_mgmt_agent_sup, start_link, []},
restart => temporary,
shutdown => ?SUPERVISOR_WAIT,
type => supervisor,
modules => [rabbit_mgmt_agent_sup]
}.

init([]) ->
{ok, {{one_for_one, 0, 1}, [sup()]}}.
Flags = #{
strategy => one_for_one,
intensity => 0,
period => 1
},
PgScope = #{
id => ?MANAGEMENT_PG_SCOPE,
start => {pg, start_link, [?MANAGEMENT_PG_SCOPE]},
restart => temporary,
shutdown => ?SUPERVISOR_WAIT,
type => supervisor,
modules => []
},
Specs = [
PgScope,
sup()
],
{ok, {Flags, Specs}}.

start_link() ->
supervisor2:start_link({local, ?MODULE}, ?MODULE, []).
supervisor:start_link({local, ?MODULE}, ?MODULE, []).