You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by "nickva (via GitHub)" <gi...@apache.org> on 2025/09/30 06:32:39 UTC
[PR] Implement db doc updating [couchdb]
nickva opened a new pull request, #5680:
URL: https://github.com/apache/couchdb/pull/5680
Resharding implements db doc updates with extra steps to ensure some resilience in case the replication ring is broken and to avoid generating conflicts. However, the implementation was specific to resharding used the `#job{}` record as an argument, so change it make it more general and expose it as a top level mem3 API.
The pattern is the same as used in the resharding logic:
* Changes, reading and writing happen on the first live node -- the leader
* Before reading, replicate all the update from other nodes to the leader. This is to handle the case when the replication ring may be broken and to avoid conflicts.
* After updating, to ensure all copies get the update immediately, the change is broadcast to all the live copies.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement db doc updating [couchdb]
Posted by "rnewson (via GitHub)" <gi...@apache.org>.
rnewson commented on code in PR #5680:
URL: https://github.com/apache/couchdb/pull/5680#discussion_r2391262548
##########
src/mem3/src/mem3_db_doc_updater.erl:
##########
@@ -0,0 +1,107 @@
+% 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(mem3_db_doc_updater).
+
+-behaviour(gen_server).
+
+-export([
+ get_db_doc/1,
+ update_db_doc/1,
+
+ start_link/0,
+
+ init/1,
+ handle_call/3,
+ handle_cast/2
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+% Early return shortcut
+%
+-define(THROW(RES), throw({reply, RES, nil})).
+
+get_db_doc(DocId) when is_binary(DocId) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {get_db_doc, DocId}, Timeout).
+
+update_db_doc(#doc{} = Doc) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {update_db_doc, Doc}, Timeout).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init(_) ->
+ {ok, nil}.
+
+handle_call({get_db_doc, DocId}, _From, nil = St) ->
+ {reply, get_db_doc_int(DocId), St};
+handle_call({update_db_doc, #doc{} = Doc}, _From, nil = St) ->
+ {reply, update_db_doc_int(Doc), St};
+handle_call(Msg, _From, nil = St) ->
+ {stop, {invalid_call, Msg}, invalid_call, St}.
+
+handle_cast(Msg, nil = St) ->
+ {stop, {invalid_cast, Msg}, St}.
+
+% Private
+
+update_db_doc_int(#doc{} = Doc) ->
+ ok = validate_coordinator(),
+ couch_util:with_db(mem3_sync:shards_db(), fun(Db) ->
+ try
+ Res = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]),
+ ok = replicate_to_all_nodes(shard_update_timeout_msec()),
+ Res
+ catch
+ conflict ->
+ ?THROW({error, conflict})
Review Comment:
```
If a callback function fails or returns a bad value, the gen_server process terminates. However, an exception of class [throw](https://www.erlang.org/doc/apps/erts/erlang#throw/1) is not regarded as an error but as a valid return, from all callback functions.
``` https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3
wow...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement db doc updating [couchdb]
Posted by "rnewson (via GitHub)" <gi...@apache.org>.
rnewson commented on code in PR #5680:
URL: https://github.com/apache/couchdb/pull/5680#discussion_r2391262548
##########
src/mem3/src/mem3_db_doc_updater.erl:
##########
@@ -0,0 +1,107 @@
+% 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(mem3_db_doc_updater).
+
+-behaviour(gen_server).
+
+-export([
+ get_db_doc/1,
+ update_db_doc/1,
+
+ start_link/0,
+
+ init/1,
+ handle_call/3,
+ handle_cast/2
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+% Early return shortcut
+%
+-define(THROW(RES), throw({reply, RES, nil})).
+
+get_db_doc(DocId) when is_binary(DocId) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {get_db_doc, DocId}, Timeout).
+
+update_db_doc(#doc{} = Doc) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {update_db_doc, Doc}, Timeout).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init(_) ->
+ {ok, nil}.
+
+handle_call({get_db_doc, DocId}, _From, nil = St) ->
+ {reply, get_db_doc_int(DocId), St};
+handle_call({update_db_doc, #doc{} = Doc}, _From, nil = St) ->
+ {reply, update_db_doc_int(Doc), St};
+handle_call(Msg, _From, nil = St) ->
+ {stop, {invalid_call, Msg}, invalid_call, St}.
+
+handle_cast(Msg, nil = St) ->
+ {stop, {invalid_cast, Msg}, St}.
+
+% Private
+
+update_db_doc_int(#doc{} = Doc) ->
+ ok = validate_coordinator(),
+ couch_util:with_db(mem3_sync:shards_db(), fun(Db) ->
+ try
+ Res = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]),
+ ok = replicate_to_all_nodes(shard_update_timeout_msec()),
+ Res
+ catch
+ conflict ->
+ ?THROW({error, conflict})
Review Comment:
> If a callback function fails or returns a bad value, the gen_server process terminates. However, an exception of class [throw](https://www.erlang.org/doc/apps/erts/erlang#throw/1) is not regarded as an error but as a valid return, from all callback functions.
from https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3
wow...
##########
src/mem3/src/mem3_db_doc_updater.erl:
##########
@@ -0,0 +1,107 @@
+% 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(mem3_db_doc_updater).
+
+-behaviour(gen_server).
+
+-export([
+ get_db_doc/1,
+ update_db_doc/1,
+
+ start_link/0,
+
+ init/1,
+ handle_call/3,
+ handle_cast/2
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+% Early return shortcut
+%
+-define(THROW(RES), throw({reply, RES, nil})).
+
+get_db_doc(DocId) when is_binary(DocId) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {get_db_doc, DocId}, Timeout).
+
+update_db_doc(#doc{} = Doc) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {update_db_doc, Doc}, Timeout).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init(_) ->
+ {ok, nil}.
+
+handle_call({get_db_doc, DocId}, _From, nil = St) ->
+ {reply, get_db_doc_int(DocId), St};
+handle_call({update_db_doc, #doc{} = Doc}, _From, nil = St) ->
+ {reply, update_db_doc_int(Doc), St};
+handle_call(Msg, _From, nil = St) ->
+ {stop, {invalid_call, Msg}, invalid_call, St}.
+
+handle_cast(Msg, nil = St) ->
+ {stop, {invalid_cast, Msg}, St}.
+
+% Private
+
+update_db_doc_int(#doc{} = Doc) ->
+ ok = validate_coordinator(),
+ couch_util:with_db(mem3_sync:shards_db(), fun(Db) ->
+ try
+ Res = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]),
+ ok = replicate_to_all_nodes(shard_update_timeout_msec()),
+ Res
+ catch
+ conflict ->
+ ?THROW({error, conflict})
Review Comment:
> If a callback function fails or returns a bad value, the gen_server process terminates. However, an exception of class [throw](https://www.erlang.org/doc/apps/erts/erlang#throw/1) is not regarded as an error but as a valid return, from all callback functions.
from https://www.erlang.org/doc/apps/stdlib/gen_server.html#c:handle_call/3
wow...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement db doc updating [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva merged PR #5680:
URL: https://github.com/apache/couchdb/pull/5680
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement db doc updating [couchdb]
Posted by "rnewson (via GitHub)" <gi...@apache.org>.
rnewson commented on code in PR #5680:
URL: https://github.com/apache/couchdb/pull/5680#discussion_r2390553606
##########
src/mem3/src/mem3_db_doc_updater.erl:
##########
@@ -0,0 +1,107 @@
+% 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(mem3_db_doc_updater).
+
+-behaviour(gen_server).
+
+-export([
+ get_db_doc/1,
+ update_db_doc/1,
+
+ start_link/0,
+
+ init/1,
+ handle_call/3,
+ handle_cast/2
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+% Early return shortcut
+%
+-define(THROW(RES), throw({reply, RES, nil})).
+
+get_db_doc(DocId) when is_binary(DocId) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {get_db_doc, DocId}, Timeout).
+
+update_db_doc(#doc{} = Doc) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {update_db_doc, Doc}, Timeout).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init(_) ->
+ {ok, nil}.
+
+handle_call({get_db_doc, DocId}, _From, nil = St) ->
+ {reply, get_db_doc_int(DocId), St};
+handle_call({update_db_doc, #doc{} = Doc}, _From, nil = St) ->
+ {reply, update_db_doc_int(Doc), St};
+handle_call(Msg, _From, nil = St) ->
+ {stop, {invalid_call, Msg}, invalid_call, St}.
+
+handle_cast(Msg, nil = St) ->
+ {stop, {invalid_cast, Msg}, St}.
+
+% Private
+
+update_db_doc_int(#doc{} = Doc) ->
+ ok = validate_coordinator(),
+ couch_util:with_db(mem3_sync:shards_db(), fun(Db) ->
+ try
+ Res = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]),
+ ok = replicate_to_all_nodes(shard_update_timeout_msec()),
+ Res
+ catch
+ conflict ->
+ ?THROW({error, conflict})
Review Comment:
throwing instead of returning a reply tuple in the handle_call callback? does that work?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement db doc updating [couchdb]
Posted by "nickva (via GitHub)" <gi...@apache.org>.
nickva commented on code in PR #5680:
URL: https://github.com/apache/couchdb/pull/5680#discussion_r2391475466
##########
src/mem3/src/mem3_db_doc_updater.erl:
##########
@@ -0,0 +1,107 @@
+% 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(mem3_db_doc_updater).
+
+-behaviour(gen_server).
+
+-export([
+ get_db_doc/1,
+ update_db_doc/1,
+
+ start_link/0,
+
+ init/1,
+ handle_call/3,
+ handle_cast/2
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+% Early return shortcut
+%
+-define(THROW(RES), throw({reply, RES, nil})).
+
+get_db_doc(DocId) when is_binary(DocId) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {get_db_doc, DocId}, Timeout).
+
+update_db_doc(#doc{} = Doc) ->
+ Timeout = shard_update_timeout_msec(),
+ gen_server:call(first_node(), {update_db_doc, Doc}, Timeout).
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init(_) ->
+ {ok, nil}.
+
+handle_call({get_db_doc, DocId}, _From, nil = St) ->
+ {reply, get_db_doc_int(DocId), St};
+handle_call({update_db_doc, #doc{} = Doc}, _From, nil = St) ->
+ {reply, update_db_doc_int(Doc), St};
+handle_call(Msg, _From, nil = St) ->
+ {stop, {invalid_call, Msg}, invalid_call, St}.
+
+handle_cast(Msg, nil = St) ->
+ {stop, {invalid_cast, Msg}, St}.
+
+% Private
+
+update_db_doc_int(#doc{} = Doc) ->
+ ok = validate_coordinator(),
+ couch_util:with_db(mem3_sync:shards_db(), fun(Db) ->
+ try
+ Res = couch_db:update_doc(Db, Doc, [?ADMIN_CTX]),
+ ok = replicate_to_all_nodes(shard_update_timeout_msec()),
+ Res
+ catch
+ conflict ->
+ ?THROW({error, conflict})
Review Comment:
At least they documented it so it’s official. It’s been a hidden feature for years
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org