diff options
-rw-r--r-- | src/mem3.erl | 42 | ||||
-rw-r--r-- | test/mem3_test.erl | 24 |
2 files changed, 37 insertions, 29 deletions
diff --git a/src/mem3.erl b/src/mem3.erl index b3416219..b2aa4394 100644 --- a/src/mem3.erl +++ b/src/mem3.erl @@ -41,7 +41,7 @@ -define(STATE_FILE_PREFIX, "membership"). %% types - stick somewhere in includes? --type join_type() :: first | new | replace. +-type join_type() :: init | join | replace | leave. -type join_order() :: non_neg_integer(). -type options() :: list(). -type mem_node() :: {join_order(), node(), options()}. @@ -173,15 +173,17 @@ init(Args) -> %% new node(s) joining to this node handle_call({join, JoinType, ExtNodes, PingNode}, _From, State) -> - % NewState = handle_join(JoinType, ExtNodes, PingNode, State), - % {reply, ok, NewState}; - try - NewState = handle_join(JoinType, ExtNodes, PingNode, State), - {reply, ok, NewState} - catch _:Error -> - showroom_log:message(error, "~p", [Error]), - {reply, Error, State} - end; + % {ok, NewState} = handle_join(JoinType, ExtNodes, PingNode, State), + % {reply, ok, NewState}; + try + case handle_join(JoinType, ExtNodes, PingNode, State) of + {ok, NewState} -> {reply, ok, NewState}; + Other -> {reply, Other, State} + end + catch _:Error -> + showroom_log:message(error, "~p", [Error]), + {reply, Error, State} + end; %% clock handle_call(clock, _From, #mem{clock=Clock} = State) -> @@ -306,13 +308,13 @@ handle_init(_Test, #mem{nodes=Nodes, args=Args} = OldState) -> end. -%% handle join activities, return NewState -handle_join(first, ExtNodes, nil, State) -> +%% handle join activities, return {ok,NewState} +handle_join(init, ExtNodes, nil, State) -> {_,Nodes,_} = lists:unzip3(ExtNodes), ping_all_yall(Nodes), int_join(ExtNodes, State); -handle_join(new, ExtNodes, PingNode, #mem{args=Args} = State) -> +handle_join(join, ExtNodes, PingNode, #mem{args=Args} = State) -> NewState = case get_test(Args) of undefined -> get_pingnode_state(PingNode); _ -> State % testing, so meh @@ -324,10 +326,14 @@ handle_join(replace, [_OldNode | _], _PingNode, _State) -> % TODO implement me ok; +handle_join(leave, [_OldNode | _], _PingNode, _State) -> + % TODO implement me + ok; + handle_join(JoinType, _, PingNode, _) -> showroom_log:message(info, "membership: unknown join type: ~p " "for ping node: ~p", [JoinType, PingNode]), - {error, {unknown_join_type, JoinType}}. + {error, unknown_join_type}. int_join(ExtNodes, #mem{nodes=Nodes, clock=Clock} = State) -> @@ -339,7 +345,7 @@ int_join(ExtNodes, #mem{nodes=Nodes, clock=Clock} = State) -> NewClock = vector_clock:increment(node(), Clock), NewState = State#mem{nodes=NewNodes1, clock=NewClock}, install_new_state(NewState), - NewState. + {ok, NewState}. get_pingnode_state(PingNode) -> @@ -507,9 +513,11 @@ check_pos(Pos, Node, Nodes) -> {_,OldNode,_} = Found, if OldNode =:= Node -> - throw({error, {node_exists_at_position, Pos, Node}}); + Msg = "node_exists_at_position_" ++ integer_to_list(Pos), + throw({error, list_to_binary(Msg)}); true -> - throw({error, {position_exists, Pos, OldNode}}) + Msg = "position_exists_" ++ integer_to_list(Pos), + throw({error, list_to_binary(Msg)}) end end. diff --git a/test/mem3_test.erl b/test/mem3_test.erl index 80699559..b8622005 100644 --- a/test/mem3_test.erl +++ b/test/mem3_test.erl @@ -29,8 +29,8 @@ all_tests_test_() -> [ fun init/1, fun clock/1, - fun join_first/1, - fun join_first_with_hints/1, + fun join_init/1, + fun join_init_with_hints/1, fun join_new_node/1, fun join_two_new_nodes/1, fun join_with_wrong_order/1 @@ -64,17 +64,17 @@ clock(_Pid) -> ?assertMatch([], Clock). -join_first(_Pid) -> +join_init(_Pid) -> mem3:reset(), - mem3:join(first, [{1, a, []}, {2, b, []}], nil), + mem3:join(init, [{1, a, []}, {2, b, []}], nil), {ok, Nodes} = mem3:nodes(), ?assertEqual(2, length(Nodes)), ok. -join_first_with_hints(_Pid) -> +join_init_with_hints(_Pid) -> mem3:reset(), - mem3:join(first, [{1, a, []}, + mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, [{hints, [?HINT_C1, ?HINT_C2]}]}, {4, d, []}, @@ -90,10 +90,10 @@ join_first_with_hints(_Pid) -> join_new_node(_Pid) -> mem3:reset(), - mem3:join(first, [{1, a, []}, {2, b, []}, {3, c, []}], nil), + mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), {ok, Nodes1} = mem3:nodes(), ?assertEqual(3, length(Nodes1)), - mem3:join(new, [{4, d, []}], a), + mem3:join(join, [{4, d, []}], a), {ok, Nodes2} = mem3:nodes(), ?assertEqual(4, length(Nodes2)), ok. @@ -101,10 +101,10 @@ join_new_node(_Pid) -> join_two_new_nodes(_Pid) -> mem3:reset(), - mem3:join(first, [{1, a, []}, {2, b, []}, {3, c, []}], nil), + mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), {ok, Nodes1} = mem3:nodes(), ?assertEqual(3, length(Nodes1)), - Res = mem3:join(new, [{4, d, []}, {5, e, []}], b), + Res = mem3:join(join, [{4, d, []}, {5, e, []}], b), ?assertEqual(ok, Res), {ok, Nodes2} = mem3:nodes(), ?assertEqual(5, length(Nodes2)), @@ -114,10 +114,10 @@ join_two_new_nodes(_Pid) -> join_with_wrong_order(_Pid) -> mem3:reset(), - mem3:join(first, [{1, a, []}, {2, b, []}, {3, c, []}], nil), + mem3:join(init, [{1, a, []}, {2, b, []}, {3, c, []}], nil), % ?assertEqual([], mem3:parts_for_node(d)), %?debugFmt("~nFullmap: ~p~n", [mem3:fullmap()]), - Res = mem3:join(new, [{3, d, []}], c), + Res = mem3:join(join, [{3, d, []}], c), ?assertEqual({error,{position_exists,3,c}}, Res), %?debugFmt("~nFullmap: ~p~n", [mem3:fullmap()]), ok. |