diff options
author | Robert Newson <rnewson@apache.org> | 2013-03-09 18:22:19 -0600 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2013-03-20 06:02:53 -0500 |
commit | 0861ab00177223afdaeb10d329ab2daa79cb053e (patch) | |
tree | b22c022bb1e9d4f8ffb28ea07a9bf9830d91bdbc /src/couch_index | |
parent | 5df0942935ad379a53dfd17b835b0cae497fd305 (diff) | |
download | couchdb-0861ab00177223afdaeb10d329ab2daa79cb053e.tar.gz |
Major change to use the new config app
This replaces couch_config with the config app. The config app is mostly
a direct port of couch_config to a new namespace with the addition of a
rewritten config change notification mechanism. The new change mechanism
removes the ability to register an anonymous function that breaks code
upgrades and generally cleans up the various listening patterns using a
new behavior definition.
Diffstat (limited to 'src/couch_index')
-rw-r--r-- | src/couch_index/src/couch_index.erl | 26 | ||||
-rw-r--r-- | src/couch_index/src/couch_index_server.erl | 35 | ||||
-rw-r--r-- | src/couch_index/src/couch_index_util.erl | 4 |
3 files changed, 46 insertions, 19 deletions
diff --git a/src/couch_index/src/couch_index.erl b/src/couch_index/src/couch_index.erl index 483dc59d7..3253a32b2 100644 --- a/src/couch_index/src/couch_index.erl +++ b/src/couch_index/src/couch_index.erl @@ -12,18 +12,20 @@ -module(couch_index). -behaviour(gen_server). - +-behaviour(config_listener). %% API -export([start_link/1, stop/1, get_state/2, get_info/1]). -export([trigger_update/2]). -export([compact/1, compact/2]). --export([config_change/3]). %% gen_server callbacks -export([init/1, terminate/2, code_change/3]). -export([handle_call/3, handle_cast/2, handle_info/2]). +% config_listener api +-export([handle_config_change/5]). + -include_lib("couch/include/couch_db.hrl"). @@ -72,12 +74,8 @@ compact(Pid, Options) -> end. -config_change("query_server_config", "commit_freq", NewValue) -> - ok = gen_server:cast(?MODULE, {config_update, NewValue}). - - init({Mod, IdxState}) -> - ok = couch_config:register(fun ?MODULE:config_change/3), + ok = config:listen_for_changes(?MODULE, nil), DbName = Mod:get(db_name, IdxState), Resp = couch_util:with_db(DbName, fun(Db) -> case Mod:open(Db, IdxState) of @@ -92,7 +90,7 @@ init({Mod, IdxState}) -> {ok, NewIdxState} -> {ok, UPid} = couch_index_updater:start_link(self(), Mod), {ok, CPid} = couch_index_compactor:start_link(self(), Mod), - Delay = couch_config:get("query_server_config", "commit_freq", "5"), + Delay = config:get("query_server_config", "commit_freq", "5"), MsDelay = 1000 * list_to_integer(Delay), State = #st{ mod=Mod, @@ -288,6 +286,12 @@ handle_cast(_Mesg, State) -> {stop, unhandled_cast, State}. +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(commit, #st{committed=true}=State) -> {noreply, State}; handle_info(commit, State) -> @@ -321,6 +325,12 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}. +handle_config_change("query_server_config", "commit_freq", Val, _, _) -> + {ok, gen_server:cast(?MODULE, {config_update, Val})}; +handle_config_change(_, _, _, _, _) -> + {ok, nil}. + + maybe_restart_updater(#st{waiters=[]}) -> ok; maybe_restart_updater(#st{mod=Mod, idx_state=IdxState}=State) -> diff --git a/src/couch_index/src/couch_index_server.erl b/src/couch_index/src/couch_index_server.erl index 8c76a62a2..a106f14e1 100644 --- a/src/couch_index/src/couch_index_server.erl +++ b/src/couch_index/src/couch_index_server.erl @@ -12,13 +12,17 @@ -module(couch_index_server). -behaviour(gen_server). +-behaviour(config_listener). -export([start_link/0, get_index/4, get_index/3, get_index/2]). --export([config_change/2, update_notify/1]). +-export([update_notify/1]). -export([init/1, terminate/2, code_change/3]). -export([handle_call/3, handle_cast/2, handle_info/2]). +% config_listener api +-export([handle_config_change/5]). + -include_lib("couch/include/couch_db.hrl"). -define(BY_SIG, couchdb_indexes_by_sig). @@ -81,14 +85,14 @@ get_index(Module, IdxState) -> init([]) -> process_flag(trap_exit, true), - couch_config:register(fun ?MODULE:config_change/2), + ok = config:listen_for_changes(?MODULE, nil), ets:new(?BY_SIG, [protected, set, named_table]), ets:new(?BY_PID, [private, set, named_table]), ets:new(?BY_DB, [protected, bag, named_table]), couch_db_update_notifier:start_link(fun ?MODULE:update_notify/1), RootDir = couch_index_util:root_dir(), % Deprecation warning if it wasn't index_dir - case couch_config:get("couchdb", "index_dir") of + case config:get("couchdb", "index_dir") of undefined -> Msg = "Deprecation warning: 'view_index_dir' is now 'index_dir'", ?LOG_ERROR(Msg, []); @@ -137,6 +141,12 @@ handle_cast({reset_indexes, DbName}, State) -> {noreply, State}. +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, State#st.root_dir), + {noreply, State}; handle_info({'EXIT', Pid, Reason}, Server) -> case ets:lookup(?BY_PID, Pid) of [{Pid, {DbName, Sig}}] -> @@ -158,6 +168,19 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}. +handle_config_change("couchdb", "index_dir", RootDir, _, RootDir) -> + {ok, RootDir}; +handle_config_change("couchdb", "view_index_dir", RootDir, _, RootDir) -> + {ok, RootDir}; +handle_config_change("couchdb", "index_dir", _, _, _) -> + exit(whereis(couch_index_server), config_change), + remove_handler; +handle_config_change("couchdb", "view_index_dir", _, _, _) -> + exit(whereis(couch_index_server), config_change), + remove_handler; +handle_config_change(_, _, _, _, RootDir) -> + {ok, RootDir}. + new_index({Mod, IdxState, DbName, Sig}) -> DDocId = Mod:get(idx_name, IdxState), case couch_index:start_link({Mod, IdxState}) of @@ -197,12 +220,6 @@ rem_from_ets(DbName, Sig, DDocId, Pid) -> ets:delete_object(?BY_DB, {DbName, {DDocId, Sig}}). -config_change("couchdb", "view_index_dir") -> - exit(whereis(?MODULE), config_change); -config_change("couchdb", "index_dir") -> - exit(whereis(?MODULE), config_change). - - update_notify({deleted, DbName}) -> gen_server:cast(?MODULE, {reset_indexes, DbName}); update_notify({created, DbName}) -> diff --git a/src/couch_index/src/couch_index_util.erl b/src/couch_index/src/couch_index_util.erl index cef05f5c1..cf1ff7561 100644 --- a/src/couch_index/src/couch_index_util.erl +++ b/src/couch_index/src/couch_index_util.erl @@ -19,8 +19,8 @@ root_dir() -> - case couch_config:get("couchdb", "index_dir") of - undefined -> couch_config:get("couchdb", "view_index_dir"); + case config:get("couchdb", "index_dir") of + undefined -> config:get("couchdb", "view_index_dir"); Value -> Value end. |