Skip to content

Commit 1b30283

Browse files
Merge pull request #12072 from rabbitmq/mergify/bp/v3.13.x/pr-12071
Expose quorum queue status at GET /api/queues/quorum/{vhost}/{name}/status by @SimonUnge (backport #12070) (backport #12071)
2 parents 861dc88 + 42b3a6f commit 1b30283

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
@@ -249,10 +249,13 @@ assert_code(CodeExp, CodeAct, Type, Path, Body) ->
249249
end.
250250

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

255+
decode_body(ResBody) ->
256+
JSON = rabbit_data_coercion:to_binary(ResBody),
257+
atomize_map_keys(rabbit_json:decode(JSON)).
258+
256259
atomize_map_keys(L) when is_list(L) ->
257260
[atomize_map_keys(I) || I <- L];
258261
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
@@ -99,6 +99,7 @@ def all_beam_files(name = "all_beam_files"):
9999
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
100100
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
101101
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
102+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
102103
"src/rabbit_mgmt_wm_rebalance_queues.erl",
103104
"src/rabbit_mgmt_wm_redirect.erl",
104105
"src/rabbit_mgmt_wm_reset.erl",
@@ -232,6 +233,7 @@ def all_test_beam_files(name = "all_test_beam_files"):
232233
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
233234
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
234235
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
236+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
235237
"src/rabbit_mgmt_wm_rebalance_queues.erl",
236238
"src/rabbit_mgmt_wm_redirect.erl",
237239
"src/rabbit_mgmt_wm_reset.erl",
@@ -455,6 +457,7 @@ def all_srcs(name = "all_srcs"):
455457
"src/rabbit_mgmt_wm_quorum_queue_replicas_delete_member.erl",
456458
"src/rabbit_mgmt_wm_quorum_queue_replicas_grow.erl",
457459
"src/rabbit_mgmt_wm_quorum_queue_replicas_shrink.erl",
460+
"src/rabbit_mgmt_wm_quorum_queue_status.erl",
458461
"src/rabbit_mgmt_wm_rebalance_queues.erl",
459462
"src/rabbit_mgmt_wm_redirect.erl",
460463
"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
@@ -145,6 +145,7 @@ dispatcher() ->
145145
{"/queues/quorum/:vhost/:queue/replicas/delete", rabbit_mgmt_wm_quorum_queue_replicas_delete_member, []},
146146
{"/queues/quorum/replicas/on/:node/grow", rabbit_mgmt_wm_quorum_queue_replicas_grow, []},
147147
{"/queues/quorum/replicas/on/:node/shrink", rabbit_mgmt_wm_quorum_queue_replicas_shrink, []},
148+
{"/queues/quorum/:vhost/:queue/status", rabbit_mgmt_wm_quorum_queue_status, []},
148149
{"/bindings", rabbit_mgmt_wm_bindings, [all]},
149150
{"/bindings/:vhost", rabbit_mgmt_wm_bindings, [all]},
150151
{"/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
@@ -18,6 +18,7 @@
1818
open_unmanaged_connection/3]).
1919
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2,
2020
assert_keys/2, assert_no_keys/2,
21+
decode_body/1,
2122
http_get/2, http_get/3, http_get/5,
2223
http_get_no_auth/3,
2324
http_get_no_decode/5,
@@ -196,6 +197,7 @@ all_tests() -> [
196197
user_limit_set_test,
197198
config_environment_test,
198199
disabled_qq_replica_opers_test,
200+
qq_status_test,
199201
list_deprecated_features_test,
200202
list_used_deprecated_features_test
201203
].
@@ -3730,6 +3732,28 @@ disabled_qq_replica_opers_test(Config) ->
37303732
http_delete(Config, "/queues/quorum/replicas/on/" ++ Nodename ++ "/shrink", ?METHOD_NOT_ALLOWED),
37313733
passed.
37323734

3735+
qq_status_test(Config) ->
3736+
QQArgs = [{durable, true}, {arguments, [{'x-queue-type', 'quorum'}]}],
3737+
http_get(Config, "/queues/%2f/qq_status", ?NOT_FOUND),
3738+
http_put(Config, "/queues/%2f/qq_status", QQArgs, {group, '2xx'}),
3739+
[MapRes] = http_get(Config, "/queues/quorum/%2f/qq_status/status", ?OK),
3740+
Keys = ['Commit Index','Last Applied','Last Log Index',
3741+
'Last Written','Machine Version','Membership','Node Name',
3742+
'Raft State','Snapshot Index','Term'],
3743+
?assertEqual(lists:sort(Keys), lists:sort(maps:keys(MapRes))),
3744+
http_delete(Config, "/queues/%2f/qq_status", {group, '2xx'}),
3745+
3746+
3747+
CQArgs = [{durable, true}],
3748+
http_get(Config, "/queues/%2F/cq_status", ?NOT_FOUND),
3749+
http_put(Config, "/queues/%2F/cq_status", CQArgs, {group, '2xx'}),
3750+
ResBody = http_get_no_decode(Config, "/queues/quorum/%2f/cq_status/status", "guest", "guest", 503),
3751+
?assertEqual(#{reason => <<"classic_queue_not_supported">>,
3752+
status => <<"failed">>}, decode_body(ResBody)),
3753+
http_delete(Config, "/queues/%2f/cq_status", {group, '2xx'}),
3754+
passed.
3755+
3756+
37333757
list_deprecated_features_test(Config) ->
37343758
Desc = "This is a deprecated feature",
37353759
DocUrl = "https://rabbitmq.com/",

moduleindex.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ rabbitmq_management:
10151015
- rabbit_mgmt_wm_quorum_queue_replicas_delete_member
10161016
- rabbit_mgmt_wm_quorum_queue_replicas_grow
10171017
- rabbit_mgmt_wm_quorum_queue_replicas_shrink
1018+
- rabbit_mgmt_wm_quorum_queue_status
10181019
- rabbit_mgmt_wm_rebalance_queues
10191020
- rabbit_mgmt_wm_redirect
10201021
- rabbit_mgmt_wm_reset

0 commit comments

Comments
 (0)