You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2015/06/02 21:35:48 UTC

[17/50] couch commit: updated refs/heads/2080-port-cors-to-chttpd to 529339b

Improve couch_compaction_daemon test performance

COUCHDB-2547


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

Branch: refs/heads/2080-port-cors-to-chttpd
Commit: 17937d53e9692c2d999c20a789616b26da0f41ee
Parents: 7d7e039
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Tue Feb 24 12:22:01 2015 -0800
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Tue Feb 24 12:22:01 2015 -0800

----------------------------------------------------------------------
 src/couch_compaction_daemon.erl    | 23 +++++++---
 test/couchdb_compaction_daemon.erl | 76 ++++++++++++++++++++++-----------
 2 files changed, 70 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/17937d53/src/couch_compaction_daemon.erl
----------------------------------------------------------------------
diff --git a/src/couch_compaction_daemon.erl b/src/couch_compaction_daemon.erl
index 2294fbc..7888eb4 100644
--- a/src/couch_compaction_daemon.erl
+++ b/src/couch_compaction_daemon.erl
@@ -15,7 +15,7 @@
 -behaviour(config_listener).
 
 % public API
--export([start_link/0]).
+-export([start_link/0, in_progress/0]).
 
 % gen_server callbacks
 -export([init/1, handle_call/3, handle_info/2, handle_cast/2]).
@@ -30,7 +30,8 @@
 -define(CONFIG_ETS, couch_compaction_daemon_config).
 
 -record(state, {
-    loop_pid
+    loop_pid,
+    in_progress = []
 }).
 
 -record(config, {
@@ -50,6 +51,8 @@
 start_link() ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
+in_progress() ->
+    gen_server:call(?MODULE, in_progress).
 
 init(_) ->
     process_flag(trap_exit, true),
@@ -82,6 +85,14 @@ handle_cast({config_update, DbName, Config}, #state{loop_pid = Loop} = State) ->
     {noreply, State}.
 
 
+handle_call({start, DbName}, {Pid, _},
+        #state{loop_pid = Pid, in_progress = InProgress} = State) ->
+    {reply, ok, State#state{in_progress = [DbName|InProgress]}};
+handle_call({stop, DbName}, {Pid, _},
+        #state{loop_pid = Pid, in_progress = InProgress} = State) ->
+    {reply, ok, State#state{in_progress = InProgress -- [DbName]}};
+handle_call(in_progress, _From, #state{in_progress = InProgress} = State) ->
+    {reply, InProgress, State};
 handle_call(Msg, _From, State) ->
     {stop, {unexpected_call, Msg}, State}.
 
@@ -123,7 +134,7 @@ compact_loop(Parent) ->
                 {ok, Config} ->
                     case check_period(Config) of
                     true ->
-                        maybe_compact_db(DbName, Config);
+                        maybe_compact_db(Parent, DbName, Config);
                     false ->
                         ok
                     end
@@ -142,12 +153,13 @@ compact_loop(Parent) ->
     compact_loop(Parent).
 
 
-maybe_compact_db(DbName, Config) ->
+maybe_compact_db(Parent, DbName, Config) ->
     case (catch couch_db:open_int(DbName, [?ADMIN_CTX])) of
     {ok, Db} ->
         DDocNames = db_ddoc_names(Db),
         case can_db_compact(Config, Db) of
         true ->
+            gen_server:call(Parent, {start, DbName}),
             {ok, _} = couch_db:start_compact(Db),
             TimeLeft = compact_time_left(Config),
             case Config#config.parallel_view_compact of
@@ -192,7 +204,8 @@ maybe_compact_db(DbName, Config) ->
                     unlink(ViewsCompactPid),
                     exit(ViewsCompactPid, kill)
                 end
-            end;
+            end,
+            gen_server:call(Parent, {stop, DbName});
         false ->
             couch_db:close(Db),
             maybe_compact_views(DbName, DDocNames, Config)

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/17937d53/test/couchdb_compaction_daemon.erl
----------------------------------------------------------------------
diff --git a/test/couchdb_compaction_daemon.erl b/test/couchdb_compaction_daemon.erl
index 7d771fd..92ed8ac 100644
--- a/test/couchdb_compaction_daemon.erl
+++ b/test/couchdb_compaction_daemon.erl
@@ -15,7 +15,6 @@
 -include_lib("couch/include/couch_eunit.hrl").
 -include_lib("couch/include/couch_db.hrl").
 
--define(DELAY, 100).
 -define(TIMEOUT, 30000).
 -define(TIMEOUT_S, ?TIMEOUT div 1000).
 
@@ -70,15 +69,18 @@ should_compact_by_default_rule(DbName) ->
         {_, DbFileSize} = get_db_frag(DbName),
         {_, ViewFileSize} = get_view_frag(DbName),
 
-        ok = config:set("compactions", "_default",
-            "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
-            false),
+        with_config_change(DbName, fun() ->
+            ok = config:set("compactions", "_default",
+                "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
+                false)
+        end),
 
-        ok = timer:sleep(5000), % something >= check_interval
+        wait_compaction_started(DbName),
         wait_compaction_finished(DbName),
 
-        ok = config:delete("compactions", "_default", false),
-        ok = timer:sleep(1000), % need to wait so gen_server:cast would complete
+        with_config_change(DbName, fun() ->
+            ok = config:delete("compactions", "_default", false)
+        end),
 
         {DbFrag2, DbFileSize2} = get_db_frag(DbName),
         {ViewFrag2, ViewFileSize2} = get_view_frag(DbName),
@@ -101,14 +103,18 @@ should_compact_by_dbname_rule(DbName) ->
         {_, DbFileSize} = get_db_frag(DbName),
         {_, ViewFileSize} = get_view_frag(DbName),
 
-        ok = config:set("compactions", ?b2l(DbName),
-            "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
-            false),
+        with_config_change(DbName, fun() ->
+            ok = config:set("compactions", ?b2l(DbName),
+                "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
+                false)
+        end),
 
-        ok = timer:sleep(4000), % something >= check_interval
+        wait_compaction_started(DbName),
         wait_compaction_finished(DbName),
-        ok = config:delete("compactions", ?b2l(DbName), false),
-        ok = timer:sleep(1000), % need to wait so gen_server:cast would complete
+
+        with_config_change(DbName, fun() ->
+            ok = config:delete("compactions", ?b2l(DbName), false)
+        end),
 
         {DbFrag2, DbFileSize2} = get_db_frag(DbName),
         {ViewFrag2, ViewFileSize2} = get_view_frag(DbName),
@@ -198,12 +204,29 @@ get_view_frag(DbName) ->
 get_size(Kind, Info) ->
     couch_util:get_nested_json_value({Info}, [sizes, Kind]).
 
+wait_compaction_started(DbName) ->
+    WaitFun = fun() ->
+        case is_compaction_running(DbName) of
+            false -> wait;
+            true ->  ok
+        end
+    end,
+    case test_util:wait(WaitFun, 10000) of
+        timeout ->
+            erlang:error({assertion_failed,
+                          [{module, ?MODULE},
+                           {line, ?LINE},
+                           {reason, "Compaction starting timeout"}]});
+        _ ->
+            ok
+    end.
+
 wait_compaction_finished(DbName) ->
     WaitFun = fun() ->
-       case is_compaction_running(DbName) of
-           true -> wait;
-           false -> ok
-       end
+        case is_compaction_running(DbName) of
+            true -> wait;
+            false -> ok
+        end
     end,
     case test_util:wait(WaitFun, 10000) of
         timeout ->
@@ -215,16 +238,21 @@ wait_compaction_finished(DbName) ->
             ok
     end.
 
-is_compaction_running(DbName) ->
-    {ok, Db} = couch_db:open_int(DbName, []),
-    {ok, DbInfo} = couch_db:get_db_info(Db),
-    {ok, ViewInfo} = couch_mrview:get_info(Db, <<"_design/foo">>),
-    couch_db:close(Db),
-    (couch_util:get_value(compact_running, ViewInfo) =:= true)
-        orelse (couch_util:get_value(compact_running, DbInfo) =:= true).
+is_compaction_running(_DbName) ->
+    couch_compaction_daemon:in_progress() /= [].
 
 is_idle(DbName) ->
     {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]),
     Monitors = couch_db:monitored_by(Db),
     ok = couch_db:close(Db),
     not lists:any(fun(M) -> M /= self() end, Monitors).
+
+with_config_change(DbName, Fun) ->
+    Current = ets:info(couch_compaction_daemon_config, size),
+    Fun(),
+    test_util:wait(fun() ->
+        case ets:info(couch_compaction_daemon_config, size) == Current of
+            false -> ok;
+            true -> wait
+        end
+    end).