Skip to content

Commit 44016e6

Browse files
committed
CLI: Extend set_disk_free_limit to set mount limits
With this change you can say: rabbitmqctl set_disk_limit mount Streaming 2GiB This applies the limit only to the "Streaming" mount.
1 parent 215ea6f commit 44016e6

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

deps/rabbit/src/rabbit_disk_monitor.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
terminate/2, code_change/3]).
2727

2828
-export([get_disk_free_limit/0, set_disk_free_limit/1,
29+
set_disk_free_limit/2,
2930
get_min_check_interval/0, set_min_check_interval/1,
3031
get_max_check_interval/0, set_max_check_interval/1,
3132
get_disk_free/0, get_mount_free/0, set_enabled/1]).
@@ -105,6 +106,11 @@ get_disk_free_limit() ->
105106
set_disk_free_limit(Limit) ->
106107
gen_server:call(?MODULE, {set_disk_free_limit, Limit}).
107108

109+
-spec set_disk_free_limit(MountName :: binary(), integer()) -> 'ok'.
110+
set_disk_free_limit(MountName, Limit)
111+
when is_binary(MountName) andalso is_integer(Limit) ->
112+
gen_server:call(?MODULE, {set_disk_free_limit, MountName, Limit}).
113+
108114
-spec get_min_check_interval() -> integer().
109115
get_min_check_interval() ->
110116
safe_ets_lookup(min_check_interval, ?DEFAULT_MIN_DISK_CHECK_INTERVAL).
@@ -189,6 +195,23 @@ handle_call({set_disk_free_limit, _}, _From, #state{enabled = false} = State) ->
189195
handle_call({set_disk_free_limit, Limit}, _From, State) ->
190196
{reply, ok, set_disk_limits(State, Limit)};
191197

198+
handle_call({set_disk_free_limit, Name, Limit}, _From,
199+
#state{mounts = Mounts0} = State) ->
200+
MatchingMount = lists:search(
201+
fun({_Path, #mount{name = N}}) ->
202+
Name =:= N
203+
end, maps:to_list(Mounts0)),
204+
case MatchingMount of
205+
{value, {Path, Mount}} ->
206+
?LOG_INFO("Updated disk free limit of mount '~ts'", [Name]),
207+
Mounts = Mounts0#{Path := Mount#mount{limit = Limit}},
208+
{reply, ok, State#state{mounts = Mounts}};
209+
false ->
210+
?LOG_WARNING("Cannot set disk free limit for mount '~ts' since "
211+
"the name does not match any known mounts.", [Name]),
212+
{reply, ok, State}
213+
end;
214+
192215
handle_call(get_max_check_interval, _From, State) ->
193216
{reply, State#state.max_interval, State};
194217

deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/set_disk_free_limit_command.ex

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand do
2424
{:validation_failure, :too_many_args}
2525
end
2626

27+
def validate(["mount" | _] = args, _) when length(args) < 3 do
28+
{:validation_failure, :not_enough_args}
29+
end
30+
31+
def validate(["mount" | _] = args, _) when length(args) > 3 do
32+
{:validation_failure, :too_many_args}
33+
end
34+
2735
def validate([limit], _) do
2836
case Integer.parse(limit) do
2937
{_, ""} ->
@@ -47,12 +55,41 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand do
4755
end
4856
end
4957

58+
def validate(["mount", _name, limit], _) do
59+
case Integer.parse(limit) do
60+
{_, ""} ->
61+
:ok
62+
63+
{limit_val, units} ->
64+
case memory_unit_absolute(limit_val, units) do
65+
scaled_limit when is_integer(scaled_limit) -> :ok
66+
_ -> {:validation_failure, :bad_argument}
67+
end
68+
69+
_ ->
70+
{:validation_failure, :bad_argument}
71+
end
72+
end
73+
5074
def validate([_ | rest], _) when length(rest) > 0 do
5175
{:validation_failure, :too_many_args}
5276
end
5377

5478
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
5579

80+
def run(["mount", mount_name, limit], %{node: node_name}) do
81+
limit =
82+
case Integer.parse(limit) do
83+
{limit, ""} -> limit
84+
{limit, units} ->
85+
case memory_unit_absolute(limit, units) do
86+
scaled_limit when is_integer(scaled_limit) ->
87+
scaled_limit
88+
end
89+
end
90+
make_rpc_call(node_name, [mount_name, limit])
91+
end
92+
5693
def run(["mem_relative", _] = args, opts) do
5794
set_disk_free_limit_relative(args, opts)
5895
end
@@ -70,19 +107,24 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetDiskFreeLimitCommand do
70107

71108
use RabbitMQ.CLI.DefaultOutput
72109

110+
def banner(["mount", mount, limit], %{node: node_name}) do
111+
"Setting disk free limit for mount #{mount} on #{node_name} to #{limit} ..."
112+
end
113+
73114
def banner(["mem_relative", arg], %{node: node_name}) do
74115
"Setting disk free limit on #{node_name} to #{arg} times the total RAM ..."
75116
end
76117

77118
def banner([arg], %{node: node_name}),
78119
do: "Setting disk free limit on #{node_name} to #{arg} bytes ..."
79120

80-
def usage, do: "set_disk_free_limit <disk_limit> | mem_relative <fraction>"
121+
def usage, do: "set_disk_free_limit <disk_limit> | mem_relative <fraction> | mount <name> <disk_limit>"
81122

82123
def usage_additional() do
83124
[
84125
["<disk_limit>", "New limit as an absolute value with units, e.g. 1GB"],
85-
["mem_relative <fraction>", "New limit as a fraction of total memory reported by the OS"]
126+
["mem_relative <fraction>", "New limit as a fraction of total memory reported by the OS"],
127+
["mount <name> <disk_limit>", "New limit for the given mount name as an absolute value with units, e.g. 1GB"]
86128
]
87129
end
88130

0 commit comments

Comments
 (0)