You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2025/01/28 10:05:16 UTC
(couchdb) 12/13: couch_ra app
This is an automated email from the ASF dual-hosted git repository.
rnewson pushed a commit to branch ra
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit f2082aa9315d09409193558f05230b85cab0ec26
Author: Robert Newson <rn...@apache.org>
AuthorDate: Mon Jan 27 17:12:14 2025 +0000
couch_ra app
---
rebar.config.script | 1 +
rel/reltool.config | 2 ++
src/chttpd/src/chttpd.app.src | 1 +
src/chttpd/src/chttpd_app.erl | 40 ----------------------
.../src/couch_ra.app.src} | 14 +++-----
.../fabric.app.src => couch_ra/src/couch_ra.erl} | 25 +++++---------
.../src/couch_ra_app.erl} | 39 ++++++++-------------
.../src/couch_ra_sup.erl} | 28 +++++++--------
src/fabric/src/fabric.app.src | 1 -
src/fabric/src/fabric_doc_update.erl | 12 +++++--
src/mem3/src/mem3_sup.erl | 1 +
11 files changed, 54 insertions(+), 110 deletions(-)
diff --git a/rebar.config.script b/rebar.config.script
index ad61a72cd..690ff52f7 100644
--- a/rebar.config.script
+++ b/rebar.config.script
@@ -131,6 +131,7 @@ SubDirs = [
"src/couch_peruser",
"src/couch_tests",
"src/couch_dist",
+ "src/couch_ra",
"src/custodian",
"src/ddoc_cache",
"src/dreyfus",
diff --git a/rel/reltool.config b/rel/reltool.config
index 4aa07e713..20bf5b3a0 100644
--- a/rel/reltool.config
+++ b/rel/reltool.config
@@ -42,6 +42,7 @@
couch_event,
couch_peruser,
couch_dist,
+ couch_ra,
custodian,
ddoc_cache,
dreyfus,
@@ -109,6 +110,7 @@
{app, couch_event, [{incl_cond, include}]},
{app, couch_peruser, [{incl_cond, include}]},
{app, couch_dist ,[{incl_cond, include}]},
+ {app, couch_ra, [{incl_cond, include}]},
{app, custodian, [{incl_cond, include}]},
{app, ddoc_cache, [{incl_cond, include}]},
{app, dreyfus, [{incl_cond, include}]},
diff --git a/src/chttpd/src/chttpd.app.src b/src/chttpd/src/chttpd.app.src
index d48f02b97..a0770c090 100644
--- a/src/chttpd/src/chttpd.app.src
+++ b/src/chttpd/src/chttpd.app.src
@@ -28,6 +28,7 @@
couch,
ets_lru,
fabric,
+ couch_ra,
ioq
]},
{mod, {chttpd_app, []}}
diff --git a/src/chttpd/src/chttpd_app.erl b/src/chttpd/src/chttpd_app.erl
index 8f1c2c78e..d7a5aef86 100644
--- a/src/chttpd/src/chttpd_app.erl
+++ b/src/chttpd/src/chttpd_app.erl
@@ -14,48 +14,8 @@
-behaviour(application).
-export([start/2, stop/1]).
-% temp location
--export([couch_write_queue_name/1]).
-
start(_Type, StartArgs) ->
- start_ra(),
chttpd_sup:start_link(StartArgs).
stop(_State) ->
ok.
-
-start_ra() ->
- %% ensure Ra logs reach couch.log
- logger:add_handler(couch_log, couch_logger_handler, #{}),
-
- %% start Ra
- RaEnv = [
- {data_dir, config:get("couchdb", "ra_data_dir")}
- ],
- ra:start(RaEnv),
-
- %% create Ra clusters for ordering doc writes
- [start_cluster(Nodes) || Nodes <- combinations(3, mem3:nodes())].
-
-start_cluster(Nodes) ->
- Name = couch_write_queue_name(Nodes),
- ra:start_cluster(
- default,
- Name,
- {module, couch_write_queue, #{}},
- [{Name, N} || N <- Nodes]
- ).
-
-couch_write_queue_name(Nodes) ->
- list_to_atom(
- "couch_write_queue_" ++
- lists:flatten(lists:join("_", [atom_to_list(N) || N <- lists:sort(Nodes)]))
- ).
-
-%% https://rosettacode.org/wiki/Combinations#Erlang
-combinations(0, _Nodes) ->
- [[]];
-combinations(_, []) ->
- [];
-combinations(N, [Node | Rest]) ->
- [[Node | List] || List <- combinations(N - 1, Rest)] ++ combinations(N, Rest).
diff --git a/src/fabric/src/fabric.app.src b/src/couch_ra/src/couch_ra.app.src
similarity index 77%
copy from src/fabric/src/fabric.app.src
copy to src/couch_ra/src/couch_ra.app.src
index e9fd0333c..c11b3db81 100644
--- a/src/fabric/src/fabric.app.src
+++ b/src/couch_ra/src/couch_ra.app.src
@@ -10,19 +10,15 @@
% License for the specific language governing permissions and limitations under
% the License.
-{application, fabric, [
- {description, "Routing and proxying layer for CouchDB cluster"},
+{application, couch_ra, [
+ {description, "CouchDB Ra"},
{vsn, git},
- {registered, []},
+ {mod, {couch_ra_app, []}},
{applications, [
kernel,
stdlib,
- config,
- couch,
- ra,
- rexi,
- mem3,
couch_log,
- couch_stats
+ mem3,
+ config
]}
]}.
diff --git a/src/fabric/src/fabric.app.src b/src/couch_ra/src/couch_ra.erl
similarity index 64%
copy from src/fabric/src/fabric.app.src
copy to src/couch_ra/src/couch_ra.erl
index e9fd0333c..2157c5324 100644
--- a/src/fabric/src/fabric.app.src
+++ b/src/couch_ra/src/couch_ra.erl
@@ -10,19 +10,12 @@
% License for the specific language governing permissions and limitations under
% the License.
-{application, fabric, [
- {description, "Routing and proxying layer for CouchDB cluster"},
- {vsn, git},
- {registered, []},
- {applications, [
- kernel,
- stdlib,
- config,
- couch,
- ra,
- rexi,
- mem3,
- couch_log,
- couch_stats
- ]}
-]}.
+-module(couch_ra).
+
+-export([couch_write_queue_name/1]).
+
+couch_write_queue_name(Nodes) ->
+ list_to_atom(
+ "couch_write_queue_" ++
+ lists:flatten(lists:join("_", [atom_to_list(N) || N <- lists:sort(Nodes)]))
+ ).
diff --git a/src/chttpd/src/chttpd_app.erl b/src/couch_ra/src/couch_ra_app.erl
similarity index 66%
copy from src/chttpd/src/chttpd_app.erl
copy to src/couch_ra/src/couch_ra_app.erl
index 8f1c2c78e..36efeefa5 100644
--- a/src/chttpd/src/chttpd_app.erl
+++ b/src/couch_ra/src/couch_ra_app.erl
@@ -1,57 +1,43 @@
% 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
+% 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
+% 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(chttpd_app).
+-module(couch_ra_app).
-behaviour(application).
-export([start/2, stop/1]).
-% temp location
--export([couch_write_queue_name/1]).
-
-start(_Type, StartArgs) ->
- start_ra(),
- chttpd_sup:start_link(StartArgs).
-
-stop(_State) ->
- ok.
-
-start_ra() ->
+start(_Type, []) ->
%% ensure Ra logs reach couch.log
logger:add_handler(couch_log, couch_logger_handler, #{}),
%% start Ra
RaEnv = [
- {data_dir, config:get("couchdb", "ra_data_dir")}
- ],
+ {data_dir, config:get("couchdb", "ra_data_dir")}
+ ],
ra:start(RaEnv),
%% create Ra clusters for ordering doc writes
- [start_cluster(Nodes) || Nodes <- combinations(3, mem3:nodes())].
+ [start_cluster(Nodes) || Nodes <- combinations(3, mem3:nodes())],
+
+ couch_ra_sup:start_link().
start_cluster(Nodes) ->
- Name = couch_write_queue_name(Nodes),
+ Name = couch_ra:couch_write_queue_name(Nodes),
ra:start_cluster(
- default,
+ couchdb,
Name,
{module, couch_write_queue, #{}},
[{Name, N} || N <- Nodes]
).
-couch_write_queue_name(Nodes) ->
- list_to_atom(
- "couch_write_queue_" ++
- lists:flatten(lists:join("_", [atom_to_list(N) || N <- lists:sort(Nodes)]))
- ).
-
%% https://rosettacode.org/wiki/Combinations#Erlang
combinations(0, _Nodes) ->
[[]];
@@ -59,3 +45,6 @@ combinations(_, []) ->
[];
combinations(N, [Node | Rest]) ->
[[Node | List] || List <- combinations(N - 1, Rest)] ++ combinations(N, Rest).
+
+stop([]) ->
+ ok.
diff --git a/src/fabric/src/fabric.app.src b/src/couch_ra/src/couch_ra_sup.erl
similarity index 64%
copy from src/fabric/src/fabric.app.src
copy to src/couch_ra/src/couch_ra_sup.erl
index e9fd0333c..69215a7eb 100644
--- a/src/fabric/src/fabric.app.src
+++ b/src/couch_ra/src/couch_ra_sup.erl
@@ -10,19 +10,15 @@
% License for the specific language governing permissions and limitations under
% the License.
-{application, fabric, [
- {description, "Routing and proxying layer for CouchDB cluster"},
- {vsn, git},
- {registered, []},
- {applications, [
- kernel,
- stdlib,
- config,
- couch,
- ra,
- rexi,
- mem3,
- couch_log,
- couch_stats
- ]}
-]}.
+-module(couch_ra_sup).
+
+-behaviour(supervisor).
+
+-export([init/1]).
+-export([start_link/0]).
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+ {ok, {{one_for_one, 10, 10}, []}}.
diff --git a/src/fabric/src/fabric.app.src b/src/fabric/src/fabric.app.src
index e9fd0333c..d7686ca1a 100644
--- a/src/fabric/src/fabric.app.src
+++ b/src/fabric/src/fabric.app.src
@@ -19,7 +19,6 @@
stdlib,
config,
couch,
- ra,
rexi,
mem3,
couch_log,
diff --git a/src/fabric/src/fabric_doc_update.erl b/src/fabric/src/fabric_doc_update.erl
index 76aad2048..2e2db24c9 100644
--- a/src/fabric/src/fabric_doc_update.erl
+++ b/src/fabric/src/fabric_doc_update.erl
@@ -26,9 +26,15 @@ go(DbName, AllDocs0, Options0) ->
UpdateFun = fun(Doc) ->
[Shard | _] = Shards = mem3_shards:for_docid(DbName, Doc#doc.id),
Nodes = [S#shard.node || S <- Shards],
- ServerId = chttpd_app:couch_write_queue_name(Nodes),
- {ok, Reply, _Leader} = ra:process_command(ServerId, {update_doc, Shard#shard.name, Doc, Options1}),
- Reply
+ ServerId = couch_ra:couch_write_queue_name(Nodes),
+ case ra:process_command(ServerId, {update_doc, Shard#shard.name, Doc, Options1}) of
+ {ok, Reply, _Leader} ->
+ Reply;
+ {error, Reason} ->
+ {error, Reason};
+ {timeout, _ServerId} ->
+ {error, timeout}
+ end
end,
{ok, lists:map(UpdateFun, AllDocs1)}.
diff --git a/src/mem3/src/mem3_sup.erl b/src/mem3/src/mem3_sup.erl
index 862ef6b50..7423f6208 100644
--- a/src/mem3/src/mem3_sup.erl
+++ b/src/mem3/src/mem3_sup.erl
@@ -40,3 +40,4 @@ child(mem3_reshard_sup = Child) ->
{Child, MFA, permanent, infinity, supervisor, [Child]};
child(Child) ->
{Child, {Child, start_link, []}, permanent, 1000, worker, [Child]}.
+