Skip to content

Commit 867228e

Browse files
John BurwellBrett Hazen
authored andcommitted
Continued work on implementing the new r_t v2 API
1 parent c5fe86c commit 867228e

File tree

1 file changed

+87
-51
lines changed

1 file changed

+87
-51
lines changed

src/rt_riak_node.erl

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
version/1]).
5252

5353
%% gen_fsm callbacks
54-
-export([init/1, state_name/2, state_name/3, handle_event/3,
55-
handle_sync_event/4, handle_info/3, ready/3, terminate/3, code_change/4]).
54+
-export([init/1, handle_event/3, stopped/3, handle_sync_event/4,
55+
handle_info/3, ready/3, terminate/3, code_change/4]).
5656

5757
-define(SERVER, ?MODULE).
5858

@@ -62,11 +62,23 @@
6262
-record(configuration, {one :: proplists:proplist(),
6363
two :: proplists:proplist()}).
6464

65+
-type path() :: string().
66+
-record(directory_overlay, {bin_dir :: path(),
67+
conf_dir :: path(),
68+
data_dir :: path(),
69+
home_dir :: path(),
70+
lib_dir :: path(),
71+
log_dir :: path()}).
72+
73+
74+
-type command() :: string().
6575
-record(state, {config :: #configuration{},
6676
host :: host(),
6777
id :: node_id(),
68-
install_type :: module(),
78+
directory_overlay :: #directory_overlay{},
6979
name :: node(),
80+
start_command :: command(),
81+
stop_command :: command(),
7082
transport :: module(),
7183
version :: string()}).
7284

@@ -180,12 +192,11 @@ owners_according_to(Node) ->
180192
%% @doc Get list of partitions owned by node (primary).
181193
-spec partitions(node()) -> [term()].
182194
partitions(Node) ->
183-
lager:error("partitions(~p) is not implemented.", [Node]),
184-
[].
195+
gen_fsm:sync_send_event(Node, partitions).
185196

186197
-spec ping(node()) -> boolean().
187198
ping(Node) ->
188-
gen_fsm:sync_send_event(Node, ping).
199+
gen_fsm:sync_send_all_state_event(Node, ping).
189200

190201
-spec plan(node()) -> rt_util:result().
191202
plan(Node) ->
@@ -227,7 +238,6 @@ start_link(Host, NodeId, NodeName, Config, Version) ->
227238
status_of_according_to(Node) ->
228239
gen_fsm:sync_send_event(Node, status_of_according_to).
229240

230-
231241
-spec stop(node()) -> rt_util:result().
232242
stop(Node) ->
233243
stop(Node, true).
@@ -256,7 +266,7 @@ wait_until_unpingable(Node) ->
256266
gen_fsm:sync_send_event(Node, wait_until_unpingable).
257267

258268
version(Node) ->
259-
gen_fsm:sync_send_event(Node, version).
269+
gen_fsm:sync_send_all_state_event(Node, version).
260270

261271
%%%===================================================================
262272
%%% gen_fsm callbacks
@@ -275,44 +285,60 @@ version(Node) ->
275285
%% {stop, StopReason}
276286
%% @end
277287
%%--------------------------------------------------------------------
278-
init([Host, NodeId, NodeName, Config, Version]) ->
288+
init([Host, NodeType, NodeId, NodeName, Config, Version]) ->
289+
DirOverlay = create_directory_overlay(NodeType, Version),
279290
State = #state{host=Host,
280291
id=NodeId,
281292
name=NodeName,
282293
config=Config,
294+
directory_overlay=DirOverlay,
295+
start_command=start_command(NodeType, DirOverlay),
296+
stop_command=stop_command(NodeType, DirOverlay),
283297
version=Version},
284298
{ok, allocated, State}.
285299

286-
%%--------------------------------------------------------------------
287-
%% @private
288-
%% @doc
289-
%% There should be one instance of this function for each possible
290-
%% state name. Whenever a gen_fsm receives an event sent using
291-
%% gen_fsm:send_event/2, the instance of this function with the same
292-
%% name as the current state name StateName is called to handle
293-
%% the event. It is also called if a timeout occurs.
294-
%%
295-
%% @spec state_name(Event, State) ->
296-
%% {next_state, NextStateName, NextState} |
297-
%% {next_state, NextStateName, NextState, Timeout} |
298-
%% {stop, Reason, NewState}
299-
%% @end
300-
%%--------------------------------------------------------------------
301-
state_name(_Event, State) ->
302-
{next_state, state_name, State}.
303300

301+
stopped(start, _From, State) ->
302+
transition_stopped_to_started(State);
303+
stopped(stop, _From, State=#state{name=NodeName}) ->
304+
lager:debug("Stop called on an already stopped node ~p", [NodeName]),
305+
{reply, ok, stopped, State};
306+
stopped(_Event, _From, State) ->
307+
%% The state of the node is not harmed. Therefore, we leave the FSM running
308+
%% in the stopped state, but refuse to execute the command ...
309+
{reply, {error, invalid_state}, stopped, State}.
310+
311+
312+
transition_stopped_to_started(State=#state{start_command=StartCommand, transport=Transport}) ->
313+
transition_stopped_to_started(Transport:exec(StartCommand), State).
314+
315+
transition_stopped_to_started(ok, State) ->
316+
{reply, ok, started, State};
317+
transition_stopped_to_started(Error={error, _}, State) ->
318+
{stop, Error, State}.
319+
320+
321+
ready({admin, Args, Options}, _From, State=#state{directory_overlay=DirOverlay, transport=Transport}) ->
322+
Result = Transport:exec(riak_admin_path(DirOverlay), Args, Options),
323+
{reply, Result, ready, State};
304324
ready(get_ring, _From, #state{name=NodeName}=State) ->
305325
{ok, Ring} = maybe_get_ring(NodeName),
306326
{reply, Ring, ready, State};
307327
ready(members_according_to, _From, #state{name=NodeName}=State) ->
308328
Members = maybe_members_according_to(NodeName),
309329
{reply, Members, ready, State};
330+
ready(owners_according_to, _From, #state{name=NodeName}=State) ->
331+
Owners = maybe_owners_according_to(NodeName),
332+
{reply, Owners, ready, State};
310333
ready(partitions, _From, #state{name=NodeName}=State) ->
311334
Partitions = maybe_partitions(NodeName),
312335
{reply, Partitions, ready, State};
313-
ready(owners_according_to, _From, #state{name=NodeName}=State) ->
314-
Owners = maybe_owners_according_to(NodeName),
315-
{reply, Owners, ready, State}.
336+
ready({riak, Args}, _From, State=#state{directory_overlay=DirOverlay, transport=Transport}) ->
337+
Result = Transport:exec(riak_path(DirOverlay), Args),
338+
{reply, Result, ready, State};
339+
ready({riak_repl, Args}, _From, State=#state{directory_overlay=DirOverlay, transport=Transport}) ->
340+
Result = Transport:exec(riak_repl_path(DirOverlay), Args),
341+
{reply, Result, ready, State}.
316342

317343
-spec maybe_get_ring(node()) -> rt_rpc_result().
318344
maybe_get_ring(NodeName) ->
@@ -358,28 +384,6 @@ maybe_rpc_call({badrpc, _}) ->
358384
maybe_rpc_call(Result) ->
359385
Result.
360386

361-
%%--------------------------------------------------------------------
362-
%% @private
363-
%% @doc
364-
%% There should be one instance of this function for each possible
365-
%% state name. Whenever a gen_fsm receives an event sent using
366-
%% gen_fsm:sync_send_event/[2,3], the instance of this function with
367-
%% the same name as the current state name StateName is called to
368-
%% handle the event.
369-
%%
370-
%% @spec state_name(Event, From, State) ->
371-
%% {next_state, NextStateName, NextState} |
372-
%% {next_state, NextStateName, NextState, Timeout} |
373-
%% {reply, Reply, NextStateName, NextState} |
374-
%% {reply, Reply, NextStateName, NextState, Timeout} |
375-
%% {stop, Reason, NewState} |
376-
%% {stop, Reason, Reply, NewState}
377-
%% @end
378-
%%--------------------------------------------------------------------
379-
state_name(_Event, _From, State) ->
380-
Reply = ok,
381-
{reply, Reply, state_name, State}.
382-
383387
%%--------------------------------------------------------------------
384388
%% @private
385389
%% @doc
@@ -461,3 +465,35 @@ code_change(_OldVsn, StateName, State, _Extra) ->
461465
%%%===================================================================
462466
%%% Internal functions
463467
%%%===================================================================
468+
469+
%% TODO Convert to the version() type when it is exported ..
470+
-spec create_directory_overlay({atom(), path()}, {string(), string()}) -> #directory_overlay{}.
471+
create_directory_overlay([devrel, RootPath], {Product, Version}) ->
472+
HomeDir = filename:join([RootPath], Product ++ "-" ++ Version),
473+
#directory_overlay{bin_dir=filename:join([HomeDir, "bin"]),
474+
conf_dir=filename:join([HomeDir, "etc"]),
475+
data_dir=filename:join([HomeDir, "data"]),
476+
home_dir=HomeDir,
477+
lib_dir=filename:join([HomeDir, "lib"]),
478+
log_dir=filename:join([HomeDir, "log"])}.
479+
480+
-spec start_command({devrel, string()}, #directory_overlay{}) -> command().
481+
start_command({devrel, _}, DirOverlay) ->
482+
riak_path(DirOverlay) ++ " " ++ "start".
483+
484+
-spec stop_command({devrel, string()}, #directory_overlay{}) -> command().
485+
stop_command({devrel, _}, DirOverlay) ->
486+
riak_path(DirOverlay) ++ " " ++ "stop".
487+
488+
-spec riak_path(#directory_overlay{}) -> path().
489+
riak_path(#directory_overlay{bin_dir=BinDir}) ->
490+
filename:join([BinDir, "riak"]).
491+
492+
-spec riak_admin_path(#directory_overlay{}) -> path().
493+
riak_admin_path(#directory_overlay{bin_dir=BinDir}) ->
494+
filename:join([BinDir, "riak-admin"]).
495+
496+
-spec riak_repl_path(#directory_overlay{}) -> path().
497+
riak_repl_path(#directory_overlay{bin_dir=BinDir}) ->
498+
filename:join([BinDir, "riak-repl"]).
499+

0 commit comments

Comments
 (0)