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