From 077231a02a7872a10a4cb642e493718c0be21059 Mon Sep 17 00:00:00 2001 From: Jeffrey Massung Date: Fri, 18 Nov 2011 13:49:48 -0700 Subject: [PATCH] Cache handoffs in the manager so we can query and see which ones are active. --- src/riak_core_handoff_manager.erl | 38 ++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/riak_core_handoff_manager.erl b/src/riak_core_handoff_manager.erl index b67e44065..33a2f15f3 100644 --- a/src/riak_core_handoff_manager.erl +++ b/src/riak_core_handoff_manager.erl @@ -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}}). @@ -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}) ->