Skip to content

Commit 8bfe02b

Browse files
Merge pull request #12071 from rabbitmq/mergify/bp/v4.0.x/pr-12070
Expose quorum queue status at GET /api/queues/quorum/{vhost}/{name}/status by @SimonUnge (backport #12070)
2 parents fc12ac8 + 97f8062 commit 8bfe02b

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

deps/rabbitmq_ct_helpers/src/rabbit_mgmt_test_util.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,13 @@ assert_code(CodeExp, CodeAct, Type, Path, Body) ->
250250
end.
251251

252252
decode(?OK, _Headers, ResBody) ->
253-
JSON = rabbit_data_coercion:to_binary(ResBody),
254-
atomize_map_keys(rabbit_json:decode(JSON));
253+
decode_body(ResBody);
255254
decode(_, Headers, _ResBody) -> Headers.
256255

256+
decode_body(ResBody) ->
257+
JSON = rabbit_data_coercion:to_binary(ResBody),
258+
atomize_map_keys(rabbit_json:decode(JSON)).
259+
257260
atomize_map_keys(L) when is_list(L) ->
258261
[atomize_map_keys(I) || I <- L];
259262
atomize_map_keys(M) when is_map(M) ->

deps/rabbitmq_management/app.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def all_beam_files(name = "all_beam_files"):
9898
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
9999
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
100100
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
101+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
101102
"src/rabbit_mgmt_wm_rebalance_queues.erl",
102103
"src/rabbit_mgmt_wm_redirect.erl",
103104
"src/rabbit_mgmt_wm_reset.erl",
@@ -230,6 +231,7 @@ def all_test_beam_files(name = "all_test_beam_files"):
230231
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
231232
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
232233
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
234+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
233235
"src/rabbit_mgmt_wm_rebalance_queues.erl",
234236
"src/rabbit_mgmt_wm_redirect.erl",
235237
"src/rabbit_mgmt_wm_reset.erl",
@@ -453,6 +455,7 @@ def all_srcs(name = "all_srcs"):
453455
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
454456
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
455457
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
458+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
456459
"src/rabbit_mgmt_wm_rebalance_queues.erl",
457460
"src/rabbit_mgmt_wm_redirect.erl",
458461
"src/rabbit_mgmt_wm_reset.erl",

deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ dispatcher() ->
154154
{"/queues/quorum/:vhost/:queue/replicas/delete", rabbit_mgmt_wm_quorum_queue_replicas_delete_member, []},
155155
{"/queues/quorum/replicas/on/:node/grow", rabbit_mgmt_wm_quorum_queue_replicas_grow, []},
156156
{"/queues/quorum/replicas/on/:node/shrink", rabbit_mgmt_wm_quorum_queue_replicas_shrink, []},
157+
{"/queues/quorum/:vhost/:queue/status", rabbit_mgmt_wm_quorum_queue_status, []},
157158
{"/bindings", rabbit_mgmt_wm_bindings, [all]},
158159
{"/bindings/:vhost", rabbit_mgmt_wm_bindings, [all]},
159160
{"/bindings/:vhost/e/:source/:dtype/:destination", rabbit_mgmt_wm_bindings, [source_destination]},
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
%% An HTTP API counterpart of 'rabbitmq-diagnostics check_if_node_is_quorum_critical'
9+
-module(rabbit_mgmt_wm_quorum_queue_status).
10+
11+
-export([init/2, to_json/2, content_types_provided/2, is_authorized/2, allowed_methods/2]).
12+
-export([resource_exists/2]).
13+
-export([variances/2]).
14+
15+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
16+
17+
%%--------------------------------------------------------------------
18+
19+
init(Req, _State) ->
20+
{cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}.
21+
22+
variances(Req, Context) ->
23+
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
24+
25+
allowed_methods(ReqData, Context) ->
26+
{[<<"GET">>, <<"OPTIONS">>], ReqData, Context}.
27+
28+
content_types_provided(ReqData, Context) ->
29+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
30+
31+
resource_exists(ReqData, Context) ->
32+
{case queue(ReqData) of
33+
not_found -> false;
34+
_ -> true
35+
end, ReqData, Context}.
36+
37+
to_json(ReqData, Context) ->
38+
case queue(ReqData) of
39+
{error, Reason} ->
40+
failure(Reason, ReqData, Context);
41+
Res ->
42+
rabbit_mgmt_util:reply(Res, ReqData, Context)
43+
end.
44+
45+
queue(ReqData) ->
46+
case rabbit_mgmt_util:vhost(ReqData) of
47+
not_found -> not_found;
48+
VHost -> queue(VHost, rabbit_mgmt_util:id(queue, ReqData))
49+
end.
50+
51+
queue(VHost, QName) ->
52+
Name = rabbit_misc:r(VHost, queue, QName),
53+
case rabbit_amqqueue:lookup(Name) of
54+
{ok, _Q} -> rabbit_quorum_queue:status(VHost, QName);
55+
{error, not_found} -> not_found
56+
end.
57+
58+
59+
failure(Reason, ReqData, Context) ->
60+
{Response, ReqData1, Context1} = rabbit_mgmt_util:reply([{status, failed},
61+
{reason, Reason}],
62+
ReqData, Context),
63+
{stop, cowboy_req:reply(503, #{}, Response, ReqData1), Context1}.
64+
65+
is_authorized(ReqData, Context) ->
66+
rabbit_mgmt_util:is_authorized(ReqData, Context).

deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
eventually/1]).
2222
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2,
2323
assert_keys/2, assert_no_keys/2,
24+
decode_body/1,
2425
http_get/2, http_get/3, http_get/5,
2526
http_get_no_auth/3,
2627
http_get_no_decode/5,
@@ -198,6 +199,7 @@ all_tests() -> [
198199
user_limit_set_test,
199200
config_environment_test,
200201
disabled_qq_replica_opers_test,
202+
qq_status_test,
201203
list_deprecated_features_test,
202204
list_used_deprecated_features_test
203205
].
@@ -3866,6 +3868,28 @@ disabled_qq_replica_opers_test(Config) ->
38663868
http_delete(Config, "/queues/quorum/replicas/on/" ++ Nodename ++ "/shrink", ?METHOD_NOT_ALLOWED),
38673869
passed.
38683870

3871+
qq_status_test(Config) ->
3872+
QQArgs = [{durable, true}, {arguments, [{'x-queue-type', 'quorum'}]}],
3873+
http_get(Config, "/queues/%2f/qq_status", ?NOT_FOUND),
3874+
http_put(Config, "/queues/%2f/qq_status", QQArgs, {group, '2xx'}),
3875+
[MapRes] = http_get(Config, "/queues/quorum/%2f/qq_status/status", ?OK),
3876+
Keys = ['Commit Index','Last Applied','Last Log Index',
3877+
'Last Written','Machine Version','Membership','Node Name',
3878+
'Raft State','Snapshot Index','Term'],
3879+
?assertEqual(lists:sort(Keys), lists:sort(maps:keys(MapRes))),
3880+
http_delete(Config, "/queues/%2f/qq_status", {group, '2xx'}),
3881+
3882+
3883+
CQArgs = [{durable, true}],
3884+
http_get(Config, "/queues/%2F/cq_status", ?NOT_FOUND),
3885+
http_put(Config, "/queues/%2F/cq_status", CQArgs, {group, '2xx'}),
3886+
ResBody = http_get_no_decode(Config, "/queues/quorum/%2f/cq_status/status", "guest", "guest", 503),
3887+
?assertEqual(#{reason => <<"classic_queue_not_supported">>,
3888+
status => <<"failed">>}, decode_body(ResBody)),
3889+
http_delete(Config, "/queues/%2f/cq_status", {group, '2xx'}),
3890+
passed.
3891+
3892+
38693893
list_deprecated_features_test(Config) ->
38703894
Desc = "This is a deprecated feature",
38713895
DocUrl = "https://rabbitmq.com/",

moduleindex.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ rabbitmq_management:
988988
- rabbit_mgmt_wm_quorum_queue_replicas_delete_member
989989
- rabbit_mgmt_wm_quorum_queue_replicas_grow
990990
- rabbit_mgmt_wm_quorum_queue_replicas_shrink
991+
- rabbit_mgmt_wm_quorum_queue_status
991992
- rabbit_mgmt_wm_rebalance_queues
992993
- rabbit_mgmt_wm_redirect
993994
- rabbit_mgmt_wm_reset

0 commit comments

Comments
 (0)