You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2014/08/28 14:12:18 UTC

[4/5] couch-index commit: updated refs/heads/master to 9e638f6

Periodically check if an index should close

We need to periodically check if our index file still exists on disk
because index cleanups don't notify the couch_index process when a file
has been deleted. If we don't check for this condition then the index
can remain open indefinitely wasting disk space.

We make sure that we're idle before closing by looking to see if we
have any clients waiting for an update.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/commit/6a5e4860
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/tree/6a5e4860
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/diff/6a5e4860

Branch: refs/heads/master
Commit: 6a5e4860e0b634d8196a88b8f7f6116562f00702
Parents: f99e082
Author: Paul J. Davis <pa...@gmail.com>
Authored: Sun Aug 17 15:12:02 2014 -0500
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Sun Aug 17 15:14:41 2014 -0500

----------------------------------------------------------------------
 src/couch_index.erl | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-index/blob/6a5e4860/src/couch_index.erl
----------------------------------------------------------------------
diff --git a/src/couch_index.erl b/src/couch_index.erl
index d14a74a..40150b7 100644
--- a/src/couch_index.erl
+++ b/src/couch_index.erl
@@ -31,6 +31,9 @@
 -include_lib("couch/include/couch_db.hrl").
 
 
+-define(CHECK_INTERVAL, 600000). % 10 minutes
+
+
 -record(st, {
     mod,
     idx_state,
@@ -85,6 +88,7 @@ config_change("query_server_config", "commit_freq", NewValue) ->
 init({Mod, IdxState}) ->
     ok = config:listen_for_changes(?MODULE, nil),
     DbName = Mod:get(db_name, IdxState),
+    erlang:send_after(?CHECK_INTERVAL, self(), maybe_close),
     Resp = couch_util:with_db(DbName, fun(Db) ->
         case Mod:open(Db, IdxState) of
             {ok, IdxSt} ->
@@ -324,6 +328,29 @@ handle_info(commit, State) ->
             erlang:send_after(Delay, self(), commit),
             {noreply, State}
     end;
+handle_info(maybe_close, State) ->
+    % We need to periodically check if our index file still
+    % exists on disk because index cleanups don't notify
+    % the couch_index process when a file has been deleted. If
+    % we don't check for this condition then the index can
+    % remain open indefinitely wasting disk space.
+    %
+    % We make sure that we're idle before closing by looking
+    % to see if we have any clients waiting for an update.
+    Mod = State#st.mod,
+    case State#st.waiters of
+        [] ->
+            case Mod:index_file_exists(State#st.idx_state) of
+                true ->
+                    erlang:send_after(?CHECK_INTERVAL, self(), maybe_close),
+                    {noreply, State};
+                false ->
+                    {stop, normal, State}
+            end;
+        _ ->
+            erlang:send_after(?CHECK_INTERVAL, self, maybe_close),
+            {noreply, State}
+    end;
 handle_info({'DOWN', _, _, _Pid, _}, #st{mod=Mod, idx_state=IdxState}=State) ->
     Args = [Mod:get(db_name, IdxState), Mod:get(idx_name, IdxState)],
     ?LOG_INFO("Index shutdown by monitor notice for db: ~s idx: ~s", Args),