From da7636f7ca9606e2bcca2c8a8e82b626dacf3c8b Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Tue, 23 Oct 2012 21:28:56 +0100 Subject: [PATCH 1/3] Give each couchdb server a unique identifier --- src/couchdb/couch_httpd_misc_handlers.erl | 1 + src/couchdb/couch_server.erl | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/couchdb/couch_httpd_misc_handlers.erl b/src/couchdb/couch_httpd_misc_handlers.erl index f7a4d753332..2150bea629e 100644 --- a/src/couchdb/couch_httpd_misc_handlers.erl +++ b/src/couchdb/couch_httpd_misc_handlers.erl @@ -32,6 +32,7 @@ handle_welcome_req(#httpd{method='GET'}=Req, WelcomeMessage) -> send_json(Req, {[ {couchdb, WelcomeMessage}, + {uuid, couch_server:get_uuid()}, {version, list_to_binary(couch_server:get_version())} ] ++ case couch_config:get("vendor") of [] -> diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl index 0f40c51d750..5d3b8d18106 100644 --- a/src/couchdb/couch_server.erl +++ b/src/couchdb/couch_server.erl @@ -13,7 +13,7 @@ -module(couch_server). -behaviour(gen_server). --export([open/2,create/2,delete/2,get_version/0]). +-export([open/2,create/2,delete/2,get_version/0,get_uuid/0]). -export([all_databases/0, all_databases/2]). -export([init/1, handle_call/3,sup_start_link/0]). -export([handle_cast/2,code_change/3,handle_info/2,terminate/2]). @@ -43,6 +43,15 @@ get_version() -> "0.0.0" end. +get_uuid() -> + case couch_config:get("couchdb", "uuid", nil) of + nil -> + UUID = couch_uuids:random(), + couch_config:set("couchdb", "uuid", ?b2l(UUID)), + UUID; + UUID -> ?l2b(UUID) + end. + get_stats() -> {ok, #server{start_time=Time,dbs_open=Open}} = gen_server:call(couch_server, get_server), From 39fe998257a5f29b264c7632c75c2aafc6da5892 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Wed, 14 Nov 2012 09:59:50 +0000 Subject: [PATCH 2/3] Use local UUID instead of local host:port --- src/couch_replicator/src/couch_replicator.hrl | 2 +- src/couch_replicator/src/couch_replicator_utils.erl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/couch_replicator/src/couch_replicator.hrl b/src/couch_replicator/src/couch_replicator.hrl index 20c0bc39a8d..018aa4b649e 100644 --- a/src/couch_replicator/src/couch_replicator.hrl +++ b/src/couch_replicator/src/couch_replicator.hrl @@ -10,7 +10,7 @@ % License for the specific language governing permissions and limitations under % the License. --define(REP_ID_VERSION, 2). +-define(REP_ID_VERSION, 3). -record(rep, { id, diff --git a/src/couch_replicator/src/couch_replicator_utils.erl b/src/couch_replicator/src/couch_replicator_utils.erl index 46793215166..d7778db871b 100644 --- a/src/couch_replicator/src/couch_replicator_utils.erl +++ b/src/couch_replicator/src/couch_replicator_utils.erl @@ -59,6 +59,12 @@ replication_id(#rep{options = Options} = Rep) -> % If a change is made to how replications are identified, % please add a new clause and increase ?REP_ID_VERSION. +replication_id(#rep{user_ctx = UserCtx} = Rep, 3) -> + UUID = couch_server:get_uuid(), + Src = get_rep_endpoint(UserCtx, Rep#rep.source), + Tgt = get_rep_endpoint(UserCtx, Rep#rep.target), + maybe_append_filters([UUID, Src, Tgt], Rep); + replication_id(#rep{user_ctx = UserCtx} = Rep, 2) -> {ok, HostName} = inet:gethostname(), Port = case (catch mochiweb_socket_server:get(couch_httpd, port)) of From ae798352d1aad67355bfee6828d9ddd0546591c4 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Wed, 14 Nov 2012 10:31:00 +0000 Subject: [PATCH 3/3] Ensure uuid is generated before port is bound --- src/couchdb/couch_httpd.erl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 3774b85cc32..ba28ffd5cdf 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -128,6 +128,10 @@ start_link(Name, Options) -> set_auth_handlers(), + % ensure uuid is set so that concurrent replications + % get the same value. + couch_server:get_uuid(), + Loop = fun(Req)-> case SocketOptions of [] ->