This is an automated email from the ASF dual-hosted git repository.
nickva 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 6e82a001a Return stub batch error per document so it's counted towards
the quorum
6e82a001a is described below
commit 6e82a001a3cee9334eff6b455d48b4f85ad0e63a
Author: Álvaro Olivié <[email protected]>
AuthorDate: Fri Oct 23 16:20:34 2026 +0000
Return stub batch error per document so it's counted towards the quorum
We used to throw a missing stub in the coordinator for any copy with a stub
error. User got a 412 even after other copies had accepted the update. Now
we
count it with a quorum to decide what to do.
`_bulk_docs` now returns 201 with per-document `missing_stub` errors instead
of failing the whole request with a 412.
Co-authored-by: Nick Vatamaniuc <[email protected]>
---
src/fabric/src/fabric.erl | 2 +
src/fabric/src/fabric_doc_update.erl | 117 +++++++++++++++++++++++++++++++++--
2 files changed, 113 insertions(+), 6 deletions(-)
diff --git a/src/fabric/src/fabric.erl b/src/fabric/src/fabric.erl
index 5397df507..3d309a4c6 100644
--- a/src/fabric/src/fabric.erl
+++ b/src/fabric/src/fabric.erl
@@ -409,6 +409,8 @@ update_doc(DbName, Doc, Options) ->
% replication success
#doc{revs = {Pos, [RevId | _]}} = doc(DbName, Doc),
{ok, {Pos, RevId}};
+ {error, [{{_Id, _Rev}, Error}]} ->
+ throw(Error);
{error, [Error]} ->
throw(Error)
end.
diff --git a/src/fabric/src/fabric_doc_update.erl
b/src/fabric/src/fabric_doc_update.erl
index e7c123fe7..e5dc9c527 100644
--- a/src/fabric/src/fabric_doc_update.erl
+++ b/src/fabric/src/fabric_doc_update.erl
@@ -151,12 +151,10 @@ handle_message({ok, Replies}, Worker, #acc{} = Acc0) ->
reply = DocReplyDict
})}
end;
-handle_message({missing_stub, Stub}, _, _) ->
- throw({missing_stub, Stub});
+handle_message({missing_stub, _} = X, Worker, #acc{} = Acc0) ->
+ reply_for_all_docs(X, Worker, Acc0);
handle_message({not_found, no_db_file} = X, Worker, #acc{} = Acc0) ->
- #acc{grouped_docs = GroupedDocs} = Acc0,
- Docs = couch_util:get_value(Worker, GroupedDocs),
- handle_message({ok, [X || _D <- Docs]}, Worker, Acc0);
+ reply_for_all_docs(X, Worker, Acc0);
handle_message({bad_request, Msg}, _, _) ->
throw({bad_request, Msg});
handle_message({bad_request, Error, Reason}, _, _) ->
@@ -166,6 +164,19 @@ handle_message({forbidden, Msg}, _, _) ->
handle_message({request_entity_too_large, Entity}, _, _) ->
throw({request_entity_too_large, Entity}).
+% Count batch errors as a single error for all the documents in the batch.
+% Replicated changes report per-doc errors as {{Id, {Pos, Rev}}, Error}
+reply_for_all_docs(Error, Worker, #acc{} = Acc0) ->
+ #acc{grouped_docs = GroupedDocs, update_options = Options} = Acc0,
+ Docs = couch_util:get_value(Worker, GroupedDocs),
+ Replicated = proplists:get_value(?REPLICATED_CHANGES, Options),
+ handle_message({ok, [doc_error(D, Error, Replicated) || D <- Docs]},
Worker, Acc0).
+
+doc_error(#doc{id = Id, revs = {Pos, [Rev | _]}}, Error, true) ->
+ {{Id, {Pos, Rev}}, Error};
+doc_error(#doc{}, Error, _Replicated) ->
+ Error.
+
before_doc_update(DbName, Docs, Opts) ->
% Use the same pattern as in couch_db:validate_doc_update/3. If the
document was already
% checked during the interactive edit we don't want to spend time in the
internal replicator
@@ -247,7 +258,7 @@ force_reply(Doc, [FirstReply | _] = Replies, {Health, W,
SWS, Acc}) ->
{forbidden, ForbiddenReply} ->
{Health, W, SWS, [{Doc, ForbiddenReply} |
Acc]};
false ->
- {error, W, SWS, [{Doc, FirstReply} | Acc]}
+ {error, W, SWS, [{Doc,
first_error(Replies)} | Acc]}
end
end;
[AcceptedRev | _] ->
@@ -262,6 +273,11 @@ force_reply(Doc, [FirstReply | _] = Replies, {Health, W,
SWS, Acc}) ->
end
end.
+% In replicated changes we use noreply for a successul write. Anything else is
+% an error.
+first_error(Replies) ->
+ hd([R || R <- Replies, R =/= noreply]).
+
maybe_reply(_, _, continue) ->
% we didn't meet quorum for all docs, so we're fast-forwarding the fold
continue;
@@ -580,6 +596,11 @@ doc_update_test_() ->
fun one_success_two_forbid/0,
fun worker_before_doc_update_forbidden/0,
fun handle_bad_request/0,
+ fun two_ok_one_missing_stub/0,
+ fun all_missing_stub/0,
+ fun replicated_all_missing_stub/0,
+ fun replicated_one_missing_stub/0,
+ fun replicated_two_missing_stub/0,
fun filter_conflicts_drops_seen_docs/0,
fun parallel_in_flight_after_conflict/0,
fun serial_filters_conflicts_at_cast/0,
@@ -1118,6 +1139,90 @@ handle_bad_request() ->
handle_message({bad_request, err, reason}, hd(Shards), Acc)
).
+two_ok_one_missing_stub() ->
+ Docs = [Doc1] = tag_docs([#doc{revs = {1, [~"foo"]}}]),
+ Shards =
+ mem3_util:create_partition_map("foo", 3, 1, ["node1", "node2",
"node3"]),
+ GroupedDocs = group_docs_by_shard_hack(~"foo", Shards, Docs),
+ Acc0 = #acc{
+ waiting_count = length(Shards),
+ doc_count = length(Docs),
+ w = 2,
+ grouped_docs = GroupedDocs,
+ reply = reply_map(Docs)
+ },
+ MissingStub = {missing_stub, ~"stub"},
+
+ {ok, Acc1} = handle_message(MissingStub, hd(Shards), Acc0),
+ {ok, Acc2} = handle_message({ok, [{ok, Doc1}]}, lists:nth(2, Shards),
Acc1),
+ {stop, Reply} = handle_message({ok, [{ok, Doc1}]}, lists:nth(3, Shards),
Acc2),
+ ?assertEqual({ok, [{Doc1, {ok, Doc1}}]}, Reply).
+
+all_missing_stub() ->
+ Docs = [Doc1] = tag_docs([#doc{revs = {1, [~"foo"]}}]),
+ Shards =
+ mem3_util:create_partition_map("foo", 3, 1, ["node1", "node2",
"node3"]),
+ GroupedDocs = group_docs_by_shard_hack(~"foo", Shards, Docs),
+ Acc0 = #acc{
+ waiting_count = length(Shards),
+ doc_count = length(Docs),
+ w = 2,
+ grouped_docs = GroupedDocs,
+ reply = reply_map(Docs)
+ },
+ MissingStub = {missing_stub, ~"stub"},
+
+ {ok, Acc1} = handle_message(MissingStub, hd(Shards), Acc0),
+ {ok, Acc2} = handle_message(MissingStub, lists:nth(2, Shards), Acc1),
+ {stop, Reply} = handle_message(MissingStub, lists:nth(3, Shards), Acc2),
+ ?assertEqual({ok, [{Doc1, MissingStub}]}, Reply).
+
+repl_acc(Docs, Shards) ->
+ #acc{
+ waiting_count = length(Shards),
+ doc_count = length(Docs),
+ w = 2,
+ grouped_docs = group_docs_by_shard_hack(~"foo", Shards, Docs),
+ reply = reply_map(Docs),
+ update_options = [?REPLICATED_CHANGES],
+ serialize_worker_startup = false
+ }.
+
+% No parent. Expect a stub error
+replicated_all_missing_stub() ->
+ Docs = [Doc1] = tag_docs([#doc{id = ~"d", revs = {2, [~"b", ~"a"]}}]),
+ [S1, S2, S3] = Shards = mem3_util:create_partition_map("foo", 3, 1, ["n1",
"n2", "n3"]),
+ MissingStub = {missing_stub, ~"stub"},
+ {ok, Acc1} = handle_message(MissingStub, S1, repl_acc(Docs, Shards)),
+ {ok, Acc2} = handle_message(MissingStub, S2, Acc1),
+ ?assertEqual(
+ {stop, {ok, [{Doc1, {{~"d", {2, ~"b"}}, MissingStub}}]}},
+ handle_message(MissingStub, S3, Acc2)
+ ).
+
+% Two copies win (noreply) and one is lagging: two copies win and we hide the
error.
+replicated_one_missing_stub() ->
+ Docs = [Doc1] = tag_docs([#doc{id = ~"d", revs = {2, [~"b", ~"a"]}}]),
+ [S1, S2, S3] = Shards = mem3_util:create_partition_map("foo", 3, 1, ["n1",
"n2", "n3"]),
+ {ok, Acc1} = handle_message({missing_stub, ~"stub"}, S1, repl_acc(Docs,
Shards)),
+ {ok, Acc2} = handle_message({ok, []}, S2, Acc1),
+ ?assertEqual({stop, {ok, [{Doc1, noreply}]}}, handle_message({ok, []}, S3,
Acc2)).
+
+% Only one copy has a parent rev. Two stubs win and replicator would have to
+% resend the attachments. It follows the quorum logic.
+replicated_two_missing_stub() ->
+ Docs = [Doc1] = tag_docs([#doc{id = ~"d", revs = {2, [~"b", ~"a"]}}]),
+ [S1, S2, S3] = Shards = mem3_util:create_partition_map("foo", 3, 1, ["n1",
"n2", "n3"]),
+ MissingStub = {missing_stub, ~"stub"},
+ Expect = {stop, {error, [{Doc1, {{~"d", {2, ~"b"}}, MissingStub}}]}},
+ Acc0 = repl_acc(Docs, Shards),
+ {ok, Acc1} = handle_message({ok, []}, S1, Acc0),
+ {ok, Acc2} = handle_message(MissingStub, S2, Acc1),
+ ?assertEqual(Expect, handle_message(MissingStub, S3, Acc2)),
+ {ok, Acc3} = handle_message(MissingStub, S1, Acc0),
+ {ok, Acc4} = handle_message(MissingStub, S2, Acc3),
+ ?assertEqual(Expect, handle_message({ok, []}, S3, Acc4)).
+
filter_conflicts_drops_seen_docs() ->
Doc1 = #doc{revs = {1, [<<"foo">>]}},
Doc2 = #doc{revs = {1, [<<"bar">>]}},