-
Notifications
You must be signed in to change notification settings - Fork 3.9k
WIP: Add per-queue-type disk limits #14086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
the-mikedavis
wants to merge
1
commit into
main
Choose a base branch
from
md/queue-type-disk-usage-alarms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
%% This Source Code Form is subject to the terms of the Mozilla Public | ||
%% License, v. 2.0. If a copy of the MPL was not distributed with this | ||
%% file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
%% | ||
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
%% | ||
|
||
-module(rabbit_disk_usage). | ||
|
||
%% Functions for calculating disk usage of a given directory. | ||
|
||
-include_lib("kernel/include/file.hrl"). | ||
|
||
-export([scan/1]). | ||
|
||
%% @doc Calculates the disk usage in bytes of the given directory. | ||
%% | ||
%% On especially large directories this can be an expensive operation since | ||
%% each sub-directory is scanned recursively and each file's metadata must be | ||
%% read. | ||
-spec scan(Dir) -> {ok, Size} | {error, Error} when | ||
Dir :: filename:filename_all(), | ||
Size :: non_neg_integer(), | ||
Error :: not_directory | file:posix() | badarg. | ||
scan(Dir) -> | ||
case file:read_file_info(Dir) of | ||
{ok, #file_info{type = directory, size = S}} -> | ||
{ok, Gatherer} = gatherer:start_link(), | ||
scan_directory(Dir, Gatherer), | ||
Size = sum(Gatherer, S), | ||
gatherer:stop(Gatherer), | ||
{ok, Size}; | ||
{ok, #file_info{}} -> | ||
{error, not_directory}; | ||
{error, _} = Err -> | ||
Err | ||
end. | ||
|
||
scan_directory(Dir, Gatherer) -> | ||
gatherer:fork(Gatherer), | ||
worker_pool:submit_async(fun() -> scan_directory0(Dir, Gatherer) end). | ||
|
||
scan_directory0(Dir, Gatherer) -> | ||
link(Gatherer), | ||
Size = case file:list_dir_all(Dir) of | ||
{ok, Entries} -> | ||
lists:foldl( | ||
fun(Entry, Acc) -> | ||
Path = filename:join(Dir, Entry), | ||
case file:read_file_info(Path) of | ||
{ok, #file_info{type = directory, | ||
size = S}} -> | ||
scan_directory(Path, Gatherer), | ||
Acc + S; | ||
{ok, #file_info{size = S}} -> | ||
Acc + S; | ||
_ -> | ||
Acc | ||
end | ||
end, 0, Entries); | ||
_ -> | ||
0 | ||
end, | ||
gatherer:in(Gatherer, Size), | ||
gatherer:finish(Gatherer), | ||
unlink(Gatherer), | ||
ok. | ||
|
||
-spec sum(pid(), non_neg_integer()) -> non_neg_integer(). | ||
sum(Gatherer, Size) -> | ||
case gatherer:out(Gatherer) of | ||
empty -> | ||
Size; | ||
{value, S} -> | ||
sum(Gatherer, Size + S) | ||
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
%% This Source Code Form is subject to the terms of the Mozilla Public | ||
%% License, v. 2.0. If a copy of the MPL was not distributed with this | ||
%% file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
%% | ||
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
%% | ||
|
||
-module(rabbit_queue_type_disk_monitor). | ||
|
||
%% A server for alarming on high disk usage per queue type. | ||
%% | ||
%% The server scans periodically and checks each queue type against its limit | ||
%% using the `disk_footprint/0' and `disk_limit/0' callbacks in | ||
%% `rabbit_queue_type'. Typically this callback uses `rabbit_disk_usage:scan/1'. | ||
%% | ||
%% Also see `rabbit_disk_monitoring' which periodically checks the total space | ||
%% taken on the mounted disk containing `rabbit:data_dir/0'. | ||
|
||
-include_lib("kernel/include/logger.hrl"). | ||
|
||
-behaviour(gen_server). | ||
|
||
-export([start_link/0]). | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | ||
terminate/2, code_change/3]). | ||
|
||
-record(limit, {queue_type :: queue_type(), | ||
type_module :: module(), | ||
limit :: Bytes :: non_neg_integer()}). | ||
|
||
-record(state, {limits :: [#limit{}], | ||
alarmed = alarmed() :: alarmed(), | ||
timer :: timer:tref() | undefined}). | ||
|
||
-type queue_type() :: atom(). | ||
-type alarmed() :: sets:set(queue_type()). | ||
|
||
-type disk_usage_limit_spec() :: %% A total number of bytes | ||
{absolute, non_neg_integer()} | | ||
%% %% A fraction of the disk's capacity. | ||
%% {relative, float()} | | ||
%% A string which will be parsed and | ||
%% interpreted as an absolute limit. | ||
string(). | ||
|
||
%%---------------------------------------------------------------------------- | ||
|
||
start_link() -> | ||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). | ||
|
||
init([]) -> | ||
Limits = lists:foldl( | ||
fun({Type, TypeModule}, Acc) -> | ||
case get_limit(Type, TypeModule) of | ||
{ok, Limit} -> | ||
[#limit{queue_type = Type, | ||
type_module = TypeModule, | ||
limit = Limit} | Acc]; | ||
error -> | ||
Acc | ||
end | ||
end, [], rabbit_registry:lookup_all(queue)), | ||
Timer = erlang:send_after(5_000, self(), scan), | ||
{ok, #state{limits = Limits, timer = Timer}}. | ||
|
||
handle_call(_Request, _From, State) -> | ||
{noreply, State}. | ||
|
||
handle_cast(_Request, State) -> | ||
{noreply, State}. | ||
|
||
handle_info(scan, #state{alarmed = Alarmed0} = State) -> | ||
Alarmed = lists:foldl(fun scan/2, alarmed(), State#state.limits), | ||
ok = handle_alarmed(Alarmed0, Alarmed), | ||
Timer = erlang:send_after(5_000, self(), scan), | ||
{noreply, State#state{alarmed = Alarmed, timer = Timer}}; | ||
handle_info(Info, State) -> | ||
?LOG_DEBUG("~tp unhandled msg: ~tp", [?MODULE, Info]), | ||
{noreply, State}. | ||
|
||
terminate(_Reason, _State) -> | ||
ok. | ||
|
||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. | ||
|
||
%%---------------------------------------------------------------------------- | ||
|
||
alarmed() -> sets:new([{version, 2}]). | ||
|
||
-spec get_limit(atom(), module()) -> {ok, disk_usage_limit_spec()} | error. | ||
get_limit(QType, QTypeModule) -> | ||
try QTypeModule:disk_limit() of | ||
undefined -> | ||
error; | ||
{absolute, Abs} when is_integer(Abs) andalso Abs >= 0 -> | ||
{ok, Abs}; | ||
%% {relative, Rel} when is_float(Rel) andalso Rel >= 0.0 -> | ||
%% TODO: to convert to abs we need to cache the disk capacity for | ||
%% the first `relative' spec we see. | ||
%% Do we even need relative? Should it be proportional to the disk | ||
%% capacity or to the other components? | ||
%% {ok, {relative, Rel}}; | ||
String when is_list(String) -> | ||
case rabbit_resource_monitor_misc:parse_information_unit(String) of | ||
{ok, Bytes} -> | ||
{ok, Bytes}; | ||
{error, parse_error} -> | ||
?LOG_WARNING("Unable to parse disk limit ~tp for queue " | ||
"type '~ts'", [String, QType]), | ||
error | ||
end | ||
catch | ||
error:undef -> | ||
error | ||
end. | ||
|
||
-spec scan(Limit :: #limit{}, alarmed()) -> alarmed(). | ||
scan(#limit{queue_type = QType, | ||
type_module = QTypeModule, | ||
limit = Limit}, Alarmed) -> | ||
%% NOTE: `disk_footprint/0' is an optional callback but it should always | ||
%% be implemented if the queue type implements `disk_limit/0'. | ||
case QTypeModule:disk_footprint() of | ||
{ok, Bytes} -> | ||
%% TODO: remove this printf debugging... | ||
?LOG_INFO("Measured queue type '~ts' at ~p bytes (limit ~p)", [QType, Bytes, Limit]), | ||
case Bytes >= Limit of | ||
true -> sets:add_element(QTypeModule, Alarmed); | ||
false -> Alarmed | ||
end; | ||
{error, enoent} -> | ||
Alarmed; | ||
{error, Error} -> | ||
?LOG_WARNING("Failed to calculate disk usage of queue type '~ts': " | ||
"~tp", [QType, Error]), | ||
Alarmed | ||
end. | ||
|
||
-spec handle_alarmed(Before :: alarmed(), After :: alarmed()) -> ok. | ||
handle_alarmed(NoChange, NoChange) -> | ||
ok; | ||
handle_alarmed(Before, After) -> | ||
Added = sets:subtract(After, Before), | ||
?LOG_WARNING("Newly alarmed: ~p", [Added]), | ||
ok = sets:fold( | ||
fun(QType, ok) -> | ||
rabbit_alarm:set_alarm({alarm(QType), []}) | ||
end, ok, Added), | ||
Removed = sets:subtract(Before, After), | ||
?LOG_WARNING("Stopped alarming: ~p", [Removed]), | ||
ok = sets:fold( | ||
fun(QType, ok) -> | ||
rabbit_alarm:clear_alarm(alarm(QType)) | ||
end, ok, Removed), | ||
ok. | ||
|
||
alarm(QType) -> | ||
{resource_limit, {queue_type_disk, QType}, node()}. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better for queue types to increment/decrement an internal counter (that may be stored in persistent_term).