Skip to content

Commit

Permalink
Cache handoffs in the manager so we can query and see which ones are …
Browse files Browse the repository at this point in the history
…active.
  • Loading branch information
massung committed Nov 18, 2011
1 parent 5dd8bc2 commit 077231a
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/riak_core_handoff_manager.erl
Expand Up @@ -21,13 +21,26 @@
-export([add_exclusion/2, get_handoff_lock/1, get_exclusions/1]).
-export([remove_exclusion/2]).
-export([release_handoff_lock/2]).
-record(state, {excl}).
-export([add_handoff/3,remove_handoff/2,all_handoffs/0,get_handoffs/1]).
-record(state, {excl,handoffs}).

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

init([]) ->
{ok, #state{excl=ordsets:new()}}.
{ok, #state{excl=ordsets:new(), handoffs=[]}}.

add_handoff(Module, Idx, Node) ->
gen_server:cast(?MODULE, {add_handoff, {Module, Idx, Node}}).

remove_handoff(Module, Idx) ->
gen_server:cast(?MODULE, {remove_handoff, {Module, Idx}}).

all_handoffs() ->
gen_server:call(?MODULE, all_handoffs).

get_handoffs(Idx) ->
gen_server:call(?MODULE, {get_handoffs, Idx}).

add_exclusion(Module, Index) ->
gen_server:cast(?MODULE, {add_exclusion, {Module, Index}}).
Expand Down Expand Up @@ -57,8 +70,27 @@ release_handoff_lock(LockId, Token) ->

handle_call({get_exclusions, Module}, _From, State=#state{excl=Excl}) ->
Reply = [I || {M, I} <- ordsets:to_list(Excl), M =:= Module],
{reply, {ok, Reply}, State}.
{reply, {ok, Reply}, State};
handle_call(all_handoffs, _From, State=#state{handoffs=Hoffs}) ->
{reply, {ok, Hoffs}, State};
handle_call({get_handoffs, Idx}, _From, State=#state{handoffs=Hoffs}) ->
All=[{Mod,dict:find(Idx,ModHoffs)} || {Mod,ModHoffs} <- Hoffs],
Filtered=lists:filter(fun ({_,{ok,_}}) -> true;
(_) -> false
end,
All),
{reply, {ok, [{Mod,Value} || {Mod,{ok,Value}} <- Filtered]}, State}.

handle_cast({add_handoff, {Mod, Idx, Node}}, State=#state{handoffs=Hoffs}) ->
ModHoffs=proplists:get_value(Mod,Hoffs,dict:new()),
NewModHoffs=dict:store(Idx,Node,ModHoffs),
NewHoffs=lists:keystore(Mod,1,Hoffs,{Mod,NewModHoffs}),
{noreply, State#state{handoffs=NewHoffs}};
handle_cast({remove_handoff, {Mod, Idx}}, State=#state{handoffs=Hoffs}) ->
ModHoffs=proplists:get_value(Mod,Hoffs,dict:new()),
NewModHoffs=dict:erase(Idx,ModHoffs),
NewHoffs=lists:keystore(Mod,1,Hoffs,{Mod,NewModHoffs}),
{noreply, State#state{handoffs=NewHoffs}};
handle_cast({del_exclusion, {Mod, Idx}}, State=#state{excl=Excl}) ->
{noreply, State#state{excl=ordsets:del_element({Mod, Idx}, Excl)}};
handle_cast({add_exclusion, {Mod, Idx}}, State=#state{excl=Excl}) ->
Expand Down

0 comments on commit 077231a

Please sign in to comment.