Skip to content

Commit 1e771b0

Browse files
committed
Queue, connection and exchange count functions
Connection lookup using tracking tables. Used in the management only HTTP API with all stats disabled [#166445368]
1 parent f613fd0 commit 1e771b0

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/rabbit_amqqueue.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
-export([list/0, list/1, info_keys/0, info/1, info/2, info_all/1, info_all/2,
3131
emit_info_all/5, list_local/1, info_local/1,
3232
emit_info_local/4, emit_info_down/4]).
33+
-export([count/0]).
3334
-export([list_down/1, count/1, list_names/0, list_names/1, list_local_names/0,
3435
list_with_possible_retry/1]).
3536
-export([list_by_type/1]).
@@ -794,6 +795,11 @@ list() ->
794795
do_list() ->
795796
mnesia:dirty_match_object(rabbit_queue, amqqueue:pattern_match_all()).
796797

798+
-spec count() -> non_neg_integer().
799+
800+
count() ->
801+
mnesia:table_info(rabbit_queue, size).
802+
797803
-spec list_names() -> [rabbit_amqqueue:name()].
798804

799805
list_names() -> mnesia:dirty_all_keys(rabbit_queue).

src/rabbit_connection_tracking.erl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
list/0, list/1, list_on_node/1, list_on_node/2, list_of_user/1,
4040
tracked_connection_from_connection_created/1,
4141
tracked_connection_from_connection_state/1,
42-
count_connections_in/1]).
42+
count_connections_in/1,
43+
lookup/1,
44+
count/0]).
4345

4446
-include_lib("rabbit.hrl").
4547

@@ -287,6 +289,20 @@ unregister_connection(ConnId = {Node, _Name}) when Node =:= node() ->
287289
mnesia:dirty_delete(TableName, ConnId)
288290
end.
289291

292+
-spec lookup(rabbit_types:connection_name()) -> rabbit_types:tracked_connection() | 'not_found'.
293+
294+
lookup(Name) ->
295+
Nodes = rabbit_mnesia:cluster_nodes(running),
296+
lookup(Name, Nodes).
297+
298+
lookup(_, []) ->
299+
not_found;
300+
lookup(Name, [Node | Nodes]) ->
301+
TableName = tracked_connection_table_name_for(Node),
302+
case mnesia:dirty_read(TableName, {Node, Name}) of
303+
[] -> lookup(Name, Nodes);
304+
[Row] -> Row
305+
end.
290306

291307
-spec list() -> [rabbit_types:tracked_connection()].
292308

@@ -297,6 +313,14 @@ list() ->
297313
Acc ++ mnesia:dirty_match_object(Tab, #tracked_connection{_ = '_'})
298314
end, [], rabbit_mnesia:cluster_nodes(running)).
299315

316+
-spec count() -> non_neg_integer().
317+
318+
count() ->
319+
lists:foldl(
320+
fun (Node, Acc) ->
321+
Tab = tracked_connection_table_name_for(Node),
322+
Acc + mnesia:table_info(Tab, size)
323+
end, 0, rabbit_mnesia:cluster_nodes(running)).
300324

301325
-spec list(rabbit_types:vhost()) -> [rabbit_types:tracked_connection()].
302326

src/rabbit_exchange.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
lookup/1, lookup_or_die/1, list/0, list/1, lookup_scratch/2,
2424
update_scratch/3, update_decorators/1, immutable/1,
2525
info_keys/0, info/1, info/2, info_all/1, info_all/2, info_all/4,
26-
route/2, delete/3, validate_binding/2]).
26+
route/2, delete/3, validate_binding/2, count/0]).
2727
-export([list_names/0]).
2828
%% these must be run inside a mnesia tx
2929
-export([maybe_auto_delete/2, serial/1, peek_serial/1, update/2]).
@@ -238,6 +238,11 @@ lookup_or_die(Name) ->
238238

239239
list() -> mnesia:dirty_match_object(rabbit_exchange, #exchange{_ = '_'}).
240240

241+
-spec count() -> non_neg_integer().
242+
243+
count() ->
244+
mnesia:table_info(rabbit_exchange, size).
245+
241246
-spec list_names() -> [rabbit_exchange:name()].
242247

243248
list_names() -> mnesia:dirty_all_keys(rabbit_exchange).

0 commit comments

Comments
 (0)