jiangphcn commented on a change in pull request #1370: [5/5] Clustered Purge Implementation URL: https://github.com/apache/couchdb/pull/1370#discussion_r196833296
########## File path: src/couch/src/couch_db.erl ########## @@ -369,8 +379,153 @@ get_full_doc_info(Db, Id) -> get_full_doc_infos(Db, Ids) -> couch_db_engine:open_docs(Db, Ids). -purge_docs(#db{main_pid=Pid}, IdsRevs) -> - gen_server:call(Pid, {purge_docs, IdsRevs}). +purge_docs(Db, IdRevs) -> + purge_docs(Db, IdRevs, []). + +-spec purge_docs(#db{}, [{UUId, Id, [Rev]}], [PurgeOption]) -> + {ok, [Reply]} when + UUId :: binary(), + Id :: binary(), + Rev :: {non_neg_integer(), binary()}, + PurgeOption :: interactive_edit | replicated_changes, + Reply :: {ok, []} | {ok, [Rev]}. +purge_docs(#db{main_pid = Pid} = Db, UUIDsIdsRevs, Options) -> + % Check here if any UUIDs already exist when + % we're not replicating purge infos + IsRepl = lists:member(replicated_changes, Options), + if IsRepl -> ok; true -> + UUIDs = [UUID || {UUID, _, _} <- UUIDsIdsRevs], + lists:foreach(fun(Resp) -> + if Resp == not_found -> ok; true -> + Fmt = "Duplicate purge info UIUD: ~s", + Reason = io_lib:format(Fmt, [element(2, Resp)]), + throw({badreq, Reason}) + end + end, get_purge_infos(Db, UUIDs)) + end, + increment_stat(Db, [couchdb, database_purges]), + gen_server:call(Pid, {purge_docs, UUIDsIdsRevs, Options}). + +-spec get_purge_infos(#db{}, [UUId]) -> [PurgeInfo] when + UUId :: binary(), + PurgeInfo :: {PurgeSeq, UUId, Id, [Rev]} | not_found, + PurgeSeq :: non_neg_integer(), + Id :: binary(), + Rev :: {non_neg_integer(), binary()}. +get_purge_infos(Db, UUIDs) -> + couch_db_engine:load_purge_infos(Db, UUIDs). + + +get_minimum_purge_seq(#db{} = Db) -> + PurgeSeq = couch_db_engine:get_purge_seq(Db), + OldestPurgeSeq = couch_db_engine:get_oldest_purge_seq(Db), + PurgeInfosLimit = couch_db_engine:get_purge_infos_limit(Db), + + FoldFun = fun(#doc{id = DocId, body = {Props}}, SeqAcc) -> + case DocId of + <<?LOCAL_DOC_PREFIX, "purge-", _/binary>> -> + ClientSeq = couch_util:get_value(<<"purge_seq">>, Props), + case ClientSeq of + CS when is_integer(CS), CS >= PurgeSeq - PurgeInfosLimit -> + {ok, SeqAcc}; + CS when is_integer(CS) -> + case purge_client_exists(Db, DocId, Props) of + true -> {ok, erlang:min(CS, SeqAcc)}; + false -> {ok, SeqAcc} + end; + _ -> + % If there's a broken doc we have to keep every + % purge info until the doc is fixed or removed. + Fmt = "Invalid purge doc '~s' with purge_seq '~w'", + couch_log:error(Fmt, [DocId, ClientSeq]), + {ok, erlang:min(OldestPurgeSeq, SeqAcc)} + end; + _ -> + {stop, SeqAcc} + end + end, + InitMinSeq = PurgeSeq - PurgeInfosLimit, + Opts = [ + {start_key, list_to_binary(?LOCAL_DOC_PREFIX ++ "purge-")} + ], + {ok, MinIdxSeq} = couch_db:fold_local_docs(Db, FoldFun, InitMinSeq, Opts), + FinalSeq = case MinIdxSeq < PurgeSeq - PurgeInfosLimit of + true -> MinIdxSeq; + false -> erlang:max(0, PurgeSeq - PurgeInfosLimit) + end, + % Log a warning if we've got a purge sequence exceeding the + % configured threshold. + if FinalSeq >= (PurgeSeq - PurgeInfosLimit) -> ok; true -> + Fmt = "The purge sequence for '~s' exceeds configured threshold", + couch_log:warning(Fmt, [couch_db:name(Db)]) + end, + FinalSeq. + + +purge_client_exists(DbName, DocId, Props) -> + % Warn about clients that have not updated their purge + % checkpoints in the last "index_lag_warn_seconds" + LagWindow = config:get_integer( + "purge", "index_lag_warn_seconds", 86400), % Default 24 hours + + {Mega, Secs, _} = os:timestamp(), + NowSecs = Mega * 1000000 + Secs, + LagThreshold = NowSecs - LagWindow, + + try + CheckFun = get_purge_client_fun(DocId, Props), + Exists = CheckFun(DbName, DocId, Props), + if not Exists -> ok; true -> + Updated = couch_util:get_value(<<"updated_on">>, Props), + if is_integer(Updated) and Updated > LagThreshold -> ok; true -> + Diff = NowSecs - Updated, + Fmt = "Purge checkpoint '~s' not updated in ~p seconds", + couch_log:error(Fmt, [DocId, Diff]) + end + end, + Exists + catch _:_ -> + % If we fail to check for a client we have to assume that + % it exists. + true + end. + + +get_purge_client_fun(DocId, Props) -> + M0 = couch_util:get_value(<<"verify_module">>, Props), Review comment: I agreed that there is still potential risk even if only module name and function name are specified by users. Perhaps there are mature methodology to solve such issue. For me, I thought to use blacklist to check whether specified M:F/1 is attack-able. However, this may not be enumerable. As you suggested, we may consider whitelist or something like this. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services