Skip to content

Commit

Permalink
MB-6517 Delete index files only if there are no aliases
Browse files Browse the repository at this point in the history
Regression introduced with MB-6415.

Change-Id: Ie6b5d0c61234d67d17a2767867b871ff765a7e82
Reviewed-on: http://review.couchbase.org/20481
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
  • Loading branch information
fdmanana authored and Peter Wansch committed Sep 5, 2012
1 parent 00d291d commit 884ba59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
15 changes: 9 additions & 6 deletions src/couch_set_view/src/couch_set_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,12 @@ cleanup_index_files(SetName) ->
couch_db:close(Db),

% make unique list of group sigs
Sigs0 = lists:map(fun(DDoc) ->
Sigs = lists:map(fun(DDoc) ->
#set_view_group{sig = Sig} =
couch_set_view_util:design_doc_to_set_view_group(SetName, DDoc),
couch_util:to_hex(Sig)
end,
[DD || DD <- DesignDocs, not DD#doc.deleted]),
SigsPrevRevs = ets:match(couch_sig_to_setview_pid, {{SetName, '$1'}, '$2'}),
Sigs = Sigs0 ++ [couch_util:to_hex(Sig) || {{_SetName, Sig}, _Pid} <- SigsPrevRevs],

FileList = list_index_files(SetName),

Expand Down Expand Up @@ -1022,9 +1020,14 @@ handle_db_event({ddoc_updated, {DbName, #doc{id = DDocId} = DDoc}}) ->
lists:foreach(
fun({_SetName, {_DDocId, Sig}}) ->
case ets:lookup(couch_sig_to_setview_pid, {SetName, Sig}) of
[{_, GroupPid}] ->
(catch gen_server:cast(GroupPid, {ddoc_updated, NewSig}));
[] ->
[{_, GroupPid}] when is_pid(GroupPid), Sig =/= NewSig->
Aliases = [
AliasDDocId || {_SetName2, {AliasDDocId, _Sig2}} <-
ets:match_object(couch_setview_name_to_sig, {SetName, {'$1', Sig}}),
AliasDDocId =/= DDocId
],
ok = gen_server:cast(GroupPid, {ddoc_updated, NewSig, Aliases});
_ ->
ok
end
end,
Expand Down
24 changes: 17 additions & 7 deletions src/couch_set_view/src/couch_set_view_group.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
waiting_list = [] :: [#waiter{}],
cleaner_pid = nil :: 'nil' | pid(),
shutdown = false :: boolean(),
shutdown_aliases :: [binary()],
auto_cleanup = true :: boolean(),
replica_partitions = [] :: ordsets:ordset(partition_id()),
pending_transition_waiters = [] :: [{From::{pid(), reference()}, #set_view_group_req{}}],
Expand Down Expand Up @@ -824,26 +825,29 @@ handle_cast({partial_update, _, _}, State) ->
%% message from an old (probably pre-compaction) updater; ignore
{noreply, State, ?TIMEOUT};

handle_cast({ddoc_updated, NewSig}, State) ->
handle_cast({ddoc_updated, NewSig, Aliases}, State) ->
#state{
waiting_list = Waiters,
group = #set_view_group{sig = CurSig}
} = State,
?LOG_INFO("Set view `~s`, ~s group `~s`, signature `~s', design document was updated~n"
" new signature: ~s~n"
" current aliases: ~p~n"
" shutdown flag: ~s~n"
" waiting clients: ~p~n",
[?set_name(State), ?type(State), ?group_id(State),
hex_sig(CurSig), hex_sig(NewSig), State#state.shutdown, length(Waiters)]),
hex_sig(CurSig), hex_sig(NewSig), Aliases,
State#state.shutdown, length(Waiters)]),
case NewSig of
CurSig ->
{noreply, State#state{shutdown = false}, ?TIMEOUT};
{noreply, State#state{shutdown = false, shutdown_aliases = undefined}, ?TIMEOUT};
_ ->
State2 = State#state{shutdown = true, shutdown_aliases = Aliases},
case Waiters of
[] ->
{stop, normal, State#state{shutdown = true}};
{stop, normal, State2};
_ ->
{noreply, State#state{shutdown = true}}
{noreply, State2}
end
end;

Expand Down Expand Up @@ -1192,8 +1196,14 @@ terminate(Reason, #state{group = #set_view_group{sig = Sig} = Group} = State) ->
% group got spawned again. Problem here is during rebalance, where one of the
% databases might have been deleted after the last ddoc update and while or after
% we're here in terminate, so we must ensure we don't leave old index files around.
delete_index_file(?root_dir(State), Group, main),
delete_index_file(?root_dir(State), Group, replica);
% MB-6415 and MB-6517
case State#state.shutdown_aliases of
[] ->
delete_index_file(?root_dir(State), Group, main),
delete_index_file(?root_dir(State), Group, replica);
_ ->
ok
end;
_ ->
ok
end,
Expand Down
20 changes: 4 additions & 16 deletions src/couch_set_view/test/02-old-index-cleanup.t
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ test() ->
ok = timer:sleep(1500),

AllIndexFiles3 = all_index_files(),
etap:is(lists:member(NewIndexFile, AllIndexFiles3), false,
"Index file not found after deleting original ddoc"),
etap:is(lists:member(NewIndexFile, AllIndexFiles3), true,
"Index file found after deleting original ddoc"),

etap:is(ets:lookup(couch_setview_name_to_sig, test_set_name()),
[],
Expand All @@ -160,13 +160,7 @@ test() ->

NewGroupPid3 = couch_set_view:get_group_pid(test_set_name(), ddoc_id_copy()),
etap:isnt(NewGroupPid3, NewGroupPid2, "New group pid after deleting original ddoc"),
ok = couch_set_view_test_util:define_set_view(
test_set_name(),
ddoc_id_copy(),
num_set_partitions(),
lists:seq(0, num_set_partitions() - 1),
[]),
query_view(ddoc_id_copy(), 0, "stale=ok"),
query_view(ddoc_id_copy(), num_docs(), "stale=ok"),
etap:diag("Got empty results after deleting original ddoc and querying "
"ddoc copy with ?stale=ok"),
etap:is(is_process_alive(NewGroupPid3), true,
Expand Down Expand Up @@ -249,13 +243,7 @@ test_recreate_ddoc_with_copy() ->
[],
"No group entry in couch_sig_to_setview_pid ets table"),

ok = couch_set_view_test_util:define_set_view(
test_set_name(),
ddoc_id(),
num_set_partitions(),
lists:seq(0, num_set_partitions() - 1),
[]),
query_view(ddoc_id(), 0, "stale=ok"),
query_view(ddoc_id(), num_docs(), "stale=ok"),

etap:diag("Deleting original ddoc"),
ok = couch_set_view_test_util:delete_ddoc(test_set_name(), ddoc_id()),
Expand Down

0 comments on commit 884ba59

Please sign in to comment.