summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gen_server2.erl9
-rw-r--r--src/rabbit_msg_store.erl37
2 files changed, 32 insertions, 14 deletions
diff --git a/src/gen_server2.erl b/src/gen_server2.erl
index c725082724..c48061518d 100644
--- a/src/gen_server2.erl
+++ b/src/gen_server2.erl
@@ -25,8 +25,11 @@
%% handle_pre_hibernate/1 and handle_post_hibernate/1. These will be
%% called immediately prior to and post hibernation, respectively. If
%% handle_pre_hibernate returns {hibernate, NewState} then the process
-%% will hibernate. If the module does not implement
-%% handle_pre_hibernate/1 then the default action is to hibernate.
+%% will hibernate. If handle_pre_hibernate returns {insomniate,
+%% NewState} then the process will go around again, trying to receive
+%% for up to the current timeout value before attempting to hibernate
+%% again. If the module does not implement handle_pre_hibernate/1 then
+%% the default action is to hibernate.
%%
%% 6) init can return a 4th arg, {backoff, InitialTimeout,
%% MinimumTimeout, DesiredHibernatePeriod} (all in
@@ -36,7 +39,7 @@
%% InitialTimeout supplied from init). After this timeout has
%% occurred, hibernation will occur as normal. Upon awaking, a new
%% current timeout value will be calculated.
-%%
+%%
%% The purpose is that the gen_server2 takes care of adjusting the
%% current timeout value such that the process will increase the
%% timeout value repeatedly if it is unable to sleep for the
diff --git a/src/rabbit_msg_store.erl b/src/rabbit_msg_store.erl
index 0b711b1336..6306ac32b0 100644
--- a/src/rabbit_msg_store.erl
+++ b/src/rabbit_msg_store.erl
@@ -39,7 +39,7 @@
-export([sync/0]). %% internal
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3]).
+ terminate/2, code_change/3, handle_pre_hibernate/1]).
-define(SERVER, ?MODULE).
@@ -112,6 +112,9 @@
-define(READ_AHEAD_MODE, [read_ahead | ?READ_MODE]).
-define(WRITE_MODE, [write]).
+-define(HIBERNATE_AFTER_MIN, 1000).
+-define(DESIRED_HIBERNATE, 10000).
+
%% The components:
%%
%% MsgLocation: this is an ets table which contains:
@@ -299,7 +302,8 @@ init([Dir, MsgRefDeltaGen, MsgRefDeltaGenInit]) ->
{ok, Offset} = file_handle_cache:position(FileHdl, Offset),
ok = file_handle_cache:truncate(FileHdl),
- {ok, State1 #msstate { current_file_handle = FileHdl }}.
+ {ok, State1 #msstate { current_file_handle = FileHdl }, hibernate,
+ {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}.
handle_call({read, MsgId}, From, State) ->
case read_message(MsgId, State) of
@@ -424,6 +428,13 @@ terminate(_Reason, State = #msstate { msg_locations = MsgLocations,
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
+handle_pre_hibernate(State) ->
+ {Result, State1} = maybe_compact1(State),
+ {case Result of
+ true -> insomniate;
+ false -> hibernate
+ end, State1}.
+
%%----------------------------------------------------------------------------
%% general helper functions
%%----------------------------------------------------------------------------
@@ -437,11 +448,11 @@ reply(Reply, State) ->
{reply, Reply, State1, Timeout}.
next_state(State = #msstate { on_sync = [], sync_timer_ref = undefined }) ->
- {State, infinity};
+ {State, hibernate};
next_state(State = #msstate { sync_timer_ref = undefined }) ->
{start_sync_timer(State), 0};
next_state(State = #msstate { on_sync = [] }) ->
- {stop_sync_timer(State), infinity};
+ {stop_sync_timer(State), hibernate};
next_state(State) ->
{State, 0}.
@@ -933,19 +944,23 @@ maybe_roll_to_new_file(Offset,
maybe_roll_to_new_file(_, State) ->
State.
-maybe_compact(State = #msstate { sum_valid_data = SumValid,
- sum_file_size = SumFileSize,
- gc_pid = undefined,
- file_summary = FileSummary })
+maybe_compact(State) ->
+ {_Bool, State1} = maybe_compact1(State),
+ State1.
+
+maybe_compact1(State = #msstate { sum_valid_data = SumValid,
+ sum_file_size = SumFileSize,
+ gc_pid = undefined,
+ file_summary = FileSummary })
when (SumFileSize - SumValid) / SumFileSize > ?GARBAGE_FRACTION ->
%% Pid = spawn_link(fun() ->
%% io:format("GC process!~n")
%% %% gen_server2:pcast(?SERVER, 9, {gc_finished, self(),}),
%% end),
%% State #msstate { gc_pid = Pid };
- State;
-maybe_compact(State) ->
- State.
+ {true, State};
+maybe_compact1(State) ->
+ {false, State}.
compact(Files, State) ->
%% smallest number, hence eldest, hence left-most, first