Skip to content

Commit

Permalink
Fix replication _scheduler/docs total_rows
Browse files Browse the repository at this point in the history
The total_rows property was decremented by one to account for the VDU
that was automatically added to the total. Now that a BDU has replaced
the VDU [1] total_rows is one less than it should be.

This removes the decrement so that total_rows equals the actual doc
count.

[1] #4274
  • Loading branch information
jaydoane committed Jan 11, 2023
1 parent 1c43d02 commit b3b57e4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/couch_replicator/src/couch_replicator_httpd_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ docs_cb({meta, Meta}, #vacc{meta_sent = false, row_sent = false} = Acc) ->
Parts =
case couch_util:get_value(total, Meta) of
undefined -> [];
Total -> [io_lib:format("\"total_rows\":~p", [adjust_total(Total)])]
Total -> [io_lib:format("\"total_rows\":~p", [Total])]
end ++
case couch_util:get_value(offset, Meta) of
undefined -> [];
Expand Down Expand Up @@ -193,9 +193,3 @@ row_to_json(Row) ->
Doc0 = couch_util:get_value(doc, Row),
Doc1 = update_db_name(Doc0),
?JSON_ENCODE(Doc1).

%% Adjust Total as there is an automatically created validation design doc
adjust_total(Total) when is_integer(Total), Total > 0 ->
Total - 1;
adjust_total(Total) when is_integer(Total) ->
0.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_replicator_scheduler_docs_tests).

-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").

scheduler_docs_test_() ->
{
foreach,
fun() ->
Ctx = couch_replicator_test_helper:test_setup(),
ok = config:set("replicator", "cluster_start_period", "0", false),
Opts = [{q, 1}, {n, 1}, ?ADMIN_CTX],
case fabric:create_db(<<"_replicator">>, Opts) of
ok -> ok;
{error, file_exists} -> ok
end,
Ctx
end,
fun(Ctx) ->
ok = config:delete("replicator", "cluster_start_period"),
ok = fabric:delete_db(<<"_replicator">>, [?ADMIN_CTX]),
couch_replicator_test_helper:test_teardown(Ctx)
end,
[
?TDEF_FE(t_scheduler_docs_total_rows, 10)
]
}.

t_scheduler_docs_total_rows({_Ctx, {Source, Target}}) ->
SourceUrl = couch_replicator_test_helper:cluster_db_url(Source),
TargetUrl = couch_replicator_test_helper:cluster_db_url(Target),
RepDoc = jiffy:encode(
{[
{<<"source">>, SourceUrl},
{<<"target">>, TargetUrl}
]}
),
RepDocUrl = couch_replicator_test_helper:cluster_db_url(
list_to_binary("/_replicator/" ++ ?docid())
),
{ok, 201, _, _} = test_request:put(binary_to_list(RepDocUrl), [], RepDoc),
SchedulerDocsUrl =
couch_replicator_test_helper:cluster_db_url(<<"/_scheduler/docs">>),
Body = test_util:wait(
fun() ->
case test_request:get(binary_to_list(SchedulerDocsUrl), []) of
{ok, 200, _, JsonBody} ->
Decoded = jiffy:decode(JsonBody, [return_maps]),
case maps:get(<<"docs">>, Decoded) of
[] ->
wait;
_ ->
Decoded
end;
_ ->
wait
end
end,
10000,
1000
),
Docs = maps:get(<<"docs">>, Body),
TotalRows = maps:get(<<"total_rows">>, Body),
?assertEqual(TotalRows, length(Docs)),
ok.

0 comments on commit b3b57e4

Please sign in to comment.