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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
Copyright (c) 2019, 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 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 "router_component_testutils.h"
#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
#include "my_rapidjson_size_t.h"
#endif
#include <gmock/gmock.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <chrono>
#include <fstream>
#include <thread>
#include "mysql/harness/net_ts/buffer.h"
#include "mysql/harness/net_ts/impl/resolver.h"
#include "mysql/harness/net_ts/impl/socket.h"
#include "mysql/harness/net_ts/internet.h"
#include "mysql/harness/net_ts/socket.h"
#include "mysqlrouter/mock_server_rest_client.h"
#include "router_test_helpers.h"
namespace {
// default allocator for rapidJson (MemoryPoolAllocator) is broken for
// SparcSolaris
using JsonAllocator = rapidjson::CrtAllocator;
using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>, JsonAllocator>;
using JsonDocument =
rapidjson::GenericDocument<rapidjson::UTF8<>, JsonAllocator>;
using JsonStringBuffer =
rapidjson::GenericStringBuffer<rapidjson::UTF8<>, rapidjson::CrtAllocator>;
} // namespace
using namespace std::chrono_literals;
using native_handle_type = net::impl::socket::native_handle_type;
std::string create_state_file_content(
const std::string &cluster_type_specific_id,
const std::string &clusterset_id,
const std::vector<uint16_t> &metadata_servers_ports,
const uint64_t view_id /*= 0*/) {
std::string metadata_servers;
for (std::size_t i = 0; i < metadata_servers_ports.size(); i++) {
metadata_servers +=
"\"mysql://127.0.0.1:" + std::to_string(metadata_servers_ports[i]) +
"\"";
if (i < metadata_servers_ports.size() - 1) metadata_servers += ",";
}
std::string view_id_str;
if (view_id > 0) view_id_str = R"(, "view-id":)" + std::to_string(view_id);
std::string cluster_id;
if (!cluster_type_specific_id.empty()) {
cluster_id =
R"("group-replication-id": ")" + cluster_type_specific_id + R"(",)";
}
if (!clusterset_id.empty()) {
cluster_id += (R"("clusterset-id": ")" + clusterset_id + R"(",)");
}
const std::string version = clusterset_id.empty() ? "1.1.0" : "1.0.0";
// clang-format off
const std::string result =
"{"
R"("version": ")" + version + R"(",)"
R"("metadata-cache": {)"
+ cluster_id +
R"("cluster-metadata-servers": [)" + metadata_servers + "]"
+ view_id_str +
"}"
"}";
// clang-format on
return result;
}
#define CHECK_TRUE(expr) \
if (!(expr)) return false
static bool check_state_file_helper(
const std::string &state_file_content,
const mysqlrouter::ClusterType cluster_type,
const std::string &expected_cluster_type_specific_id,
const std::vector<uint16_t> expected_cluster_nodes,
const uint64_t expected_view_id /*= 0*/,
const std::string node_address /*= "127.0.0.1"*/) {
JsonDocument json_doc;
if (json_doc.Parse<0>(state_file_content.c_str()).HasParseError())
return false;
const std::string kExpectedVersion =
cluster_type == mysqlrouter::ClusterType::GR_CS ? "1.1.0" : "1.0.0";
CHECK_TRUE(json_doc.HasMember("version"));
CHECK_TRUE(json_doc["version"].IsString());
CHECK_TRUE(kExpectedVersion == json_doc["version"].GetString());
CHECK_TRUE(json_doc.HasMember("metadata-cache"));
CHECK_TRUE(json_doc["metadata-cache"].IsObject());
auto metadata_cache_section = json_doc["metadata-cache"].GetObject();
const std::string cluster_type_specific_id_field =
cluster_type == mysqlrouter::ClusterType::GR_CS ? "clusterset-id"
: "group-replication-id";
CHECK_TRUE(
metadata_cache_section.HasMember(cluster_type_specific_id_field.c_str()));
CHECK_TRUE(metadata_cache_section[cluster_type_specific_id_field.c_str()]
.IsString());
CHECK_TRUE(expected_cluster_type_specific_id ==
metadata_cache_section[cluster_type_specific_id_field.c_str()]
.GetString());
if (expected_view_id > 0) {
CHECK_TRUE(metadata_cache_section.HasMember("view-id"));
CHECK_TRUE(metadata_cache_section["view-id"].IsInt());
CHECK_TRUE(expected_view_id ==
metadata_cache_section["view-id"].GetUint64());
}
CHECK_TRUE(metadata_cache_section.HasMember("cluster-metadata-servers"));
CHECK_TRUE(metadata_cache_section["cluster-metadata-servers"].IsArray());
auto cluster_nodes =
metadata_cache_section["cluster-metadata-servers"].GetArray();
CHECK_TRUE(expected_cluster_nodes.size() == cluster_nodes.Size());
for (unsigned i = 0; i < cluster_nodes.Size(); ++i) {
CHECK_TRUE(cluster_nodes[i].IsString());
const std::string expected_cluster_node =
"mysql://" + node_address + ":" +
std::to_string(expected_cluster_nodes[i]);
CHECK_TRUE(expected_cluster_node == cluster_nodes[i].GetString());
}
return true;
}
void check_state_file(const std::string &state_file,
const mysqlrouter::ClusterType cluster_type,
const std::string &expected_cluster_type_specific_id,
const std::vector<uint16_t> expected_cluster_nodes,
const uint64_t expected_view_id /*= 0*/,
const std::string node_address /*= "127.0.0.1"*/,
std::chrono::milliseconds max_wait_time /*= 5000*/) {
bool result = false;
std::string state_file_content;
auto kRetryStep = 50ms;
if (getenv("WITH_VALGRIND")) {
max_wait_time *= 10;
kRetryStep *= 10;
}
do {
state_file_content = get_file_output(state_file);
result = check_state_file_helper(
state_file_content, cluster_type, expected_cluster_type_specific_id,
expected_cluster_nodes, expected_view_id, node_address);
if (!result) {
std::this_thread::sleep_for(kRetryStep);
max_wait_time -= kRetryStep;
}
} while ((!result) && (max_wait_time > kRetryStep));
if (!result) {
std::string expected_cluster_nodes_str;
for (size_t i = 0; i < expected_cluster_nodes.size(); ++i) {
expected_cluster_nodes_str +=
std::to_string(expected_cluster_nodes[i]) + " ";
}
FAIL() << "Unexpected state file content." << std::endl
<< "cluster_type_specific_id: " << expected_cluster_type_specific_id
<< std::endl
<< "expected_cluster_nodes: " << expected_cluster_nodes_str
<< std::endl
<< "expected_view_id: " << expected_view_id << std::endl
<< "node_address: " << node_address << std::endl
<< "state_file_content: " << state_file_content;
}
// check that we have write access to the file
// just append it with an empty line, that will not break it
EXPECT_NO_THROW({
std::ofstream ofs(state_file, std::ios::app);
ofs << "\n";
});
}
int get_int_field_value(const std::string &json_string,
const std::string &field_name) {
rapidjson::Document json_doc;
json_doc.Parse(json_string.c_str());
if (!json_doc.HasMember(field_name.c_str())) {
// that can mean this has not been set yet
return 0;
}
if (!json_doc[field_name.c_str()].IsInt()) {
// that can mean this has not been set yet
return 0;
}
return json_doc[field_name.c_str()].GetInt();
}
int get_transaction_count(const std::string &json_string) {
return get_int_field_value(json_string, "transaction_count");
}
int get_transaction_count(const uint16_t http_port) {
std::string server_globals =
MockServerRestClient(http_port).get_globals_as_json_string();
return get_transaction_count(server_globals);
}
bool wait_for_transaction_count(const uint16_t http_port,
const int expected_queries_count,
std::chrono::milliseconds timeout) {
const std::chrono::milliseconds kStep = 20ms;
do {
std::string server_globals =
MockServerRestClient(http_port).get_globals_as_json_string();
if (get_transaction_count(server_globals) >= expected_queries_count)
return true;
std::this_thread::sleep_for(kStep);
timeout -= kStep;
} while (timeout > 0ms);
return false;
}
bool wait_for_transaction_count_increase(const uint16_t http_port,
const int increment_by,
std::chrono::milliseconds timeout) {
if (getenv("WITH_VALGRIND")) {
timeout *= 10;
}
std::string server_globals =
MockServerRestClient(http_port).get_globals_as_json_string();
int expected_queries_count =
get_transaction_count(server_globals) + increment_by;
return wait_for_transaction_count(http_port, expected_queries_count, timeout);
}
bool wait_connection_dropped(mysqlrouter::MySQLSession &session,
std::chrono::milliseconds timeout) {
auto kStep = 50ms;
if (getenv("WITH_VALGRIND")) {
timeout *= 10;
kStep *= 5;
}
do {
try {
session.query_one("select @@@port");
} catch (const mysqlrouter::MySQLSession::Error &) {
return true;
}
std::this_thread::sleep_for(kStep);
timeout -= kStep;
} while (timeout >= 0ms);
return false;
}
size_t count_str_occurences(const std::string &s, const std::string &needle) {
if (needle.length() == 0) return 0;
size_t result = 0;
for (size_t pos = s.find(needle); pos != std::string::npos;
pos = s.find(needle, pos + needle.length())) {
++result;
}
return result;
}
void make_bad_connection(uint16_t port) {
net::io_context io_ctx;
net::ip::tcp::socket sock(io_ctx);
net::ip::tcp::endpoint ep(net::ip::address_v4::loopback(), port);
auto connect_res = sock.connect(ep);
if (!connect_res) throw std::system_error(connect_res.error());
// MySQL protocol handshake phase
// To simplify code, instead of alternating between reading and writing
// protocol packets, we write a lot of garbage upfront, and then read
// whatever Router sends back. Router will read what we wrote in chunks,
// in between its writes, thinking they're replies to its handshake packets.
// Eventually it will finish the handshake with error and disconnect.
std::vector<char> bogus_data(3, 0);
const auto write_res = net::write(sock, net::buffer(bogus_data));
if (!write_res) throw std::system_error(write_res.error(), "write() failed");
sock.shutdown(net::socket_base::shutdown_type::shutdown_send);
std::array<char, 1024> buf;
while (net::read(sock, net::buffer(buf))) {
// read until error.
}
}
|