1. roder
  2. riak

Commits

dizzyd  committed 2740312

Adding simple test functionality to riak-admin

  • Participants
  • Parent commits 9a3b5a9
  • Branches default

Comments (0)

Files changed (4)

File apps/riak/ebin/riak.app

View file
              riak_vnode,
              riak_vnode_master,
              riak_web,
+             slide,
              spiraltime,
              vclock
             ]},

File apps/riak/src/riak.erl

View file
 -export([stop/0, stop/1]).
 -export([get_app_env/1,get_app_env/2]).
 -export([client_connect/1,client_connect/2,
+         client_test/1,
          local_client/0,local_client/1,
          join/1]).
 -export([code_hash/0]).
 client_connect(Node, Other) ->
     client_connect(Node, <<(erlang:phash2(Other)):32>>).
 
+%%
+%% @doc Validate that a specified node is accessible and functional.
+%%
+client_test(NodeStr) when is_list(NodeStr) ->
+    client_test(riak_util:str_to_node(NodeStr));
+client_test(Node) ->
+    case net_adm:ping(Node) of
+        pong ->
+            case client_connect(Node) of
+                {ok, Client} ->
+                    case client_test_phase1(Client) of
+                        ok ->
+                            error_logger:info_msg("Successfully completed 1 read/write cycle to ~p\n", [Node]),
+                            ok;
+                        error ->
+                            error
+                    end;
+                Error ->
+                    error_logger:error_msg("Error creating client connection to ~s: ~p\n",
+                                           [Node, Error]),
+                    error
+            end;
+        pang ->
+            error_logger:error_msg("Node ~p is not reachable from ~p.\n", [Node, node()]),
+            error
+    end.
+
+    
+%%
+%% @doc Join the ring found on the specified remote node
+%%
 join(NodeStr) when is_list(NodeStr) ->
     join(riak_util:str_to_node(NodeStr));
 join(Node) when is_atom(Node) ->
                          [C || {_, C, _} <- [code:get_object_code(M) || M <- AllMods]]
                         )),
     riak_util:integer_to_list(MD5Sum, 62).
+
+
+%%
+%% Internal functions for testing a Riak node through single read/write cycle
+%%
+-define(CLIENT_TEST_BUCKET, <<"__riak_client_test__">>).
+-define(CLIENT_TEST_KEY, <<"key1">>).
+
+client_test_phase1(Client) ->
+    case Client:get(?CLIENT_TEST_BUCKET, ?CLIENT_TEST_KEY, 1) of
+        {ok, Object} ->
+            client_test_phase2(Client, Object);
+        {error, notfound} ->
+            client_test_phase2(Client, riak_object:new(?CLIENT_TEST_BUCKET, ?CLIENT_TEST_KEY, undefined));
+        Error ->
+            error_logger:error_msg("Failed to read test value: ~p\n", [Error]),
+            error
+    end.
+
+client_test_phase2(Client, Object0) ->
+    Now = calendar:universal_time(),
+    Object = riak_object:update_value(Object0, Now),
+    case Client:put(Object, 1) of
+        ok ->
+            client_test_phase3(Client, Now);
+        Error ->
+            error_logger:error_msg("Failed to write test value: ~p\n", [Error]),
+            error
+    end.
+
+client_test_phase3(Client, WrittenValue) ->
+    case Client:get(?CLIENT_TEST_BUCKET, ?CLIENT_TEST_KEY, 1) of
+        {ok, Object} ->
+            case lists:member(WrittenValue, riak_object:get_values(Object)) of
+                true ->
+                    ok;
+                false ->
+                    error_logger:error_msg("Failed to find test value in list of objects. Expected: ~p\Actual: ~p\n",
+                                           [WrittenValue, riak_object:get_values(Object)]),
+                    error
+            end;
+        Error ->
+            error_logger:error_msg("Failed to read test value: ~p\n", [Error]),
+            error
+    end.

File rel/overlay/bin/riak-admin

View file
                        -eval "riak_event_logger:start('$NODE', \"$FILENAME\")."            
         ;;
 
+    test)
+        # Make sure the local node IS running
+        RES=`$NODETOOL ping`
+        if [ "$RES" != "pong" ]; then
+            echo "Node is not running!"
+            exit 1
+        fi
+
+        shift
+
+        # Parse out the node name to pass to the client
+        NODE_NAME=${NAME_ARG#* }
+
+        $ERTS_PATH/erl -noshell -name riak_test $COOKIE_ARG \
+                       -eval "riak:client_test(\"$NODE_NAME\")" -s init stop
+
+        ;;
     *)
-        echo "Usage: $SCRIPT { join | backup | restore | logger }"
+        echo "Usage: $SCRIPT { join | backup | restore | logger | test }"
         exit 1
         ;;
 esac

File riak_demo.escript

-#!/usr/bin/env escript
-%% -*- erlang -*-
-%%! -name riak_demo@127.0.0.1 -pa ebin -setcookie xyzzy
-main([ConfigFile]) ->
-    case file:consult(ConfigFile) of
-        {ok, Config} ->
-            run_demo(proplists:get_value(riak_nodename, Config),
-                     proplists:get_value(riak_hostname, Config),
-                     proplists:get_value(riak_cookie, Config));
-        Error ->
-            io:format("Error: could not open config file: ~p~n", [Error]),
-            usage()
-    end;
-main(_) ->
-    usage().
-
-usage() ->
-    io:format("usage: riak_demo.escript CONFIG_FILENAME~n"),
-    halt(1).
-
-run_demo(NodeName, Hostname, Cookie) when is_atom(NodeName),
-                                          is_list(Hostname),
-                                          is_atom(Cookie) ->
-    Node = list_to_atom(atom_to_list(NodeName)++"@"++Hostname),
-    io:format("Attempting to connect to ~p with cookie ~p...~n",
-              [Node, Cookie]),
-    erlang:set_cookie(Node, Cookie),
-    case riak:client_connect(Node) of
-        {ok, Client} ->
-            io:format("Connected successfully~n"),
-            continue_demo(Client);
-        Error ->
-            io:format("Error: failed to connect to Riak cluster: ~p", [Error])
-    end;
-run_demo(NodeName, Hostname, Cookie) ->
-    io:format("Error: invalid configuration file:~n"),
-    if is_list(NodeName) -> ok;
-       true -> io:format("  riak_nodename must be an atom (ex. 'riak')~n")
-    end,
-    if is_list(Hostname) -> ok;
-       true -> io:format("  riak_hostname must be a list (ex. \"127.0.0.1\")~n")
-    end,
-    if is_atom(Cookie) -> ok;
-       true -> io:format("  riak_cookie must be an atom (ex. 'riak_cookie')~n")
-    end,
-    usage().
-
-continue_demo(Client) ->
-    io:format("Looking for pre-existing object at {<<\"riak_demo\">>, <<\"demo\">>}...~n"),
-    WrittenValue =
-        case Client:get(<<"riak_demo">>, <<"demo">>, 1) of
-            {error, notfound} ->
-                io:format("  No pre-existing object found, creating new~n"),
-                demo_write(Client, riak_object:new(<<"riak_demo">>, <<"demo">>, undefined));
-            {ok, Object} ->
-                io:format("  Pre-existing object found, modifying~n"),
-                demo_write(Client, Object);
-            Error ->
-                io:format("Error: request for {<<\"riak_demo\">>, <<\"demo\">>} failed: ~p~n",
-                          [Error]),
-                halt(1)
-        end,
-    demo_read(Client, WrittenValue),
-    io:format("SUCCESS~n").
-
-demo_write(Client, Object0) ->
-    Now = calendar:universal_time(),
-    Object = riak_object:update_value(Object0, Now),
-    io:format("Storing object with new value...~n"),
-    case Client:put(Object, 1) of
-        ok ->
-            io:format("  Written successfully~n"),
-            Now;
-        Error ->
-            io:format("Error: write request failed: ~p~n", [Error]),
-            halt(1)
-    end.
-
-demo_read(Client, WrittenValue) ->
-    io:format("Fetching object at {<<\"riak_demo\">>, <<\"demo\">>}...~n"),
-    case Client:get(<<"riak_demo">>, <<"demo">>, 1) of
-        {ok, Object} ->
-            io:format("  Fetched successfully~n"),
-            case lists:member(WrittenValue, riak_object:get_values(Object)) of
-                true ->
-                    io:format("  Object contained correct value~n");
-                false ->
-                    io:format("Error: Object did not contain correct value~n"),
-                    halt(1)
-            end;
-        Error ->
-            io:format("Error: fetch request failed: ~p~n", [Error]),
-            halt(1)
-    end.