Anonymous avatar Anonymous committed cae484f

Create start-logger.sh to demonstrate event logging. Get rid of cookies for eventers. Convert back to old config files (riak_nodename, riak_hostname).

Comments (0)

Files changed (12)

 # create a brand new riak ring, using configfile as app config params
 # run interactively, w/o heartbeat
 . riak-env.sh
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
-exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
+exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1
 # join an existing riak ring via a node known to be listening on ip:port
 # run in interactive debug shell mode
 . riak-env.sh
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
-exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup join_cluster $2
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
+exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup join_cluster $2
 # debug-restart <configfile>
 # assuming we've run a node from here before, start back up in debug mode
 . riak-env.sh
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
-exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup rejoin
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
+exec erl -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup rejoin
     {ok, riak_client:new(Node, riak_util:mkclientid(Node))}.
 
 
+
 %% @spec ensure_started(Application :: atom()) -> ok
 %% @doc Start the named application if not already started.
 ensure_started(App) ->

src/riak_doorbell.erl

+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License.  You may obtain
+%% a copy of the License at
+
+%%   http://www.apache.org/licenses/LICENSE-2.0
+
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied.  See the License for the
+%% specific language governing permissions and limitations
+%% under the License.    
+
+%% @doc The doorbell (and door knocker) is a UDP socket server that provides a discovery mechanism for other nodes to connect into the Riak cluster.
+
+-module(riak_doorbell).
+
+-behaviour(gen_server).
+-export([start_link/0]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+	 terminate/2, code_change/3]).
+-export([ring/2,knock/3, stop/0]).
+
+-include_lib("eunit/include/eunit.hrl").
+	 
+-record(state, {port, sock}).
+
+%% @spec start_link() -> {ok, pid()}
+start_link() ->
+    Port = riak:get_app_env(doorbell_port),
+    pong = net_adm:ping(node()), % fail if not distributed
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [Port], []).
+
+%% @spec knock(IP :: list(), Port :: integer(), RiakCookie :: atom()) ->
+%%       ok | {error, Reason}
+%% @doc This is used by a node not seeking to be a member of the cluster,
+%%      to establish a distributed-erlang connection used by a riak_client.
+knock(IP, Port, RiakCookie) ->
+    % for non-riak nodes seeking a client proc
+    Nonce = random:uniform(),
+    {ok, SendSock} = gen_udp:open(0),
+    gen_udp:send(SendSock, IP, Port,term_to_binary({
+              {knock,Nonce,self(),erlang:get_cookie(),RiakCookie},node()})),
+    gen_udp:close(SendSock),
+    Nonce.
+
+%% @spec ring(IP :: list(), Port :: integer()) ->
+%%       ok | {error, Reason}
+%% @doc This is used by a node joining the riak cluster.
+ring(IP, Port) ->
+    % for riak nodes joining
+    {ok, SendSock} = gen_udp:open(0),
+    Res = gen_udp:send(SendSock, IP, Port, term_to_binary({ring,node()})),
+    gen_udp:close(SendSock),
+    Res.
+
+stop() -> gen_server:cast(?MODULE, stop).
+    
+% @private
+init([Port]) ->
+    Opts = [{active, true},
+            list,
+            {reuseaddr, true}],
+    {ok, Sock} = gen_udp:open(Port, Opts),
+    {ok, #state{port=Port,sock=Sock}}.
+
+% @private
+handle_info({udp, _Socket, IP, _InPortNo, Packet0},State) ->
+    {RingType, Node} = binary_to_term(list_to_binary(Packet0)),
+    case RingType of
+        ring ->
+            case net_adm:ping(Node) of
+                pong -> riak_eventer:notify(riak_doorbell,connected,{IP,Node});
+                pang -> riak_eventer:notify(riak_doorbell,connectfail,{IP,Node})
+            end;
+        {knock,Nonce,Pid,Cookie,RiakCookie} ->
+            case riak:get_app_env(riak_cookie) of
+                RiakCookie ->
+                    riak_eventer:notify(riak_doorbell, client_connected,
+                                        {IP, Pid, Node}),
+                    erlang:set_cookie(Node,Cookie),
+                    Pid ! {riak_connect, Nonce, node()};
+                _ ->
+                    riak_eventer:notify(riak_doorbell, client_connectfail,
+                                        {IP, Pid, Node})
+            end
+    end,
+    {noreply, State};
+handle_info(_Info, State) -> {noreply, State}.
+
+%% @private
+handle_cast(stop, State) -> {stop, normal,State}.
+
+%% @private
+handle_call(_Request, _From, State) -> {reply, no_call, State}.
+
+%% @private
+terminate(_Reason, _State=#state{sock=Sock}) ->
+    gen_udp:close(Sock),
+    ok.
+
+%% @private
+code_change(_OldVsn, State, _Extra) ->  {ok, State}.
+
+knock_test() ->
+    application:set_env(riak, doorbell_port, 9001),
+    application:set_env(riak, riak_cookie, default_riak_cookie),
+    {ok, _Pid} = riak_doorbell:start_link(),
+    Nonce = riak_doorbell:knock("127.0.0.1", 9001, default_riak_cookie),
+    receive
+        {riak_connect, Nonce, _} ->
+            ok
+    after 1000 ->
+            throw(knock_test_timeout)
+    end,
+    riak_doorbell:stop().
+       
+    
+            
+    
+
+    
+

src/riak_event_logger.erl

 
 -define (RECONNECT_INTERVAL, 200).
 -define (SERVER, ?MODULE).
--record (state, {pid, hostname, port, cookie, verbosity, fd}).
--export ([start/4, start_link/4]).
+-record (state, {node, pid, fd}).
+-export ([start/2, start_link/2]).
 -export ([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
 %% @private
-start(Hostname, Port, Cookie, Filename) ->
-    gen_server:start({local, ?SERVER}, ?MODULE, [Hostname, Port, Cookie, Filename], []).
+start(Node, Filename) ->
+    gen_server:start({local, ?SERVER}, ?MODULE, [Node, Filename], []).
 
-start_link(Hostname, Port, Cookie, Filename) -> 
-    gen_server:start_link({local, ?SERVER}, ?MODULE, [Hostname, Port, Cookie, Filename], []).
+start_link(Node, Filename) -> 
+    gen_server:start_link({local, ?SERVER}, ?MODULE, [Node, Filename], []).
 
 %% @private
-init([Hostname, Port, Cookie, Filename]) -> 
+init([Node, Filename]) -> 
     % If this gen_server dies and is supervised, then it will
     % be restarted under a new pid. If this happens, then we will
     % lose our connection to the Riak cluster. So, send a keepalive
     end,
     
     State = #state {
-        hostname = Hostname,
-        port = Port,
-        cookie = Cookie,
+        node = Node,
         fd = FD
     },
     {ok, State}.
 %% @private
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 
+register_for_events(State) ->
+    % Get the client...
+    {ok, C} = riak:client_connect(State#state.node),
 
-register_for_events(State) ->
-    {ok, C} = riak:client_connect(State#state.hostname, State#state.port, State#state.cookie),
+    % Attach the eventer...
     Desc = io_lib:format("~s (~s)", [?SERVER, node()]),
-    C:add_event_handler(self(), Desc).
+    C:add_event_handler(self(), Desc, {'_', '_', '_', '_'}, []).
 
 
 %%% DATE FUNCTIONS %%%

src/riak_eventer.erl

 %% @private
 code_change(_OldVsn, State, _Extra) ->  {ok, State}.
 
-%% make_handler/4 -
+%% make_handler/5 -
 %% Create an handler record from the supplied params.
 make_handler(Pid, Desc, MatchHead, MatchGuard) ->
     ID = get_handler_id(Pid, MatchHead, MatchGuard),
     Handlers = [
         make_handler(self(), "All 1", '_', []),
         make_handler(self(), "All 2", {'_', '_', '_', '_'}, []),
-        make_handler(self(), "Only riak_vnode 1", {riak_vnode, '_', '_', '_'}, []),
-        make_handler(self(), "Only riak_vnode 2", {'$1', '_', '_', '_'}, [{'==', '$1', riak_vnode}]),
-        make_handler(self(), "Only riak_vnode delete", {riak_vnode, delete, '_', '_'}, []),
-        make_handler(self(), "Only riak_vnode put, get, or delete", {'$1', '$2', '_', '_'}, [
+        make_handler(self(),"Only riak_vnode 1", {riak_vnode, '_', '_', '_'}, []),
+        make_handler(self(),"Only riak_vnode 2", {'$1', '_', '_', '_'}, [{'==', '$1', riak_vnode}]),
+        make_handler(self(),"Only riak_vnode delete", {riak_vnode, delete, '_', '_'}, []),
+        make_handler(self(),"Only riak_vnode put, get, or delete", {'$1', '$2', '_', '_'}, [
             {'andalso', {'==', '$1', riak_vnode}, {'orelse', {'==', '$2', get}, {'==', '$2', put}, {'==', '$2', delete}}}
         ])
     ],

start-eventer.sh

-#!/usr/bin/env bash
-# ./start-eventer.sh <clustername> <cookie> <ip> <port> <nodename> <eventmodname> <eventmodarg> <matchspec>
-# This will:
-#  Join riak cluster <clustername> using erlcookie <cookie>
-#  via the node listening at <ip>:<port>
-#  and register a riak event handler
-if [ $# -lt 7 ]; then
-    echo Usage: 1>&2
-    echo "    `basename $0` <clustername> <cookie> <ip in ring> " 1>&2
-    echo "                  <doorbell port> <nodename> <event module name>" 1>&2
-    echo "                  <event module args> <matchspec>" 1>&2
-    exit 1
-fi
-. riak-env.sh
-erl -noshell -pa deps/*/ebin -pa ebin -name $5 -run riak_eventer eventer_config $1 $2 -run riak start -run riak_eventer do_eventer $3 $4 $6 $7 $8
 # start-fresh <configfile>
 # create a brand new riak ring, using configfile as app config params
 . riak-env.sh
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
 export HEART_COMMAND=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_heart_command)])" -run init stop)
-exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1
+exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1
     exit 1
 fi
 . riak-env.sh
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
 export HEART_COMMAND=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_heart_command)])" -run init stop)
-exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup join_cluster $2
+exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup join_cluster $2
+#!/usr/bin/env bash
+# ./start-eventer.sh <clustername> <cookie> <ip> <port> <nodename> <eventmodname> <eventmodarg> <matchspec>
+# This will:
+#  Join riak cluster <clustername> using erlcookie <cookie>
+#  via the node listening at <ip>:<port>
+#  and register a riak event handler
+if [ $# -lt 3 ]; then
+    echo Usage: 1>&2
+    echo "    `basename $0` <node> <cookie> <filename>"
+    exit 1
+fi
+. riak-env.sh
+NODE=$1
+COOKIE=$2
+FILENAME=$3
+erl -noshell -pa deps/*/ebin -pa ebin -name riak_logger -setcookie $COOKIE -eval "riak_event_logger:start('$NODE', \"$FILENAME\")."
 # assuming we've run a node from here before, start back up
 . riak-env.sh
 echo $ERL_MAX_ETS_TABLES
-NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_nodename)])" -run init stop)
-COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(erlang_cookie)])" -run init stop)
+NODENAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_nodename)])" -run init stop)
+HOSTNAME=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_hostname)])" -run init stop)
+COOKIE=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_cookie)])" -run init stop)
 export HEART_COMMAND=$(erl -noshell -pa ebin -eval "error_logger:tty(false), riak_app:read_config(\"$1\"), io:format(\"~s~n\",[riak:get_app_env(riak_heart_command)])" -run init stop)
-exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup rejoin
+exec erl -heart -detached -connect_all false -pa deps/*/ebin -pa ebin -name ${NODENAME}@${HOSTNAME} -setcookie ${COOKIE} -run riak start $1 -run riak_startup rejoin
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.