syn_backbone.erl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_backbone).
  27. -behaviour(gen_server).
  28. %% API
  29. -export([start_link/0]).
  30. -export([get_event_handler_module/0]).
  31. -export([get_anti_entropy_settings/1]).
  32. %% gen_server callbacks
  33. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  34. %% macros
  35. -define(DEFAULT_EVENT_HANDLER_MODULE, syn_event_handler).
  36. -define(DEFAULT_ANTI_ENTROPY_MAX_DEVIATION_MS, 60000).
  37. %% records
  38. -record(state, {}).
  39. %% includes
  40. -include("syn.hrl").
  41. %% ===================================================================
  42. %% API
  43. %% ===================================================================
  44. -spec start_link() -> {ok, pid()} | {error, any()}.
  45. start_link() ->
  46. Options = [],
  47. gen_server:start_link({local, ?MODULE}, ?MODULE, [], Options).
  48. -spec get_event_handler_module() -> module().
  49. get_event_handler_module() ->
  50. %% get handler
  51. CustomEventHandler = application:get_env(syn, event_handler, ?DEFAULT_EVENT_HANDLER_MODULE),
  52. %% ensure that is it loaded (not using code:ensure_loaded/1 to support embedded mode)
  53. catch CustomEventHandler:module_info(exports),
  54. %% return
  55. CustomEventHandler.
  56. -spec get_anti_entropy_settings(Module :: registry | groups) ->
  57. {IntervalMs :: non_neg_integer() | undefined, IntervalMaxDeviationMs :: non_neg_integer() | undefined}.
  58. get_anti_entropy_settings(Module) ->
  59. case application:get_env(syn, anti_entropy, undefined) of
  60. undefined ->
  61. {undefined, undefined};
  62. AntiEntropySettings ->
  63. case proplists:get_value(Module, AntiEntropySettings) of
  64. undefined ->
  65. {undefined, undefined};
  66. ModSettings ->
  67. case proplists:get_value(interval, ModSettings) of
  68. undefined ->
  69. {undefined, undefined};
  70. I ->
  71. IntervalMs = I * 1000,
  72. IntervalMaxDeviationMs = proplists:get_value(
  73. interval_max_deviation,
  74. ModSettings,
  75. ?DEFAULT_ANTI_ENTROPY_MAX_DEVIATION_MS
  76. ) * 1000,
  77. %% return
  78. {IntervalMs, IntervalMaxDeviationMs}
  79. end
  80. end
  81. end.
  82. %% ===================================================================
  83. %% Callbacks
  84. %% ===================================================================
  85. %% ----------------------------------------------------------------------------------------------------------
  86. %% Init
  87. %% ----------------------------------------------------------------------------------------------------------
  88. -spec init([]) ->
  89. {ok, #state{}} |
  90. {ok, #state{}, Timeout :: non_neg_integer()} |
  91. ignore |
  92. {stop, Reason :: any()}.
  93. init([]) ->
  94. %% create ETS tables
  95. %% entries have structure {Name, Pid, Meta, Clock, MonitorRef, Node}
  96. ets:new(syn_registry_by_name, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  97. %% entries have format {{Pid, Name}, Meta, Clock, MonitorRef, Node}
  98. ets:new(syn_registry_by_pid, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  99. %% entries have format {{GroupName, Pid}, Meta, MonitorRef, Node}
  100. ets:new(syn_groups_by_name, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  101. %% entries have format {{Pid, GroupName}, Meta, MonitorRef, Node}
  102. ets:new(syn_groups_by_pid, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  103. %% init
  104. {ok, #state{}}.
  105. %% ----------------------------------------------------------------------------------------------------------
  106. %% Call messages
  107. %% ----------------------------------------------------------------------------------------------------------
  108. -spec handle_call(Request :: any(), From :: any(), #state{}) ->
  109. {reply, Reply :: any(), #state{}} |
  110. {reply, Reply :: any(), #state{}, Timeout :: non_neg_integer()} |
  111. {noreply, #state{}} |
  112. {noreply, #state{}, Timeout :: non_neg_integer()} |
  113. {stop, Reason :: any(), Reply :: any(), #state{}} |
  114. {stop, Reason :: any(), #state{}}.
  115. handle_call(Request, From, State) ->
  116. error_logger:warning_msg("Syn(~p): Received from ~p an unknown call message: ~p~n", [node(), Request, From]),
  117. {reply, undefined, State}.
  118. %% ----------------------------------------------------------------------------------------------------------
  119. %% Cast messages
  120. %% ----------------------------------------------------------------------------------------------------------
  121. -spec handle_cast(Msg :: any(), #state{}) ->
  122. {noreply, #state{}} |
  123. {noreply, #state{}, Timeout :: non_neg_integer()} |
  124. {stop, Reason :: any(), #state{}}.
  125. handle_cast(Msg, State) ->
  126. error_logger:warning_msg("Syn(~p): Received an unknown cast message: ~p~n", [node(), Msg]),
  127. {noreply, State}.
  128. %% ----------------------------------------------------------------------------------------------------------
  129. %% All non Call / Cast messages
  130. %% ----------------------------------------------------------------------------------------------------------
  131. -spec handle_info(Info :: any(), #state{}) ->
  132. {noreply, #state{}} |
  133. {noreply, #state{}, Timeout :: non_neg_integer()} |
  134. {stop, Reason :: any(), #state{}}.
  135. handle_info(Info, State) ->
  136. error_logger:warning_msg("Syn(~p): Received an unknown info message: ~p~n", [node(), Info]),
  137. {noreply, State}.
  138. %% ----------------------------------------------------------------------------------------------------------
  139. %% Terminate
  140. %% ----------------------------------------------------------------------------------------------------------
  141. -spec terminate(Reason :: any(), #state{}) -> terminated.
  142. terminate(Reason, _State) ->
  143. error_logger:info_msg("Syn(~p): Terminating with reason: ~p~n", [node(), Reason]),
  144. %% delete ETS tables
  145. ets:delete(syn_registry_by_name),
  146. ets:delete(syn_registry_by_pid),
  147. ets:delete(syn_groups_by_name),
  148. ets:delete(syn_groups_by_pid),
  149. %% return
  150. terminated.
  151. %% ----------------------------------------------------------------------------------------------------------
  152. %% Convert process state when code is changed.
  153. %% ----------------------------------------------------------------------------------------------------------
  154. -spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
  155. code_change(_OldVsn, State, _Extra) ->
  156. {ok, State}.