diff options
Diffstat (limited to 'src/custodian/src/custodian_util.erl')
-rw-r--r-- | src/custodian/src/custodian_util.erl | 58 |
1 files changed, 23 insertions, 35 deletions
diff --git a/src/custodian/src/custodian_util.erl b/src/custodian/src/custodian_util.erl index ee217108f..6d5a56093 100644 --- a/src/custodian/src/custodian_util.erl +++ b/src/custodian/src/custodian_util.erl @@ -11,7 +11,6 @@ % the License. -module(custodian_util). --include("custodian.hrl"). -include_lib("mem3/include/mem3.hrl"). -include_lib("couch/include/couch_db.hrl"). @@ -19,7 +18,10 @@ -export([summary/0, report/0]). -export([ensure_dbs_exists/0]). --record(state, {live, safe, n, callback, db, acc}). +% Old design doc which should be cleaned up +-define(CUSTODIAN_ID, <<"_design/custodian">>). + +-record(state, {live, safe, callback, db, acc}). %% public functions. @@ -45,7 +47,7 @@ report() -> ensure_dbs_exists() -> DbName = mem3_sync:shards_db(), {ok, Db} = mem3_util:ensure_exists(DbName), - ensure_custodian_ddoc_exists(Db), + ensure_custodian_ddoc_is_deleted(Db), {ok, Db}. %% private functions. @@ -53,10 +55,9 @@ ensure_dbs_exists() -> fold_dbs(Acc, Fun) -> Safe = maybe_redirect([node() | nodes()]), Live = Safe -- maintenance_nodes(Safe), - N = cluster_n(), {ok, Db} = ensure_dbs_exists(), try - State0 = #state{live=Live, safe=Safe, n=N, callback=Fun, db=Db, acc=Acc}, + State0 = #state{live=Live, safe=Safe, callback=Fun, db=Db, acc=Acc}, {ok, State1} = couch_db:fold_docs(Db, fun fold_dbs1/2, State0, []), State1#state.acc after @@ -80,9 +81,9 @@ fold_dbs1(#full_doc_info{id = Id} = FDI, State) -> fold_dbs(Id, Shards, State) -> IsSafe = fun(#shard{node = N}) -> lists:member(N, State#state.safe) end, IsLive = fun(#shard{node = N}) -> lists:member(N, State#state.live) end, - TargetN = State#state.n, LiveShards = lists:filter(IsLive, Shards), SafeShards = lists:filter(IsSafe, Shards), + TargetN = mem3_util:calculate_max_n(Shards), Acc0 = State#state.acc, Acc1 = case mem3_util:calculate_max_n(LiveShards) of LiveN when LiveN < TargetN -> @@ -180,41 +181,28 @@ count_conflicts(#full_doc_info{rev_tree = T}) -> Leafs = [1 || {#leaf{deleted=false}, _} <- couch_key_tree:get_all_leafs(T)], length(Leafs) - 1. -ensure_custodian_ddoc_exists(Db) -> + +% Ensure the design doc which was added 3.2.0 is deleted as we switched to using a BDU +% function instead. After a few releases this function could be removed as well +% +ensure_custodian_ddoc_is_deleted(Db) -> case couch_db:open_doc(Db, ?CUSTODIAN_ID, [ejson_body]) of {not_found, _Reason} -> - try couch_db:update_doc(Db, custodian_ddoc(), []) of - {ok, _} -> - ok - catch conflict -> - {ok, NewDb} = couch_db:reopen(Db), - ensure_custodian_ddoc_exists(NewDb) - end; + ok; {ok, Doc} -> - {Props} = couch_doc:to_json_obj(Doc, []), - Props1 = lists:keystore(<<"validate_doc_update">>, 1, Props, {<<"validate_doc_update">>, ?CUSTODIAN_VALIDATION}), - case Props =:= Props1 of - true -> - ok; - false -> - try couch_db:update_doc(Db, couch_doc:from_json_obj({Props1}), []) of - {ok, _} -> - ok - catch conflict -> - {ok, NewDb} = couch_db:reopen(Db), - ensure_custodian_ddoc_exists(NewDb) - end + DeletedDoc = Doc#doc{deleted = true, body = {[]}}, + try couch_db:update_doc(Db, DeletedDoc, [?ADMIN_CTX]) of + {ok, _} -> + LogMsg = "~p : deleted custodian ddoc ~s", + couch_log:notice(LogMsg, [?MODULE, ?CUSTODIAN_ID]), + ok + catch + conflict -> + {ok, NewDb} = couch_db:reopen(Db), + ensure_custodian_ddoc_is_deleted(NewDb) end end. -custodian_ddoc() -> - Props = [ - {<<"_id">>, ?CUSTODIAN_ID}, - {<<"language">>, <<"javascript">>}, - {<<"validate_doc_update">>, ?CUSTODIAN_VALIDATION} - ], - couch_doc:from_json_obj({Props}). - -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). |