Skip to content

Commit 78b49ae

Browse files
Backport clustering_utils from main, bazel run gazelle
1 parent 714d67f commit 78b49ae

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

deps/rabbit/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ eunit(
12291229
":test_rabbit_ha_test_producer_beam",
12301230
":test_test_util_beam",
12311231
":test_test_rabbit_event_handler_beam",
1232+
":test_clustering_utils_beam",
12321233
],
12331234
target = ":test_erlang_app",
12341235
)

deps/rabbit/app.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,3 +1961,11 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
19611961
erlc_opts = "//:test_erlc_opts",
19621962
deps = ["//deps/amqp_client:erlang_app", "//deps/rabbitmq_ct_helpers:erlang_app"],
19631963
)
1964+
erlang_bytecode(
1965+
name = "test_clustering_utils_beam",
1966+
testonly = True,
1967+
srcs = ["test/clustering_utils.erl"],
1968+
outs = ["test/clustering_utils.beam"],
1969+
app_name = "rabbit",
1970+
erlc_opts = "//:test_erlc_opts",
1971+
)

deps/rabbit/test/clustering_utils.erl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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) 2018-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
%%
7+
-module(clustering_utils).
8+
9+
-export([
10+
assert_cluster_status/2,
11+
assert_clustered/1,
12+
assert_not_clustered/1
13+
]).
14+
15+
-define(LOOP_RECURSION_DELAY, 100).
16+
17+
assert_cluster_status({All, Disc, Running}, Nodes) ->
18+
assert_cluster_status({All, Running, All, Disc, Running}, Nodes);
19+
assert_cluster_status(Status0, Nodes) ->
20+
Status = sort_cluster_status(Status0),
21+
AllNodes = case Status of
22+
{undef, undef, All, _, _} ->
23+
%% Support mixed-version clusters
24+
All;
25+
{All, _, _, _, _} ->
26+
All
27+
end,
28+
wait_for_cluster_status(Status, AllNodes, Nodes).
29+
30+
wait_for_cluster_status(Status, AllNodes, Nodes) ->
31+
Max = 10000 / ?LOOP_RECURSION_DELAY,
32+
wait_for_cluster_status(0, Max, Status, AllNodes, Nodes).
33+
34+
wait_for_cluster_status(N, Max, Status, _AllNodes, Nodes) when N >= Max ->
35+
erlang:error({cluster_status_max_tries_failed,
36+
[{nodes, Nodes},
37+
{expected_status, Status},
38+
{max_tried, Max},
39+
{status, sort_cluster_status(cluster_status(hd(Nodes)))}]});
40+
wait_for_cluster_status(N, Max, Status, AllNodes, Nodes) ->
41+
case lists:all(fun (Node) ->
42+
verify_status_equal(Node, Status, AllNodes)
43+
end, Nodes) of
44+
true -> ok;
45+
false -> timer:sleep(?LOOP_RECURSION_DELAY),
46+
wait_for_cluster_status(N + 1, Max, Status, AllNodes, Nodes)
47+
end.
48+
49+
verify_status_equal(Node, Status, AllNodes) ->
50+
NodeStatus = sort_cluster_status(cluster_status(Node)),
51+
IsClustered = case rpc:call(Node, rabbit_db_cluster, is_clustered, []) of
52+
{badrpc, {'EXIT', {undef, _}}} ->
53+
rpc:call(Node, rabbit_mnesia, is_clustered, []);
54+
Ret ->
55+
Ret
56+
end,
57+
(AllNodes =/= [Node]) =:= IsClustered andalso equal(Status, NodeStatus).
58+
59+
equal({_, _, A, B, C}, {undef, undef, A, B, C}) ->
60+
true;
61+
equal({_, _, _, _, _}, {undef, undef, _, _, _}) ->
62+
false;
63+
equal(Status0, Status1) ->
64+
Status0 == Status1.
65+
66+
cluster_status(Node) ->
67+
AllMembers = rpc:call(Node, rabbit_nodes, list_members, []),
68+
RunningMembers = rpc:call(Node, rabbit_nodes, list_running, []),
69+
70+
AllDbNodes = case rpc:call(Node, rabbit_db_cluster, members, []) of
71+
{badrpc, {'EXIT', {undef, _}}} ->
72+
rpc:call(Node, rabbit_mnesia, cluster_nodes, [all]);
73+
Ret ->
74+
Ret
75+
end,
76+
DiscDbNodes = rpc:call(Node, rabbit_mnesia, cluster_nodes, [disc]),
77+
RunningDbNodes = rpc:call(Node, rabbit_mnesia, cluster_nodes, [running]),
78+
79+
{AllMembers,
80+
RunningMembers,
81+
AllDbNodes,
82+
DiscDbNodes,
83+
RunningDbNodes}.
84+
85+
sort_cluster_status({{badrpc, {'EXIT', {undef, _}}}, {badrpc, {'EXIT', {undef, _}}}, AllM, DiscM, RunningM}) ->
86+
{undef, undef, lists:sort(AllM), lists:sort(DiscM), lists:sort(RunningM)};
87+
sort_cluster_status({All, Running, AllM, DiscM, RunningM}) ->
88+
{lists:sort(All), lists:sort(Running), lists:sort(AllM), lists:sort(DiscM), lists:sort(RunningM)}.
89+
90+
assert_clustered(Nodes) ->
91+
assert_cluster_status({Nodes, Nodes, Nodes, Nodes, Nodes}, Nodes).
92+
93+
assert_not_clustered(Node) ->
94+
assert_cluster_status({[Node], [Node], [Node], [Node], [Node]}, [Node]).
95+

0 commit comments

Comments
 (0)