Browse Source

Refactor supervisors.

Roberto Ostinelli 3 years ago
parent
commit
1d512083a9
5 changed files with 71 additions and 62 deletions
  1. 3 3
      src/syn.erl
  2. 3 3
      src/syn_gen_scope.erl
  3. 3 3
      src/syn_registry.erl
  4. 14 43
      src/syn_scope_sup.erl
  5. 48 10
      src/syn_sup.erl

+ 3 - 3
src/syn.erl

@@ -48,16 +48,16 @@ stop() ->
 %% ----- \/ scopes ---------------------------------------------------
 %% ----- \/ scopes ---------------------------------------------------
 -spec get_node_scopes() -> [atom()].
 -spec get_node_scopes() -> [atom()].
 get_node_scopes() ->
 get_node_scopes() ->
-    syn_scopes_sup:get_node_scopes().
+    syn_sup:get_node_scopes().
 
 
 -spec add_node_to_scope(Scope :: atom()) -> ok.
 -spec add_node_to_scope(Scope :: atom()) -> ok.
 add_node_to_scope(Scope) ->
 add_node_to_scope(Scope) ->
-    syn_scopes_sup:add_node_to_scope(Scope).
+    syn_sup:add_node_to_scope(Scope).
 
 
 -spec add_node_to_scopes(Scopes :: [atom()]) -> ok.
 -spec add_node_to_scopes(Scopes :: [atom()]) -> ok.
 add_node_to_scopes(Scopes) ->
 add_node_to_scopes(Scopes) ->
     lists:foreach(fun(Scope) ->
     lists:foreach(fun(Scope) ->
-        syn_scopes_sup:add_node_to_scope(Scope)
+        syn_sup:add_node_to_scope(Scope)
     end, Scopes).
     end, Scopes).
 
 
 %% ----- \/ registry -------------------------------------------------
 %% ----- \/ registry -------------------------------------------------

+ 3 - 3
src/syn_gen_scope.erl

@@ -64,7 +64,7 @@
     {noreply, NewState :: term()} |
     {noreply, NewState :: term()} |
     {noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
     {noreply, NewState :: term(), timeout() | hibernate | {continue, term()}} |
     {stop, Reason :: term(), NewState :: term()}.
     {stop, Reason :: term(), NewState :: term()}.
--callback save_remote_data(RemoteScopePid :: pid(), RemoteData :: any(), State :: term()) -> any().
+-callback save_remote_data(RemoteData :: any(), State :: term()) -> any().
 -callback get_local_data(State :: term()) -> {ok, Data :: any()} | undefined.
 -callback get_local_data(State :: term()) -> {ok, Data :: any()} | undefined.
 -callback purge_local_data_for_node(Node :: node(), State :: term()) -> any().
 -callback purge_local_data_for_node(Node :: node(), State :: term()) -> any().
 
 
@@ -90,7 +90,7 @@ call(Handler, Scope, Message) ->
     call(Handler, node(), Scope, Message).
     call(Handler, node(), Scope, Message).
 
 
 -spec call(Handler :: module(), Node :: atom(), Scope :: atom(), Message :: any()) -> Response :: any().
 -spec call(Handler :: module(), Node :: atom(), Scope :: atom(), Message :: any()) -> Response :: any().
-call(Handler,Node, Scope, Message) ->
+call(Handler, Node, Scope, Message) ->
     ProcessName = get_process_name_for_scope(Handler, Scope),
     ProcessName = get_process_name_for_scope(Handler, Scope),
     gen_server:call({ProcessName, Node}, Message).
     gen_server:call({ProcessName, Node}, Message).
 
 
@@ -214,7 +214,7 @@ handle_info({'3.0', ack_sync, RemoteScopePid, Data}, #state{
         [node(), RemoteScopeNode, Scope]
         [node(), RemoteScopeNode, Scope]
     ),
     ),
     %% save remote data
     %% save remote data
-    Handler:save_remote_data(RemoteScopePid, Data, State),
+    Handler:save_remote_data(Data, State),
     %% is this a new node?
     %% is this a new node?
     case maps:is_key(RemoteScopeNode, Nodes) of
     case maps:is_key(RemoteScopeNode, Nodes) of
         true ->
         true ->

+ 3 - 3
src/syn_registry.erl

@@ -39,7 +39,7 @@
     init/1,
     init/1,
     handle_call/3,
     handle_call/3,
     handle_info/2,
     handle_info/2,
-    save_remote_data/3,
+    save_remote_data/2,
     get_local_data/1,
     get_local_data/1,
     purge_local_data_for_node/2
     purge_local_data_for_node/2
 ]).
 ]).
@@ -296,8 +296,8 @@ handle_info(Info, State) ->
 get_local_data(#state{scope = Scope}) ->
 get_local_data(#state{scope = Scope}) ->
     {ok, get_registry_tuples_for_node(Scope, node())}.
     {ok, get_registry_tuples_for_node(Scope, node())}.
 
 
--spec save_remote_data(RemoteScopePid :: pid(), RemoteData :: any(), State :: term()) -> any().
-save_remote_data(RemoteScopePid, RegistryTuplesOfRemoteNode, #state{scope = Scope} = State) ->
+-spec save_remote_data(RemoteData :: any(), State :: term()) -> any().
+save_remote_data(RegistryTuplesOfRemoteNode, #state{scope = Scope} = State) ->
     %% insert tuples
     %% insert tuples
     lists:foreach(fun({Name, Pid, Meta, Time}) ->
     lists:foreach(fun({Name, Pid, Meta, Time}) ->
         handle_registry_sync(Scope, Name, Pid, Meta, Time, State)
         handle_registry_sync(Scope, Name, Pid, Meta, Time, State)

+ 14 - 43
src/syn_scopes_sup.erl → src/syn_scope_sup.erl

@@ -23,12 +23,11 @@
 %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 %% THE SOFTWARE.
 %% THE SOFTWARE.
 %% ==========================================================================================================
 %% ==========================================================================================================
--module(syn_scopes_sup).
+-module(syn_scope_sup).
 -behaviour(supervisor).
 -behaviour(supervisor).
 
 
 %% API
 %% API
--export([start_link/0]).
--export([get_node_scopes/0, add_node_to_scope/1]).
+-export([start_link/1]).
 
 
 %% Supervisor callbacks
 %% Supervisor callbacks
 -export([init/1]).
 -export([init/1]).
@@ -36,51 +35,23 @@
 %% ===================================================================
 %% ===================================================================
 %% API
 %% API
 %% ===================================================================
 %% ===================================================================
--spec start_link() -> {ok, pid()} | {already_started, pid()} | shutdown.
-start_link() ->
-    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-
--spec get_node_scopes() -> [atom()].
-get_node_scopes() ->
-    case application:get_env(syn, syn_custom_scopes) of
-        undefined -> [default];
-        {ok, Scopes} -> [default] ++ maps:keys(Scopes)
-    end.
-
--spec add_node_to_scope(Scope :: atom()) -> ok.
-add_node_to_scope(Scope) ->
-    error_logger:info_msg("SYN[~s] Adding node to scope '~s'", [node(), Scope]),
-    %% save to ENV (failsafe if sup is restarted)
-    CustomScopes0 = case application:get_env(syn, syn_custom_scopes) of
-        undefined -> #{};
-        {ok, Scopes} -> Scopes
-    end,
-    CustomScopes = CustomScopes0#{Scope => #{}},
-    application:set_env(syn, syn_custom_scopes, CustomScopes),
-    %% create ETS tables
-    syn_backbone:create_tables_for_scope(Scope),
-    %% start children
-    supervisor:start_child(?MODULE, scope_child_spec(syn_registry, Scope)),
-    supervisor:start_child(?MODULE, scope_child_spec(syn_groups, Scope)),
-    ok.
+-spec start_link(Scope :: atom()) -> {ok, pid()} | {already_started, pid()} | shutdown.
+start_link(Scope) ->
+    supervisor:start_link(?MODULE, [Scope]).
 
 
 %% ===================================================================
 %% ===================================================================
 %% Callbacks
 %% Callbacks
 %% ===================================================================
 %% ===================================================================
--spec init([]) ->
+-spec init([Scope :: atom()]) ->
     {ok, {{supervisor:strategy(), non_neg_integer(), pos_integer()}, [supervisor:child_spec()]}}.
     {ok, {{supervisor:strategy(), non_neg_integer(), pos_integer()}, [supervisor:child_spec()]}}.
-init([]) ->
-    %% empty on application start, but populated if the supervisor is restarted
-    Children = lists:foldl(fun(Scope, Acc) ->
-        %% create ETS tables
-        syn_backbone:create_tables_for_scope(Scope),
-        %% add to specs
-        [
-            scope_child_spec(syn_registry, Scope),
-            scope_child_spec(syn_groups, Scope)
-            | Acc
-        ]
-    end, [], get_node_scopes()),
+init([Scope]) ->
+    %% create ETS tables
+    syn_backbone:create_tables_for_scope(Scope),
+    %% set children
+    Children = [
+        scope_child_spec(syn_registry, Scope),
+        scope_child_spec(syn_groups, Scope)
+    ],
     {ok, {{one_for_one, 10, 10}, Children}}.
     {ok, {{one_for_one, 10, 10}, Children}}.
 
 
 %% ===================================================================
 %% ===================================================================

+ 48 - 10
src/syn_sup.erl

@@ -28,6 +28,7 @@
 
 
 %% API
 %% API
 -export([start_link/0]).
 -export([start_link/0]).
+-export([get_node_scopes/0, add_node_to_scope/1]).
 
 
 %% Supervisor callbacks
 %% Supervisor callbacks
 -export([init/1]).
 -export([init/1]).
@@ -39,28 +40,65 @@
 start_link() ->
 start_link() ->
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
 
 
+-spec get_node_scopes() -> [atom()].
+get_node_scopes() ->
+    %% always have a default scope for all nodes
+    case application:get_env(syn, syn_custom_scopes) of
+        undefined -> [default];
+        {ok, Scopes} -> [default] ++ maps:keys(Scopes)
+    end.
+
+-spec add_node_to_scope(Scope :: atom()) -> ok.
+add_node_to_scope(Scope) ->
+    error_logger:info_msg("SYN[~s] Adding node to scope '~s'", [node(), Scope]),
+    %% save to ENV (failsafe if sup is restarted)
+    CustomScopes0 = case application:get_env(syn, syn_custom_scopes) of
+        undefined -> #{};
+        {ok, Scopes} -> Scopes
+    end,
+    CustomScopes = CustomScopes0#{Scope => #{}},
+    application:set_env(syn, syn_custom_scopes, CustomScopes),
+    %% start child
+    supervisor:start_child(?MODULE, child_spec(Scope)),
+    ok.
+
 %% ===================================================================
 %% ===================================================================
 %% Callbacks
 %% Callbacks
 %% ===================================================================
 %% ===================================================================
 -spec init([]) ->
 -spec init([]) ->
     {ok, {{supervisor:strategy(), non_neg_integer(), pos_integer()}, [supervisor:child_spec()]}}.
     {ok, {{supervisor:strategy(), non_neg_integer(), pos_integer()}, [supervisor:child_spec()]}}.
 init([]) ->
 init([]) ->
-    Children = [
-        child_spec(syn_backbone, worker),
-        child_spec(syn_scopes_sup, supervisor)
-    ],
+    %% backbone
+    BackboneChildSpec = #{
+        id => syn_backbone,
+        start => {syn_backbone, start_link, []},
+        type => worker,
+        shutdown => 10000,
+        restart => permanent,
+        modules => [syn_backbone]
+    },
+
+    %% build children
+    Children = [BackboneChildSpec] ++
+        %% add scopes sup
+        lists:foldl(fun(Scope, Acc) ->
+            %% add to specs
+            [child_spec(Scope) | Acc]
+        end, [], get_node_scopes()),
+
+    %% return
     {ok, {{one_for_one, 10, 10}, Children}}.
     {ok, {{one_for_one, 10, 10}, Children}}.
 
 
 %% ===================================================================
 %% ===================================================================
 %% Internals
 %% Internals
 %% ===================================================================
 %% ===================================================================
--spec child_spec(module(), worker | supervisor) -> supervisor:child_spec().
-child_spec(Module, Type) ->
+-spec child_spec(Scope :: atom()) -> supervisor:child_spec().
+child_spec(Scope) ->
     #{
     #{
-        id => Module,
-        start => {Module, start_link, []},
-        type => Type,
+        id => {syn_scope_sup, Scope},
+        start => {syn_scope_sup, start_link, [Scope]},
+        type => supervisor,
         shutdown => 10000,
         shutdown => 10000,
         restart => permanent,
         restart => permanent,
-        modules => [Module]
+        modules => [syn_scope_sup]
     }.
     }.