Skip to content

Commit

Permalink
MB-4411 A worker should wait for a signal from its parent process.
Browse files Browse the repository at this point in the history
A batch worker might finish its execution before its parent
process gets the worker's monitor Ref value. Consequently, this
causes a crash in mc_daemon process. To resolve this issue,
each batch worker should wait for a signal from its parent process
before starting its execution.

Change-Id: Iab63c1608a6e3fe6367654d996b7484d9f56b623
Reviewed-on: http://review.couchbase.org/10752
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Reviewed-by: Aliaksey Artamonau <aliaksiej.artamonau@gmail.com>
  • Loading branch information
chiyoung committed Nov 10, 2011
1 parent 2ea6aba commit 0f5c8ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/mc_batch_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ init([]) ->
start_worker(Sup, CurrentVBucket, CurrentList, BucketName, Socket) ->
{ok, Pid} = supervisor:start_child(Sup, [CurrentVBucket, CurrentList, BucketName, Socket]),
Ref = monitor(process, Pid),
Pid ! can_start,
{ok, Ref}.

start_link_worker(CurrentVBucket, CurrentList, BucketName, Socket) ->
{ok, proc_lib:spawn_link(?MODULE, sync_update_docs,
[CurrentVBucket, CurrentList, BucketName, Socket])}.

sync_update_docs(CurrentVBucket, CurrentList, BucketName, Socket) ->
receive
can_start ->
ok
end,

UpdateOptions =
[clobber, return_errors_only] ++
case couch_config:get("mc_couch", "optimistic_writes", "true") of
Expand Down
25 changes: 15 additions & 10 deletions src/mc_daemon.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
errors = [],
next_vb_batch,
current_vbucket,
current_vbucket_list,
current_vbucket_list = [],
max_workers = 4,
worker_sup,
caller = nil,
Expand Down Expand Up @@ -275,9 +275,9 @@ batching({?NOOP, Opaque}, From, State) ->
socket = Socket,
current_vbucket = CurrentVBucket, current_vbucket_list = CurrentList
} = State,
mc_batch_sup:sync_update_docs(CurrentVBucket, CurrentList, State#state.db, Socket),
State2 = State#state{current_vbucket = 0, current_vbucket_list = []},
case num_workers(State) of
State2 = maybe_start_worker(State#state{next_vb_batch = {CurrentVBucket, CurrentList},
current_vbucket = 0, current_vbucket_list = []}),
case num_workers(State2) of
0 ->
mc_connection:respond(Socket, ?NOOP, Opaque, #mc_response{}),
gen_fsm:reply(From, ok),
Expand Down Expand Up @@ -336,12 +336,17 @@ maybe_start_worker(State) ->
worker_refs = WorkerRefs,
next_vb_batch = {VBucket, ItemList}
} = State,
{ok, WorkerRef} = mc_batch_sup:start_worker(WorkerSup, VBucket, ItemList,
State#state.db, Socket),
State#state{
next_vb_batch = {},
worker_refs = [WorkerRef|WorkerRefs]
}.
case ItemList /= [] of
true ->
{ok, WorkerRef} = mc_batch_sup:start_worker(WorkerSup, VBucket, ItemList,
State#state.db, Socket),
State#state{
next_vb_batch = {VBucket, []},
worker_refs = [WorkerRef|WorkerRefs]
};
false ->
State
end.

handle_info({'DOWN', Ref, process, _Pid, normal}, batching, State) ->
#state{caller = From, worker_refs = WorkerRefs} = State,
Expand Down

0 comments on commit 0f5c8ce

Please sign in to comment.