diff options
-rw-r--r-- | rel/overlay/etc/default.ini | 3 | ||||
-rw-r--r-- | src/smoosh/src/smoosh_channel.erl | 20 | ||||
-rw-r--r-- | src/smoosh/src/smoosh_priority_queue.erl | 2 | ||||
-rw-r--r-- | src/smoosh/test/smoosh_tests.erl | 31 |
4 files changed, 44 insertions, 12 deletions
diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini index 71c0d59d9..316c7960c 100644 --- a/rel/overlay/etc/default.ini +++ b/rel/overlay/etc/default.ini @@ -659,6 +659,9 @@ state_dir = {{state_dir}} ; Sets the log level for informational compaction related entries. ;compaction_log_level = debug +; Enable persistence for smoosh state +; persist = false + [ioq] ; The maximum number of concurrent in-flight IO requests that ;concurrency = 10 diff --git a/src/smoosh/src/smoosh_channel.erl b/src/smoosh/src/smoosh_channel.erl index 7671d120b..50274f704 100644 --- a/src/smoosh/src/smoosh_channel.erl +++ b/src/smoosh/src/smoosh_channel.erl @@ -105,8 +105,9 @@ init(Name) -> erlang:send_after(60 * 1000, self(), check_window), process_flag(trap_exit, true), Waiting = smoosh_priority_queue:new(Name), + Persist = config:get_boolean("smoosh", "persist", false), State = - case smoosh_utils:is_view_channel(Name) of + case smoosh_utils:is_view_channel(Name) orelse Persist =:= false of true -> schedule_unpause(), #state{name = Name, waiting = Waiting, paused = true, activated = true}; @@ -273,8 +274,7 @@ handle_info(start_recovery, #state{name = Name, waiting = Waiting0} = State0) -> handle_info(activate, State) -> {noreply, activate_channel(State)}; handle_info(persist, State) -> - persist_queue(State), - erlang:send_after(?CHECKPOINT_INTERVAL_IN_MSEC, self(), persist), + ok = persist_and_reschedule(State), {noreply, State}; handle_info(pause, State) -> {noreply, State#state{paused = true}}; @@ -284,8 +284,7 @@ handle_info(unpause, State) -> terminate(_Reason, _State) -> ok. -persist_queue(State) -> - write_state_to_file(State). +% private functions. recover(FilePath) -> case do_recover(FilePath) of @@ -333,7 +332,12 @@ parse_state(1, ?VSN, Binary) -> parse_state(Vsn, ?VSN, _) -> error({unsupported_version, Vsn}). -write_state_to_file(#state{name = Name, active = Active, starting = Starting, waiting = Waiting}) -> +persist_and_reschedule(State) -> + persist_queue(State), + erlang:send_after(?CHECKPOINT_INTERVAL_IN_MSEC, self(), persist), + ok. + +persist_queue(#state{name = Name, active = Active, starting = Starting, waiting = Waiting}) -> Active1 = lists:foldl( fun({DbName, _}, Acc) -> [DbName | Acc] @@ -358,8 +362,6 @@ active_file_name(Name) -> starting_file_name(Name) -> filename:join(config:get("smoosh", "state_dir", "."), Name ++ ".starting"). -% private functions. - add_to_queue(Key, Priority, State) -> #state{active = Active, waiting = Q} = State, case lists:keymember(Key, 1, Active) of @@ -414,7 +416,7 @@ activate_channel(#state{name = Name, waiting = Waiting0, requests = Requests0} = State1 = maybe_start_compaction(State0#state{ waiting = Waiting2, paused = false, activated = true, requests = [] }), - handle_info(persist, State1), + ok = persist_and_reschedule(State1), schedule_unpause(), State1#state{paused = true}. diff --git a/src/smoosh/src/smoosh_priority_queue.erl b/src/smoosh/src/smoosh_priority_queue.erl index 30dcf4d20..16deb0f76 100644 --- a/src/smoosh/src/smoosh_priority_queue.erl +++ b/src/smoosh/src/smoosh_priority_queue.erl @@ -22,7 +22,7 @@ -export([is_empty/1]). --export([file_name/1, write_to_file/1]). +-export([write_to_file/1]). -define(VSN, 1). diff --git a/src/smoosh/test/smoosh_tests.erl b/src/smoosh/test/smoosh_tests.erl index 453a99d15..73876888b 100644 --- a/src/smoosh/test/smoosh_tests.erl +++ b/src/smoosh/test/smoosh_tests.erl @@ -13,14 +13,17 @@ setup(ChannelType) -> couch_db:close(Db), {ok, ChannelPid} = smoosh_server:get_channel(ChannelType), smoosh_channel:flush(ChannelPid), + ok = config:set("smoosh", "persist", "true", false), ok = config:set(config_section(ChannelType), "min_size", "1", false), ok = config:set(config_section(ChannelType), "min_priority", "1", false), DbName. teardown(ChannelType, DbName) -> ok = couch_server:delete(DbName, [?ADMIN_CTX]), + ok = config:delete("smoosh", "persist", false), ok = config:delete(config_section(DbName), "min_size", false), ok = config:delete(config_section(DbName), "min_priority", false), + meck:unload(), {ok, ChannelPid} = smoosh_server:get_channel(ChannelType), smoosh_channel:flush(ChannelPid), ok. @@ -48,10 +51,12 @@ smoosh_test_() -> persistence_tests() -> Tests = [ - fun should_persist_queue/2 + fun should_persist_queue/2, + fun should_call_recover/2, + fun should_not_call_recover/2 ], { - "Should persist queue state", + "Various persistence tests", [ make_test_case("ratio_dbs", Tests) ] @@ -96,6 +101,28 @@ should_persist_queue(ChannelType, DbName) -> ok end). +should_call_recover(_ChannelType, _DbName) -> + ?_test(begin + ok = application:stop(smoosh), + ok = config:set("smoosh", "persist", "true", false), + meck:new(smoosh_priority_queue, [passthrough]), + ok = application:start(smoosh), + timer:sleep(1000), + ?assertNotEqual(0, meck:num_calls(smoosh_priority_queue, recover, '_')), + ok + end). + +should_not_call_recover(_ChannelType, _DbName) -> + ?_test(begin + ok = application:stop(smoosh), + ok = config:set("smoosh", "persist", "false", false), + meck:new(smoosh_priority_queue, [passthrough]), + ok = application:start(smoosh), + timer:sleep(1000), + ?assertEqual(0, meck:num_calls(smoosh_priority_queue, recover, '_')), + ok + end). + grow_db_file(DbName, SizeInKb) -> {ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]), Data = b64url:encode(crypto:strong_rand_bytes(SizeInKb * 1024)), |