syn_groups.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2016 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_groups).
  27. -behaviour(gen_server).
  28. %% API
  29. -export([start_link/0]).
  30. -export([join/2, join/3]).
  31. -export([leave/2]).
  32. -export([member/2]).
  33. -export([get_members/1, get_members/2]).
  34. -export([publish/2]).
  35. -export([multi_call/2, multi_call/3]).
  36. -export([multi_call_reply/2]).
  37. %% gen_server callbacks
  38. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  39. %% internal
  40. -export([multi_call_and_receive/4]).
  41. %% records
  42. -record(state, {
  43. process_groups_process_exit_callback_module = undefined :: atom(),
  44. process_groups_process_exit_callback_function = undefined :: atom()
  45. }).
  46. %% macros
  47. -define(DEFAULT_MULTI_CALL_TIMEOUT_MS, 5000).
  48. %% include
  49. -include("syn.hrl").
  50. %% ===================================================================
  51. %% API
  52. %% ===================================================================
  53. -spec start_link() -> {ok, pid()} | {error, any()}.
  54. start_link() ->
  55. Options = [],
  56. gen_server:start_link({local, ?MODULE}, ?MODULE, [], Options).
  57. -spec join(Name :: any(), Pid :: pid()) -> ok.
  58. join(Name, Pid) ->
  59. join(Name, Pid, undefined).
  60. -spec join(Name :: any(), Pid :: pid(), Meta :: any()) -> ok.
  61. join(Name, Pid, Meta) when is_pid(Pid) ->
  62. Node = node(Pid),
  63. gen_server:call({?MODULE, Node}, {join, Name, Pid, Meta}).
  64. -spec leave(Name :: any(), Pid :: pid()) -> ok | {error, pid_not_in_group}.
  65. leave(Name, Pid) when is_pid(Pid) ->
  66. Node = node(Pid),
  67. gen_server:call({?MODULE, Node}, {leave, Name, Pid}).
  68. -spec member(Pid :: pid(), Name :: any()) -> boolean().
  69. member(Pid, Name) when is_pid(Pid) ->
  70. i_member(Pid, Name).
  71. -spec get_members(Name :: any()) -> [pid()].
  72. get_members(Name) ->
  73. i_get_members(Name).
  74. -spec get_members(Name :: any(), with_meta) -> [{pid(), Meta :: any()}].
  75. get_members(Name, with_meta) ->
  76. i_get_members(Name, with_meta).
  77. -spec publish(Name :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  78. publish(Name, Message) ->
  79. MemberPids = i_get_members(Name),
  80. FSend = fun(Pid) ->
  81. Pid ! Message
  82. end,
  83. lists:foreach(FSend, MemberPids),
  84. {ok, length(MemberPids)}.
  85. -spec multi_call(Name :: any(), Message :: any()) -> {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  86. multi_call(Name, Message) ->
  87. multi_call(Name, Message, ?DEFAULT_MULTI_CALL_TIMEOUT_MS).
  88. -spec multi_call(Name :: any(), Message :: any(), Timeout :: non_neg_integer()) ->
  89. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  90. multi_call(Name, Message, Timeout) ->
  91. Self = self(),
  92. MemberPids = i_get_members(Name),
  93. FSend = fun(Pid) ->
  94. spawn_link(?MODULE, multi_call_and_receive, [Self, Pid, Message, Timeout])
  95. end,
  96. lists:foreach(FSend, MemberPids),
  97. collect_replies(MemberPids).
  98. -spec multi_call_reply(CallerPid :: pid(), Reply :: any()) -> {syn_multi_call_reply, pid(), Reply :: any()}.
  99. multi_call_reply(CallerPid, Reply) ->
  100. CallerPid ! {syn_multi_call_reply, self(), Reply}.
  101. %% ===================================================================
  102. %% Callbacks
  103. %% ===================================================================
  104. %% ----------------------------------------------------------------------------------------------------------
  105. %% Init
  106. %% ----------------------------------------------------------------------------------------------------------
  107. -spec init([]) ->
  108. {ok, #state{}} |
  109. {ok, #state{}, Timeout :: non_neg_integer()} |
  110. ignore |
  111. {stop, Reason :: any()}.
  112. init([]) ->
  113. %% trap linked processes signal
  114. process_flag(trap_exit, true),
  115. %% get options
  116. {ok, [ProcessExitCallbackModule, ProcessExitCallbackFunction]} = syn_utils:get_env_value(
  117. process_groups_process_exit_callback,
  118. [undefined, undefined]
  119. ),
  120. %% build state
  121. {ok, #state{
  122. process_groups_process_exit_callback_module = ProcessExitCallbackModule,
  123. process_groups_process_exit_callback_function = ProcessExitCallbackFunction
  124. }}.
  125. %% ----------------------------------------------------------------------------------------------------------
  126. %% Call messages
  127. %% ----------------------------------------------------------------------------------------------------------
  128. -spec handle_call(Request :: any(), From :: any(), #state{}) ->
  129. {reply, Reply :: any(), #state{}} |
  130. {reply, Reply :: any(), #state{}, Timeout :: non_neg_integer()} |
  131. {noreply, #state{}} |
  132. {noreply, #state{}, Timeout :: non_neg_integer()} |
  133. {stop, Reason :: any(), Reply :: any(), #state{}} |
  134. {stop, Reason :: any(), #state{}}.
  135. handle_call({join, Name, Pid, Meta}, _From, State) ->
  136. %% check if pid is already in group
  137. case find_by_pid_and_name(Pid, Name) of
  138. undefined ->
  139. ok;
  140. Process ->
  141. %% remove old reference
  142. mnesia:dirty_delete_object(Process)
  143. end,
  144. %% add to group
  145. mnesia:dirty_write(#syn_groups_table{
  146. name = Name,
  147. pid = Pid,
  148. node = node(),
  149. meta = Meta
  150. }),
  151. %% link
  152. erlang:link(Pid),
  153. %% return
  154. {reply, ok, State};
  155. handle_call({leave, Name, Pid}, _From, State) ->
  156. case find_by_pid_and_name(Pid, Name) of
  157. undefined ->
  158. {reply, {error, pid_not_in_group}, State};
  159. Process ->
  160. %% remove from table
  161. remove_process(Process),
  162. %% unlink
  163. erlang:unlink(Pid),
  164. %% reply
  165. {reply, ok, State}
  166. end;
  167. handle_call(Request, From, State) ->
  168. error_logger:warning_msg("Received from ~p an unknown call message: ~p", [Request, From]),
  169. {reply, undefined, State}.
  170. %% ----------------------------------------------------------------------------------------------------------
  171. %% Cast messages
  172. %% ----------------------------------------------------------------------------------------------------------
  173. -spec handle_cast(Msg :: any(), #state{}) ->
  174. {noreply, #state{}} |
  175. {noreply, #state{}, Timeout :: non_neg_integer()} |
  176. {stop, Reason :: any(), #state{}}.
  177. handle_cast(Msg, State) ->
  178. error_logger:warning_msg("Received an unknown cast message: ~p", [Msg]),
  179. {noreply, State}.
  180. %% ----------------------------------------------------------------------------------------------------------
  181. %% All non Call / Cast messages
  182. %% ----------------------------------------------------------------------------------------------------------
  183. -spec handle_info(Info :: any(), #state{}) ->
  184. {noreply, #state{}} |
  185. {noreply, #state{}, Timeout :: non_neg_integer()} |
  186. {stop, Reason :: any(), #state{}}.
  187. handle_info({'EXIT', Pid, Reason}, #state{
  188. process_groups_process_exit_callback_module = ProcessExitCallbackModule,
  189. process_groups_process_exit_callback_function = ProcessExitCallbackFunction
  190. } = State) ->
  191. %% check if pid is in table
  192. case find_groups_by_pid(Pid) of
  193. [] ->
  194. %% log
  195. case Reason of
  196. normal -> ok;
  197. killed -> ok;
  198. _ ->
  199. error_logger:error_msg("Received an exit message from an unlinked process ~p with reason: ~p", [Pid, Reason])
  200. end;
  201. Processes ->
  202. F = fun(Process) ->
  203. %% get group & meta
  204. Name = Process#syn_groups_table.name,
  205. Meta = Process#syn_groups_table.meta,
  206. %% log
  207. case Reason of
  208. normal -> ok;
  209. killed -> ok;
  210. _ ->
  211. error_logger:error_msg("Process of group ~p and pid ~p exited with reason: ~p", [Name, Pid, Reason])
  212. end,
  213. %% delete from table
  214. remove_process(Process),
  215. %% callback in separate process
  216. case ProcessExitCallbackModule of
  217. undefined ->
  218. ok;
  219. _ ->
  220. spawn(fun() ->
  221. ProcessExitCallbackModule:ProcessExitCallbackFunction(Name, Pid, Meta, Reason)
  222. end)
  223. end
  224. end,
  225. lists:foreach(F, Processes)
  226. end,
  227. %% return
  228. {noreply, State};
  229. handle_info(Info, State) ->
  230. error_logger:warning_msg("Received an unknown info message: ~p", [Info]),
  231. {noreply, State}.
  232. %% ----------------------------------------------------------------------------------------------------------
  233. %% Terminate
  234. %% ----------------------------------------------------------------------------------------------------------
  235. -spec terminate(Reason :: any(), #state{}) -> terminated.
  236. terminate(Reason, _State) ->
  237. error_logger:info_msg("Terminating syn_groups with reason: ~p", [Reason]),
  238. terminated.
  239. %% ----------------------------------------------------------------------------------------------------------
  240. %% Convert process state when code is changed.
  241. %% ----------------------------------------------------------------------------------------------------------
  242. -spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
  243. code_change(_OldVsn, State, _Extra) ->
  244. {ok, State}.
  245. %% ===================================================================
  246. %% Internal
  247. %% ===================================================================
  248. -spec find_by_pid_and_name(Pid :: pid(), Name :: any()) -> Process :: #syn_groups_table{} | undefined.
  249. find_by_pid_and_name(Pid, Name) when is_tuple(Name) ->
  250. i_find_by_pid_and_name(Pid, {'==', '$1', {Name}});
  251. find_by_pid_and_name(Pid, Name) ->
  252. i_find_by_pid_and_name(Pid, {'=:=', '$1', Name}).
  253. -spec i_find_by_pid_and_name(Pid :: pid(), NameGuard :: any()) -> Process :: #syn_groups_table{} | undefined.
  254. i_find_by_pid_and_name(Pid, NameGuard) ->
  255. %% build match specs
  256. MatchHead = #syn_groups_table{name = '$1', pid = '$2', _ = '_'},
  257. Guards = [NameGuard, {'=:=', '$2', Pid}],
  258. Result = '$_',
  259. %% select
  260. case mnesia:dirty_select(syn_groups_table, [{MatchHead, Guards, [Result]}]) of
  261. [] -> undefined;
  262. [Process] -> Process
  263. end.
  264. -spec i_member(Pid :: pid(), Name :: any()) -> boolean().
  265. i_member(Pid, Name) ->
  266. case find_by_pid_and_name(Pid, Name) of
  267. undefined -> false;
  268. _ -> true
  269. end.
  270. -spec i_get_members(Name :: any()) -> [pid()].
  271. i_get_members(Name) ->
  272. Processes = mnesia:dirty_read(syn_groups_table, Name),
  273. Pids = lists:map(fun(Process) ->
  274. Process#syn_groups_table.pid
  275. end, Processes),
  276. lists:sort(Pids).
  277. -spec i_get_members(Name :: any(), with_meta) -> [{pid(), Meta :: any()}].
  278. i_get_members(Name, with_meta) ->
  279. Processes = mnesia:dirty_read(syn_groups_table, Name),
  280. PidsWithMeta = lists:map(fun(Process) ->
  281. {Process#syn_groups_table.pid, Process#syn_groups_table.meta}
  282. end, Processes),
  283. lists:keysort(1, PidsWithMeta).
  284. -spec find_groups_by_pid(Pid :: pid()) -> [Process :: #syn_groups_table{}].
  285. find_groups_by_pid(Pid) ->
  286. mnesia:dirty_index_read(syn_groups_table, Pid, #syn_groups_table.pid).
  287. -spec remove_process(Process :: #syn_groups_table{}) -> ok.
  288. remove_process(Process) ->
  289. mnesia:dirty_delete_object(syn_groups_table, Process).
  290. -spec multi_call_and_receive(
  291. CollectorPid :: pid(),
  292. Pid :: pid(),
  293. Message :: any(),
  294. Timeout :: non_neg_integer()
  295. ) -> any().
  296. multi_call_and_receive(CollectorPid, Pid, Message, Timeout) ->
  297. MonitorRef = monitor(process, Pid),
  298. Pid ! {syn_multi_call, self(), Message},
  299. receive
  300. {syn_multi_call_reply, Pid, Reply} ->
  301. CollectorPid ! {reply, Pid, Reply};
  302. {'DOWN', MonitorRef, _, _, _} ->
  303. CollectorPid ! {bad_pid, Pid}
  304. after Timeout ->
  305. CollectorPid ! {bad_pid, Pid}
  306. end.
  307. -spec collect_replies(MemberPids :: [pid()]) -> {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  308. collect_replies(MemberPids) ->
  309. collect_replies(MemberPids, [], []).
  310. -spec collect_replies(MemberPids :: [pid()], [{pid(), Reply :: any()}], [pid()]) ->
  311. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  312. collect_replies([], Replies, BadPids) -> {Replies, BadPids};
  313. collect_replies(MemberPids, Replies, BadPids) ->
  314. receive
  315. {reply, Pid, Reply} ->
  316. MemberPids1 = lists:delete(Pid, MemberPids),
  317. collect_replies(MemberPids1, [{Pid, Reply} | Replies], BadPids);
  318. {bad_pid, Pid} ->
  319. MemberPids1 = lists:delete(Pid, MemberPids),
  320. collect_replies(MemberPids1, Replies, [Pid | BadPids])
  321. end.