Skip to content

Commit 183d0e5

Browse files
committed
Test and clean up metrics dependent on both process and entity
It leaked consumer_created when the queue still existed, now both entity and process must exists to keep the metrics.
1 parent 4807207 commit 183d0e5

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/rabbit_core_metrics_gc.erl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ gc_connections() ->
6464
gc_process(connection_coarse_metrics).
6565

6666
gc_channels() ->
67-
%% TODO channel stats
6867
gc_process(channel_created),
6968
gc_process(channel_metrics),
7069
gc_process(channel_process_metrics),
@@ -107,7 +106,6 @@ gc_process(Pid, Table, Key) ->
107106
true ->
108107
none;
109108
false ->
110-
%% TODO catch?
111109
ets:delete(Table, Key),
112110
none
113111
end.
@@ -128,34 +126,29 @@ gc_entity(Id, Table, Key, GbSet) ->
128126
true ->
129127
none;
130128
false ->
131-
%% TODO catch?
132129
ets:delete(Table, Key),
133130
none
134131
end.
135132

136133
gc_process_and_entity(Table, GbSet) ->
137134
ets:foldl(fun({{Pid, Id} = Key, _, _, _, _, _, _, _}, none)
138135
when Table == channel_queue_metrics ->
139-
gc_entity(Id, Table, Key, GbSet),
140-
gc_process(Pid, Table, Key);
136+
gc_process_and_entity(Id, Pid, Table, Key, GbSet);
141137
({{Pid, Id} = Key, _, _, _, _}, none)
142138
when Table == channel_exchange_metrics ->
143-
gc_entity(Id, Table, Key, GbSet),
144-
gc_process(Pid, Table, Key);
139+
gc_process_and_entity(Id, Pid, Table, Key, GbSet);
145140
({{Id, Pid, _} = Key, _, _, _, _}, none)
146141
when Table == consumer_created ->
147-
gc_entity(Id, Table, Key, GbSet),
148-
gc_process(Pid, Table, Key);
142+
gc_process_and_entity(Id, Pid, Table, Key, GbSet);
149143
({{{Pid, Id}, _} = Key, _, _, _, _}, none) ->
150144
gc_process_and_entity(Id, Pid, Table, Key, GbSet)
151145
end, none, Table).
152146

153147
gc_process_and_entity(Id, Pid, Table, Key, GbSet) ->
154-
case rabbit_misc:is_process_alive(Pid) orelse gb_sets:is_member(Id, GbSet) of
148+
case rabbit_misc:is_process_alive(Pid) andalso gb_sets:is_member(Id, GbSet) of
155149
true ->
156150
none;
157151
false ->
158-
%% TODO catch?
159152
ets:delete(Table, Key),
160153
none
161154
end.

test/rabbit_core_metrics_gc_SUITE.erl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ groups() ->
3434
connection_metrics,
3535
channel_metrics,
3636
node_metrics,
37-
gen_server2_metrics
37+
gen_server2_metrics,
38+
consumer_metrics
3839
]
3940
}
4041
].
@@ -286,6 +287,36 @@ gen_server2_metrics(Config) ->
286287

287288
ok.
288289

290+
consumer_metrics(Config) ->
291+
A = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename),
292+
Ch = rabbit_ct_client_helpers:open_channel(Config, A),
293+
294+
amqp_channel:call(Ch, #'queue.declare'{queue = <<"queue_metrics">>}),
295+
amqp_channel:call(Ch, #'basic.consume'{queue = <<"queue_metrics">>}),
296+
timer:sleep(200),
297+
298+
DeadPid = rabbit_ct_broker_helpers:rpc(Config, A, ?MODULE, dead_pid, []),
299+
300+
QName = q(<<"queue_metrics">>),
301+
CTag = <<"tag">>,
302+
rabbit_ct_broker_helpers:rpc(Config, A, rabbit_core_metrics,
303+
consumer_created, [DeadPid, CTag, true, true,
304+
QName, 1, []]),
305+
Id = {QName, DeadPid, CTag},
306+
[_] = rabbit_ct_broker_helpers:rpc(Config, A, ets, lookup, [consumer_created, Id]),
307+
308+
%% Trigger gc. When the gen_server:call returns, the gc has already finished.
309+
rabbit_ct_broker_helpers:rpc(Config, A, erlang, send, [rabbit_core_metrics_gc, start_gc]),
310+
rabbit_ct_broker_helpers:rpc(Config, A, gen_server, call, [rabbit_core_metrics_gc, test]),
311+
312+
[_|_] = rabbit_ct_broker_helpers:rpc(Config, A, ets, tab2list, [consumer_created]),
313+
[] = rabbit_ct_broker_helpers:rpc(Config, A, ets, lookup, [consumer_created, Id]),
314+
315+
amqp_channel:call(Ch, #'queue.delete'{queue = <<"queue_metrics">>}),
316+
rabbit_ct_client_helpers:close_channel(Ch),
317+
318+
ok.
319+
289320
dead_pid() ->
290321
spawn(fun() -> ok end).
291322

0 commit comments

Comments
 (0)