Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Use config app instead of couch_config
BugzID: 13179
  • Loading branch information
kocolosk committed Feb 27, 2013
1 parent 2e9b66b commit c2c5478af2a72d10fc0a2855fca77b0ac55baed4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
@@ -10,5 +10,5 @@
mem3_sync_nodes,
mem3_sup
]},
{applications, [kernel, stdlib, sasl, crypto, mochiweb, couch, rexi, twig]}
{applications, [kernel, stdlib, config, sasl, crypto, mochiweb, couch, rexi, twig]}
]}.
@@ -79,7 +79,7 @@ shards(DbName) when is_list(DbName) ->
shards(list_to_binary(DbName));
shards(DbName) ->
ShardDbName =
list_to_binary(couch_config:get("mem3", "shard_db", "dbs")),
list_to_binary(config:get("mem3", "shard_db", "dbs")),
case DbName of
ShardDbName ->
%% shard_db is treated as a single sharded db to support calls to db_info
@@ -157,7 +157,7 @@ choose_shards(DbName, Nodes, Options) ->
true -> ok
end,
Q = mem3_util:to_integer(couch_util:get_value(q, Options,
couch_config:get("cluster", "q", "8"))),
config:get("cluster", "q", "8"))),
%% rotate to a random entry in the nodelist for even distribution
{A, B} = lists:split(crypto:rand_uniform(1,length(Nodes)+1), Nodes),
RotatedNodes = B ++ A,
@@ -166,7 +166,7 @@ choose_shards(DbName, Nodes, Options) ->
get_placement(Options) ->
case couch_util:get_value(placement, Options) of
undefined ->
case couch_config:get("cluster", "placement") of
case config:get("cluster", "placement") of
undefined ->
undefined;
PlacementStr ->
@@ -95,7 +95,7 @@ code_change(_OldVsn, State, _Extra) ->
%% internal functions

initialize_nodelist() ->
DbName = couch_config:get("mem3", "node_db", "nodes"),
DbName = config:get("mem3", "node_db", "nodes"),
{ok, Db} = mem3_util:ensure_exists(DbName),
{ok, _, Db} = couch_btree:fold(Db#db.id_tree, fun first_fold/3, Db, []),
% add self if not already present
@@ -120,7 +120,7 @@ first_fold(#full_doc_info{id=Id}=DocInfo, _, Db) ->
{ok, Db}.

listen_for_changes(Since) ->
DbName = couch_config:get("mem3", "node_db", "nodes"),
DbName = config:get("mem3", "node_db", "nodes"),
{ok, Db} = mem3_util:ensure_exists(DbName),
Args = #changes_args{
feed = "continuous",
@@ -14,13 +14,15 @@

-module(mem3_shards).
-behaviour(gen_server).
-behaviour(config_listener).

-export([init/1, terminate/2, code_change/3]).
-export([handle_call/3, handle_cast/2, handle_info/2]).
-export([handle_config_change/5]).

-export([start_link/0]).
-export([for_db/1, for_docid/2, get/3, local/1, fold/2]).
-export([set_max_size/1, config_change/3]).
-export([set_max_size/1]).

-record(st, {
max_size = 25000,
@@ -87,7 +89,7 @@ local(DbName) ->
lists:filter(Pred, for_db(DbName)).

fold(Fun, Acc) ->
DbName = couch_config:get("mem3", "shards_db", "dbs"),
DbName = config:get("mem3", "shards_db", "dbs"),
{ok, Db} = mem3_util:ensure_exists(DbName),
FAcc = {Db, Fun, Acc},
try
@@ -101,18 +103,18 @@ fold(Fun, Acc) ->
set_max_size(Size) when is_integer(Size), Size > 0 ->
gen_server:call(?MODULE, {set_max_size, Size}).

config_change("mem3", "shard_cache_size", SizeList) ->
handle_config_change("mem3", "shard_cache_size", SizeList, _, _) ->
Size = list_to_integer(SizeList),
ok = gen_server:call(?MODULE, {set_max_size, Size}, infinity);
config_change("mem3", "shard_db", _DbName) ->
ok = gen_server:call(?MODULE, shard_db_changed, infinity).
{ok, gen_server:call(?MODULE, {set_max_size, Size}, infinity)};
handle_config_change("mem3", "shard_db", _DbName, _, _) ->
{ok, gen_server:call(?MODULE, shard_db_changed, infinity)}.

init([]) ->
ets:new(?SHARDS, [bag, protected, named_table, {keypos,#shard.dbname}]),
ets:new(?DBS, [set, protected, named_table]),
ets:new(?ATIMES, [ordered_set, protected, named_table]),
ok = couch_config:register(fun ?MODULE:config_change/3),
SizeList = couch_config:get("mem3", "shard_cache_size", "25000"),
ok = config:listen_for_changes(?MODULE, nil),
SizeList = config:get("mem3", "shard_cache_size", "25000"),
{Pid, _} = spawn_monitor(fun() -> listen_for_changes(get_update_seq()) end),
{ok, #st{
max_size = list_to_integer(SizeList),
@@ -156,6 +158,12 @@ handle_info({'DOWN', _, _, Pid, Reason}, #st{changes_pid=Pid}=St) ->
handle_info({start_listener, Seq}, St) ->
{NewPid, _} = spawn_monitor(fun() -> listen_for_changes(Seq) end),
{noreply, St#st{changes_pid=NewPid}};
handle_info({gen_event_EXIT, {config_listener, ?MODULE}, _Reason}, State) ->
erlang:send_after(5000, self(), restart_config_listener),
{noreply, State};
handle_info(restart_config_listener, State) ->
ok = config:listen_for_changes(?MODULE, nil),
{noreply, State};
handle_info(_Msg, St) ->
{noreply, St}.

@@ -183,13 +191,13 @@ fold_fun(#doc_info{}=DI, _, {Db, UFun, UAcc}) ->
end.

get_update_seq() ->
DbName = couch_config:get("mem3", "shards_db", "dbs"),
DbName = config:get("mem3", "shards_db", "dbs"),
{ok, Db} = mem3_util:ensure_exists(DbName),
couch_db:close(Db),
Db#db.update_seq.

listen_for_changes(Since) ->
DbName = couch_config:get("mem3", "shards_db", "dbs"),
DbName = config:get("mem3", "shards_db", "dbs"),
{ok, Db} = mem3_util:ensure_exists(DbName),
Args = #changes_args{
feed = "continuous",
@@ -228,7 +236,7 @@ changes_callback(timeout, _) ->
ok.

load_shards_from_disk(DbName) when is_binary(DbName) ->
X = ?l2b(couch_config:get("mem3", "shard_db", "dbs")),
X = ?l2b(config:get("mem3", "shard_db", "dbs")),
{ok, Db} = mem3_util:ensure_exists(X),
try
load_shards_from_db(Db, DbName)
@@ -252,7 +260,7 @@ load_shards_from_disk(DbName, DocId)->
[S || #shard{range = [B,E]} = S <- Shards, B =< HashKey, HashKey =< E].

create_if_missing(Name) ->
DbDir = couch_config:get("couchdb", "database_dir"),
DbDir = config:get("couchdb", "database_dir"),
Filename = filename:join(DbDir, ?b2l(Name) ++ ".couch"),
case filelib:is_regular(Filename) of
true ->
@@ -65,7 +65,7 @@ remove_node(Node) ->

init([]) ->
process_flag(trap_exit, true),
Concurrency = couch_config:get("mem3", "sync_concurrency", "10"),
Concurrency = config:get("mem3", "sync_concurrency", "10"),
gen_event:add_handler(mem3_events, mem3_sync_event, []),
{ok, Pid} = start_update_notifier(),
initial_sync(),
@@ -328,16 +328,16 @@ local_dbs() ->
[nodes_db(), shards_db(), users_db()].

nodes_db() ->
?l2b(couch_config:get("mem3", "node_db", "nodes")).
?l2b(config:get("mem3", "node_db", "nodes")).

shards_db() ->
?l2b(couch_config:get("mem3", "shard_db", "dbs")).
?l2b(config:get("mem3", "shard_db", "dbs")).

users_db() ->
?l2b(couch_config:get("couch_httpd_auth", "authentication_db", "_users")).
?l2b(config:get("couch_httpd_auth", "authentication_db", "_users")).

maybe_redirect(Node) ->
case couch_config:get("mem3.redirects", atom_to_list(Node)) of
case config:get("mem3.redirects", atom_to_list(Node)) of
undefined ->
Node;
Redirect ->
@@ -69,12 +69,12 @@ attach_nodes([S | Rest], Acc, [Node | Nodes], UsedNodes) ->
attach_nodes(Rest, [S#shard{node=Node} | Acc], Nodes, [Node | UsedNodes]).

open_db_doc(DocId) ->
DbName = ?l2b(couch_config:get("mem3", "shard_db", "dbs")),
DbName = ?l2b(config:get("mem3", "shard_db", "dbs")),
{ok, Db} = couch_db:open(DbName, []),
try couch_db:open_doc(Db, DocId, []) after couch_db:close(Db) end.

write_db_doc(Doc) ->
DbName = ?l2b(couch_config:get("mem3", "shard_db", "dbs")),
DbName = ?l2b(config:get("mem3", "shard_db", "dbs")),
write_db_doc(DbName, Doc, true).

write_db_doc(DbName, #doc{id=Id, body=Body} = Doc, ShouldMutate) ->
@@ -100,7 +100,7 @@ write_db_doc(DbName, #doc{id=Id, body=Body} = Doc, ShouldMutate) ->

delete_db_doc(DocId) ->
gen_server:cast(mem3_shards, {cache_remove, DocId}),
DbName = ?l2b(couch_config:get("mem3", "shard_db", "dbs")),
DbName = ?l2b(config:get("mem3", "shard_db", "dbs")),
delete_db_doc(DbName, DocId, true).

delete_db_doc(DbName, DocId, ShouldMutate) ->
@@ -153,7 +153,7 @@ to_integer(N) when is_list(N) ->
list_to_integer(N).

n_val(undefined, NodeCount) ->
n_val(couch_config:get("cluster", "n", "3"), NodeCount);
n_val(config:get("cluster", "n", "3"), NodeCount);
n_val(N, NodeCount) when is_list(N) ->
n_val(list_to_integer(N), NodeCount);
n_val(N, NodeCount) when is_integer(NodeCount), N > NodeCount ->

0 comments on commit c2c5478

Please sign in to comment.