You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ko...@apache.org on 2021/03/27 22:58:25 UTC

[couchdb] 01/01: Improve search for FDB cluster files

This is an automated email from the ASF dual-hosted git repository.

kocolosk pushed a commit to branch respect-FDB_CLUSTER_FILE
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit de8971da1385fdede0a006d24ce79307d9e92e6f
Author: Adam Kocoloski <ko...@apache.org>
AuthorDate: Sat Mar 27 21:50:02 2021 +0000

    Improve search for FDB cluster files
    
    This patch introduces a handful of related improvments to the code that
    chooses the FoundationDB cluster file. We now check for a cluster file
    in up to 4 locations, in priority order:
    
    1. The value of `[erlfdb] cluster_file` in the server config
    2. The value of the FDB_CLUSTER_FILE environment variable
    3. /usr/local/etc/foundationdb/fdb.cluster (MacOS package default)
    4. /etc/foundationdb/fdb.cluster (Linux packages default)
    
    The second and fourth locations are new. We also check that the file
    has appropriate access permissions, namely that is RW accessible by the
    user running CouchDB. Finally, we add some more detailed logging to
    help users debug any problems in this department on startup.
    
    If a location is specified using either one of the first two "custom"
    approaches and there is a problem with that location, CouchDB will
    abort rather than try any further option. This avoids a situation where
    a user intended to override the default cluster file but CouchDB starts
    up with the default one because e.g. the user failed to actually
    install the new file with the correct permissions.
---
 src/fabric/src/fabric2_server.erl | 40 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/fabric/src/fabric2_server.erl b/src/fabric/src/fabric2_server.erl
index be674b1..32f89db 100644
--- a/src/fabric/src/fabric2_server.erl
+++ b/src/fabric/src/fabric2_server.erl
@@ -42,9 +42,10 @@
 
 
 -include_lib("couch/include/couch_db.hrl").
+-include_lib("kernel/include/file.hrl").
 
-
--define(CLUSTER_FILE, "/usr/local/etc/foundationdb/fdb.cluster").
+-define(CLUSTER_FILE_MACOS, "/usr/local/etc/foundationdb/fdb.cluster").
+-define(CLUSTER_FILE_LINUX, "/etc/foundationdb/fdb.cluster").
 -define(FDB_DIRECTORY, fdb_directory).
 -define(FDB_CLUSTER, fdb_cluster).
 -define(DEFAULT_FDB_DIRECTORY, <<"couchdb">>).
@@ -212,7 +213,7 @@ get_db_and_cluster(EunitDbOpts) ->
         {ok, true} ->
             {<<"eunit_test">>, erlfdb_util:get_test_db(EunitDbOpts)};
         undefined ->
-            ClusterFileStr = config:get("erlfdb", "cluster_file", ?CLUSTER_FILE),
+            ClusterFileStr = get_cluster_file_string(),
             {ok, ConnectionStr} = file:read_file(ClusterFileStr),
             DbHandle = erlfdb:open(iolist_to_binary(ClusterFileStr)),
             {string:trim(ConnectionStr), DbHandle}
@@ -220,6 +221,39 @@ get_db_and_cluster(EunitDbOpts) ->
     apply_tx_options(Db, config:get(?TX_OPTIONS_SECTION)),
     {Cluster, Db}.
 
+get_cluster_file_string() ->
+    Locations = [
+        {custom, config:get("erlfdb", "cluster_file", false)},
+        {custom, os:getenv("FDB_CLUSTER_FILE")},
+        {default, ?CLUSTER_FILE_MACOS},
+        {default, ?CLUSTER_FILE_LINUX}
+    ],
+    case find_cluster_file(Locations) of
+        {ok, Location} ->
+            Location;
+        {error, Reason} ->
+            erlang:error(Reason)
+    end.
+
+find_cluster_file([]) ->
+    {error, cluster_file_missing};
+find_cluster_file([{custom, false} | Rest]) ->
+    find_cluster_file(Rest);
+find_cluster_file([{Type, Location} | Rest]) ->
+    case file:read_file_info(Location, [posix]) of
+        {ok, #file_info{access = read_write}} ->
+            couch_log:info("Using ~s FDB cluster file: ~s", [Type, Location]),
+            {ok, Location};
+        {ok, _} ->
+            couch_log:error("CouchDB needs read/write access to FDB cluster file: ~s", [Location]),
+            {error, cluster_file_permissions};
+        {error, Reason} when Type =:= custom ->
+            couch_log:error("Encountered ~p error looking for FDB cluster file: ~s", [Reason, Location]),
+            {error, Reason};
+        {error, Reason} when Type =:= default ->
+            couch_log:warning("Encountered ~p error looking for FDB cluster file: ~s", [Reason, Location]),
+            find_cluster_file(Rest)
+    end.
 
 apply_tx_options(Db, Cfg) ->
     maps:map(fun(Option, {Type, Default}) ->