diff options
| author | Daniil Fedotov <dfedotov@pivotal.io> | 2015-12-02 17:25:03 +0000 |
|---|---|---|
| committer | Daniil Fedotov <dfedotov@pivotal.io> | 2015-12-02 17:25:03 +0000 |
| commit | 829890be2a664a00205a3cbf04cdf00e18b022a0 (patch) | |
| tree | b77f80eaece90ff8e791aca7454e0839c3bcde05 | |
| parent | 38d05e8ec5404d15f2d613fda272c1a98e6ac356 (diff) | |
| download | rabbitmq-server-git-829890be2a664a00205a3cbf04cdf00e18b022a0.tar.gz | |
Moved parse function to separate module and specific parses to monitor modules
| -rw-r--r-- | src/rabbit_alarm.erl | 54 | ||||
| -rw-r--r-- | src/rabbit_control_main.erl | 2 | ||||
| -rw-r--r-- | src/rabbit_disk_monitor.erl | 16 | ||||
| -rw-r--r-- | src/rabbit_resource_monitor_misc.erl | 49 | ||||
| -rw-r--r-- | src/vm_memory_monitor.erl | 17 |
5 files changed, 80 insertions, 58 deletions
diff --git a/src/rabbit_alarm.erl b/src/rabbit_alarm.erl index 1b11c60870..fdb673a61b 100644 --- a/src/rabbit_alarm.erl +++ b/src/rabbit_alarm.erl @@ -39,10 +39,7 @@ -define(SERVER, ?MODULE). --define(DEFAULT_VM_MEMORY_HIGH_WATERMARK, 0.4). --define(DEFAULT_DISK_FREE_LIMIT, 50000000). --export([parse_limit/1]). %%---------------------------------------------------------------------------- @@ -66,8 +63,6 @@ -spec(on_node_up/1 :: (node()) -> 'ok'). -spec(on_node_down/1 :: (node()) -> 'ok'). -spec(get_alarms/0 :: () -> [{alarm(), []}]). --spec(parse_limit/1 :: (integer() | string()) -> - {ok, integer()} | {error, parse_error}). -else. @@ -86,7 +81,7 @@ start() -> {ok, MemoryWatermark} = application:get_env(vm_memory_high_watermark), rabbit_sup:start_restartable_child( - vm_memory_monitor, [parse_mem_limit(MemoryWatermark), + vm_memory_monitor, [MemoryWatermark, fun (Alarm) -> background_gc:run(), set_alarm(Alarm) @@ -94,54 +89,9 @@ start() -> fun clear_alarm/1]), {ok, DiskLimit} = application:get_env(disk_free_limit), rabbit_sup:start_delayed_restartable_child( - rabbit_disk_monitor, [parse_disk_limit(DiskLimit)]), + rabbit_disk_monitor, [DiskLimit]), ok. -parse_mem_limit({absolute, Limit}) -> - case parse_limit(Limit) of - {ok, ParsedLimit} -> {absolute, ParsedLimit}; - {error, parse_error} -> - rabbit_log:error("Unable to parse vm_memory_high_watermark value ~p", [Limit]), - ?DEFAULT_VM_MEMORY_HIGH_WATERMARK - end; -parse_mem_limit(Relative) when is_float(Relative), Relative < 1 -> - Relative; -parse_mem_limit(_) -> - ?DEFAULT_VM_MEMORY_HIGH_WATERMARK. - -parse_disk_limit({mem_relative, Relative}) - when is_float(Relative), Relative < 1 -> - {mem_relative, Relative}; -parse_disk_limit(Limit) -> - case parse_limit(Limit) of - {ok, ParsedLimit} -> ParsedLimit; - {error, parse_error} -> - rabbit_log:error("Unable to parse disk_free_limit value ~p", [Limit]), - ?DEFAULT_DISK_FREE_LIMIT - end. - -parse_limit(MemLim) when is_integer(MemLim) -> {ok, MemLim}; -parse_limit(MemLim) when is_list(MemLim) -> - case re:run(MemLim, - "^(?<VAL>[1-9][0-9]*)(?<UNIT>kB|MB|GB|kiB|MiB|GiB|k|M|G)$", - [{capture, all_names}]) of - {match, [{NumPos, NumLength}, {UnitPos, UnitLength}]} -> - Num = list_to_integer(string:substr(MemLim, NumPos+1, NumLength)), - Unit = string:substr(MemLim, UnitPos+1, UnitLength), - Multiplier = case Unit of - KiB when KiB == "k"; KiB == "kiB" -> 1024; - MiB when MiB == "M"; MiB == "MiB" -> 1024*1024; - GiB when GiB == "G"; GiB == "GiB" -> 1024*1024*1024; - "KB" -> 1000; - "MB" -> 1000000; - "GB" -> 1000000000 - end, - {ok, Num * Multiplier}; - nomatch -> - % log error - {error, parse_error} - end. - stop() -> ok. %% Registers a handler that should be called on every resource alarm change. diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl index 6f2c9fd702..91a079da93 100644 --- a/src/rabbit_control_main.erl +++ b/src/rabbit_control_main.erl @@ -417,7 +417,7 @@ action(set_vm_memory_high_watermark, Node, [Arg], _Opts, Inform) -> rpc_call(Node, vm_memory_monitor, set_vm_memory_high_watermark, [Frac]); action(set_vm_memory_high_watermark, Node, ["absolute", Arg], _Opts, Inform) -> - case rabbit_alarm:parse_limit(Arg) of + case rabbit_resource_monitor_misc:parse_information_unit(Arg) of {ok, Limit} -> Inform("Setting memory threshold on ~p to ~p bytes", [Node, Limit]), rpc_call(Node, vm_memory_monitor, set_vm_memory_high_watermark, diff --git a/src/rabbit_disk_monitor.erl b/src/rabbit_disk_monitor.erl index 88b7469cc0..30b11c0c11 100644 --- a/src/rabbit_disk_monitor.erl +++ b/src/rabbit_disk_monitor.erl @@ -44,6 +44,7 @@ -define(SERVER, ?MODULE). -define(DEFAULT_MIN_DISK_CHECK_INTERVAL, 100). -define(DEFAULT_MAX_DISK_CHECK_INTERVAL, 10000). +-define(DEFAULT_DISK_FREE_LIMIT, 50000000). %% 250MB/s i.e. 250kB/ms -define(FAST_RATE, (250 * 1000)). @@ -233,10 +234,17 @@ parse_free_win32(CommandResult) -> [{capture, all_but_first, list}]), list_to_integer(lists:reverse(Free)). -interpret_limit({mem_relative, R}) -> - round(R * vm_memory_monitor:get_total_memory()); -interpret_limit(L) -> - L. +interpret_limit({mem_relative, Relative}) + when is_float(Relative), Relative < 1 -> + round(Relative * vm_memory_monitor:get_total_memory()); +interpret_limit(Absolute) -> + case rabbit_resource_monitor_misc:parse_information_unit(Absolute) of + {ok, ParsedAbsolute} -> ParsedAbsolute; + {error, parse_error} -> + rabbit_log:error("Unable to parse disk_free_limit value ~p", + [Absolute]), + ?DEFAULT_DISK_FREE_LIMIT + end. emit_update_info(StateStr, CurrentFree, Limit) -> rabbit_log:info( diff --git a/src/rabbit_resource_monitor_misc.erl b/src/rabbit_resource_monitor_misc.erl new file mode 100644 index 0000000000..0994fffc51 --- /dev/null +++ b/src/rabbit_resource_monitor_misc.erl @@ -0,0 +1,49 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is GoPivotal, Inc. +%% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved. +%% + + +-module(rabbit_resource_monitor_misc). + +-export([parse_information_unit/1]). + +-ifdef(use_spec). + +-spec(parse_information_unit/1 :: (integer() | string()) -> + {ok, integer()} | {error, parse_error}). + +-endif. + +parse_information_unit(MemLim) when is_integer(MemLim) -> {ok, MemLim}; +parse_information_unit(MemLim) when is_list(MemLim) -> + case re:run(MemLim, + "^(?<VAL>[0-9]+)(?<UNIT>kB|MB|GB|kiB|MiB|GiB|k|M|G)?$", + [{capture, all_names, list}]) of + {match, [[], _]} -> + {ok, list_to_integer(MemLim)}; + {match, [Unit, Num]} -> + Multiplier = case Unit of + KiB when KiB == "k"; KiB == "kiB" -> 1024; + MiB when MiB == "M"; MiB == "MiB" -> 1024*1024; + GiB when GiB == "G"; GiB == "GiB" -> 1024*1024*1024; + "KB" -> 1000; + "MB" -> 1000000; + "GB" -> 1000000000 + end, + {ok, Num * Multiplier}; + nomatch -> + % log error + {error, parse_error} + end.
\ No newline at end of file diff --git a/src/vm_memory_monitor.erl b/src/vm_memory_monitor.erl index f07ccce89f..e599f50103 100644 --- a/src/vm_memory_monitor.erl +++ b/src/vm_memory_monitor.erl @@ -49,6 +49,7 @@ %% wrong. Scale by vm_memory_high_watermark in configuration to get a %% sensible value. -define(MEMORY_SIZE_FOR_UNKNOWN_OS, 1073741824). +-define(DEFAULT_VM_MEMORY_HIGH_WATERMARK, 0.4). -record(state, {total_memory, memory_limit, @@ -208,7 +209,7 @@ set_mem_limits(State, MemLimit) -> _ -> TotalMemory end, - MemLim = interpret_limit(MemLimit, UsableMemory), + MemLim = interpret_limit(parse_mem_limit(MemLimit), UsableMemory), error_logger:info_msg("Memory limit set to ~pMB of ~pMB total.~n", [trunc(MemLim/?ONE_MB), trunc(TotalMemory/?ONE_MB)]), internal_update(State #state { total_memory = TotalMemory, @@ -220,6 +221,20 @@ interpret_limit({'absolute', MemLim}, UsableMemory) -> interpret_limit(MemFraction, UsableMemory) -> trunc(MemFraction * UsableMemory). + +parse_mem_limit({absolute, Limit}) -> + case rabbit_resource_monitor_misc:parse_information_unit(Limit) of + {ok, ParsedLimit} -> {absolute, ParsedLimit}; + {error, parse_error} -> + rabbit_log:error("Unable to parse vm_memory_high_watermark value ~p", [Limit]), + ?DEFAULT_VM_MEMORY_HIGH_WATERMARK + end; +parse_mem_limit(Relative) when is_float(Relative), Relative < 1 -> + Relative; +parse_mem_limit(_) -> + ?DEFAULT_VM_MEMORY_HIGH_WATERMARK. + + internal_update(State = #state { memory_limit = MemLimit, alarmed = Alarmed, alarm_funs = {AlarmSet, AlarmClear} }) -> |
