Skip to content
This repository
Browse code

Merge pull request #139 from basho/az533-join-claim-improvement

Changes to riak_kv_console to support new join/claim approach in riak_core
  • Loading branch information...
commit cfc1964aa3cc6adc5ffc20acf56d4906749fb347 2 parents 6b6be0c + 93dd56a
Joseph Blomstedt jtuple authored

Showing 2 changed files with 172 additions and 24 deletions. Show diff stats Hide diff stats

  1. +43 24 src/riak_kv_console.erl
  2. +129 0 test/cluster_test.erl
67 src/riak_kv_console.erl
@@ -31,53 +31,72 @@ join([NodeStr]) ->
31 31 try
32 32 case riak_core:join(NodeStr) of
33 33 ok ->
34   - io:format("Sent join request to ~s\n", [NodeStr]),
  34 + io:format("Sent join request to ~s~n", [NodeStr]),
35 35 ok;
36 36 {error, not_reachable} ->
37   - io:format("Node ~s is not reachable!\n", [NodeStr]),
  37 + io:format("Node ~s is not reachable!~n", [NodeStr]),
38 38 error;
39 39 {error, different_ring_sizes} ->
40 40 io:format("Failed: ~s has a different ring_creation_size~n",
41   - [NodeStr]),
  41 + [NodeStr]);
  42 + {error, _} ->
  43 + io:format("Join failed. Try again in a few moments.~n", []),
42 44 error
43 45 end
44 46 catch
45 47 Exception:Reason ->
46   - lager:error("Join failed ~p:~p", [Exception,
47   - Reason]),
  48 + lager:error("Join failed ~p:~p", [Exception, Reason]),
48 49 io:format("Join failed, see log for details~n"),
49 50 error
50 51 end.
51 52
52 53
53 54 leave([]) ->
54   - remove_node(node()).
55   -
56   -remove([Node]) ->
57   - remove_node(list_to_atom(Node)).
58   -
59   -remove_node(Node) when is_atom(Node) ->
60   - try
61   - case catch(riak_core:remove_from_cluster(Node)) of
62   - {'EXIT', {badarg, [{erlang, hd, [[]]}|_]}} ->
63   - %% This is a workaround because
64   - %% riak_core_gossip:remove_from_cluster doesn't check if
65   - %% the result of subtracting the current node from the
66   - %% cluster member list results in the empty list. When
67   - %% that code gets refactored this can probably go away.
68   - io:format("Leave failed, this node is the only member.~n"),
  55 + try
  56 + case riak_core:leave() of
  57 + ok ->
  58 + io:format("Success: ~p will shutdown after handing off "
  59 + "its data~n", [node()]),
  60 + ok;
  61 + {error, already_leaving} ->
  62 + io:format("~p is already in the process of leaving the "
  63 + "cluster.~n", [node()]),
  64 + ok;
  65 + {error, not_member} ->
  66 + io:format("Failed: ~p is not a member of the cluster.~n",
  67 + [node()]),
69 68 error;
70   - Res ->
71   - io:format(" ~p\n", [Res])
  69 + {error, only_member} ->
  70 + io:format("Failed: ~p is the only member.~n", [node()]),
  71 + error
72 72 end
73 73 catch
74 74 Exception:Reason ->
75   - lager:error("Leave failed ~p:~p", [Exception,
76   - Reason]),
  75 + lager:error("Leave failed ~p:~p", [Exception, Reason]),
77 76 io:format("Leave failed, see log for details~n"),
78 77 error
79 78 end.
80 79
  80 +remove([Node]) ->
  81 + try
  82 + case riak_core:remove(list_to_atom(Node)) of
  83 + ok ->
  84 + io:format("Success: ~p removed from the cluster~n", [Node]),
  85 + ok;
  86 + {error, not_member} ->
  87 + io:format("Failed: ~p is not a member of the cluster.~n",
  88 + [node()]),
  89 + error;
  90 + {error, only_member} ->
  91 + io:format("Failed: ~p is the only member.~n", [Node]),
  92 + error
  93 + end
  94 + catch
  95 + Exception:Reason ->
  96 + lager:error("Remove failed ~p:~p", [Exception, Reason]),
  97 + io:format("Remove failed, see log for details~n"),
  98 + error
  99 + end.
81 100
82 101 -spec(status([]) -> ok).
83 102 status([]) ->
129 test/cluster_test.erl
... ... @@ -0,0 +1,129 @@
  1 +%% -------------------------------------------------------------------
  2 +%%
  3 +%% Copyright (c) 2011 Basho Technologies, Inc.
  4 +%%
  5 +%% This file is provided to you under the Apache License,
  6 +%% Version 2.0 (the "License"); you may not use this file
  7 +%% except in compliance with the License. You may obtain
  8 +%% a copy of the License at
  9 +%%
  10 +%% http://www.apache.org/licenses/LICENSE-2.0
  11 +%%
  12 +%% Unless required by applicable law or agreed to in writing,
  13 +%% software distributed under the License is distributed on an
  14 +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15 +%% KIND, either express or implied. See the License for the
  16 +%% specific language governing permissions and limitations
  17 +%% under the License.
  18 +%%
  19 +%% -------------------------------------------------------------------
  20 +
  21 +%% Test various cluster membership behaviors.
  22 +%%
  23 +%% This test is designed to run against running Riak nodes, and will be
  24 +%% skipped if the following environment variables are not set:
  25 +%% (example values corresponding to standard dev release follow)
  26 +%% RIAK_TEST_NODE_1="dev1@127.0.0.1"
  27 +%% RIAK_TEST_NODE_2="dev2@127.0.0.1"
  28 +%% RIAK_TEST_NODE_3="dev3@127.0.0.1"
  29 +%% RIAK_TEST_COOKIE="riak"
  30 +%% RIAK_EUNIT_NODE="eunit@127.0.0.1"
  31 +
  32 +-ifdef(TEST).
  33 +-include_lib("eunit/include/eunit.hrl").
  34 +-module(cluster_test).
  35 +-compile(export_all).
  36 +
  37 +cluster_test_() ->
  38 + Envs = ["RIAK_TEST_NODE_1", "RIAK_TEST_NODE_2", "RIAK_TEST_NODE_3",
  39 + "RIAK_TEST_COOKIE", "RIAK_EUNIT_NODE"],
  40 + Vals = [os:getenv(Env) || Env <- Envs],
  41 + case lists:member(false, Vals) of
  42 + true ->
  43 + ?debugFmt("Skipping cluster_test~n", []),
  44 + [];
  45 + false ->
  46 + [Node1V, Node2V, Node3V, CookieV, ENodeV] = Vals,
  47 + Node1 = list_to_atom(Node1V),
  48 + Node2 = list_to_atom(Node2V),
  49 + Node3 = list_to_atom(Node3V),
  50 + Cookie = list_to_atom(CookieV),
  51 + ENode = list_to_atom(ENodeV),
  52 +
  53 + {spawn, [{setup,
  54 + fun() ->
  55 + net_kernel:start([ENode]),
  56 + erlang:set_cookie(node(), Cookie),
  57 + {Node1, Node2, Node3}
  58 + end,
  59 + fun(_) ->
  60 + ok
  61 + end,
  62 + fun(Args) ->
  63 + [{timeout, 1000000, ?_test(read_case(Args))}
  64 + ]
  65 + end
  66 + }]}
  67 + end.
  68 +
  69 +read_case({Node1, Node2, _Node3}) ->
  70 + rpc:call(Node1, application, set_env, [riak_core, ring_creation_size, 8]),
  71 + rpc:call(Node2, application, set_env, [riak_core, ring_creation_size, 8]),
  72 +
  73 + application:set_env(riak_core, ring_creation_size, 4),
  74 +
  75 + Ring1 = rpc:call(Node1, riak_core_ring, fresh, []),
  76 + Ring2 = rpc:call(Node2, riak_core_ring, fresh, []),
  77 + %% Ring3 = rpc:call(Node3, riak_core_ring, fresh, []),
  78 +
  79 + rpc:call(Node1, riak_core_ring_manager, set_my_ring, [Ring1]),
  80 + rpc:call(Node2, riak_core_ring_manager, set_my_ring, [Ring2]),
  81 + %% rpc:call(Node3, riak_core_ring_manager, set_my_ring, [Ring3]),
  82 +
  83 + %% Restart vnodes
  84 + rpc:call(Node1, supervisor, terminate_child, [riak_core_sup, riak_core_vnode_sup]),
  85 + rpc:call(Node1, supervisor, restart_child, [riak_core_sup, riak_core_vnode_sup]),
  86 + rpc:call(Node2, supervisor, terminate_child, [riak_core_sup, riak_core_vnode_sup]),
  87 + rpc:call(Node2, supervisor, restart_child, [riak_core_sup, riak_core_vnode_sup]),
  88 +
  89 + %% Load data
  90 + ?debugFmt("Loading data~n", []),
  91 + {ok, RC} = riak:client_connect(Node1),
  92 + Data1M = list_to_binary(lists:duplicate(4096,lists:seq(0,255))),
  93 + Count = 1024,
  94 + [RC:put(riak_object:new(<<"cluster_test">>, term_to_binary(N), Data1M))
  95 + || N <- lists:seq(0,Count)],
  96 +
  97 + ?debugFmt("Joining nodes~n", []),
  98 + rpc:call(Node2, riak_kv_console, join, [[Node1]]),
  99 + %%rpc:call(Node2, riak_core_jc, join, [Node1]),
  100 +
  101 + ?debugFmt("Sleeping~n", []),
  102 + timer:sleep(65000),
  103 +
  104 + ?debugFmt("Reading~n", []),
  105 + %% Read
  106 + read(RC, Count, 60000000),
  107 + ok.
  108 +
  109 +read(RC, Size, Duration) ->
  110 + read(RC, Size, Duration, erlang:now()).
  111 +read(RC, Size, Duration, T1) ->
  112 + case timer:now_diff(erlang:now(), T1) > Duration of
  113 + true ->
  114 + ok;
  115 + false ->
  116 + lists:foreach(
  117 + fun(N) ->
  118 + case RC:get(<<"cluster_test">>, term_to_binary(N), 1, 300) of
  119 + {error, notfound} ->
  120 + ?debugFmt("Not found~n", []),
  121 + throw(failed);
  122 + _ ->
  123 + ok
  124 + end
  125 + end, lists:seq(0,Size)),
  126 + read(RC, Size, Duration, T1)
  127 + end.
  128 +
  129 +-endif.

0 comments on commit cfc1964

Please sign in to comment.
Something went wrong with that request. Please try again.