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 2018/05/24 14:02:49 UTC
[couchdb] branch 1338-node-flavors created (now 521ce6b)
This is an automated email from the ASF dual-hosted git repository.
kocolosk pushed a change to branch 1338-node-flavors
in repository https://gitbox.apache.org/repos/asf/couchdb.git.
at 521ce6b Listen for changes to [node] config and apply them
This branch includes the following new commits:
new 12c883d Import [node] config settings into /_nodes DB
new 521ce6b Listen for changes to [node] config and apply them
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
--
To stop receiving notification emails like this one, please contact
kocolosk@apache.org.
[couchdb] 01/02: Import [node] config settings into /_nodes DB
Posted by ko...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
kocolosk pushed a commit to branch 1338-node-flavors
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 12c883d7163553f1f14e10a159feb25513476e62
Author: Adam Kocoloski <ko...@apache.org>
AuthorDate: Thu May 24 08:41:17 2018 -0400
Import [node] config settings into /_nodes DB
This patch allows administrators to configure settings in the [node]
block of the server configuration and have those settings propagated to
the internal nodes database. A current use case for this functionality
is to configure the `zone` of the node for use in cluster placement
topologies. Additional attributes can be written directly into the
document in the nodes DB, but in case of a conflict between the entry
in the database and the entry in the server configuration the latter
will be preferred (and the attribute in the database overwritten).
Related to #1338.
---
src/mem3/src/mem3_nodes.erl | 41 +++++++++++++++++++++++++++++------------
1 file changed, 29 insertions(+), 12 deletions(-)
diff --git a/src/mem3/src/mem3_nodes.erl b/src/mem3/src/mem3_nodes.erl
index 019ceaf..4423ea9 100644
--- a/src/mem3/src/mem3_nodes.erl
+++ b/src/mem3/src/mem3_nodes.erl
@@ -92,16 +92,28 @@ code_change(_OldVsn, #state{}=State, _Extra) ->
initialize_nodelist() ->
DbName = config:get("mem3", "nodes_db", "_nodes"),
{ok, Db} = mem3_util:ensure_exists(DbName),
- {ok, _} = couch_db:fold_docs(Db, fun first_fold/2, Db, []),
- % add self if not already present
- case ets:lookup(?MODULE, node()) of
- [_] ->
- ok;
- [] ->
- ets:insert(?MODULE, {node(), []}),
- Doc = #doc{id = couch_util:to_binary(node())},
- {ok, _} = couch_db:update_doc(Db, Doc, [])
+ SelfId = couch_util:to_binary(node()),
+ NodeProps = [{?l2b(K), ?l2b(V)} || {K,V} <- config:get("node")],
+ {ok, {_, _, Doc}} = couch_db:fold_docs(Db, fun first_fold/2, {Db, SelfId, nil}, []),
+ case Doc of
+ nil ->
+ ets:insert(?MODULE, {node(), NodeProps}),
+ NewDoc = #doc{id = SelfId, body = {NodeProps}},
+ {ok, _} = couch_db:update_doc(Db, NewDoc, []);
+ #doc{id = SelfId, body={DocProps}} = Doc ->
+ SortedNodeProps = lists:keysort(1, NodeProps),
+ SortedDocProps = lists:keysort(1, DocProps),
+ case lists:ukeymerge(1, SortedNodeProps, SortedDocProps) of
+ SortedDocProps ->
+ % typical case, internal doc state matches .ini
+ ok;
+ NewProps ->
+ ets:insert(?MODULE, {node(), NewProps}),
+ NewDoc = Doc#doc{body = {NewProps}},
+ {ok, _} = couch_db:update_doc(Db, NewDoc, [])
+ end
end,
+ % TODO is this ignoring any update we just made above?
Seq = couch_db:get_update_seq(Db),
couch_db:close(Db),
Seq.
@@ -110,10 +122,15 @@ first_fold(#full_doc_info{id = <<"_design/", _/binary>>}, Acc) ->
{ok, Acc};
first_fold(#full_doc_info{deleted=true}, Acc) ->
{ok, Acc};
-first_fold(#full_doc_info{id=Id}=DocInfo, Db) ->
- {ok, #doc{body={Props}}} = couch_db:open_doc(Db, DocInfo, [ejson_body]),
+first_fold(#full_doc_info{id=Id}=DocInfo, {Db, SelfId, DocAcc}) ->
+ {ok, Doc} = couch_db:open_doc(Db, DocInfo, [ejson_body]),
+ #doc{body = {Props}} = Doc,
ets:insert(?MODULE, {mem3_util:to_atom(Id), Props}),
- {ok, Db}.
+ if Id =:= SelfId ->
+ {ok, {Db, SelfId, Doc}};
+ true ->
+ {ok, {Db, SelfId, DocAcc}}
+ end.
listen_for_changes(Since) ->
DbName = config:get("mem3", "nodes_db", "_nodes"),
--
To stop receiving notification emails like this one, please contact
kocolosk@apache.org.
[couchdb] 02/02: Listen for changes to [node] config and apply them
Posted by ko...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
kocolosk pushed a commit to branch 1338-node-flavors
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 521ce6bf155556a8e155693807a5670016666e7c
Author: Adam Kocoloski <ko...@apache.org>
AuthorDate: Thu May 24 09:42:03 2018 -0400
Listen for changes to [node] config and apply them
This adds the config_listener behaviour to mem3_nodes and ensures that
any updates made to the [node] block of configuration via the _config
API will propgagate down to the _nodes DB and associated mem3_nodes ets
table.
---
src/mem3/src/mem3_nodes.erl | 51 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 12 deletions(-)
diff --git a/src/mem3/src/mem3_nodes.erl b/src/mem3/src/mem3_nodes.erl
index 4423ea9..33e8d06 100644
--- a/src/mem3/src/mem3_nodes.erl
+++ b/src/mem3/src/mem3_nodes.erl
@@ -12,11 +12,14 @@
-module(mem3_nodes).
-behaviour(gen_server).
+-behaviour(config_listener).
-vsn(1).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-export([start_link/0, get_nodelist/0, get_node_info/2]).
+-export([handle_config_change/5, handle_config_terminate/3]).
+
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").
@@ -43,6 +46,7 @@ get_node_info(Node, Key) ->
init([]) ->
ets:new(?MODULE, [named_table, {read_concurrency, true}]),
UpdateSeq = initialize_nodelist(),
+ ok = config:listen_for_changes(?MODULE, nil),
{Pid, _} = spawn_monitor(fun() -> listen_for_changes(UpdateSeq) end),
{ok, #state{changes_pid = Pid, update_seq = UpdateSeq}}.
@@ -87,6 +91,15 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, #state{}=State, _Extra) ->
{ok, State}.
+handle_config_change("node", Key, Value, _, State) ->
+ update_metadata([{?l2b(Key), ?l2b(Value)}]),
+ {ok, State};
+handle_config_change(_, _, _, _, State) ->
+ {ok, State}.
+
+handle_config_terminate(_Server, _Reason, _State) ->
+ ok.
+
%% internal functions
initialize_nodelist() ->
@@ -100,18 +113,8 @@ initialize_nodelist() ->
ets:insert(?MODULE, {node(), NodeProps}),
NewDoc = #doc{id = SelfId, body = {NodeProps}},
{ok, _} = couch_db:update_doc(Db, NewDoc, []);
- #doc{id = SelfId, body={DocProps}} = Doc ->
- SortedNodeProps = lists:keysort(1, NodeProps),
- SortedDocProps = lists:keysort(1, DocProps),
- case lists:ukeymerge(1, SortedNodeProps, SortedDocProps) of
- SortedDocProps ->
- % typical case, internal doc state matches .ini
- ok;
- NewProps ->
- ets:insert(?MODULE, {node(), NewProps}),
- NewDoc = Doc#doc{body = {NewProps}},
- {ok, _} = couch_db:update_doc(Db, NewDoc, [])
- end
+ #doc{id = SelfId} = Doc ->
+ update_metadata(Db, Doc, NodeProps)
end,
% TODO is this ignoring any update we just made above?
Seq = couch_db:get_update_seq(Db),
@@ -162,3 +165,27 @@ changes_callback({change, {Change}, _}, _) ->
{ok, couch_util:get_value(<<"seq">>, Change)};
changes_callback(timeout, _) ->
{ok, nil}.
+
+update_metadata(NewProps) ->
+ DbName = config:get("mem3", "nodes_db", "_nodes"),
+ {ok, Db} = mem3_util:ensure_exists(DbName),
+ try
+ {ok, Doc} = couch_db:open_doc(Db, couch_util:to_binary(node()), [ejson_body]),
+ update_metadata(Db, Doc, NewProps)
+ after
+ couch_db:close(Db)
+ end.
+
+update_metadata(Db, #doc{body = {DocProps}} = Doc, NewProps) ->
+ SortedNewProps = lists:keysort(1, NewProps),
+ SortedDocProps = lists:keysort(1, DocProps),
+ case lists:ukeymerge(1, SortedNewProps, SortedDocProps) of
+ SortedDocProps ->
+ % internal doc state matches .ini
+ ok;
+ MergedProps ->
+ ets:insert(?MODULE, {node(), MergedProps}),
+ NewDoc = Doc#doc{body = {MergedProps}},
+ {ok, _} = couch_db:update_doc(Db, NewDoc, [])
+ end,
+ ok.
--
To stop receiving notification emails like this one, please contact
kocolosk@apache.org.