[erlang-questions] noob gen_leader question

Ulf Wiger (TN/EAB) <>
Mon Apr 7 22:46:19 CEST 2008


db skrev:
> I have looked at gdict example.  Storing values into global dict goes to 
> the leader and lookup is done by workers on the local gen_leader instance.
> 
> It seems gen_leader works by having worker pull task rather than master 
> push task.  Am I right?

No, the leader pushes the whole dictionary when elected, and
then each update function, as all updates go through the
leader process.

So, for example (expanding the macro definition):

-define(store(Dict,Expr,Legend),
         gen_leader:leader_call(Dict, {store, fun(D) ->
                                                      Expr
                                              end})).

-define(lookup(Dict, Expr, Legend),
         gen_leader:call(Dict, {lookup, fun(D) ->
                                                Expr
                                        end})).

%% dict functions that modify state:
append(Key, Value, Dict) ->
     gen_leader:leader_call(
         Dict, {store, fun(D) ->
                       dict:append(Key,Value,D) 

                end})).

then, in the callback module:

handle_leader_call({store,F}, _From, Dict, _E) ->
     NewDict = F(Dict),
     {reply, ok, {store, F}, NewDict};
                   ^^^^ <- this is the push


and here's where the workers receive it:

from_leader({store,F}, Dict, _E) ->
     NewDict = F(Dict),
     {ok, NewDict}.



BR,
Ulf W



More information about the erlang-questions mailing list