|
| 1 | +%% The contents of this file are subject to the Mozilla Public License |
| 2 | +%% Version 2.0 (the "License"); you may not use this file except in |
| 3 | +%% compliance with the License. You may obtain a copy of the License |
| 4 | +%% at https://www.mozilla.org/en-US/MPL/2.0/ |
| 5 | +%% |
| 6 | +%% Software distributed under the License is distributed on an "AS IS" |
| 7 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +%% the License for the specific language governing rights and |
| 9 | +%% limitations under the License. |
| 10 | +%% |
| 11 | +%% The Original Code is RabbitMQ. |
| 12 | +%% |
| 13 | +%% The Initial Developer of the Original Code is Pivotal Software, Inc. |
| 14 | +%% Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved. |
| 15 | +%% |
| 16 | + |
| 17 | +-module(rabbit_stream_sac_coordinator). |
| 18 | + |
| 19 | +-behaviour(gen_server). |
| 20 | + |
| 21 | +%% API functions |
| 22 | +-export([start_link/0]). |
| 23 | +%% gen_server callbacks |
| 24 | +-export([init/1, |
| 25 | + handle_call/3, |
| 26 | + handle_cast/2, |
| 27 | + handle_info/2, |
| 28 | + terminate/2, |
| 29 | + code_change/3]). |
| 30 | +-export([register_consumer/4]). |
| 31 | + |
| 32 | +-type stream() :: binary(). |
| 33 | +-type consumer_name() :: binary(). |
| 34 | +-type subscription_id() :: byte(). |
| 35 | + |
| 36 | +-record(consumer, |
| 37 | + {pid :: pid(), subscription_id :: subscription_id()}). |
| 38 | +-record(group, {consumers :: [#consumer{}]}). |
| 39 | +-record(stream_groups, {groups :: #{consumer_name() => #group{}}}). |
| 40 | +-record(state, {stream_groups :: #{stream() => #stream_groups{}}}). |
| 41 | + |
| 42 | +register_consumer(Stream, |
| 43 | + ConsumerName, |
| 44 | + ConnectionPid, |
| 45 | + SubscriptionId) -> |
| 46 | + call({register_consumer, |
| 47 | + Stream, |
| 48 | + ConsumerName, |
| 49 | + ConnectionPid, |
| 50 | + SubscriptionId}). |
| 51 | + |
| 52 | +call(Request) -> |
| 53 | + gen_server:call({global, ?MODULE}, |
| 54 | + Request).%%%=================================================================== |
| 55 | + %%% API functions |
| 56 | + %%%=================================================================== |
| 57 | + |
| 58 | +%%-------------------------------------------------------------------- |
| 59 | +%% @doc |
| 60 | +%% Starts the server |
| 61 | +%% |
| 62 | +%% @spec start_link() -> {ok, Pid} | ignore | {error, Error} |
| 63 | +%% @end |
| 64 | +%%-------------------------------------------------------------------- |
| 65 | +start_link() -> |
| 66 | + case gen_server:start_link({global, ?MODULE}, ?MODULE, [], []) of |
| 67 | + {error, {already_started, _Pid}} -> |
| 68 | + ignore; |
| 69 | + R -> |
| 70 | + R |
| 71 | + end. |
| 72 | + |
| 73 | +%%%=================================================================== |
| 74 | +%%% gen_server callbacks |
| 75 | +%%%=================================================================== |
| 76 | + |
| 77 | +%%-------------------------------------------------------------------- |
| 78 | +%% @private |
| 79 | +%% @doc |
| 80 | +%% Initializes the server |
| 81 | +%% |
| 82 | +%% @spec init(Args) -> {ok, State} | |
| 83 | +%% {ok, State, Timeout} | |
| 84 | +%% ignore | |
| 85 | +%% {stop, Reason} |
| 86 | +%% @end |
| 87 | +%%-------------------------------------------------------------------- |
| 88 | +init([]) -> |
| 89 | + {ok, #state{stream_groups = #{}}}. |
| 90 | + |
| 91 | +%%-------------------------------------------------------------------- |
| 92 | +%% @private |
| 93 | +%% @doc |
| 94 | +%% Handling call messages |
| 95 | +%% |
| 96 | +%% @spec handle_call(Request, From, State) -> |
| 97 | +%% {reply, Reply, State} | |
| 98 | +%% {reply, Reply, State, Timeout} | |
| 99 | +%% {noreply, State} | |
| 100 | +%% {noreply, State, Timeout} | |
| 101 | +%% {stop, Reason, Reply, State} | |
| 102 | +%% {stop, Reason, State} |
| 103 | +%% @end |
| 104 | +%%-------------------------------------------------------------------- |
| 105 | +handle_call({register_consumer, |
| 106 | + Stream, |
| 107 | + ConsumerName, |
| 108 | + ConnectionPid, |
| 109 | + SubscriptionId}, |
| 110 | + _From, #state{stream_groups = StreamGroups0} = State) -> |
| 111 | + StreamGroups1 = |
| 112 | + maybe_create_group(Stream, ConsumerName, StreamGroups0), |
| 113 | + Group0 = lookup_group(Stream, ConsumerName, StreamGroups1), |
| 114 | + Consumer = |
| 115 | + #consumer{pid = ConnectionPid, subscription_id = SubscriptionId}, |
| 116 | + Group = add_to_group(Consumer, Group0), |
| 117 | + Active = is_active(Consumer, Group), |
| 118 | + StreamGroups2 = |
| 119 | + update_groups(Stream, ConsumerName, Group, StreamGroups1), |
| 120 | + {reply, {ok, Active}, State#state{stream_groups = StreamGroups2}}; |
| 121 | +handle_call(which_children, _From, State) -> |
| 122 | + {reply, [], State}. |
| 123 | + |
| 124 | +maybe_create_group(Stream, ConsumerName, StreamGroups) -> |
| 125 | + case StreamGroups of |
| 126 | + #{Stream := #stream_groups{groups = #{ConsumerName := _Consumers}}} -> |
| 127 | + %% the group already exists |
| 128 | + StreamGroups; |
| 129 | + #{Stream := #stream_groups{groups = GroupsForTheStream} = SG} -> |
| 130 | + %% there are groups for this streams, but not one for this consumer name |
| 131 | + GroupsForTheStream1 = |
| 132 | + maps:put(ConsumerName, #group{consumers = []}, |
| 133 | + GroupsForTheStream), |
| 134 | + StreamGroups#{Stream => |
| 135 | + SG#stream_groups{groups = GroupsForTheStream1}}; |
| 136 | + SGS -> |
| 137 | + SG = maps:get(Stream, SGS, #stream_groups{groups = #{}}), |
| 138 | + #stream_groups{groups = Groups} = SG, |
| 139 | + Groups1 = maps:put(ConsumerName, #group{consumers = []}, Groups), |
| 140 | + SGS#{Stream => SG#stream_groups{groups = Groups1}} |
| 141 | + end. |
| 142 | + |
| 143 | +lookup_group(Stream, ConsumerName, StreamGroups) -> |
| 144 | + case StreamGroups of |
| 145 | + #{Stream := #stream_groups{groups = #{ConsumerName := Group}}} -> |
| 146 | + Group; |
| 147 | + _ -> |
| 148 | + error |
| 149 | + end. |
| 150 | + |
| 151 | +add_to_group(Consumer, #group{consumers = Consumers} = Group) -> |
| 152 | + Group#group{consumers = Consumers ++ [Consumer]}. |
| 153 | + |
| 154 | +is_active(Consumer, #group{consumers = [Consumer]}) -> |
| 155 | + true; |
| 156 | +is_active(Consumer, #group{consumers = [Consumer | _]}) -> |
| 157 | + true; |
| 158 | +is_active(_, _) -> |
| 159 | + false. |
| 160 | + |
| 161 | +update_groups(Stream, ConsumerName, Group, StreamGroups) -> |
| 162 | + #{Stream := #stream_groups{groups = Groups}} = StreamGroups, |
| 163 | + Groups1 = maps:put(ConsumerName, Group, Groups), |
| 164 | + StreamGroups#{Stream => #stream_groups{groups = Groups1}}. |
| 165 | + |
| 166 | +handle_cast(_Msg, State) -> |
| 167 | + {noreply, State}. |
| 168 | + |
| 169 | +%%-------------------------------------------------------------------- |
| 170 | +%% @private |
| 171 | +%% @doc |
| 172 | +%% Handling cast messages |
| 173 | +%% |
| 174 | +%% @spec handle_cast(Msg, State) -> {noreply, State} | |
| 175 | +%% {noreply, State, Timeout} | |
| 176 | +%% {stop, Reason, State} |
| 177 | +%% @end |
| 178 | +%%-------------------------------------------------------------------- |
| 179 | + |
| 180 | +%%-------------------------------------------------------------------- |
| 181 | +%% @private |
| 182 | +%% @doc |
| 183 | +%% Handling all non call/cast messages |
| 184 | +%% |
| 185 | +%% @spec handle_info(Info, State) -> {noreply, State} | |
| 186 | +%% {noreply, State, Timeout} | |
| 187 | +%% {stop, Reason, State} |
| 188 | +%% @end |
| 189 | +%%-------------------------------------------------------------------- |
| 190 | +handle_info(_Info, State) -> |
| 191 | + {noreply, State}. |
| 192 | + |
| 193 | +%%-------------------------------------------------------------------- |
| 194 | +%% @private |
| 195 | +%% @doc |
| 196 | +%% This function is called by a gen_server when it is about to |
| 197 | +%% terminate. It should be the opposite of Module:init/1 and do any |
| 198 | +%% necessary cleaning up. When it returns, the gen_server terminates |
| 199 | +%% with Reason. The return value is ignored. |
| 200 | +%% |
| 201 | +%% @spec terminate(Reason, State) -> void() |
| 202 | +%% @end |
| 203 | +%%-------------------------------------------------------------------- |
| 204 | +terminate(_Reason, _State) -> |
| 205 | + ok. |
| 206 | + |
| 207 | +%%-------------------------------------------------------------------- |
| 208 | +%% @private |
| 209 | +%% @doc |
| 210 | +%% Convert process state when code is changed |
| 211 | +%% |
| 212 | +%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState} |
| 213 | +%% @end |
| 214 | +%%-------------------------------------------------------------------- |
| 215 | +code_change(_OldVsn, State, _Extra) -> |
| 216 | + {ok, State}. |
| 217 | + |
| 218 | +%%%=================================================================== |
| 219 | +%%% Internal functions |
| 220 | +%%%=================================================================== |
0 commit comments