syn_groups.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. {error, pid_not_in_group};
  159. Process ->
  160. %% remove from table
  161. remove_process(Process),
  162. %% unlink only when process is no more in groups
  163. case find_groups_by_pid(Pid) of
  164. [] -> erlang:unlink(Pid);
  165. _ -> nop
  166. end,
  167. %% reply
  168. {reply, ok, State}
  169. end;
  170. handle_call(Request, From, State) ->
  171. error_logger:warning_msg("Received from ~p an unknown call message: ~p", [Request, From]),
  172. {reply, undefined, State}.
  173. %% ----------------------------------------------------------------------------------------------------------
  174. %% Cast messages
  175. %% ----------------------------------------------------------------------------------------------------------
  176. -spec handle_cast(Msg :: any(), #state{}) ->
  177. {noreply, #state{}} |
  178. {noreply, #state{}, Timeout :: non_neg_integer()} |
  179. {stop, Reason :: any(), #state{}}.
  180. handle_cast(Msg, State) ->
  181. error_logger:warning_msg("Received an unknown cast message: ~p", [Msg]),
  182. {noreply, State}.
  183. %% ----------------------------------------------------------------------------------------------------------
  184. %% All non Call / Cast messages
  185. %% ----------------------------------------------------------------------------------------------------------
  186. -spec handle_info(Info :: any(), #state{}) ->
  187. {noreply, #state{}} |
  188. {noreply, #state{}, Timeout :: non_neg_integer()} |
  189. {stop, Reason :: any(), #state{}}.
  190. handle_info({'EXIT', Pid, Reason}, #state{
  191. process_groups_process_exit_callback_module = ProcessExitCallbackModule,
  192. process_groups_process_exit_callback_function = ProcessExitCallbackFunction
  193. } = State) ->
  194. %% check if pid is in table
  195. case find_groups_by_pid(Pid) of
  196. [] ->
  197. %% log
  198. case Reason of
  199. normal -> ok;
  200. killed -> ok;
  201. _ ->
  202. error_logger:error_msg("Received an exit message from an unlinked process ~p with reason: ~p", [Pid, Reason])
  203. end;
  204. Processes ->
  205. F = fun(Process) ->
  206. %% get group & meta
  207. Name = Process#syn_groups_table.name,
  208. Meta = Process#syn_groups_table.meta,
  209. %% log
  210. case Reason of
  211. normal -> ok;
  212. killed -> ok;
  213. _ ->
  214. error_logger:error_msg("Process of group ~p and pid ~p exited with reason: ~p", [Name, Pid, Reason])
  215. end,
  216. %% delete from table
  217. remove_process(Process),
  218. %% callback in separate process
  219. case ProcessExitCallbackModule of
  220. undefined ->
  221. ok;
  222. _ ->
  223. spawn(fun() ->
  224. ProcessExitCallbackModule:ProcessExitCallbackFunction(Name, Pid, Meta, Reason)
  225. end)
  226. end
  227. end,
  228. lists:foreach(F, Processes)
  229. end,
  230. %% return
  231. {noreply, State};
  232. handle_info(Info, State) ->
  233. error_logger:warning_msg("Received an unknown info message: ~p", [Info]),
  234. {noreply, State}.
  235. %% ----------------------------------------------------------------------------------------------------------
  236. %% Terminate
  237. %% ----------------------------------------------------------------------------------------------------------
  238. -spec terminate(Reason :: any(), #state{}) -> terminated.
  239. terminate(Reason, _State) ->
  240. error_logger:info_msg("Terminating syn_groups with reason: ~p", [Reason]),
  241. terminated.
  242. %% ----------------------------------------------------------------------------------------------------------
  243. %% Convert process state when code is changed.
  244. %% ----------------------------------------------------------------------------------------------------------
  245. -spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
  246. code_change(_OldVsn, State, _Extra) ->
  247. {ok, State}.
  248. %% ===================================================================
  249. %% Internal
  250. %% ===================================================================
  251. -spec find_by_pid_and_name(Pid :: pid(), Name :: any()) -> Process :: #syn_groups_table{} | undefined.
  252. find_by_pid_and_name(Pid, Name) when is_tuple(Name) ->
  253. i_find_by_pid_and_name(Pid, {'==', '$1', {Name}});
  254. find_by_pid_and_name(Pid, Name) ->
  255. i_find_by_pid_and_name(Pid, {'=:=', '$1', Name}).
  256. -spec i_find_by_pid_and_name(Pid :: pid(), NameGuard :: any()) -> Process :: #syn_groups_table{} | undefined.
  257. i_find_by_pid_and_name(Pid, NameGuard) ->
  258. %% build match specs
  259. MatchHead = #syn_groups_table{name = '$1', pid = '$2', _ = '_'},
  260. Guards = [NameGuard, {'=:=', '$2', Pid}],
  261. Result = '$_',
  262. %% select
  263. case mnesia:dirty_select(syn_groups_table, [{MatchHead, Guards, [Result]}]) of
  264. [] -> undefined;
  265. [Process] -> Process
  266. end.
  267. -spec i_member(Pid :: pid(), Name :: any()) -> boolean().
  268. i_member(Pid, Name) ->
  269. case find_by_pid_and_name(Pid, Name) of
  270. undefined -> false;
  271. _ -> true
  272. end.
  273. -spec i_get_members(Name :: any()) -> [pid()].
  274. i_get_members(Name) ->
  275. Processes = mnesia:dirty_read(syn_groups_table, Name),
  276. Pids = lists:map(fun(Process) ->
  277. Process#syn_groups_table.pid
  278. end, Processes),
  279. lists:sort(Pids).
  280. -spec i_get_members(Name :: any(), with_meta) -> [{pid(), Meta :: any()}].
  281. i_get_members(Name, with_meta) ->
  282. Processes = mnesia:dirty_read(syn_groups_table, Name),
  283. PidsWithMeta = lists:map(fun(Process) ->
  284. {Process#syn_groups_table.pid, Process#syn_groups_table.meta}
  285. end, Processes),
  286. lists:keysort(1, PidsWithMeta).
  287. -spec find_groups_by_pid(Pid :: pid()) -> [Process :: #syn_groups_table{}].
  288. find_groups_by_pid(Pid) ->
  289. mnesia:dirty_index_read(syn_groups_table, Pid, #syn_groups_table.pid).
  290. -spec remove_process(Process :: #syn_groups_table{}) -> ok.
  291. remove_process(Process) ->
  292. mnesia:dirty_delete_object(syn_groups_table, Process).
  293. -spec multi_call_and_receive(
  294. CollectorPid :: pid(),
  295. Pid :: pid(),
  296. Message :: any(),
  297. Timeout :: non_neg_integer()
  298. ) -> any().
  299. multi_call_and_receive(CollectorPid, Pid, Message, Timeout) ->
  300. MonitorRef = monitor(process, Pid),
  301. Pid ! {syn_multi_call, self(), Message},
  302. receive
  303. {syn_multi_call_reply, Pid, Reply} ->
  304. CollectorPid ! {reply, Pid, Reply};
  305. {'DOWN', MonitorRef, _, _, _} ->
  306. CollectorPid ! {bad_pid, Pid}
  307. after Timeout ->
  308. CollectorPid ! {bad_pid, Pid}
  309. end.
  310. -spec collect_replies(MemberPids :: [pid()]) -> {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  311. collect_replies(MemberPids) ->
  312. collect_replies(MemberPids, [], []).
  313. -spec collect_replies(MemberPids :: [pid()], [{pid(), Reply :: any()}], [pid()]) ->
  314. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  315. collect_replies([], Replies, BadPids) -> {Replies, BadPids};
  316. collect_replies(MemberPids, Replies, BadPids) ->
  317. receive
  318. {reply, Pid, Reply} ->
  319. MemberPids1 = lists:delete(Pid, MemberPids),
  320. collect_replies(MemberPids1, [{Pid, Reply} | Replies], BadPids);
  321. {bad_pid, Pid} ->
  322. MemberPids1 = lists:delete(Pid, MemberPids),
  323. collect_replies(MemberPids1, Replies, [Pid | BadPids])
  324. end.