diff options
Diffstat (limited to 'src/rexi/src/rexi_server_mon.erl')
-rw-r--r-- | src/rexi/src/rexi_server_mon.erl | 54 |
1 files changed, 21 insertions, 33 deletions
diff --git a/src/rexi/src/rexi_server_mon.erl b/src/rexi/src/rexi_server_mon.erl index cfe1144ce..9057807e6 100644 --- a/src/rexi/src/rexi_server_mon.erl +++ b/src/rexi/src/rexi_server_mon.erl @@ -1,5 +1,5 @@ % Copyright 2010-2013 Cloudant -% +% % Licensed under the Apache License, Version 2.0 (the "License"); you may not % use this file except in compliance with the License. You may obtain a copy of % the License at @@ -17,13 +17,11 @@ -behaviour(mem3_cluster). -vsn(1). - -export([ start_link/1, status/0 ]). - -export([ init/1, terminate/2, @@ -34,23 +32,19 @@ ]). -export([ - cluster_stable/1, - cluster_unstable/1 + cluster_stable/1, + cluster_unstable/1 ]). - -define(CLUSTER_STABILITY_PERIOD_SEC, 15). - start_link(ChildMod) -> Name = list_to_atom(lists:concat([ChildMod, "_mon"])), gen_server:start_link({local, Name}, ?MODULE, ChildMod, []). - status() -> gen_server:call(?MODULE, status). - % Mem3 cluster callbacks cluster_unstable(Server) -> @@ -62,21 +56,22 @@ cluster_stable(Server) -> gen_server:cast(Server, cluster_stable), Server. - % gen_server callbacks init(ChildMod) -> - {ok, _Mem3Cluster} = mem3_cluster:start_link(?MODULE, self(), - ?CLUSTER_STABILITY_PERIOD_SEC, ?CLUSTER_STABILITY_PERIOD_SEC), + {ok, _Mem3Cluster} = mem3_cluster:start_link( + ?MODULE, + self(), + ?CLUSTER_STABILITY_PERIOD_SEC, + ?CLUSTER_STABILITY_PERIOD_SEC + ), start_servers(ChildMod), couch_log:notice("~s : started servers", [ChildMod]), {ok, ChildMod}. - terminate(_Reason, _St) -> ok. - handle_call(status, _From, ChildMod) -> case missing_servers(ChildMod) of [] -> @@ -84,7 +79,6 @@ handle_call(status, _From, ChildMod) -> Missing -> {reply, {waiting, length(Missing)}, ChildMod} end; - handle_call(Msg, _From, St) -> couch_log:notice("~s ignored_call ~w", [?MODULE, Msg]), {reply, ignored, St}. @@ -96,7 +90,6 @@ handle_cast(cluster_unstable, ChildMod) -> couch_log:notice("~s : cluster unstable", [ChildMod]), start_servers(ChildMod), {noreply, ChildMod}; - % When cluster is stable, start any servers for new nodes and stop servers for % the ones that disconnected. handle_cast(cluster_stable, ChildMod) -> @@ -104,51 +97,48 @@ handle_cast(cluster_stable, ChildMod) -> start_servers(ChildMod), stop_servers(ChildMod), {noreply, ChildMod}; - handle_cast(Msg, St) -> couch_log:notice("~s ignored_cast ~w", [?MODULE, Msg]), {noreply, St}. - handle_info(Msg, St) -> couch_log:notice("~s ignored_info ~w", [?MODULE, Msg]), {noreply, St}. - code_change(_OldVsn, nil, _Extra) -> {ok, rexi_server}; code_change(_OldVsn, St, _Extra) -> {ok, St}. - start_servers(ChildMod) -> - lists:foreach(fun(Id) -> - {ok, _} = start_server(ChildMod, Id) - end, missing_servers(ChildMod)). + lists:foreach( + fun(Id) -> + {ok, _} = start_server(ChildMod, Id) + end, + missing_servers(ChildMod) + ). stop_servers(ChildMod) -> - lists:foreach(fun(Id) -> - ok = stop_server(ChildMod, Id) - end, extra_servers(ChildMod)). - + lists:foreach( + fun(Id) -> + ok = stop_server(ChildMod, Id) + end, + extra_servers(ChildMod) + ). server_ids(ChildMod) -> Nodes = [node() | nodes()], [list_to_atom(lists:concat([ChildMod, "_", Node])) || Node <- Nodes]. - running_servers(ChildMod) -> [Id || {Id, _, _, _} <- supervisor:which_children(sup_module(ChildMod))]. - missing_servers(ChildMod) -> server_ids(ChildMod) -- running_servers(ChildMod). - extra_servers(ChildMod) -> running_servers(ChildMod) -- server_ids(ChildMod). - start_server(ChildMod, ChildId) -> ChildSpec = { ChildId, @@ -165,12 +155,10 @@ start_server(ChildMod, ChildId) -> erlang:error(Else) end. - stop_server(ChildMod, ChildId) -> SupMod = sup_module(ChildMod), ok = supervisor:terminate_child(SupMod, ChildId), ok = supervisor:delete_child(SupMod, ChildId). - sup_module(ChildMod) -> list_to_atom(lists:concat([ChildMod, "_sup"])). |