|
| 1 | +%% The contents of this file are subject to the Mozilla Public License |
| 2 | +%% Version 1.1 (the "License"); you may not use this file except in |
| 3 | +%% compliance with the License. You may obtain a copy of the License |
| 4 | +%% at http://www.mozilla.org/MPL/ |
| 5 | +%% |
| 6 | +%% Software distributed under the License is distributed on an "AS IS" |
| 7 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +%% the License for the specific language governing rights and |
| 9 | +%% limitations under the License. |
| 10 | +%% |
| 11 | +%% The Original Code is RabbitMQ. |
| 12 | +%% |
| 13 | +%% The Initial Developer of the Original Code is GoPivotal, Inc. |
| 14 | +%% Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. |
| 15 | +%% |
| 16 | +-module(rabbit_quorum_memory_manager). |
| 17 | + |
| 18 | +-include("rabbit.hrl"). |
| 19 | + |
| 20 | +-export([init/1, handle_call/2, handle_event/2, handle_info/2, |
| 21 | + terminate/2, code_change/3]). |
| 22 | +-export([register/0, unregister/0]). |
| 23 | + |
| 24 | +-record(state, {last_roll_over, |
| 25 | + interval}). |
| 26 | + |
| 27 | +-rabbit_boot_step({rabbit_quorum_memory_manager, |
| 28 | + [{description, "quorum memory manager"}, |
| 29 | + {mfa, {?MODULE, register, []}}, |
| 30 | + {cleanup, {?MODULE, unregister, []}}, |
| 31 | + {requires, rabbit_event}, |
| 32 | + {enables, recovery}]}). |
| 33 | + |
| 34 | +register() -> |
| 35 | + gen_event:add_handler(rabbit_alarm, ?MODULE, []). |
| 36 | + |
| 37 | +unregister() -> |
| 38 | + gen_event:delete_handler(rabbit_alarm, ?MODULE, []). |
| 39 | + |
| 40 | +init([]) -> |
| 41 | + {ok, #state{interval = interval()}}. |
| 42 | + |
| 43 | +handle_call( _, State) -> |
| 44 | + {ok, ok, State}. |
| 45 | + |
| 46 | +handle_event({set_alarm, {{resource_limit, memory, Node}, []}}, |
| 47 | + #state{last_roll_over = undefined} = State) when Node == node() -> |
| 48 | + {ok, force_roll_over(State)}; |
| 49 | +handle_event({set_alarm, {{resource_limit, memory, Node}, []}}, |
| 50 | + #state{last_roll_over = Last, interval = Interval } = State) |
| 51 | + when Node == node() -> |
| 52 | + Now = erlang:system_time(millisecond), |
| 53 | + case Now > (Last + Interval) of |
| 54 | + true -> |
| 55 | + {ok, force_roll_over(State)}; |
| 56 | + false -> |
| 57 | + {ok, State} |
| 58 | + end; |
| 59 | +handle_event(_, State) -> |
| 60 | + {ok, State}. |
| 61 | + |
| 62 | +handle_info(_, State) -> |
| 63 | + {ok, State}. |
| 64 | + |
| 65 | +terminate(_, _State) -> |
| 66 | + ok. |
| 67 | + |
| 68 | +code_change(_OldVsn, State, _Extra) -> |
| 69 | + {ok, State}. |
| 70 | + |
| 71 | +force_roll_over(State) -> |
| 72 | + ra_log_wal:force_roll_over(ra_log_wal), |
| 73 | + State#state{last_roll_over = erlang:system_time(millisecond)}. |
| 74 | + |
| 75 | +interval() -> |
| 76 | + application:get_env(rabbit, min_wal_roll_over_interval, 20000). |
0 commit comments