-
Notifications
You must be signed in to change notification settings - Fork 77
/
riak_ensemble_msg.erl
329 lines (282 loc) · 11.8 KB
/
riak_ensemble_msg.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% 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.
%%
%% -------------------------------------------------------------------
%% TODO: Before PR. Add module edoc + doc functions
-module(riak_ensemble_msg).
-export([send_all/4,
send_all/5,
blocking_send_all/4,
wait_for_quorum/1,
handle_reply/4,
quorum_timeout/1,
reply/3]).
-include_lib("riak_ensemble_types.hrl").
%% -define(OUT(Fmt,Args), io:format(Fmt,Args)).
-define(OUT(Fmt,Args), ok).
%%%===================================================================
-type required() :: quorum | other | all.
-record(msgstate, {awaiting = undefined :: 'undefined' | reqid(),
timer = undefined :: 'undefined' | reference(),
required = quorum :: required(),
id :: peer_id(),
views = [] :: views(),
replies = [] :: [{peer_id(), any()}]}).
-opaque msg_state() :: #msgstate{}.
-export_type([msg_state/0]).
-type timer() :: reference().
-type reqid() :: reference().
-type msg() :: term().
-type peer_nack() :: {peer_id(), nack}.
-type msg_from() :: {riak_ensemble_msg,pid(),reference()}.
-type from() :: {pid(),reference()}.
-type maybe_from() :: undefined | from().
-type future() :: undefined | pid().
-export_type([future/0, msg_from/0]).
%%%===================================================================
-spec send_all(msg(), peer_id(), peer_pids(), views()) -> msg_state().
send_all(Msg, Id, Peers, Views) ->
send_all(Msg, Id, Peers, Views, quorum).
-spec send_all(msg(), peer_id(), peer_pids(), views(), required()) -> msg_state().
send_all(_Msg, Id, _Peers=[{Id,_}], _Views, _Required) ->
?OUT("~p: self-sending~n", [Id]),
gen_fsm:send_event(self(), {quorum_met, []}),
#msgstate{awaiting=undefined, timer=undefined, replies=[], id=Id};
send_all(Msg, Id, Peers, Views, Required) ->
?OUT("~p/~p: sending to ~p: ~p~n", [Id, self(), Peers, Msg]),
{ReqId, Request} = make_request(Msg),
_ = [maybe_send_request(Id, Peer, ReqId, Request) || Peer={PeerId,_} <- Peers,
PeerId =/= Id],
Timer = send_after(?ENSEMBLE_TICK, self(), quorum_timeout),
#msgstate{awaiting=ReqId, timer=Timer, replies=[], id=Id, views=Views,
required=Required}.
%%%===================================================================
-spec maybe_send_request(peer_id(), {peer_id(), maybe_pid()}, reqid(), msg()) -> ok.
-ifdef(TEST).
maybe_send_request(Id, {PeerId, PeerPid}, ReqId, Event) ->
case riak_ensemble_test:maybe_drop(Id, PeerId) of
true ->
%% TODO: Consider nacking instead
io:format("Dropping ~p -> ~p~n", [Id, PeerId]),
ok;
false ->
send_request({PeerId, PeerPid}, ReqId, Event)
end.
-else.
maybe_send_request(_Id, {PeerId, PeerPid}, ReqId, Event) ->
send_request({PeerId, PeerPid}, ReqId, Event).
-endif.
%%%===================================================================
-spec send_request({peer_id(), maybe_pid()}, reqid(), msg()) -> ok.
send_request({PeerId, PeerPid}, ReqId, Event) ->
case PeerPid of
undefined ->
?OUT("~p: Sending offline nack for ~p~n", [self(), PeerId]),
From = make_from(self(), ReqId),
reply(From, PeerId, nack);
_ ->
?OUT("~p: Sending to ~p: ~p~n", [self(), PeerId, Event]),
gen_fsm:send_event(PeerPid, Event)
end.
%%%===================================================================
-spec reply(msg_from(), peer_id(), any()) -> ok.
reply({riak_ensemble_msg, Sender, ReqId}, Id, Reply) ->
gen_fsm:send_all_state_event(Sender, {reply, ReqId, Id, Reply}).
%%%===================================================================
-spec blocking_send_all(msg(), peer_id(), peer_pids(), views())
-> {future(), msg_state()}.
blocking_send_all(Msg, Id, Peers, Views) ->
?OUT("~p: blocking_send_all to ~p: ~p~n", [Id, Peers, Msg]),
MsgState = #msgstate{awaiting=undefined, timer=undefined, replies=[], views=Views, id=Id},
Future = case Peers of
[{Id,_}] ->
undefined;
_ ->
spawn_link(fun() ->
collector(Msg, Peers, MsgState)
end)
end,
{Future, MsgState}.
-spec collector(msg(), peer_pids(), msg_state()) -> ok.
collector(Msg, Peers, #msgstate{id=Id, views=Views}) ->
{ReqId, Request} = make_request(Msg),
_ = [maybe_send_request(Id, Peer, ReqId, Request) || Peer={PeerId,_} <- Peers,
PeerId =/= Id],
collect_replies([], undefined, Id, Views, ReqId).
-spec collect_replies([peer_reply()], maybe_from(), peer_id(), views(), reqid()) -> ok.
collect_replies(Replies, Parent, Id, Views, ReqId) ->
receive
{'$gen_all_state_event', Event} ->
{reply, ReqId, Peer, Reply} = Event,
check_enough([{Peer, Reply}|Replies], Parent, Id, Views, ReqId);
{waiting, From, Ref} when is_pid(From), is_reference(Ref) ->
check_enough(Replies, {From, Ref}, Id, Views, ReqId)
after ?ENSEMBLE_TICK ->
maybe_timeout(Replies, Parent, Id, Views)
end.
maybe_timeout(Replies, undefined, Id, Views) ->
receive {waiting, From, Ref} ->
case quorum_met(Replies, Id, Views) of
true ->
From ! {Ref, ok, Replies},
ok;
_ ->
collect_timeout(Replies, {From, Ref})
end
end;
maybe_timeout(Replies, Parent, _Id, _Views) ->
collect_timeout(Replies, Parent).
-spec collect_timeout([peer_reply()], from()) -> ok.
collect_timeout(Replies, {From, Ref}) ->
From ! {Ref, timeout, Replies},
ok.
-spec check_enough([peer_reply()], maybe_from(), peer_id(), views(), reqid()) -> ok.
check_enough(Replies, undefined, Id, Views, ReqId) ->
collect_replies(Replies, undefined, Id, Views, ReqId);
check_enough(Replies, Parent={From, Ref}, Id, Views, ReqId) ->
case quorum_met(Replies, Id, Views) of
true ->
From ! {Ref, ok, Replies},
ok;
nack ->
collect_timeout(Replies, Parent);
false ->
collect_replies(Replies, Parent, Id, Views, ReqId)
end.
-spec wait_for_quorum(future()) -> {quorum_met, [peer_reply()]} |
{timeout, [peer_reply()]}.
wait_for_quorum(undefined) ->
{quorum_met, []};
wait_for_quorum(Pid) ->
Ref = make_ref(),
Pid ! {waiting, self(), Ref},
receive
{Ref, ok, Replies} ->
{Valid, _Nacks} = find_valid(Replies),
{quorum_met, Valid};
{Ref, timeout, Replies} ->
{timeout, Replies}
end.
%%%===================================================================
-spec handle_reply(any(), peer_id(), any(), msg_state()) -> msg_state().
handle_reply(ReqId, Peer, Reply, MsgState=#msgstate{awaiting=Awaiting}) ->
case ReqId == Awaiting of
true ->
add_reply(Peer, Reply, MsgState);
false ->
MsgState
end.
-spec add_reply(peer_id(), any(), msg_state()) -> msg_state().
add_reply(Peer, Reply, MsgState=#msgstate{timer=Timer}) ->
Replies = [{Peer, Reply} | MsgState#msgstate.replies],
case quorum_met(Replies, MsgState) of
true ->
cancel_timer(Timer),
{Valid, _Nacks} = find_valid(Replies),
gen_fsm:send_event(self(), {quorum_met, Valid}),
MsgState#msgstate{replies=[], awaiting=undefined, timer=undefined};
false ->
MsgState#msgstate{replies=Replies};
nack ->
cancel_timer(Timer),
quorum_timeout(MsgState#msgstate{replies=Replies})
end.
-spec quorum_timeout(msg_state()) -> msg_state().
quorum_timeout(#msgstate{replies=Replies}) ->
{Valid, _Nacks} = find_valid(Replies),
gen_fsm:send_event(self(), {timeout, Valid}),
#msgstate{awaiting=undefined, timer=undefined, replies=[]}.
%%%===================================================================
-spec quorum_met([peer_reply()], msg_state()) -> true | false | nack.
quorum_met(Replies, #msgstate{id=Id, views=Views, required=Required}) ->
quorum_met(Replies, Id, Views, Required).
-spec quorum_met([peer_reply()], peer_id(), views()) -> true | false | nack.
quorum_met(Replies, Id, Views) ->
quorum_met(Replies, Id, Views, quorum).
-spec quorum_met([peer_reply()], peer_id(), views(), required()) -> true | false | nack.
quorum_met(_Replies, _Id, [], _Required) ->
true;
quorum_met(Replies, Id, [Members|Views], Required) ->
Filtered = [Reply || Reply={Peer,_} <- Replies,
lists:member(Peer, Members)],
{Valid, Nacks} = find_valid(Filtered),
Quorum = case Required of
quorum ->
length(Members) div 2 + 1;
other ->
length(Members) div 2 + 1;
all ->
length(Members)
end,
Heard = case (Required =/= other) andalso lists:member(Id, Members) of
true ->
length(Valid) + 1;
false ->
length(Valid)
end,
if Heard >= Quorum ->
?OUT("~p//~nM: ~p~nV: ~p~nN: ~p: view-met~n", [Id, Members, Valid, Nacks]),
quorum_met(Replies, Id, Views);
length(Nacks) >= Quorum ->
?OUT("~p//~nM: ~p~nV: ~p~nN: ~p: nack~n", [Id, Members, Valid, Nacks]),
nack;
(Heard + length(Nacks)) =:= length(Members) ->
?OUT("~p//~nM: ~p~nV: ~p~nN: ~p: nack~n", [Id, Members, Valid, Nacks]),
nack;
true ->
?OUT("~p//~nM: ~p~nV: ~p~nN: ~p: false~n", [Id, Members, Valid, Nacks]),
false
end.
-spec find_valid([peer_reply()]) -> {[peer_reply()], [peer_nack()]}.
find_valid(Replies) ->
{Valid, Nacks} = lists:partition(fun({_, nack}) ->
false;
(_) ->
true
end, Replies),
{Valid, Nacks}.
%%%===================================================================
-spec make_request(msg()) -> {reqid(), tuple()}.
make_request(Msg) ->
ReqId = make_ref(),
From = make_from(self(), ReqId),
Request = if is_tuple(Msg) ->
erlang:append_element(Msg, From);
true ->
{Msg, From}
end,
{ReqId, Request}.
-spec make_from(pid(), reqid()) -> msg_from().
make_from(Pid, ReqId) ->
{riak_ensemble_msg, Pid, ReqId}.
%%%===================================================================
-spec send_after(timeout(), pid(), msg()) -> timer().
send_after(Time, Dest, Msg) ->
erlang:send_after(Time, Dest, Msg).
-spec cancel_timer(timer()) -> ok.
cancel_timer(Timer) ->
case erlang:cancel_timer(Timer) of
false ->
receive
quorum_timeout -> ok
after
0 -> ok
end;
_ ->
ok
end.