|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_ra_systems). |
| 9 | + |
| 10 | +-include_lib("kernel/include/logger.hrl"). |
| 11 | + |
| 12 | +-include_lib("rabbit_common/include/logging.hrl"). |
| 13 | + |
| 14 | +-export([setup/0, |
| 15 | + setup/1, |
| 16 | + all_ra_systems/0, |
| 17 | + ensure_ra_system_started/1]). |
| 18 | + |
| 19 | +-type ra_system_name() :: atom(). |
| 20 | + |
| 21 | +-define(COORD_WAL_MAX_SIZE_B, 64_000_000). |
| 22 | + |
| 23 | +-spec setup() -> ok | no_return(). |
| 24 | + |
| 25 | +setup() -> |
| 26 | + setup(rabbit_prelaunch:get_context()). |
| 27 | + |
| 28 | +-spec setup(Context :: map()) -> ok | no_return(). |
| 29 | + |
| 30 | +setup(_) -> |
| 31 | + ?LOG_DEBUG("Starting Ra systems"), |
| 32 | + lists:foreach(fun ensure_ra_system_started/1, all_ra_systems()), |
| 33 | + ?LOG_DEBUG("Ra systems started"), |
| 34 | + ok. |
| 35 | + |
| 36 | +-spec all_ra_systems() -> [ra_system_name()]. |
| 37 | + |
| 38 | +all_ra_systems() -> |
| 39 | + [quorum_queues, |
| 40 | + coordination]. |
| 41 | + |
| 42 | +-spec ensure_ra_system_started(ra_system_name()) -> ok | no_return(). |
| 43 | + |
| 44 | +ensure_ra_system_started(RaSystem) -> |
| 45 | + RaSystemConfig = get_config(RaSystem), |
| 46 | + ?LOG_DEBUG( |
| 47 | + "Starting Ra system called \"~s\" with configuration:~n~p", |
| 48 | + [RaSystem, RaSystemConfig], |
| 49 | + #{domain => ?RMQLOG_DOMAIN_GLOBAL}), |
| 50 | + case ra_system:start(RaSystemConfig) of |
| 51 | + {ok, _} -> |
| 52 | + ?LOG_DEBUG( |
| 53 | + "Ra system \"~s\" ready", |
| 54 | + [RaSystem], |
| 55 | + #{domain => ?RMQLOG_DOMAIN_GLOBAL}), |
| 56 | + ok; |
| 57 | + {error, {already_started, _}} -> |
| 58 | + ?LOG_DEBUG( |
| 59 | + "Ra system \"~s\" ready", |
| 60 | + [RaSystem], |
| 61 | + #{domain => ?RMQLOG_DOMAIN_GLOBAL}), |
| 62 | + ok; |
| 63 | + Error -> |
| 64 | + ?LOG_ERROR( |
| 65 | + "Failed to start Ra system \"~s\": ~p", |
| 66 | + [RaSystem, Error], |
| 67 | + #{domain => ?RMQLOG_DOMAIN_GLOBAL}), |
| 68 | + throw(Error) |
| 69 | + end. |
| 70 | + |
| 71 | +-spec get_config(ra_system_name()) -> ra_system:config(). |
| 72 | + |
| 73 | +get_config(quorum_queues = RaSystem) -> |
| 74 | + DefaultConfig = get_default_config(), |
| 75 | + DefaultConfig#{name => RaSystem}; % names => ra_system:derive_names(quorum) |
| 76 | +get_config(coordination = RaSystem) -> |
| 77 | + DefaultConfig = get_default_config(), |
| 78 | + CoordDataDir = filename:join( |
| 79 | + [rabbit_mnesia:dir(), "coordination", node()]), |
| 80 | + DefaultConfig#{name => RaSystem, |
| 81 | + data_dir => CoordDataDir, |
| 82 | + wal_data_dir => CoordDataDir, |
| 83 | + wal_max_size_bytes => ?COORD_WAL_MAX_SIZE_B, |
| 84 | + names => ra_system:derive_names(RaSystem)}. |
| 85 | + |
| 86 | +-spec get_default_config() -> ra_system:config(). |
| 87 | + |
| 88 | +get_default_config() -> |
| 89 | + ra_system:default_config(). |
0 commit comments