Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:jashmenn/chordjerl
Browse files Browse the repository at this point in the history
  • Loading branch information
jashmenn committed Feb 28, 2009
2 parents ad08785 + 128349e commit 212a687
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
17 changes: 14 additions & 3 deletions include/defines.hrl
@@ -1,14 +1,23 @@
% Includes file for the Chordjerl
-define (DEBUG, true).

-define (DICT, dict).
-define (TRACE(X, M), io:format(user, "TRACE ~p:~p ~p ~p~n", [?MODULE, ?LINE, X, M])).
-define (NTRACE(X, M), io:format(user, "NTRACE ~p:~p ~p ~p ~p ~p ~p~n", [?MODULE, ?LINE, chordjerl_srv:registered_name(), node(), self(), X, M])).
-define (RECONNECT_TIMEOUT, 10000).
%-define (NBIT, 160). % number of bits in the hash function
-define (NBIT, 7). % number of bits in the hash function
-define (MAXFINGERS, ?NBIT). % number of bits in the hash function
-define (NBITMOD, round(math:pow(2, ?NBIT))).

-ifdef (DEBUG).
-define (NBIT, 7). % number of bits in the hash function
-else.
-define (NBIT, 160). % number of bits in the hash function
-endif.

-define (DEFAULT_CONFIG, [
{comm, chordjerl_com}
]).

% may merge finger-like attributes into the srv state. Maybe
% srv_state.finger_of_self?
-record(srv_state, {
Expand All @@ -20,7 +29,9 @@
pid,
ip,
port,
next % the index of the next finger to fix
next, % the index of the next finger to fix
% Other options
comm %
}).

-record(finger, {
Expand Down
22 changes: 22 additions & 0 deletions src/chordjerl.erl
@@ -0,0 +1,22 @@
-module (chordjerl).

-behaviour(application).

%% application callbacks
-export([start/2, stop/1]).
-export ([init/1]).
-export ([lookup/1]).

-define (MAXIMUM_RESTARTS, 10).
-define (MAX_DELAY_TIME, 60).

lookup(Key) -> chordjerl_srv:lookup(Key).

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

init([Config]) ->
ChordjerlServerSup = { chordjerl_srv, {chordjerl_srv,start_link,[Config]}, permanent,2000,worker,[]},
{ok, {_SupFlags = {one_for_one, ?MAXIMUM_RESTARTS, ?MAX_DELAY_TIME}, [ChordjerlServerSup]}}.

stop(State) -> ok.
37 changes: 23 additions & 14 deletions src/chordjerl_srv.erl
Expand Up @@ -12,11 +12,12 @@
%% API
-export([
start/0,
start_link/0,
start_link/1,
start_named/1,
create_ring/0,
join/1,
find_successor/1,
lookup/1,
closest_preceding_node/1,
stabilize/0,
claim_to_be_predecessor/1,
Expand All @@ -41,14 +42,14 @@
%% Description: Alias for start_link
%%--------------------------------------------------------------------
start() ->
start_link().
start_link(?DEFAULT_CONFIG).

%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
start_link(Config) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [Config], []).

%% for testing multiple servers
start_named(Name) ->
Expand All @@ -72,6 +73,7 @@ join(OtherNode) ->
%% Function: find_successor(Id) ->
%% Description: find best/closest known successor of Id
%%--------------------------------------------------------------------
lookup(Id) -> find_successor(Id).
find_successor(Id) ->
gen_server:call(?SERVER, {find_successor, Id}).

Expand Down Expand Up @@ -149,10 +151,17 @@ get_finger_ref() ->
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
init([Config]) ->
[Comm] = config:fetch_or_default_config([comm], Config, ?DEFAULT_CONFIG),
ShaInt = make_sha([]),
{ok, TRef} = timer:send_interval(timer:minutes(60), run_stabilization_tasks), % stub
{ok, #srv_state{sha=ShaInt,pid=self(),predecessor=undefined,next=0,tref=TRef}}.
{ok, #srv_state{
sha=ShaInt,
pid=self(),
predecessor=undefined,
next=0,
tref=TRef,
comm=Comm}}.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
Expand Down Expand Up @@ -270,14 +279,14 @@ handle_create_ring(State) ->
NewState = State#srv_state{predecessor=undefined, fingers=[]},
{ok, NewState}.

handle_join(Finger, State) ->
handle_join(Finger, #srv_state{comm = Comm} = State) ->
% ?NTRACE("handle_join", [{State#srv_state.pid, State#srv_state.sha}, {joining, Finger#finger.pid} ]),
Response = chordjerl_com:send(Finger, {find_successor, State#srv_state.sha}),
Response = Comm:send(Finger, {find_successor, State#srv_state.sha}),
case Response of
{ok, NewFinger} ->
NewFingers = [NewFinger|State#srv_state.fingers],
NewState = State#srv_state{fingers=NewFingers},
_Response = chordjerl_com:send(Finger, {joined_by, make_finger_from_self(State)}), % tell the node we joined it
_Response = Comm:send(Finger, {joined_by, make_finger_from_self(State)}), % tell the node we joined it
{ok, NewState};
_Err ->
?NTRACE("bad response", Response),
Expand All @@ -299,7 +308,7 @@ handle_joined_by(_Finger, State) ->
%% Description: find the successor of Id
%% returns in finger format
%%--------------------------------------------------------------------
handle_find_successor(Id, State) -> % could use a refactoring...
handle_find_successor(Id, #srv_state{comm = Comm} = State) -> % could use a refactoring...
SuccessorFinger = successor(State),
SuccessorId = SuccessorFinger#finger.sha,
case ch_id_utils:id_between_oc(State#srv_state.sha, SuccessorId, Id) of
Expand All @@ -311,7 +320,7 @@ handle_find_successor(Id, State) -> % could use a refactoring...
true -> % hmm, suspecious clause here.
{{ok, Finger}, State};
false ->
{chordjerl_com:send(Finger, {find_successor, Id}), State}
{Comm:send(Finger, {find_successor, Id}), State}
end
end.

Expand Down Expand Up @@ -346,13 +355,13 @@ handle_stabilize(State) ->
%% Function: handle_stabilize(State, Successor) ->
%% Arguments: Successor must not be self as a finger
%%--------------------------------------------------------------------
handle_stabilize(State, Successor) ->
handle_stabilize(#srv_state{comm = Comm} = State, Successor) ->
% ?NTRACE("stabilizing", []),
SuccPred01 = case Successor#finger.sha =:= State#srv_state.sha of
true ->
handle_return_predecessor(State);
false ->
chordjerl_com:send(Successor, {return_predecessor})
Comm:send(Successor, {return_predecessor})
end,

SuccPred = case SuccPred01 of
Expand All @@ -378,7 +387,7 @@ handle_stabilize(State, Successor) ->
end,

{SelfAsFinger, _State} = handle_return_finger_ref(State),
Response = chordjerl_com:send(RealSuccessor, {claim_to_be_predecessor, SelfAsFinger}),
Response = Comm:send(RealSuccessor, {claim_to_be_predecessor, SelfAsFinger}),
{Response, NewState}.

handle_return_predecessor(State) ->
Expand Down

0 comments on commit 212a687

Please sign in to comment.