Skip to content

Commit dfcb4d1

Browse files
Merge pull request #2024 from rabbitmq/management-only-api
Queue, connection and exchange count functions
2 parents d35182b + 50ac865 commit dfcb4d1

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
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]).
@@ -792,6 +793,11 @@ list() ->
792793
do_list() ->
793794
mnesia:dirty_match_object(rabbit_queue, amqqueue:pattern_match_all()).
794795

796+
-spec count() -> non_neg_integer().
797+
798+
count() ->
799+
mnesia:table_info(rabbit_queue, size).
800+
795801
-spec list_names() -> [rabbit_amqqueue:name()].
796802

797803
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).

test/unit_inbroker_non_parallel_SUITE.erl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
-include_lib("eunit/include/eunit.hrl").
2020
-include_lib("common_test/include/ct.hrl").
21+
-include_lib("eunit/include/eunit.hrl").
2122
-include_lib("kernel/include/file.hrl").
2223
-include_lib("amqp_client/include/amqp_client.hrl").
2324

@@ -42,7 +43,11 @@ groups() ->
4243
log_management, %% Check log files.
4344
log_file_initialised_during_startup,
4445
log_file_fails_to_initialise_during_startup,
45-
externally_rotated_logs_are_automatically_reopened %% Check log files.
46+
externally_rotated_logs_are_automatically_reopened, %% Check log files.
47+
exchange_count,
48+
queue_count,
49+
connection_count,
50+
connection_lookup
4651
]}
4752
].
4853

@@ -698,6 +703,44 @@ disk_monitor_enable1() ->
698703
application:set_env(rabbit, disk_monitor_failure_retry_interval, 120000),
699704
passed.
700705

706+
%% ---------------------------------------------------------------------------
707+
%% Count functions for management only API purposes
708+
%% ---------------------------------------------------------------------------
709+
exchange_count(Config) ->
710+
%% Default exchanges == 7
711+
?assertEqual(7, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_exchange, count, [])).
712+
713+
queue_count(Config) ->
714+
Conn = rabbit_ct_client_helpers:open_connection(Config, 0),
715+
{ok, Ch} = amqp_connection:open_channel(Conn),
716+
amqp_channel:call(Ch, #'queue.declare'{ queue = <<"my-queue">> }),
717+
718+
?assertEqual(1, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_amqqueue, count, [])),
719+
720+
amqp_channel:call(Ch, #'queue.delete'{ queue = <<"my-queue">> }),
721+
rabbit_ct_client_helpers:close_channel(Ch),
722+
rabbit_ct_client_helpers:close_connection(Conn),
723+
ok.
724+
725+
connection_count(Config) ->
726+
Conn = rabbit_ct_client_helpers:open_connection(Config, 0),
727+
728+
?assertEqual(1, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_connection_tracking, count, [])),
729+
730+
rabbit_ct_client_helpers:close_connection(Conn),
731+
ok.
732+
733+
connection_lookup(Config) ->
734+
Conn = rabbit_ct_client_helpers:open_connection(Config, 0),
735+
736+
[Connection] = rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_connection_tracking, list, []),
737+
?assertMatch(Connection, rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_connection_tracking,
738+
lookup,
739+
[Connection#tracked_connection.name])),
740+
741+
rabbit_ct_client_helpers:close_connection(Conn),
742+
ok.
743+
701744
%% ---------------------------------------------------------------------------
702745
%% rabbitmqctl helpers.
703746
%% ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)