Skip to content

Commit 7568841

Browse files
committedMar 7, 2017
dirty_query
1 parent 87e4edf commit 7568841

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ erl_crash.dump
1010
/logs/
1111
/plugins/
1212
/xrefr
13+
elvis
1314

1415
rabbitmq_rtopic_exchange.d
1516
*.plt

‎elvis.config

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
elvis,
4+
[
5+
{config,
6+
[#{dirs => ["src"],
7+
filter => "*.erl",
8+
ruleset => erl_files
9+
},
10+
#{dirs => ["."],
11+
filter => "Makefile",
12+
ruleset => makefiles
13+
},
14+
#{dirs => ["."],
15+
filter => "rebar.config",
16+
ruleset => rebar_config
17+
},
18+
#{dirs => ["."],
19+
filter => "elvis.config",
20+
ruleset => elvis_config
21+
}
22+
]
23+
}
24+
]
25+
}
26+
].

‎src/ra.erl

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
start_cluster/4,
77
send/2,
88
send_and_await_consensus/2,
9-
send_and_notify/2
9+
send_and_notify/2,
10+
dirty_query/2
1011
]).
1112

1213
start_cluster(Num, Name, ApplyFun, InitialState) ->
@@ -37,3 +38,8 @@ send_and_await_consensus(Ref, Data) ->
3738
{ok, IdxTerm::{ra_index(), ra_term()}, Leader::ra_node_proc:server_ref()}.
3839
send_and_notify(Ref, Data) ->
3940
ra_node_proc:command(Ref, Data, notify_on_consensus).
41+
42+
-spec dirty_query(Node::ra_node_proc:server_ref(), QueryFun::fun((term()) -> term())) ->
43+
{ok, {ra_index(), ra_term()}, term()}.
44+
dirty_query(Node, QueryFun) ->
45+
ra_node_proc:query(Node, QueryFun, dirty).

‎src/ra_node_proc.erl

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
%% API
88
-export([start_link/1,
9-
command/3
9+
command/3,
10+
query/3
1011
]).
1112

1213
%% State functions
@@ -37,6 +38,8 @@
3738
-type pending_reply() :: {reply | notify, {ra_index(), ra_term()},
3839
From::term()}.
3940

41+
-type query_fun() :: fun((term()) -> term()).
42+
4043
-export_type([server_ref/0]).
4144

4245
-record(state, {node_state :: ra_node:ra_node_state(),
@@ -64,6 +67,12 @@ command(ServerRef, Data, ReplyMode) ->
6467
Reply -> {ok, Reply, ServerRef}
6568
end.
6669

70+
-spec query(ra_node_proc:server_ref(), query_fun(), dirty | consensus) ->
71+
{ok, IdxTerm::{ra_index(), ra_term()}, term()}.
72+
query(ServerRef, QueryFun, dirty) ->
73+
gen_statem:call(ServerRef, {query, QueryFun, dirty}).
74+
75+
6776
%%%===================================================================
6877
%%% gen_statem callbacks
6978
%%%===================================================================
@@ -162,6 +171,9 @@ follower({call, From}, {command, _Data, _Flag},
162171
follower({call, From}, {command, _Data, _Flag} = Cmd,
163172
State = #state{pending_commands = Pending}) ->
164173
{keep_state, State#state{pending_commands = [{From, Cmd} | Pending]}};
174+
follower({call, From}, {query, QueryFun, dirty}, State = #state{node_state = NodeState}) ->
175+
Reply = perform_query(QueryFun, dirty, NodeState),
176+
{keep_state, State, [{reply, From, Reply}]};
165177
follower(EventType, Msg,
166178
State0 = #state{node_state = NodeState0 = #{id := Id}}) ->
167179
?DBG("~p follower: ~p~n", [Id, Msg]),
@@ -179,7 +191,6 @@ follower(EventType, Msg,
179191
election_timeout_action(State)}
180192
end.
181193

182-
183194
handle_event(_EventType, EventContent, StateName, State) ->
184195
?DBG("handle_event unknownn ~p~n", [EventContent]),
185196
{next_state, StateName, State}.
@@ -203,6 +214,11 @@ format_status(_Opt, [_PDict, _StateName, _State]) ->
203214
%%% Internal functions
204215
%%%===================================================================
205216

217+
perform_query(QueryFun, dirty, #{machine_state := MacState,
218+
last_applied := Last,
219+
current_term := Term}) ->
220+
{ok, {Last, Term}, QueryFun(MacState)}.
221+
206222
make_caller_reply_actions(State = #state{pending_replies = []}) ->
207223
{State, [], []};
208224
make_caller_reply_actions(State = #state{pending_replies = Pending,

‎test/ra_SUITE.erl

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ all() ->
88
[
99
queue,
1010
send_and_await_consensus,
11-
send_and_notify
11+
send_and_notify,
12+
dirty_query
1213
].
1314

1415
groups() ->
@@ -34,10 +35,7 @@ queue(_Config) ->
3435
send_and_await_consensus(_Config) ->
3536
[{APid, _A}, _B, _C] = Cluster =
3637
ra:start_cluster(3, "test", fun erlang:'+'/2, 9),
37-
3838
{ok, {1, 1}, _Leader} = ra:send_and_await_consensus(APid, 5),
39-
40-
% {{1, 1}, 14} = ra:query(APid, fun(S) -> S end, dirty),
4139
terminate_cluster(Cluster).
4240

4341
send_and_notify(_Config) ->
@@ -51,6 +49,13 @@ send_and_notify(_Config) ->
5149
end,
5250
terminate_cluster(Cluster).
5351

52+
dirty_query(_Config) ->
53+
[{APid, _A}, _B, _C] = Cluster =
54+
ra:start_cluster(3, "test", fun erlang:'+'/2, 9),
55+
{ok, {1, 1}, _Leader} = ra:send_and_await_consensus(APid, 5),
56+
{ok, {1, 1}, 14} = ra:dirty_query(APid, fun(S) -> S end),
57+
terminate_cluster(Cluster).
58+
5459
% implements a simple queue machine
5560
queue_apply({enqueue, Msg}, State =#{queue := Q0, pending_dequeues := []}) ->
5661
Q = queue:in(Msg, Q0),

‎test/ra_node_SUITE.erl

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ all() ->
1717
node_steps_down
1818
].
1919

20+
groups() ->
21+
[ {tests, [], all()} ].
22+
2023

2124
node_steps_down(_Config) ->
2225
State = base_state(3),

0 commit comments

Comments
 (0)
Please sign in to comment.