|
| 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-2023 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(clustering_recovery_SUITE). |
| 9 | + |
| 10 | +-include_lib("common_test/include/ct.hrl"). |
| 11 | +-include_lib("amqp_client/include/amqp_client.hrl"). |
| 12 | +-include_lib("eunit/include/eunit.hrl"). |
| 13 | + |
| 14 | +-compile(export_all). |
| 15 | + |
| 16 | +all() -> |
| 17 | + [ |
| 18 | + {group, mnesia_store} |
| 19 | + ]. |
| 20 | + |
| 21 | +groups() -> |
| 22 | + [{mnesia_store, [], [ |
| 23 | + {clustered_3_nodes, [], |
| 24 | + [{cluster_size_3, [], [ |
| 25 | + force_shrink_quorum_queue, |
| 26 | + force_shrink_all_quorum_queues |
| 27 | + ]} |
| 28 | + ]} |
| 29 | + ]} |
| 30 | + ]. |
| 31 | + |
| 32 | +suite() -> |
| 33 | + [ |
| 34 | + %% If a testcase hangs, no need to wait for 30 minutes. |
| 35 | + {timetrap, {minutes, 5}} |
| 36 | + ]. |
| 37 | + |
| 38 | +%% ------------------------------------------------------------------- |
| 39 | +%% Testsuite setup/teardown. |
| 40 | +%% ------------------------------------------------------------------- |
| 41 | + |
| 42 | +init_per_suite(Config) -> |
| 43 | + rabbit_ct_helpers:log_environment(), |
| 44 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 45 | + |
| 46 | +end_per_suite(Config) -> |
| 47 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 48 | + |
| 49 | +init_per_group(mnesia_store, Config) -> |
| 50 | + Config; |
| 51 | +init_per_group(clustered_3_nodes, Config) -> |
| 52 | + rabbit_ct_helpers:set_config(Config, [{rmq_nodes_clustered, true}]); |
| 53 | +init_per_group(cluster_size_3, Config) -> |
| 54 | + rabbit_ct_helpers:set_config(Config, [{rmq_nodes_count, 3}]). |
| 55 | + |
| 56 | +end_per_group(_, Config) -> |
| 57 | + Config. |
| 58 | + |
| 59 | +init_per_testcase(Testcase, Config) -> |
| 60 | + rabbit_ct_helpers:testcase_started(Config, Testcase), |
| 61 | + ClusterSize = ?config(rmq_nodes_count, Config), |
| 62 | + TestNumber = rabbit_ct_helpers:testcase_number(Config, ?MODULE, Testcase), |
| 63 | + Config1 = rabbit_ct_helpers:set_config(Config, [ |
| 64 | + {rmq_nodename_suffix, Testcase}, |
| 65 | + {tcp_ports_base, {skip_n_nodes, TestNumber * ClusterSize}}, |
| 66 | + {keep_pid_file_on_exit, true} |
| 67 | + ]), |
| 68 | + rabbit_ct_helpers:run_steps(Config1, |
| 69 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 70 | + rabbit_ct_client_helpers:setup_steps()). |
| 71 | + |
| 72 | +end_per_testcase(Testcase, Config) -> |
| 73 | + Config1 = rabbit_ct_helpers:run_steps(Config, |
| 74 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 75 | + rabbit_ct_broker_helpers:teardown_steps()), |
| 76 | + rabbit_ct_helpers:testcase_finished(Config1, Testcase). |
| 77 | + |
| 78 | +%% ------------------------------------------------------------------- |
| 79 | +%% Testcases |
| 80 | +%% ------------------------------------------------------------------- |
| 81 | + |
| 82 | +force_shrink_all_quorum_queues(Config) -> |
| 83 | + [Rabbit, Hare, Bunny] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), |
| 84 | + |
| 85 | + QName1 = quorum_test_queue(1), |
| 86 | + QName2 = quorum_test_queue(2), |
| 87 | + QName3 = quorum_test_queue(3), |
| 88 | + Args = [{<<"x-queue-type">>, longstr, <<"quorum">>}], |
| 89 | + declare_and_publish_to_queue(Config, Rabbit, QName1, Args), |
| 90 | + declare_and_publish_to_queue(Config, Rabbit, QName2, Args), |
| 91 | + declare_and_publish_to_queue(Config, Rabbit, QName3, Args), |
| 92 | + |
| 93 | + ok = rabbit_ct_broker_helpers:stop_node(Config, Hare), |
| 94 | + ok = rabbit_ct_broker_helpers:stop_node(Config, Bunny), |
| 95 | + |
| 96 | + Ch = rabbit_ct_client_helpers:open_channel(Config, Rabbit), |
| 97 | + ?assertExit( |
| 98 | + {{shutdown, {connection_closing, {server_initiated_close, 541, _}}}, _}, |
| 99 | + amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName1, |
| 100 | + consumer_tag = <<"ctag">>}, |
| 101 | + self())), |
| 102 | + |
| 103 | + ok = rabbit_ct_broker_helpers:rpc(Config, Rabbit, rabbit_quorum_queue, force_all_queues_shrink_member_to_current_member, []), |
| 104 | + |
| 105 | + ok = consume_from_queue(Config, Rabbit, QName1), |
| 106 | + ok = consume_from_queue(Config, Rabbit, QName2), |
| 107 | + ok = consume_from_queue(Config, Rabbit, QName3), |
| 108 | + |
| 109 | + ok. |
| 110 | + |
| 111 | +force_shrink_quorum_queue(Config) -> |
| 112 | + [Rabbit, Hare, Bunny] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename), |
| 113 | + |
| 114 | + QName1 = quorum_test_queue(1), |
| 115 | + Args = [{<<"x-queue-type">>, longstr, <<"quorum">>}], |
| 116 | + declare_and_publish_to_queue(Config, Rabbit, QName1, Args), |
| 117 | + |
| 118 | + ok = rabbit_ct_broker_helpers:stop_node(Config, Hare), |
| 119 | + ok = rabbit_ct_broker_helpers:stop_node(Config, Bunny), |
| 120 | + |
| 121 | + Ch = rabbit_ct_client_helpers:open_channel(Config, Rabbit), |
| 122 | + ?assertExit( |
| 123 | + {{shutdown, {connection_closing, {server_initiated_close, 541, _}}}, _}, |
| 124 | + amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName1, |
| 125 | + consumer_tag = <<"ctag">>}, |
| 126 | + self())), |
| 127 | + |
| 128 | + ok = rabbit_ct_broker_helpers:rpc(Config, Rabbit, rabbit_quorum_queue, force_shrink_member_to_current_member, [<<"/">>, QName1]), |
| 129 | + |
| 130 | + ok = consume_from_queue(Config, Rabbit, QName1), |
| 131 | + |
| 132 | + ok. |
| 133 | + |
| 134 | +%% ------------------------------------------------------------------- |
| 135 | +%% Internal utils |
| 136 | +%% ------------------------------------------------------------------- |
| 137 | +declare_and_publish_to_queue(Config, Node, QName, Args) -> |
| 138 | + {Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, Node), |
| 139 | + declare(Ch, QName, Args), |
| 140 | + publish_many(Ch, QName, 10), |
| 141 | + rabbit_ct_client_helpers:close_connection_and_channel(Conn, Ch). |
| 142 | + |
| 143 | +quorum_test_queue(Number) -> |
| 144 | + list_to_binary(io_lib:format("quorum_queue_~p", [Number])). |
| 145 | + |
| 146 | +declare(Ch, Name, Args) -> |
| 147 | + Res = amqp_channel:call(Ch, #'queue.declare'{durable = true, |
| 148 | + queue = Name, |
| 149 | + arguments = Args}), |
| 150 | + amqp_channel:call(Ch, #'queue.bind'{queue = Name, |
| 151 | + exchange = <<"amq.fanout">>}), |
| 152 | + Res. |
| 153 | + |
| 154 | +consume_from_queue(Config, Node, QName) -> |
| 155 | + {Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config, Node), |
| 156 | + subscribe(Ch, QName), |
| 157 | + consume(10), |
| 158 | + rabbit_ct_client_helpers:close_connection_and_channel(Conn, Ch). |
| 159 | + |
| 160 | +publish_many(Ch, QName, N) -> |
| 161 | + amqp_channel:call(Ch, #'confirm.select'{}), |
| 162 | + [amqp_channel:cast(Ch, #'basic.publish'{routing_key = QName}, |
| 163 | + #amqp_msg{props = #'P_basic'{delivery_mode = 2}}) |
| 164 | + || _ <- lists:seq(1, N)], |
| 165 | + amqp_channel:wait_for_confirms(Ch). |
| 166 | + |
| 167 | +subscribe(Ch, QName) -> |
| 168 | + CTag = <<"ctag">>, |
| 169 | + amqp_channel:subscribe(Ch, #'basic.consume'{queue = QName, |
| 170 | + consumer_tag = CTag}, |
| 171 | + self()), |
| 172 | + receive |
| 173 | + #'basic.consume_ok'{consumer_tag = CTag} -> |
| 174 | + ok |
| 175 | + after 10000 -> |
| 176 | + exit(consume_ok_timeout) |
| 177 | + end. |
| 178 | + |
| 179 | +consume(0) -> |
| 180 | + ok; |
| 181 | +consume(N) -> |
| 182 | + receive |
| 183 | + {#'basic.deliver'{consumer_tag = <<"ctag">>}, _} -> |
| 184 | + consume(N - 1) |
| 185 | + after 10000 -> |
| 186 | + exit(deliver_timeout) |
| 187 | + end. |
0 commit comments