Skip to content

Commit 5471694

Browse files
adrienvergewohali
authored andcommitted
Compaction: Add snooze_period_ms for finer tuning (#1880)
This commit introduces a new option `snooze_period_ms` (measured in milliseconds), and deprecates `snooze_period` while still supporting it for obvious legacy reasons.
1 parent aba6633 commit 5471694

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

rel/overlay/etc/default.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,10 @@ min_file_size = 131072
434434
; With lots of databases and/or with lots of design docs in one or more
435435
; databases, the compaction_daemon can create significant CPU load when
436436
; checking whether databases and view indexes need compacting. The
437-
; snooze_period setting ensures a smoother CPU load. Defaults to
438-
; 3 seconds wait.
439-
; snooze_period = 3
437+
; snooze_period_ms setting ensures a smoother CPU load. Defaults to
438+
; 3000 milliseconds wait. Note that this option was formerly called
439+
; snooze_period, measured in seconds (it is currently still supported).
440+
; snooze_period_ms = 3000
440441

441442
[compactions]
442443
; List of compaction rules for the compaction daemon.

src/couch/src/couch_compaction_daemon.erl

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,16 @@ handle_config_terminate(_, stop, _) ->
125125
handle_config_terminate(_Server, _Reason, _State) ->
126126
erlang:send_after(?RELISTEN_DELAY, whereis(?MODULE), restart_config_listener).
127127

128+
get_snooze_period() ->
129+
% The snooze_period_ms option should be used, but snooze_period is supported
130+
% for legacy reasons.
131+
Default = config:get_integer("compaction_daemon", "snooze_period", 3),
132+
case config:get_integer("compaction_daemon", "snooze_period_ms", -1) of
133+
-1 -> Default * 1000;
134+
SnoozePeriod -> SnoozePeriod
135+
end.
136+
128137
compact_loop(Parent) ->
129-
SnoozePeriod = config:get_integer("compaction_daemon", "snooze_period", 3),
130138
{ok, _} = couch_server:all_databases(
131139
fun(DbName, Acc) ->
132140
case ets:info(?CONFIG_ETS, size) =:= 0 of
@@ -140,7 +148,7 @@ compact_loop(Parent) ->
140148
case check_period(Config) of
141149
true ->
142150
maybe_compact_db(Parent, DbName, Config),
143-
ok = timer:sleep(SnoozePeriod * 1000);
151+
ok = timer:sleep(get_snooze_period());
144152
false ->
145153
ok
146154
end
@@ -231,8 +239,7 @@ maybe_compact_views(DbName, [DDocName | Rest], Config) ->
231239
timeout ->
232240
ok
233241
end,
234-
SnoozePeriod = config:get_integer("compaction_daemon", "snooze_period", 3),
235-
ok = timer:sleep(SnoozePeriod * 1000);
242+
ok = timer:sleep(get_snooze_period());
236243
false ->
237244
ok
238245
end.
@@ -597,4 +604,60 @@ abs_path2_test() ->
597604
?assertEqual({ok, "/a/b/"}, abs_path2("/a/b")),
598605
ok.
599606

607+
get_snooze_period_test_() ->
608+
{
609+
foreach,
610+
fun() ->
611+
meck:new(config, [passthrough])
612+
end,
613+
fun(_) ->
614+
meck:unload()
615+
end,
616+
[
617+
{"should return default value without config attributes",
618+
fun should_default_without_config/0},
619+
{"should respect old config attribute",
620+
fun should_respect_old_config/0},
621+
{"should respect old config set to zero",
622+
fun should_respect_old_config_zero/0},
623+
{"should respect new config attribute",
624+
fun should_respect_new_config/0},
625+
{"should respect new config set to zero",
626+
fun should_respect_new_config_zero/0}
627+
]
628+
}.
629+
630+
should_default_without_config() ->
631+
?assertEqual(3000, get_snooze_period()).
632+
633+
should_respect_old_config() ->
634+
meck:expect(config, get_integer, fun
635+
("compaction_daemon", "snooze_period", _) -> 1;
636+
(_, _, Default) -> Default
637+
end),
638+
?assertEqual(1000, get_snooze_period()).
639+
640+
should_respect_old_config_zero() ->
641+
meck:expect(config, get_integer, fun
642+
("compaction_daemon", "snooze_period", _) -> 0;
643+
(_, _, Default) -> Default
644+
end),
645+
?assertEqual(0, get_snooze_period()).
646+
647+
should_respect_new_config() ->
648+
meck:expect(config, get_integer, fun
649+
("compaction_daemon", "snooze_period", _) -> 1;
650+
("compaction_daemon", "snooze_period_ms", _) -> 300;
651+
(_, _, Default) -> Default
652+
end),
653+
?assertEqual(300, get_snooze_period()).
654+
655+
should_respect_new_config_zero() ->
656+
meck:expect(config, get_integer, fun
657+
("compaction_daemon", "snooze_period", _) -> 1;
658+
("compaction_daemon", "snooze_period_ms", _) -> 0;
659+
(_, _, Default) -> Default
660+
end),
661+
?assertEqual(0, get_snooze_period()).
662+
600663
-endif.

src/couch/test/couchdb_compaction_daemon_tests.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
start() ->
2525
Ctx = test_util:start_couch(),
2626
ok = config:set("compaction_daemon", "check_interval", "3", false),
27-
ok = config:set("compaction_daemon", "snooze_period", "0", false),
27+
ok = config:set("compaction_daemon", "snooze_period_ms", "0", false),
2828
ok = config:set("compaction_daemon", "min_file_size", "100000", false),
2929
ok = config:delete("compactions", "_default", false),
3030
ok = meck:new(?MODS_TO_MOCK, [passthrough]),

0 commit comments

Comments
 (0)