syn_groups.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_groups).
  27. -behaviour(gen_server).
  28. %% API
  29. -export([start_link/0]).
  30. -export([join/2, join/3]).
  31. -export([leave/2]).
  32. -export([get_members/1, get_members/2]).
  33. -export([member/2]).
  34. -export([get_local_members/1, get_local_members/2]).
  35. -export([local_member/2]).
  36. -export([publish/2]).
  37. -export([publish_to_local/2]).
  38. -export([multi_call/2, multi_call/3, multi_call_reply/2]).
  39. %% sync API
  40. -export([sync_get_local_group_tuples/1]).
  41. -export([add_to_local_table/4]).
  42. -export([remove_from_local_table/2]).
  43. %% gen_server callbacks
  44. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  45. %% internal
  46. -export([multi_call_and_receive/4]).
  47. %% records
  48. -record(state, {
  49. custom_event_handler = undefined :: module()
  50. }).
  51. %% macros
  52. -define(DEFAULT_MULTI_CALL_TIMEOUT_MS, 5000).
  53. %% includes
  54. -include("syn.hrl").
  55. %% ===================================================================
  56. %% API
  57. %% ===================================================================
  58. -spec start_link() -> {ok, pid()} | {error, any()}.
  59. start_link() ->
  60. Options = [],
  61. gen_server:start_link({local, ?MODULE}, ?MODULE, [], Options).
  62. -spec join(GroupName :: any(), Pid :: pid()) -> ok.
  63. join(GroupName, Pid) ->
  64. join(GroupName, Pid, undefined).
  65. -spec join(GroupName :: any(), Pid :: pid(), Meta :: any()) -> ok.
  66. join(GroupName, Pid, Meta) when is_pid(Pid) ->
  67. Node = node(Pid),
  68. gen_server:call({?MODULE, Node}, {join_on_node, GroupName, Pid, Meta}).
  69. -spec leave(GroupName :: any(), Pid :: pid()) -> ok | {error, Reason :: any()}.
  70. leave(GroupName, Pid) ->
  71. case find_process_entry_by_name_and_pid(GroupName, Pid) of
  72. undefined ->
  73. {error, not_in_group};
  74. _ ->
  75. Node = node(Pid),
  76. gen_server:call({?MODULE, Node}, {leave_on_node, GroupName, Pid})
  77. end.
  78. -spec get_members(Name :: any()) -> [pid()].
  79. get_members(GroupName) ->
  80. Entries = mnesia:dirty_read(syn_groups_table, GroupName),
  81. Pids = [Entry#syn_groups_table.pid || Entry <- Entries],
  82. lists:sort(Pids).
  83. -spec get_members(GroupName :: any(), with_meta) -> [{pid(), Meta :: any()}].
  84. get_members(GroupName, with_meta) ->
  85. Entries = mnesia:dirty_read(syn_groups_table, GroupName),
  86. Pids = [{Entry#syn_groups_table.pid, Entry#syn_groups_table.meta} || Entry <- Entries],
  87. lists:sort(Pids).
  88. -spec member(Pid :: pid(), GroupName :: any()) -> boolean().
  89. member(Pid, GroupName) ->
  90. case find_process_entry_by_name_and_pid(GroupName, Pid) of
  91. undefined -> false;
  92. _ -> true
  93. end.
  94. -spec get_local_members(Name :: any()) -> [pid()].
  95. get_local_members(GroupName) ->
  96. %% build name guard
  97. NameGuard = case is_tuple(GroupName) of
  98. true -> {'==', '$1', {GroupName}};
  99. _ -> {'=:=', '$1', GroupName}
  100. end,
  101. %% build match specs
  102. MatchHead = #syn_groups_table{name = '$1', node = '$2', pid = '$3', _ = '_'},
  103. Guards = [NameGuard, {'=:=', '$2', node()}],
  104. Result = '$3',
  105. %% select
  106. Pids = mnesia:dirty_select(syn_groups_table, [{MatchHead, Guards, [Result]}]),
  107. lists:sort(Pids).
  108. -spec get_local_members(GroupName :: any(), with_meta) -> [{pid(), Meta :: any()}].
  109. get_local_members(GroupName, with_meta) ->
  110. %% build name guard
  111. NameGuard = case is_tuple(GroupName) of
  112. true -> {'==', '$1', {GroupName}};
  113. _ -> {'=:=', '$1', GroupName}
  114. end,
  115. %% build match specs
  116. MatchHead = #syn_groups_table{name = '$1', node = '$2', pid = '$3', meta = '$4', _ = '_'},
  117. Guards = [NameGuard, {'=:=', '$2', node()}],
  118. Result = {{'$3', '$4'}},
  119. %% select
  120. PidsWithMeta = mnesia:dirty_select(syn_groups_table, [{MatchHead, Guards, [Result]}]),
  121. lists:keysort(1, PidsWithMeta).
  122. -spec local_member(Pid :: pid(), GroupName :: any()) -> boolean().
  123. local_member(Pid, GroupName) ->
  124. case find_process_entry_by_name_and_pid(GroupName, Pid) of
  125. undefined -> false;
  126. Entry when Entry#syn_groups_table.node =:= node() -> true;
  127. _ -> false
  128. end.
  129. -spec publish(GroupName :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  130. publish(GroupName, Message) ->
  131. MemberPids = get_members(GroupName),
  132. FSend = fun(Pid) ->
  133. Pid ! Message
  134. end,
  135. lists:foreach(FSend, MemberPids),
  136. {ok, length(MemberPids)}.
  137. -spec publish_to_local(GroupName :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  138. publish_to_local(GroupName, Message) ->
  139. MemberPids = get_local_members(GroupName),
  140. FSend = fun(Pid) ->
  141. Pid ! Message
  142. end,
  143. lists:foreach(FSend, MemberPids),
  144. {ok, length(MemberPids)}.
  145. -spec multi_call(GroupName :: any(), Message :: any()) -> {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  146. multi_call(GroupName, Message) ->
  147. multi_call(GroupName, Message, ?DEFAULT_MULTI_CALL_TIMEOUT_MS).
  148. -spec multi_call(GroupName :: any(), Message :: any(), Timeout :: non_neg_integer()) ->
  149. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  150. multi_call(GroupName, Message, Timeout) ->
  151. Self = self(),
  152. MemberPids = get_members(GroupName),
  153. FSend = fun(Pid) ->
  154. spawn_link(?MODULE, multi_call_and_receive, [Self, Pid, Message, Timeout])
  155. end,
  156. lists:foreach(FSend, MemberPids),
  157. collect_replies(MemberPids).
  158. -spec multi_call_reply(CallerPid :: pid(), Reply :: any()) -> {syn_multi_call_reply, pid(), Reply :: any()}.
  159. multi_call_reply(CallerPid, Reply) ->
  160. CallerPid ! {syn_multi_call_reply, self(), Reply}.
  161. -spec sync_get_local_group_tuples(FromNode :: node()) -> list(syn_group_tuple()).
  162. sync_get_local_group_tuples(FromNode) ->
  163. error_logger:info_msg("Syn(~p): Received request of local group tuples from remote node: ~p", [node(), FromNode]),
  164. get_group_tuples_for_node(node()).
  165. %% ===================================================================
  166. %% Callbacks
  167. %% ===================================================================
  168. %% ----------------------------------------------------------------------------------------------------------
  169. %% Init
  170. %% ----------------------------------------------------------------------------------------------------------
  171. -spec init([]) ->
  172. {ok, #state{}} |
  173. {ok, #state{}, Timeout :: non_neg_integer()} |
  174. ignore |
  175. {stop, Reason :: any()}.
  176. init([]) ->
  177. %% rebuild
  178. rebuild_monitors(),
  179. %% monitor nodes
  180. ok = net_kernel:monitor_nodes(true),
  181. %% get handler
  182. CustomEventHandler = syn_backbone:get_event_handler_module(),
  183. %% init
  184. {ok, #state{
  185. custom_event_handler = CustomEventHandler
  186. }}.
  187. %% ----------------------------------------------------------------------------------------------------------
  188. %% Call messages
  189. %% ----------------------------------------------------------------------------------------------------------
  190. -spec handle_call(Request :: any(), From :: any(), #state{}) ->
  191. {reply, Reply :: any(), #state{}} |
  192. {reply, Reply :: any(), #state{}, Timeout :: non_neg_integer()} |
  193. {noreply, #state{}} |
  194. {noreply, #state{}, Timeout :: non_neg_integer()} |
  195. {stop, Reason :: any(), Reply :: any(), #state{}} |
  196. {stop, Reason :: any(), #state{}}.
  197. handle_call({join_on_node, GroupName, Pid, Meta}, _From, State) ->
  198. %% check if pid is alive
  199. case is_process_alive(Pid) of
  200. true ->
  201. join_on_node(GroupName, Pid, Meta),
  202. %% multicast
  203. multicast_join(GroupName, Pid, Meta),
  204. %% return
  205. {reply, ok, State};
  206. _ ->
  207. {reply, {error, not_alive}, State}
  208. end;
  209. handle_call({leave_on_node, GroupName, Pid}, _From, State) ->
  210. case leave_on_node(GroupName, Pid) of
  211. ok ->
  212. %% multicast
  213. multicast_leave(GroupName, Pid),
  214. %% return
  215. {reply, ok, State};
  216. {error, Reason} ->
  217. %% return
  218. {reply, {error, Reason}, State}
  219. end;
  220. handle_call(Request, From, State) ->
  221. error_logger:warning_msg("Syn(~p): Received from ~p an unknown call message: ~p", [node(), Request, From]),
  222. {reply, undefined, State}.
  223. %% ----------------------------------------------------------------------------------------------------------
  224. %% Cast messages
  225. %% ----------------------------------------------------------------------------------------------------------
  226. -spec handle_cast(Msg :: any(), #state{}) ->
  227. {noreply, #state{}} |
  228. {noreply, #state{}, Timeout :: non_neg_integer()} |
  229. {stop, Reason :: any(), #state{}}.
  230. handle_cast(Msg, State) ->
  231. error_logger:warning_msg("Syn(~p): Received an unknown cast message: ~p", [node(), Msg]),
  232. {noreply, State}.
  233. %% ----------------------------------------------------------------------------------------------------------
  234. %% All non Call / Cast messages
  235. %% ----------------------------------------------------------------------------------------------------------
  236. -spec handle_info(Info :: any(), #state{}) ->
  237. {noreply, #state{}} |
  238. {noreply, #state{}, Timeout :: non_neg_integer()} |
  239. {stop, Reason :: any(), #state{}}.
  240. handle_info({'DOWN', _MonitorRef, process, Pid, Reason}, State) ->
  241. case find_processes_entry_by_pid(Pid) of
  242. [] ->
  243. %% handle
  244. handle_process_down(undefined, Pid, undefined, Reason, State);
  245. Entries ->
  246. lists:foreach(fun(Entry) ->
  247. %% get process info
  248. GroupName = Entry#syn_groups_table.name,
  249. Meta = Entry#syn_groups_table.meta,
  250. %% handle
  251. handle_process_down(GroupName, Pid, Meta, Reason, State),
  252. %% remove from table
  253. remove_from_local_table(Entry),
  254. %% multicast
  255. multicast_leave(GroupName, Pid)
  256. end, Entries)
  257. end,
  258. %% return
  259. {noreply, State};
  260. handle_info({nodeup, RemoteNode}, State) ->
  261. error_logger:info_msg("Syn(~p): Node ~p has joined the cluster", [node(), RemoteNode]),
  262. global:trans({{?MODULE, auto_merge_groups}, self()},
  263. fun() ->
  264. error_logger:warning_msg("Syn(~p): GROUPS AUTOMERGE ----> Initiating for remote node ~p", [node(), RemoteNode]),
  265. %% get group tuples from remote node
  266. GroupTuples = rpc:call(RemoteNode, ?MODULE, sync_get_local_group_tuples, [node()]),
  267. error_logger:warning_msg(
  268. "Syn(~p): Received ~p group entrie(s) from remote node ~p",
  269. [node(), length(GroupTuples), RemoteNode]
  270. ),
  271. write_group_tuples_for_node(GroupTuples, RemoteNode),
  272. %% exit
  273. error_logger:warning_msg("Syn(~p): GROUPS AUTOMERGE <---- Done for remote node ~p", [node(), RemoteNode])
  274. end
  275. ),
  276. %% resume
  277. {noreply, State};
  278. handle_info({nodedown, RemoteNode}, State) ->
  279. error_logger:warning_msg("Syn(~p): Node ~p has left the cluster, removing group entries on local", [node(), RemoteNode]),
  280. raw_purge_group_entries_for_node(RemoteNode),
  281. {noreply, State};
  282. handle_info(Info, State) ->
  283. error_logger:warning_msg("Syn(~p): Received an unknown info message: ~p", [node(), Info]),
  284. {noreply, State}.
  285. %% ----------------------------------------------------------------------------------------------------------
  286. %% Terminate
  287. %% ----------------------------------------------------------------------------------------------------------
  288. -spec terminate(Reason :: any(), #state{}) -> terminated.
  289. terminate(Reason, _State) ->
  290. error_logger:info_msg("Syn(~p): Terminating with reason: ~p", [node(), Reason]),
  291. terminated.
  292. %% ----------------------------------------------------------------------------------------------------------
  293. %% Convert process state when code is changed.
  294. %% ----------------------------------------------------------------------------------------------------------
  295. -spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
  296. code_change(_OldVsn, State, _Extra) ->
  297. {ok, State}.
  298. %% ===================================================================
  299. %% Internal
  300. %% ===================================================================
  301. -spec multicast_join(GroupName :: any(), Pid :: pid(), Meta :: any()) -> pid().
  302. multicast_join(GroupName, Pid, Meta) ->
  303. spawn_link(fun() ->
  304. rpc:eval_everywhere(nodes(), ?MODULE, add_to_local_table, [GroupName, Pid, Meta, undefined])
  305. end).
  306. -spec multicast_leave(GroupName :: any(), Pid :: pid()) -> pid().
  307. multicast_leave(GroupName, Pid) ->
  308. spawn_link(fun() ->
  309. rpc:eval_everywhere(nodes(), ?MODULE, remove_from_local_table, [GroupName, Pid])
  310. end).
  311. -spec join_on_node(GroupName :: any(), Pid :: pid(), Meta :: any()) -> ok.
  312. join_on_node(GroupName, Pid, Meta) ->
  313. MonitorRef = case find_processes_entry_by_pid(Pid) of
  314. [] ->
  315. %% process is not monitored yet, add
  316. erlang:monitor(process, Pid);
  317. [Entry | _] ->
  318. Entry#syn_groups_table.monitor_ref
  319. end,
  320. %% add to table
  321. add_to_local_table(GroupName, Pid, Meta, MonitorRef).
  322. -spec leave_on_node(GroupName :: any(), Pid :: pid()) -> ok | {error, Reason :: any()}.
  323. leave_on_node(GroupName, Pid) ->
  324. case find_process_entry_by_name_and_pid(GroupName, Pid) of
  325. undefined ->
  326. {error, not_in_group};
  327. Entry when Entry#syn_groups_table.monitor_ref =/= undefined ->
  328. %% is this the last group process is in?
  329. case find_processes_entry_by_pid(Pid) of
  330. [Entry] ->
  331. %% demonitor
  332. erlang:demonitor(Entry#syn_groups_table.monitor_ref);
  333. _ ->
  334. ok
  335. end,
  336. %% remove from table
  337. remove_from_local_table(Entry)
  338. end.
  339. -spec add_to_local_table(GroupName :: any(), Pid :: pid(), Meta :: any(), MonitorRef :: undefined | reference()) -> ok.
  340. add_to_local_table(GroupName, Pid, Meta, MonitorRef) ->
  341. %% clean if any
  342. remove_from_local_table(GroupName, Pid),
  343. %% write
  344. mnesia:dirty_write(#syn_groups_table{
  345. name = GroupName,
  346. pid = Pid,
  347. node = node(Pid),
  348. meta = Meta,
  349. monitor_ref = MonitorRef
  350. }).
  351. -spec remove_from_local_table(GroupName :: any(), Pid :: pid()) -> ok | {error, Reason :: any()}.
  352. remove_from_local_table(GroupName, Pid) ->
  353. case find_process_entry_by_name_and_pid(GroupName, Pid) of
  354. undefined ->
  355. {error, not_in_group};
  356. Entry ->
  357. %% remove from table
  358. remove_from_local_table(Entry)
  359. end.
  360. -spec remove_from_local_table(Entry :: #syn_groups_table{}) -> ok.
  361. remove_from_local_table(Entry) ->
  362. mnesia:dirty_delete_object(syn_groups_table, Entry).
  363. -spec find_processes_entry_by_pid(Pid :: pid()) -> Entries :: list(#syn_groups_table{}).
  364. find_processes_entry_by_pid(Pid) when is_pid(Pid) ->
  365. mnesia:dirty_index_read(syn_groups_table, Pid, #syn_groups_table.pid).
  366. -spec find_process_entry_by_name_and_pid(GroupName :: any(), Pid :: pid()) -> Entry :: #syn_groups_table{} | undefined.
  367. find_process_entry_by_name_and_pid(GroupName, Pid) ->
  368. %% build match specs
  369. MatchHead = #syn_groups_table{name = GroupName, pid = Pid, _ = '_'},
  370. Guards = [],
  371. Result = '$_',
  372. %% select
  373. case mnesia:dirty_select(syn_groups_table, [{MatchHead, Guards, [Result]}]) of
  374. [Entry] -> Entry;
  375. [] -> undefined
  376. end.
  377. -spec get_group_tuples_for_node(Node :: node()) -> [syn_group_tuple()].
  378. get_group_tuples_for_node(Node) ->
  379. %% build match specs
  380. MatchHead = #syn_groups_table{name = '$1', pid = '$2', node = '$3', meta = '$4', _ = '_'},
  381. Guard = {'=:=', '$3', Node},
  382. GroupTupleFormat = {{'$1', '$2', '$4'}},
  383. %% select
  384. mnesia:dirty_select(syn_groups_table, [{MatchHead, [Guard], [GroupTupleFormat]}]).
  385. -spec handle_process_down(GroupName :: any(), Pid :: pid(), Meta :: any(), Reason :: any(), #state{}) -> ok.
  386. handle_process_down(GroupName, Pid, Meta, Reason, #state{
  387. custom_event_handler = CustomEventHandler
  388. }) ->
  389. case GroupName of
  390. undefined ->
  391. error_logger:warning_msg(
  392. "Syn(~p): Received a DOWN message from an unjoined group process ~p with reason: ~p",
  393. [node(), Pid, Reason]
  394. );
  395. _ ->
  396. syn_event_handler:do_on_group_process_exit(GroupName, Pid, Meta, Reason, CustomEventHandler)
  397. end.
  398. -spec write_group_tuples_for_node(GroupTuples :: [syn_registry_tuple()], RemoteNode :: node()) -> ok.
  399. write_group_tuples_for_node(GroupTuples, RemoteNode) ->
  400. %% ensure that groups doesn't have any joining node's entries
  401. raw_purge_group_entries_for_node(RemoteNode),
  402. %% add
  403. lists:foreach(fun({Name, RemotePid, RemoteMeta}) ->
  404. join_on_node(Name, RemotePid, RemoteMeta)
  405. end, GroupTuples).
  406. -spec raw_purge_group_entries_for_node(Node :: atom()) -> ok.
  407. raw_purge_group_entries_for_node(Node) ->
  408. %% NB: no demonitoring is done, hence why this needs to run for a remote node
  409. %% build match specs
  410. Pattern = #syn_groups_table{node = Node, _ = '_'},
  411. ObjectsToDelete = mnesia:dirty_match_object(syn_groups_table, Pattern),
  412. %% delete
  413. DelF = fun(Record) -> mnesia:dirty_delete_object(syn_groups_table, Record) end,
  414. lists:foreach(DelF, ObjectsToDelete).
  415. -spec multi_call_and_receive(
  416. CollectorPid :: pid(),
  417. Pid :: pid(),
  418. Message :: any(),
  419. Timeout :: non_neg_integer()
  420. ) -> any().
  421. multi_call_and_receive(CollectorPid, Pid, Message, Timeout) ->
  422. MonitorRef = monitor(process, Pid),
  423. Pid ! {syn_multi_call, self(), Message},
  424. receive
  425. {syn_multi_call_reply, Pid, Reply} ->
  426. CollectorPid ! {reply, Pid, Reply};
  427. {'DOWN', MonitorRef, _, _, _} ->
  428. CollectorPid ! {bad_pid, Pid}
  429. after Timeout ->
  430. CollectorPid ! {bad_pid, Pid}
  431. end.
  432. -spec collect_replies(MemberPids :: [pid()]) -> {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  433. collect_replies(MemberPids) ->
  434. collect_replies(MemberPids, [], []).
  435. -spec collect_replies(MemberPids :: [pid()], [{pid(), Reply :: any()}], [pid()]) ->
  436. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  437. collect_replies([], Replies, BadPids) -> {Replies, BadPids};
  438. collect_replies(MemberPids, Replies, BadPids) ->
  439. receive
  440. {reply, Pid, Reply} ->
  441. MemberPids1 = lists:delete(Pid, MemberPids),
  442. collect_replies(MemberPids1, [{Pid, Reply} | Replies], BadPids);
  443. {bad_pid, Pid} ->
  444. MemberPids1 = lists:delete(Pid, MemberPids),
  445. collect_replies(MemberPids1, Replies, [Pid | BadPids])
  446. end.
  447. -spec rebuild_monitors() -> ok.
  448. rebuild_monitors() ->
  449. GroupTuples = get_group_tuples_for_node(node()),
  450. %% remove all
  451. write_group_tuples_for_node(GroupTuples, node()).