Skip to content

Commit

Permalink
first stab at handler supervision with dedicated supervisor and guard…
Browse files Browse the repository at this point in the history
… processes
  • Loading branch information
argv0 committed Mar 30, 2011
1 parent 1519f61 commit 1b9709b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ebin/riak_core.app
Expand Up @@ -23,6 +23,8 @@
riak_core_claim,
riak_core_config,
riak_core_gossip,
riak_core_handler_guard,
riak_core_handler_sup,
riak_core_handoff_listener,
riak_core_handoff_manager,
riak_core_handoff_receiver,
Expand Down
2 changes: 1 addition & 1 deletion src/riak_core_app.erl
Expand Up @@ -68,7 +68,7 @@ start(_StartType, _StartArgs) ->
%% Spin up the supervisor; prune ring files as necessary
case riak_core_sup:start_link() of
{ok, Pid} ->
ok = riak_core_ring_events:add_handler(riak_core_ring_handler, []),
ok = riak_core_ring_events:add_sup_handler(riak_core_ring_handler, []),
%% App is running; search for latest ring file and initialize with it
riak_core_ring_manager:prune_ringfiles(),
case riak_core_ring_manager:find_latest_ringfile() of
Expand Down
48 changes: 48 additions & 0 deletions src/riak_core_handler_guard.erl
@@ -0,0 +1,48 @@
%% -------------------------------------------------------------------
%%
%% riak_core_handler_guard: Guard process for persistent event handlers.
%%
%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you 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
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(riak_core_handler_guard).
-behaviour(gen_server).
-export([start_link/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {}).

start_link(HandlerMod, Handler, Args) ->
gen_server:start_link(?MODULE, [HandlerMod, Handler, Args], []).

init([HandlerMod, Handler, Args]) ->
ok = gen_event:add_sup_handler(HandlerMod, Handler, Args),
{ok, #state{}}.

handle_call(_Request, _From, State) -> {reply, ok, State}.

handle_cast(_Msg, State) -> {noreply, State}.

handle_info({gen_event_EXIT, Handler, Reason}, State) ->
error_logger:error_msg("~w: ~s: handler ~w exited for reason ~s",
[self(), ?MODULE, Handler, Reason]),
{stop, gen_event_EXIT, State}.

terminate(_Reason, _State) -> ok.

code_change(_OldVsn, State, _Extra) -> {ok, State}.

47 changes: 47 additions & 0 deletions src/riak_core_handler_sup.erl
@@ -0,0 +1,47 @@
%% -------------------------------------------------------------------
%%
%% riak_core_handler_sup: supervise minder processes for gen_event handlers
%%
%% Copyright (c) 2007-2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you 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
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc supervise riak_core_handler_guard processes

-module(riak_core_handler_sup).
-behaviour(supervisor).
-export([start_link/0, init/1]).
-export([start_handler_guard/3]).

start_handler_guard(HandlerMod, Handler, Args) ->
case supervisor:start_child(?MODULE, handler_spec(HandlerMod, Handler, Args)) of
{ok, _Pid} -> ok;
Other -> Other
end.

handler_spec(HandlerMod, Handler, Args) ->
{{HandlerMod, Handler},
{riak_core_handler_guard, start_link, [HandlerMod, Handler, Args]},
permanent, 5000, worker, [riak_core_handler_guard]}.

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% @private
init([]) ->
{ok, {{one_for_one, 10, 10}, []}}.

4 changes: 2 additions & 2 deletions src/riak_core_node_watcher_events.erl
Expand Up @@ -48,13 +48,13 @@ add_handler(Handler, Args) ->
gen_event:add_handler(?MODULE, Handler, Args).

add_sup_handler(Handler, Args) ->
gen_event:add_sup_handler(?MODULE, Handler, Args).
riak_core_handler_sup:start_handler_guard(?MODULE, Handler, Args).

add_callback(Fn) when is_function(Fn) ->
gen_event:add_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).

add_sup_callback(Fn) when is_function(Fn) ->
gen_event:add_sup_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).
riak_core_handler_sup:start_handler_guard(?MODULE, {?MODULE, make_ref()}, [Fn]).

service_update(Services) ->
gen_event:notify(?MODULE, {service_update, Services}).
Expand Down
4 changes: 2 additions & 2 deletions src/riak_core_ring_events.erl
Expand Up @@ -51,13 +51,13 @@ add_handler(Handler, Args) ->
gen_event:add_handler(?MODULE, Handler, Args).

add_sup_handler(Handler, Args) ->
gen_event:add_sup_handler(?MODULE, Handler, Args).
riak_core_handler_sup:start_handler_guard(?MODULE, Handler, Args).

add_callback(Fn) when is_function(Fn) ->
gen_event:add_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).

add_sup_callback(Fn) when is_function(Fn) ->
gen_event:add_sup_handler(?MODULE, {?MODULE, make_ref()}, [Fn]).
riak_core_handler_sup:start_handler_guard(?MODULE, {?MODULE, make_ref()}, [Fn]).

force_update() ->
{ok, Ring} = riak_core_ring_manager:get_my_ring(),
Expand Down
1 change: 1 addition & 0 deletions src/riak_core_sup.erl
Expand Up @@ -59,6 +59,7 @@ init([]) ->
Children = lists:flatten(
[?CHILD(riak_core_sysmon_minder, worker),
?CHILD(riak_core_vnode_sup, supervisor),
?CHILD(riak_core_handler_sup, supervisor),
?CHILD(riak_core_handoff_manager, worker),
?CHILD(riak_core_handoff_listener, worker),
?CHILD(riak_core_ring_events, worker),
Expand Down

0 comments on commit 1b9709b

Please sign in to comment.