/
fabric_streams.erl
360 lines (336 loc) · 12.6 KB
/
fabric_streams.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
% Licensed 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.
-module(fabric_streams).
-export([
start/2,
start/3,
start/4,
start/5,
cleanup/1
]).
-include_lib("fabric/include/fabric.hrl").
-include_lib("mem3/include/mem3.hrl").
-define(WORKER_CLEANER, fabric_worker_cleaner).
start(Workers, Keypos) ->
start(Workers, Keypos, undefined, undefined).
start(Workers, Keypos, RingOpts) ->
start(Workers, Keypos, undefined, undefined, RingOpts).
start(Workers, Keypos, StartFun, Replacements) ->
start(Workers, Keypos, StartFun, Replacements, []).
start(Workers0, Keypos, StartFun, Replacements, RingOpts) ->
Fun = fun handle_stream_start/3,
Acc = #stream_acc{
workers = fabric_dict:init(Workers0, waiting),
ready = [],
start_fun = StartFun,
replacements = Replacements,
ring_opts = RingOpts
},
ClientReq = chttpd_util:mochiweb_client_req_get(),
spawn_worker_cleaner(self(), Workers0, ClientReq),
Timeout = fabric_util:request_timeout(),
case rexi_utils:recv(Workers0, Keypos, Fun, Acc, Timeout, infinity) of
{ok, #stream_acc{ready = Workers}} ->
AckedWorkers = fabric_dict:fold(
fun(Worker, From, WorkerAcc) ->
rexi:stream_start(From),
[Worker | WorkerAcc]
end,
[],
Workers
),
{ok, AckedWorkers};
Else ->
Else
end.
cleanup(Workers) ->
% Stop the auxiliary cleaner process as we got to the point where cleanup
% happesn in the regular fashion so we don't want to send 2x the number kill
% messages
case get(?WORKER_CLEANER) of
CleanerPid when is_pid(CleanerPid) ->
erase(?WORKER_CLEANER),
exit(CleanerPid, kill);
_ ->
ok
end,
fabric_util:cleanup(Workers).
handle_stream_start({rexi_DOWN, _, {_, NodeRef}, _}, _, St) ->
#stream_acc{workers = Workers, ready = Ready, ring_opts = RingOpts} = St,
case fabric_ring:node_down(NodeRef, Workers, Ready, RingOpts) of
{ok, Workers1} ->
{ok, St#stream_acc{workers = Workers1}};
error ->
{error, {nodedown, <<"progress not possible">>}}
end;
handle_stream_start({rexi_EXIT, Reason}, Worker, St) ->
#stream_acc{
workers = Workers,
ready = Ready,
replacements = Replacements,
ring_opts = RingOpts
} = St,
case {fabric_ring:handle_error(Worker, Workers, Ready, RingOpts), Reason} of
{{ok, Workers1}, _Reason} ->
{ok, St#stream_acc{workers = Workers1}};
{error, {maintenance_mode, _Node}} when Replacements /= undefined ->
% Check if we have replacements for this range
% and start the new workers if so.
case lists:keytake(Worker#shard.range, 1, Replacements) of
{value, {_Range, WorkerReplacements}, NewReplacements} ->
FinalWorkers = lists:foldl(
fun(Repl, NewWorkers) ->
NewWorker = (St#stream_acc.start_fun)(Repl),
add_worker_to_cleaner(self(), NewWorker),
fabric_dict:store(NewWorker, waiting, NewWorkers)
end,
Workers,
WorkerReplacements
),
% Assert that our replaced worker provides us
% the oppurtunity to make progress. Need to make sure
% to include already processed responses, since we are
% checking the full range and some workers have already
% responded and were removed from the workers list
ReadyWorkers = [{W, R} || {_, W, R} <- Ready],
AllWorkers = FinalWorkers ++ ReadyWorkers,
true = fabric_ring:is_progress_possible(AllWorkers),
NewRefs = fabric_dict:fetch_keys(FinalWorkers),
{new_refs, NewRefs, St#stream_acc{
workers = FinalWorkers,
replacements = NewReplacements
}};
false ->
% If we progress isn't possible and we don't have any
% replacements then we're dead in the water.
{error, {nodedown, <<"progress not possible">>}}
end;
{error, _} ->
{error, fabric_util:error_info(Reason)}
end;
handle_stream_start(rexi_STREAM_INIT, {Worker, From}, St) ->
#stream_acc{workers = Workers, ready = Ready, ring_opts = RingOpts} = St,
case fabric_dict:lookup_element(Worker, Workers) of
undefined ->
% This worker lost the race with other partition copies, terminate
rexi:stream_cancel(From),
{ok, St};
waiting ->
case fabric_ring:handle_response(Worker, From, Workers, Ready, RingOpts) of
{ok, {Workers1, Ready1}} ->
% Don't have a full ring yet. Keep getting responses
{ok, St#stream_acc{workers = Workers1, ready = Ready1}};
{stop, Ready1} ->
% Have a full ring of workers. But don't ack the worker
% yet so they don't start sending us rows until we're ready
{stop, St#stream_acc{workers = [], ready = Ready1}}
end
end;
handle_stream_start({ok, Error}, _, St) when Error == ddoc_updated; Error == insufficient_storage ->
WaitingWorkers = [W || {W, _} <- St#stream_acc.workers],
ReadyWorkers = [W || {W, _} <- St#stream_acc.ready],
cleanup(WaitingWorkers ++ ReadyWorkers),
{stop, Error};
handle_stream_start(Else, _, _) ->
exit({invalid_stream_start, Else}).
% Spawn an auxiliary rexi worker cleaner. This will be used in cases
% when the coordinator (request) process is forceably killed and doesn't
% get a chance to process its `after` fabric:clean/1 clause.
spawn_worker_cleaner(Coordinator, Workers, ClientReq) ->
case get(?WORKER_CLEANER) of
undefined ->
Pid = spawn(fun() ->
erlang:monitor(process, Coordinator),
cleaner_loop(Coordinator, Workers, ClientReq)
end),
put(?WORKER_CLEANER, Pid),
Pid;
ExistingCleaner ->
ExistingCleaner
end.
cleaner_loop(Pid, Workers, ClientReq) ->
CheckMSec = chttpd_util:mochiweb_client_req_check_msec(),
receive
{add_worker, Pid, Worker} ->
cleaner_loop(Pid, [Worker | Workers], ClientReq);
{'DOWN', _, _, Pid, _} ->
fabric_util:cleanup(Workers)
after CheckMSec ->
chttpd_util:stop_client_process_if_disconnected(Pid, ClientReq),
cleaner_loop(Pid, Workers, ClientReq)
end.
add_worker_to_cleaner(CoordinatorPid, Worker) ->
case get(?WORKER_CLEANER) of
CleanerPid when is_pid(CleanerPid) ->
CleanerPid ! {add_worker, CoordinatorPid, Worker};
_ ->
ok
end.
-ifdef(TEST).
-include_lib("couch/include/couch_eunit.hrl").
worker_cleaner_test_() ->
{
"Fabric spawn_worker_cleaner test",
{
foreach,
fun setup/0,
fun teardown/1,
[
?TDEF_FE(should_clean_workers),
?TDEF_FE(does_not_fire_if_cleanup_called),
?TDEF_FE(should_clean_additional_worker_too),
?TDEF_FE(coordinator_is_killed_if_client_disconnects),
?TDEF_FE(coordinator_is_not_killed_if_client_is_connected)
]
}
}.
should_clean_workers(_) ->
meck:reset(rexi),
erase(?WORKER_CLEANER),
Workers = [
#shard{node = 'n1', ref = make_ref()},
#shard{node = 'n2', ref = make_ref()}
],
{Coord, _} = spawn_monitor(fun() ->
receive
die -> ok
end
end),
Cleaner = spawn_worker_cleaner(Coord, Workers, undefined),
Ref = erlang:monitor(process, Cleaner),
Coord ! die,
receive
{'DOWN', Ref, _, Cleaner, _} -> ok
end,
?assertEqual(1, meck:num_calls(rexi, kill_all, 1)).
does_not_fire_if_cleanup_called(_) ->
meck:reset(rexi),
erase(?WORKER_CLEANER),
Workers = [
#shard{node = 'n1', ref = make_ref()},
#shard{node = 'n2', ref = make_ref()}
],
{Coord, _} = spawn_monitor(fun() ->
receive
die -> ok
end
end),
Cleaner = spawn_worker_cleaner(Coord, Workers, undefined),
Ref = erlang:monitor(process, Cleaner),
cleanup(Workers),
Coord ! die,
receive
{'DOWN', Ref, _, _, _} -> ok
end,
% 2 calls would be from cleanup/1 function. If cleanup process fired
% too it would have been 4 calls total.
?assertEqual(1, meck:num_calls(rexi, kill_all, 1)).
should_clean_additional_worker_too(_) ->
meck:reset(rexi),
erase(?WORKER_CLEANER),
Workers = [
#shard{node = 'n1', ref = make_ref()}
],
{Coord, _} = spawn_monitor(fun() ->
receive
die -> ok
end
end),
Cleaner = spawn_worker_cleaner(Coord, Workers, undefined),
add_worker_to_cleaner(Coord, #shard{node = 'n2', ref = make_ref()}),
Ref = erlang:monitor(process, Cleaner),
Coord ! die,
receive
{'DOWN', Ref, _, Cleaner, _} -> ok
end,
?assertEqual(1, meck:num_calls(rexi, kill_all, 1)).
coordinator_is_killed_if_client_disconnects(_) ->
meck:reset(rexi),
erase(?WORKER_CLEANER),
Workers = [
#shard{node = 'n1', ref = make_ref()},
#shard{node = 'n2', ref = make_ref()}
],
{Coord, CoordRef} = spawn_monitor(fun() ->
receive
die -> ok
end
end),
Headers = mochiweb_headers:make([]),
{ok, Sock} = gen_tcp:listen(0, [{active, false}]),
ClientReq = mochiweb_request:new(Sock, 'GET', "/foo", {1, 1}, Headers),
% Close the socket and then expect coordinator to be killed
ok = gen_tcp:close(Sock),
Cleaner = spawn_worker_cleaner(Coord, Workers, ClientReq),
CleanerRef = erlang:monitor(process, Cleaner),
% Assert the correct behavior on the support platforms (all except Windows so far)
case os:type() of
{unix, Type} when
Type =:= linux;
Type =:= darwin;
Type =:= freebsd;
Type =:= openbsd;
Type =:= netbsd
->
% Coordinator should be torn down
receive
{'DOWN', CoordRef, _, _, Reason} ->
?assertEqual({shutdown, client_disconnected}, Reason)
end,
% Cleaner process itself should exit
receive
{'DOWN', CleanerRef, _, _, _} -> ok
end,
% Workers should have been killed
?assertEqual(1, meck:num_calls(rexi, kill_all, 1));
{_, _} = OsType ->
?debugFmt("~n * Client disconnect test not yet supported on ~p~n", [OsType])
end.
coordinator_is_not_killed_if_client_is_connected(_) ->
meck:reset(rexi),
erase(?WORKER_CLEANER),
Workers = [
#shard{node = 'n1', ref = make_ref()},
#shard{node = 'n2', ref = make_ref()}
],
{Coord, CoordRef} = spawn_monitor(fun() ->
receive
die -> ok
end
end),
Headers = mochiweb_headers:make([]),
{ok, Sock} = gen_tcp:listen(0, [{active, false}]),
ClientReq = mochiweb_request:new(Sock, 'GET', "/foo", {1, 1}, Headers),
Cleaner = spawn_worker_cleaner(Coord, Workers, ClientReq),
CleanerRef = erlang:monitor(process, Cleaner),
% Coordinator should stay up
receive
{'DOWN', CoordRef, _, Coord, _} ->
?assert(false, {unexpected_coordinator_exit, Coord})
after 1000 ->
?assert(is_process_alive(Coord))
end,
% Cleaner process stays up
?assert(is_process_alive(Cleaner)),
% Tear everything down at the end of the test
gen_tcp:close(Sock),
Coord ! die,
receive
{'DOWN', CleanerRef, _, _, _} -> ok
end.
setup() ->
ok = meck:expect(rexi, kill_all, fun(_) -> ok end),
% Speed up disconnect socket timeout for the test to 200 msec
ok = meck:expect(chttpd_util, mochiweb_client_req_check_msec, 0, 200).
teardown(_) ->
meck:unload().
-endif.