| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <atomic> |
| 25 | #include <chrono> |
| 26 | #include "gcs_base_test.h" |
| 27 | #include "plugin/group_replication/libmysqlgcs/src/bindings/xcom/gcs_xcom_communication_protocol_changer.h" |
| 28 | |
| 29 | namespace gcs_xcom_communication_protocol_changer_unittest { |
| 30 | |
| 31 | class GcsXcomCommunicationProtocolChangerTest : public GcsBaseTest { |
| 32 | public: |
| 33 | static std::atomic<bool> s_change_started; |
| 34 | static std::future<void> s_change_finished; |
| 35 | |
| 36 | GcsXcomCommunicationProtocolChangerTest() |
| 37 | : m_myself("127.0.0.1:8080"), m_engine(), m_pipeline(), m_changer() {} |
| 38 | |
| 39 | void SetUp() override { |
| 40 | m_engine.initialize(nullptr); |
| 41 | m_pipeline = std::make_unique<Gcs_message_pipeline>(); |
| 42 | ASSERT_FALSE(m_pipeline->register_pipeline({ |
| 43 | {Gcs_protocol_version::V1, {}}, |
| 44 | {Gcs_protocol_version::V2, {}}, |
| 45 | })); |
| 46 | ASSERT_FALSE(m_pipeline->set_version(Gcs_protocol_version::V1)); |
| 47 | m_changer = std::make_unique<Gcs_xcom_communication_protocol_changer>( |
| 48 | m_engine, *m_pipeline); |
| 49 | static_cast<Gcs_xcom_interface *>(Gcs_xcom_interface::get_interface()) |
| 50 | ->set_node_address(m_myself.get_member_address()); |
| 51 | } |
| 52 | |
| 53 | void TearDown() override { m_engine.finalize(nullptr); } |
| 54 | |
| 55 | protected: |
| 56 | Gcs_xcom_node_address m_myself; |
| 57 | Gcs_xcom_engine m_engine; |
| 58 | std::unique_ptr<Gcs_message_pipeline> m_pipeline; |
| 59 | std::unique_ptr<Gcs_xcom_communication_protocol_changer> m_changer; |
| 60 | }; |
| 61 | |
| 62 | std::atomic<bool> GcsXcomCommunicationProtocolChangerTest::s_change_started; |
| 63 | std::future<void> GcsXcomCommunicationProtocolChangerTest::s_change_finished; |
| 64 | |
| 65 | typedef void(xcom_set_protocol_functor)( |
| 66 | Gcs_xcom_communication_protocol_changer *, Gcs_protocol_version); |
| 67 | /** |
| 68 | Notification used to start a protocol change via the GCS engine. |
| 69 | */ |
| 70 | class Set_protocol_notification : public Parameterized_notification<false> { |
| 71 | public: |
| 72 | explicit Set_protocol_notification( |
| 73 | xcom_set_protocol_functor *functor, |
| 74 | Gcs_xcom_communication_protocol_changer *protocol_changer, |
| 75 | Gcs_protocol_version version) |
| 76 | : m_functor(functor), m_changer(protocol_changer), m_version(version) {} |
| 77 | |
| 78 | ~Set_protocol_notification() override = default; |
| 79 | |
| 80 | private: |
| 81 | void do_execute() override { (*m_functor)(m_changer, m_version); } |
| 82 | |
| 83 | xcom_set_protocol_functor *m_functor; |
| 84 | Gcs_xcom_communication_protocol_changer *m_changer; |
| 85 | Gcs_protocol_version m_version; |
| 86 | }; |
| 87 | |
| 88 | void do_function_set_protocol(Gcs_xcom_communication_protocol_changer *changer, |
| 89 | Gcs_protocol_version version) { |
| 90 | bool change_started; |
| 91 | std::future<void> change_finished; |
| 92 | std::tie(change_started, change_finished) = |
| 93 | changer->set_protocol_version(version); |
| 94 | ASSERT_TRUE(change_started); |
| 95 | |
| 96 | GcsXcomCommunicationProtocolChangerTest::s_change_finished = |
| 97 | std::move(change_finished); |
| 98 | GcsXcomCommunicationProtocolChangerTest::s_change_started = true; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | Validate that the sender successfully finishes the ongoing protocol change if |
| 103 | the sender triggers the finish condition. |
| 104 | */ |
| 105 | TEST_F(GcsXcomCommunicationProtocolChangerTest, |
| 106 | SenderThreadFinishesProtocolChange) { |
| 107 | s_change_started = false; |
| 108 | |
| 109 | // Increment the number of packets in transit to one. |
| 110 | m_changer->atomically_increment_nr_packets_in_transit( |
| 111 | Cargo_type::CT_USER_DATA); |
| 112 | |
| 113 | // Start a protocol change, will wait for the in transit packet. |
| 114 | Gcs_xcom_notification *notification = new Set_protocol_notification( |
| 115 | do_function_set_protocol, m_changer.get(), Gcs_protocol_version::V2); |
| 116 | ASSERT_NE(notification, nullptr); |
| 117 | ASSERT_TRUE(m_engine.push(notification)); |
| 118 | |
| 119 | // Wait until the change has actually started. |
| 120 | while (!s_change_started) |
| 121 | ; |
| 122 | |
| 123 | // Confirm it is indeed waiting for the in transit packet. |
| 124 | using namespace std::chrono_literals; |
| 125 | auto status = s_change_finished.wait_for(0ns); |
| 126 | ASSERT_EQ(status, std::future_status::timeout); |
| 127 | |
| 128 | // Trick to set the number of packets in transit back to zero. |
| 129 | m_changer->adjust_nr_packets_in_transit(Cargo_type::CT_USER_DATA, -1); |
| 130 | |
| 131 | /* |
| 132 | This call is concurrent with the protocol change. |
| 133 | It will increment the number of in transit packets to one, and then rollback |
| 134 | to zero, triggering the condition to finish the protocol change. |
| 135 | */ |
| 136 | m_changer->atomically_increment_nr_packets_in_transit( |
| 137 | Cargo_type::CT_USER_DATA); |
| 138 | |
| 139 | // Wait for the change to complete, and confirm it. |
| 140 | s_change_finished.get(); |
| 141 | ASSERT_EQ(m_pipeline->get_version(), Gcs_protocol_version::V2); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | Validate that the sender successfully finishes the ongoing protocol change if |
| 146 | the sender triggers the finish condition. |
| 147 | */ |
| 148 | TEST_F(GcsXcomCommunicationProtocolChangerTest, |
| 149 | ReceiverThreadFinishesProtocolChange) { |
| 150 | s_change_started = false; |
| 151 | |
| 152 | // Increment the number of packets in transit to one. |
| 153 | m_changer->atomically_increment_nr_packets_in_transit( |
| 154 | Cargo_type::CT_USER_DATA); |
| 155 | |
| 156 | // Start a protocol change, will wait for the in transit packet. |
| 157 | std::tie(s_change_started, s_change_finished) = |
| 158 | m_changer->set_protocol_version(Gcs_protocol_version::V2); |
| 159 | ASSERT_TRUE(s_change_started); |
| 160 | |
| 161 | // Confirm it is indeed waiting for the in transit packet. |
| 162 | using namespace std::chrono_literals; |
| 163 | auto status = s_change_finished.wait_for(0ns); |
| 164 | ASSERT_EQ(status, std::future_status::timeout); |
| 165 | |
| 166 | // Receive the required in transit packet. |
| 167 | bool packet_ok; |
| 168 | Gcs_packet packet; |
| 169 | std::tie(packet_ok, packet) = Gcs_packet::make_outgoing_packet( |
| 170 | Cargo_type::CT_USER_DATA, Gcs_protocol_version::V1, {}, {}, 1); |
| 171 | ASSERT_TRUE(packet_ok); |
| 172 | Gcs_xcom_nodes nodes; |
| 173 | nodes.add_node(Gcs_xcom_node_information(m_myself.get_member_address(), |
| 174 | Gcs_xcom_uuid(), 0, true)); |
| 175 | m_changer->decrement_nr_packets_in_transit(packet, nodes); |
| 176 | |
| 177 | // Wait for the change to complete, and confirm it. |
| 178 | s_change_finished.get(); |
| 179 | ASSERT_EQ(m_pipeline->get_version(), Gcs_protocol_version::V2); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | Validate that the protocol change is properly aborted if the desired |
| 184 | protocol is unsupported. |
| 185 | */ |
| 186 | TEST_F(GcsXcomCommunicationProtocolChangerTest, |
| 187 | SenderThreadFinishesProtocolChangeUnsupportedVersion) { |
| 188 | s_change_started = false; |
| 189 | |
| 190 | // Start a protocol change. |
| 191 | std::tie(s_change_started, s_change_finished) = |
| 192 | m_changer->set_protocol_version(Gcs_protocol_version::MAXIMUM); |
| 193 | |
| 194 | // Confirm it indeed failed. |
| 195 | ASSERT_FALSE(s_change_started); |
| 196 | ASSERT_EQ(m_pipeline->get_version(), Gcs_protocol_version::V1); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | Validate that the protocol change is properly aborted if the desired |
| 201 | protocol is unsupported. |
| 202 | */ |
| 203 | TEST_F(GcsXcomCommunicationProtocolChangerTest, |
| 204 | ReceiverThreadFinishesProtocolChangeUnsupportedVersion) { |
| 205 | s_change_started = false; |
| 206 | |
| 207 | // Increment the number of packets in transit to one. |
| 208 | m_changer->atomically_increment_nr_packets_in_transit( |
| 209 | Cargo_type::CT_USER_DATA); |
| 210 | |
| 211 | // Start a protocol change. |
| 212 | std::tie(s_change_started, s_change_finished) = |
| 213 | m_changer->set_protocol_version(Gcs_protocol_version::MAXIMUM); |
| 214 | |
| 215 | // Confirm it is indeed failed. |
| 216 | ASSERT_FALSE(s_change_started); |
| 217 | |
| 218 | // Receive the in transit packet. The receiver thread will try to |
| 219 | // finish the protocol change. |
| 220 | bool packet_ok; |
| 221 | Gcs_packet packet; |
| 222 | std::tie(packet_ok, packet) = Gcs_packet::make_outgoing_packet( |
| 223 | Cargo_type::CT_USER_DATA, Gcs_protocol_version::V1, {}, {}, 1); |
| 224 | ASSERT_TRUE(packet_ok); |
| 225 | Gcs_xcom_nodes nodes; |
| 226 | nodes.add_node(Gcs_xcom_node_information(m_myself.get_member_address(), |
| 227 | Gcs_xcom_uuid(), 0, true)); |
| 228 | m_changer->decrement_nr_packets_in_transit(packet, nodes); |
| 229 | |
| 230 | ASSERT_EQ(m_pipeline->get_version(), Gcs_protocol_version::V1); |
| 231 | } |
| 232 | |
| 233 | } // namespace gcs_xcom_communication_protocol_changer_unittest |