This is an automated email from the ASF dual-hosted git repository.
jiahuili430 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/main by this push:
new 69bdb7e6c Replace `gen_server:format_status/2` with `format_status/1`
69bdb7e6c is described below
commit 69bdb7e6c528c48bbee1af986c5a6dddf1c94d13
Author: Jiahui Li <[email protected]>
AuthorDate: Wed Sep 17 11:04:31 2025 -0500
Replace `gen_server:format_status/2` with `format_status/1`
`gen_server:format_status/2` is deprecated and will be replaced by
`format_status(#{})`. Therefore, replaced some of them with maps.
Also removed the `format_status/2` from couch_file.erl.
Fix the following warnings:
```log
Warning: the callback gen_server:format_status(_,_) is deprecated; use
format_status/1 instead
```
Related PR: https://github.com/erlang/otp/pull/4952
---
src/couch/src/couch_file.erl | 6 +-
src/couch/src/couch_util.erl | 6 +-
.../src/couch_replicator_auth_session.erl | 24 ++--
.../src/couch_replicator_httpc_pool.erl | 47 ++++----
.../src/couch_replicator_scheduler.erl | 22 ++--
.../src/couch_replicator_scheduler_job.erl | 122 +++++++++++----------
.../src/couch_replicator_worker.erl | 82 ++++++++------
7 files changed, 175 insertions(+), 134 deletions(-)
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index 9efc7978e..3b52aa467 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -51,7 +51,7 @@
-export([delete/2, delete/3, nuke_dir/2, init_delete_dir/1]).
% gen_server callbacks
--export([init/1, terminate/2, format_status/2]).
+-export([init/1, terminate/2]).
-export([handle_call/3, handle_cast/2, handle_info/2]).
%% helper functions
@@ -621,10 +621,6 @@ handle_info({'DOWN', Ref, process, _Pid, _Info},
#file{db_monitor = Ref} = File)
false -> {noreply, File}
end.
-format_status(_Opt, [PDict, #file{} = File]) ->
- {_Fd, FilePath} = couch_util:get_value(couch_file_fd, PDict),
- [{data, [{"State", File}, {"InitialFilePath", FilePath}]}].
-
eof(#file{fd = Fd}) ->
file:position(Fd, eof).
diff --git a/src/couch/src/couch_util.erl b/src/couch/src/couch_util.erl
index 6b69c16e9..7205edd80 100644
--- a/src/couch/src/couch_util.erl
+++ b/src/couch/src/couch_util.erl
@@ -511,13 +511,15 @@ reorder_results(Keys, SortedResults, Default) ->
Map = maps:from_list(SortedResults),
[maps:get(Key, Map, Default) || Key <- Keys].
-url_strip_password(Url) ->
+url_strip_password(Url) when is_list(Url) ->
re:replace(
Url,
"(http|https|socks5)://([^:]+):[^@]+@(.*)$",
"\\1://\\2:*****@\\3",
[{return, list}]
- ).
+ );
+url_strip_password(Other) ->
+ Other.
encode_doc_id(#doc{id = Id}) ->
encode_doc_id(Id);
diff --git a/src/couch_replicator/src/couch_replicator_auth_session.erl
b/src/couch_replicator/src/couch_replicator_auth_session.erl
index 8f0be0a1d..04807a58a 100644
--- a/src/couch_replicator/src/couch_replicator_auth_session.erl
+++ b/src/couch_replicator/src/couch_replicator_auth_session.erl
@@ -62,7 +62,7 @@
handle_call/3,
handle_cast/2,
handle_info/2,
- format_status/2
+ format_status/1
]).
-include_lib("ibrowse/include/ibrowse.hrl").
@@ -154,13 +154,21 @@ handle_info(Msg, State) ->
couch_log:error("~p : Received un-expected message ~p", [?MODULE, Msg]),
{noreply, State}.
-format_status(_Opt, [_PDict, State]) ->
- [
- {epoch, State#state.epoch},
- {user, State#state.user},
- {session_url, State#state.session_url},
- {refresh_tstamp, State#state.refresh_tstamp}
- ].
+format_status(Status) ->
+ maps:map(
+ fun
+ (state, State) ->
+ #{
+ epoch => State#state.epoch,
+ user => State#state.user,
+ session_url => State#state.session_url,
+ refresh_tstamp => State#state.refresh_tstamp
+ };
+ (_, Value) ->
+ Value
+ end,
+ Status
+ ).
%% Private helper functions
diff --git a/src/couch_replicator/src/couch_replicator_httpc_pool.erl
b/src/couch_replicator/src/couch_replicator_httpc_pool.erl
index fb15dcea1..254ffd1f9 100644
--- a/src/couch_replicator/src/couch_replicator_httpc_pool.erl
+++ b/src/couch_replicator/src/couch_replicator_httpc_pool.erl
@@ -19,7 +19,7 @@
% gen_server API
-export([init/1, handle_call/3, handle_info/2, handle_cast/2]).
--export([format_status/2]).
+-export([format_status/1]).
-include_lib("couch/include/couch_db.hrl").
@@ -135,19 +135,23 @@ handle_info({'DOWN', Ref, process, _, _}, #state{callers
= Callers} = State) ->
{noreply, State}
end.
-format_status(_Opt, [_PDict, State]) ->
- #state{
- url = Url,
- proxy_url = ProxyUrl
- } = State,
- [
- {data, [
- {"State", State#state{
- url = couch_util:url_strip_password(Url),
- proxy_url = couch_util:url_strip_password(ProxyUrl)
- }}
- ]}
- ].
+format_status(Status) ->
+ maps:map(
+ fun
+ (state, State) ->
+ #state{
+ url = Url,
+ proxy_url = ProxyUrl
+ } = State,
+ State#state{
+ url = couch_util:url_strip_password(Url),
+ proxy_url = couch_util:url_strip_password(ProxyUrl)
+ };
+ (_, Value) ->
+ Value
+ end,
+ Status
+ ).
monitor_client(Callers, Worker, {ClientPid, _}) ->
[{Worker, erlang:monitor(process, ClientPid)} | Callers].
@@ -196,13 +200,16 @@ release_worker_internal(Worker, State) ->
format_status_test_() ->
?_test(begin
- State = #state{
- url = "https://username1:password1@$ACCOUNT2.cloudant.com/db",
- proxy_url = "https://username2:[email protected]:8080/"
+ Status = #{
+ state =>
+ #state{
+ url =
"https://username1:password1@$ACCOUNT2.cloudant.com/db",
+ proxy_url =
"https://username2:[email protected]:8080/"
+ }
},
- [{data, [{"State", ScrubbedN}]}] = format_status(normal, [[], State]),
- ?assertEqual("https://username1:*****@$ACCOUNT2.cloudant.com/db",
ScrubbedN#state.url),
- ?assertEqual("https://username2:*****@proxy.thing.com:8080/",
ScrubbedN#state.proxy_url),
+ #{state := State} = format_status(Status),
+ ?assertEqual("https://username1:*****@$ACCOUNT2.cloudant.com/db",
State#state.url),
+ ?assertEqual("https://username2:*****@proxy.thing.com:8080/",
State#state.proxy_url),
ok
end).
diff --git a/src/couch_replicator/src/couch_replicator_scheduler.erl
b/src/couch_replicator/src/couch_replicator_scheduler.erl
index 379a42b38..3f9306595 100644
--- a/src/couch_replicator/src/couch_replicator_scheduler.erl
+++ b/src/couch_replicator/src/couch_replicator_scheduler.erl
@@ -25,7 +25,7 @@
handle_call/3,
handle_info/2,
handle_cast/2,
- format_status/2
+ format_status/1
]).
-export([
@@ -357,12 +357,20 @@ terminate(_Reason, _State) ->
couch_replicator_share:clear(),
ok.
-format_status(_Opt, [_PDict, State]) ->
- [
- {max_jobs, State#state.max_jobs},
- {running_jobs, running_job_count()},
- {pending_jobs, pending_job_count()}
- ].
+format_status(Status) ->
+ maps:map(
+ fun
+ (state, State) ->
+ #{
+ max_jobs => State#state.max_jobs,
+ running_jobs => running_job_count(),
+ pending_jobs => pending_job_count()
+ };
+ (_, Value) ->
+ Value
+ end,
+ Status
+ ).
%% config listener functions
diff --git a/src/couch_replicator/src/couch_replicator_scheduler_job.erl
b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
index 7f123441f..49f5bc01b 100644
--- a/src/couch_replicator/src/couch_replicator_scheduler_job.erl
+++ b/src/couch_replicator/src/couch_replicator_scheduler_job.erl
@@ -25,7 +25,7 @@
handle_call/3,
handle_info/2,
handle_cast/2,
- format_status/2,
+ format_status/1,
sum_stats/2,
report_seq_done/3
]).
@@ -477,38 +477,46 @@ terminate_cleanup(#rep_state{rep_details = #rep{id =
RepId}} = State) ->
couch_replicator_api_wrap:db_close(State#rep_state.source),
couch_replicator_api_wrap:db_close(State#rep_state.target).
-format_status(_Opt, [_PDict, State]) ->
- #rep_state{
- source = Source,
- target = Target,
- rep_details = RepDetails,
- start_seq = StartSeq,
- source_seq = SourceSeq,
- committed_seq = CommitedSeq,
- current_through_seq = ThroughSeq,
- highest_seq_done = HighestSeqDone,
- session_id = SessionId
- } = state_strip_creds(State),
- #rep{
- id = RepId,
- options = Options,
- doc_id = DocId,
- db_name = DbName
- } = RepDetails,
- [
- {rep_id, RepId},
- {source, couch_replicator_api_wrap:db_uri(Source)},
- {target, couch_replicator_api_wrap:db_uri(Target)},
- {db_name, DbName},
- {doc_id, DocId},
- {options, Options},
- {session_id, SessionId},
- {start_seq, StartSeq},
- {source_seq, SourceSeq},
- {committed_seq, CommitedSeq},
- {current_through_seq, ThroughSeq},
- {highest_seq_done, HighestSeqDone}
- ].
+format_status(Status) ->
+ maps:map(
+ fun
+ (state, State) ->
+ #rep_state{
+ source = Source,
+ target = Target,
+ rep_details = RepDetails,
+ start_seq = StartSeq,
+ source_seq = SourceSeq,
+ committed_seq = CommitedSeq,
+ current_through_seq = ThroughSeq,
+ highest_seq_done = HighestSeqDone,
+ session_id = SessionId
+ } = state_strip_creds(State),
+ #rep{
+ id = RepId,
+ options = Options,
+ doc_id = DocId,
+ db_name = DbName
+ } = RepDetails,
+ #{
+ rep_id => RepId,
+ source => couch_replicator_api_wrap:db_uri(Source),
+ target => couch_replicator_api_wrap:db_uri(Target),
+ db_name => DbName,
+ doc_id => DocId,
+ options => Options,
+ session_id => SessionId,
+ start_seq => StartSeq,
+ source_seq => SourceSeq,
+ committed_seq => CommitedSeq,
+ current_through_seq => ThroughSeq,
+ highest_seq_done => HighestSeqDone
+ };
+ (_, Value) ->
+ Value
+ end,
+ Status
+ ).
sum_stats(Pid, Stats) when is_pid(Pid) ->
gen_server:cast(Pid, {sum_stats, Stats}).
@@ -1230,29 +1238,31 @@ t_scheduler_job_format_status(_) ->
doc_id = <<"mydoc">>,
db_name = <<"mydb">>
},
- State = #rep_state{
- rep_details = Rep,
- source = Rep#rep.source,
- target = Rep#rep.target,
- session_id = <<"a">>,
- start_seq = <<"1">>,
- source_seq = <<"2">>,
- committed_seq = <<"3">>,
- current_through_seq = <<"4">>,
- highest_seq_done = <<"5">>
+ Status = #{
+ state => #rep_state{
+ rep_details = Rep,
+ source = Rep#rep.source,
+ target = Rep#rep.target,
+ session_id = <<"a">>,
+ start_seq = <<"1">>,
+ source_seq = <<"2">>,
+ committed_seq = <<"3">>,
+ current_through_seq = <<"4">>,
+ highest_seq_done = <<"5">>
+ }
},
- Format = format_status(opts_ignored, [pdict, State]),
- ?assertEqual("http://h1/d1/", proplists:get_value(source, Format)),
- ?assertEqual("http://h2/d2/", proplists:get_value(target, Format)),
- ?assertEqual({"base", "+ext"}, proplists:get_value(rep_id, Format)),
- ?assertEqual([{create_target, true}], proplists:get_value(options,
Format)),
- ?assertEqual(<<"mydoc">>, proplists:get_value(doc_id, Format)),
- ?assertEqual(<<"mydb">>, proplists:get_value(db_name, Format)),
- ?assertEqual(<<"a">>, proplists:get_value(session_id, Format)),
- ?assertEqual(<<"1">>, proplists:get_value(start_seq, Format)),
- ?assertEqual(<<"2">>, proplists:get_value(source_seq, Format)),
- ?assertEqual(<<"3">>, proplists:get_value(committed_seq, Format)),
- ?assertEqual(<<"4">>, proplists:get_value(current_through_seq, Format)),
- ?assertEqual(<<"5">>, proplists:get_value(highest_seq_done, Format)).
+ #{state := State} = format_status(Status),
+ ?assertEqual("http://h1/d1/", maps:get(source, State)),
+ ?assertEqual("http://h2/d2/", maps:get(target, State)),
+ ?assertEqual({"base", "+ext"}, maps:get(rep_id, State)),
+ ?assertEqual([{create_target, true}], maps:get(options, State)),
+ ?assertEqual(<<"mydoc">>, maps:get(doc_id, State)),
+ ?assertEqual(<<"mydb">>, maps:get(db_name, State)),
+ ?assertEqual(<<"a">>, maps:get(session_id, State)),
+ ?assertEqual(<<"1">>, maps:get(start_seq, State)),
+ ?assertEqual(<<"2">>, maps:get(source_seq, State)),
+ ?assertEqual(<<"3">>, maps:get(committed_seq, State)),
+ ?assertEqual(<<"4">>, maps:get(current_through_seq, State)),
+ ?assertEqual(<<"5">>, maps:get(highest_seq_done, State)).
-endif.
diff --git a/src/couch_replicator/src/couch_replicator_worker.erl
b/src/couch_replicator/src/couch_replicator_worker.erl
index 078e6e7e0..3a6edc0b8 100644
--- a/src/couch_replicator/src/couch_replicator_worker.erl
+++ b/src/couch_replicator/src/couch_replicator_worker.erl
@@ -19,7 +19,7 @@
% gen_server callbacks
-export([init/1]).
-export([handle_call/3, handle_cast/2, handle_info/2]).
--export([format_status/2]).
+-export([format_status/1]).
-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_replicator/include/couch_replicator_api_wrap.hrl").
@@ -242,25 +242,33 @@ handle_info({'EXIT', _Pid, {doc_write_failed, _} = Err},
State) ->
handle_info({'EXIT', Pid, Reason}, State) ->
{stop, {process_died, Pid, Reason}, State}.
-format_status(_Opt, [_PDict, State]) ->
- #state{
- cp = MainJobPid,
- loop = LoopPid,
- source = Source,
- target = Target,
- readers = Readers,
- pending_fetch = PendingFetch,
- batch = #batch{size = BatchSize}
- } = State,
- [
- {main_pid, MainJobPid},
- {loop, LoopPid},
- {source, couch_replicator_api_wrap:db_uri(Source)},
- {target, couch_replicator_api_wrap:db_uri(Target)},
- {num_readers, length(Readers)},
- {pending_fetch, PendingFetch},
- {batch_size, BatchSize}
- ].
+format_status(Status) ->
+ maps:map(
+ fun
+ (state, State) ->
+ #state{
+ cp = MainJobPid,
+ loop = LoopPid,
+ source = Source,
+ target = Target,
+ readers = Readers,
+ pending_fetch = PendingFetch,
+ batch = #batch{size = BatchSize}
+ } = State,
+ #{
+ main_pid => MainJobPid,
+ loop => LoopPid,
+ source => couch_replicator_api_wrap:db_uri(Source),
+ target => couch_replicator_api_wrap:db_uri(Target),
+ num_readers => length(Readers),
+ pending_fetch => PendingFetch,
+ batch_size => BatchSize
+ };
+ (_, Value) ->
+ Value
+ end,
+ Status
+ ).
sum_stats(Pid, Stats) when is_pid(Pid) ->
ok = gen_server:cast(Pid, {sum_stats, Stats}).
@@ -733,23 +741,25 @@ maybe_report_stats(#state{} = State) ->
-include_lib("eunit/include/eunit.hrl").
replication_worker_format_status_test() ->
- State = #state{
- cp = self(),
- loop = self(),
- source = #httpdb{url = "http://u:p@h/d1"},
- target = #httpdb{url = "http://u:p@h/d2"},
- readers = [r1, r2, r3],
- pending_fetch = nil,
- batch = #batch{size = 5}
+ Status = #{
+ state => #state{
+ cp = self(),
+ loop = self(),
+ source = #httpdb{url = "http://u:p@h/d1"},
+ target = #httpdb{url = "http://u:p@h/d2"},
+ readers = [r1, r2, r3],
+ pending_fetch = nil,
+ batch = #batch{size = 5}
+ }
},
- Format = format_status(opts_ignored, [pdict, State]),
- ?assertEqual(self(), proplists:get_value(main_pid, Format)),
- ?assertEqual(self(), proplists:get_value(loop, Format)),
- ?assertEqual("http://u:*****@h/d1", proplists:get_value(source, Format)),
- ?assertEqual("http://u:*****@h/d2", proplists:get_value(target, Format)),
- ?assertEqual(3, proplists:get_value(num_readers, Format)),
- ?assertEqual(nil, proplists:get_value(pending_fetch, Format)),
- ?assertEqual(5, proplists:get_value(batch_size, Format)).
+ #{state := State} = format_status(Status),
+ ?assertEqual(self(), maps:get(main_pid, State)),
+ ?assertEqual(self(), maps:get(loop, State)),
+ ?assertEqual("http://u:*****@h/d1", maps:get(source, State)),
+ ?assertEqual("http://u:*****@h/d2", maps:get(target, State)),
+ ?assertEqual(3, maps:get(num_readers, State)),
+ ?assertEqual(nil, maps:get(pending_fetch, State)),
+ ?assertEqual(5, maps:get(batch_size, State)).
bulk_get_attempt_test() ->
Now = erlang:monotonic_time(second),