Browse Source

Convert backbone to gen_server (owner of ETS tables).

Roberto Ostinelli 5 years ago
parent
commit
591fa52527
4 changed files with 90 additions and 44 deletions
  1. 2 2
      src/syn.app.src
  2. 1 6
      src/syn_app.erl
  3. 86 36
      src/syn_backbone.erl
  4. 1 0
      src/syn_sup.erl

+ 2 - 2
src/syn.app.src

@@ -3,14 +3,14 @@
         {description, "A global Process Registry and Process Group manager."},
         {description, "A global Process Registry and Process Group manager."},
         {vsn, "2.0.3"},
         {vsn, "2.0.3"},
         {registered, [
         {registered, [
+            syn_backbone,
             syn_groups,
             syn_groups,
             syn_registry,
             syn_registry,
             syn_sup
             syn_sup
         ]},
         ]},
         {applications, [
         {applications, [
             kernel,
             kernel,
-            stdlib,
-            mnesia
+            stdlib
         ]},
         ]},
         {mod, {syn_app, []}},
         {mod, {syn_app, []}},
         {env, []},
         {env, []},

+ 1 - 6
src/syn_app.erl

@@ -37,12 +37,7 @@
     StartArgs :: any()
     StartArgs :: any()
 ) -> {ok, pid()} | {ok, pid(), State :: any()} | {error, any()}.
 ) -> {ok, pid()} | {ok, pid(), State :: any()} | {error, any()}.
 start(_StartType, _StartArgs) ->
 start(_StartType, _StartArgs) ->
-    case syn_backbone:init() of
-        ok ->
-            syn_sup:start_link();
-        Other ->
-            Other
-    end.
+    syn_sup:start_link().
 
 
 -spec stop(State :: any()) -> ok.
 -spec stop(State :: any()) -> ok.
 stop(_State) ->
 stop(_State) ->

+ 86 - 36
src/syn_backbone.erl

@@ -24,32 +24,31 @@
 %% THE SOFTWARE.
 %% THE SOFTWARE.
 %% ==========================================================================================================
 %% ==========================================================================================================
 -module(syn_backbone).
 -module(syn_backbone).
+-behaviour(gen_server).
 
 
 %% API
 %% API
--export([init/0]).
+-export([start_link/0]).
 -export([get_event_handler_module/0]).
 -export([get_event_handler_module/0]).
 
 
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
 %% macros
 %% macros
 -define(DEFAULT_EVENT_HANDLER_MODULE, syn_event_handler).
 -define(DEFAULT_EVENT_HANDLER_MODULE, syn_event_handler).
 
 
+%% records
+-record(state, {}).
+
 %% includes
 %% includes
 -include("syn.hrl").
 -include("syn.hrl").
 
 
 %% ===================================================================
 %% ===================================================================
 %% API
 %% API
 %% ===================================================================
 %% ===================================================================
--spec init() -> ok | {error, Reason :: any()}.
-init() ->
-    drop_tables(),
-    case create_registry_table() of
-        {atomic, ok} ->
-            case create_groups_table() of
-                {atomic, ok} -> ok;
-                {aborted, Reason} -> {error, {could_not_create_syn_groups_table, Reason}}
-            end;
-        {aborted, Reason} ->
-            {error, {could_not_create_syn_registry_table, Reason}}
-    end.
+-spec start_link() -> {ok, pid()} | {error, any()}.
+start_link() ->
+    Options = [],
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], Options).
 
 
 -spec get_event_handler_module() -> module().
 -spec get_event_handler_module() -> module().
 get_event_handler_module() ->
 get_event_handler_module() ->
@@ -61,27 +60,78 @@ get_event_handler_module() ->
     CustomEventHandler.
     CustomEventHandler.
 
 
 %% ===================================================================
 %% ===================================================================
-%% Internal
+%% Callbacks
 %% ===================================================================
 %% ===================================================================
--spec drop_tables() -> ok.
-drop_tables() ->
-    mnesia:delete_table(syn_registry_table),
-    mnesia:delete_table(syn_groups_table).
-
--spec create_registry_table() -> {atomic, ok} | {aborted, Reason :: any()}.
-create_registry_table() ->
-    mnesia:create_table(syn_registry_table, [
-        {type, set},
-        {attributes, record_info(fields, syn_registry_table)},
-        {index, [#syn_registry_table.pid, #syn_groups_table.node]},
-        {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
-    ]).
-
--spec create_groups_table() -> {atomic, ok} | {aborted, Reason :: any()}.
-create_groups_table() ->
-    mnesia:create_table(syn_groups_table, [
-        {type, bag},
-        {attributes, record_info(fields, syn_groups_table)},
-        {index, [#syn_groups_table.pid, #syn_groups_table.node]},
-        {storage_properties, [{ets, [{read_concurrency, true}, {write_concurrency, true}]}]}
-    ]).
+
+%% ----------------------------------------------------------------------------------------------------------
+%% Init
+%% ----------------------------------------------------------------------------------------------------------
+-spec init([]) ->
+    {ok, #state{}} |
+    {ok, #state{}, Timeout :: non_neg_integer()} |
+    ignore |
+    {stop, Reason :: any()}.
+init([]) ->
+    %% create ETS tables
+    ets:new(syn_registry_by_name, [set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
+    ets:new(syn_registry_by_pid, [set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
+    %% init
+    {ok, #state{}}.
+
+%% ----------------------------------------------------------------------------------------------------------
+%% Call messages
+%% ----------------------------------------------------------------------------------------------------------
+-spec handle_call(Request :: any(), From :: any(), #state{}) ->
+    {reply, Reply :: any(), #state{}} |
+    {reply, Reply :: any(), #state{}, Timeout :: non_neg_integer()} |
+    {noreply, #state{}} |
+    {noreply, #state{}, Timeout :: non_neg_integer()} |
+    {stop, Reason :: any(), Reply :: any(), #state{}} |
+    {stop, Reason :: any(), #state{}}.
+
+handle_call(Request, From, State) ->
+    error_logger:warning_msg("Syn(~p): Received from ~p an unknown call message: ~p~n", [node(), Request, From]),
+    {reply, undefined, State}.
+
+%% ----------------------------------------------------------------------------------------------------------
+%% Cast messages
+%% ----------------------------------------------------------------------------------------------------------
+-spec handle_cast(Msg :: any(), #state{}) ->
+    {noreply, #state{}} |
+    {noreply, #state{}, Timeout :: non_neg_integer()} |
+    {stop, Reason :: any(), #state{}}.
+
+handle_cast(Msg, State) ->
+    error_logger:warning_msg("Syn(~p): Received an unknown cast message: ~p~n", [node(), Msg]),
+    {noreply, State}.
+
+%% ----------------------------------------------------------------------------------------------------------
+%% All non Call / Cast messages
+%% ----------------------------------------------------------------------------------------------------------
+-spec handle_info(Info :: any(), #state{}) ->
+    {noreply, #state{}} |
+    {noreply, #state{}, Timeout :: non_neg_integer()} |
+    {stop, Reason :: any(), #state{}}.
+
+handle_info(Info, State) ->
+    error_logger:warning_msg("Syn(~p): Received an unknown info message: ~p~n", [node(), Info]),
+    {noreply, State}.
+
+%% ----------------------------------------------------------------------------------------------------------
+%% Terminate
+%% ----------------------------------------------------------------------------------------------------------
+-spec terminate(Reason :: any(), #state{}) -> terminated.
+terminate(Reason, _State) ->
+    error_logger:info_msg("Syn(~p): Terminating with reason: ~p~n", [node(), Reason]),
+    %% delete ETS tables
+    ets:delete(syn_registry_by_name),
+    ets:delete(syn_registry_by_pid),
+    %% return
+    terminated.
+
+%% ----------------------------------------------------------------------------------------------------------
+%% Convert process state when code is changed.
+%% ----------------------------------------------------------------------------------------------------------
+-spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.

+ 1 - 0
src/syn_sup.erl

@@ -50,6 +50,7 @@ start_link() ->
     {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 = [
     Children = [
+        ?CHILD(syn_backbone, worker),
         ?CHILD(syn_groups, worker),
         ?CHILD(syn_groups, worker),
         ?CHILD(syn_registry, worker)
         ?CHILD(syn_registry, worker)
     ],
     ],