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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
|
/*
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 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 <chrono>
#include <cstdint>
#include <fstream>
#include <stdexcept>
#include <thread>
#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
#include "my_rapidjson_size_t.h"
#endif
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/schema.h>
#include <rapidjson/stringbuffer.h>
#include "config_builder.h"
#include "keyring/keyring_manager.h"
#include "mock_server_rest_client.h"
#include "mock_server_testutils.h"
#include "mysql/harness/string_utils.h" // split_string
#include "mysqlrouter/cluster_metadata.h"
#include "mysqlrouter/mysql_session.h"
#include "mysqlrouter/rest_client.h"
#include "process_manager.h"
#include "router_component_system_layout.h"
#include "router_component_test.h"
#include "router_component_testutils.h"
#include "router_test_helpers.h"
#include "tcp_port_pool.h"
using mysqlrouter::ClusterType;
using mysqlrouter::MySQLSession;
using namespace std::chrono_literals;
Path g_origin_path;
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>;
constexpr auto kTTL = 100ms;
} // namespace
class StateFileTest : public RouterComponentBootstrapTest {
protected:
void SetUp() override {
RouterComponentTest::SetUp();
// this test modifies the origin path so we need to restore it
ProcessManager::set_origin(g_origin_path);
}
std::pair<std::string, std::map<std::string, std::string>>
metadata_cache_section(uint16_t metadata_server_port = 0,
const std::chrono::milliseconds ttl = kTTL,
ClusterType cluster_type = ClusterType::GR_V2) {
std::map<std::string, std::string> options{
{"cluster_type", (cluster_type == ClusterType::RS_V2) ? "rs" : "gr"},
{"router_id", "1"},
{"user", "mysql_router1_user"},
{"metadata_cluster", "test"},
{"connect_timeout", "1"},
{"ttl", std::to_string(std::chrono::duration<double>(ttl).count())},
};
if (metadata_server_port != 0) {
options["bootstrap_server_addresses"] =
"mysql://localhost:" + std::to_string(metadata_server_port);
};
return {"metadata_cache:test", options};
}
std::string get_metadata_cache_section(
uint16_t metadata_server_port = 0,
const std::chrono::milliseconds ttl = kTTL,
ClusterType cluster_type = ClusterType::GR_V2) {
auto section =
metadata_cache_section(metadata_server_port, ttl, cluster_type);
return mysql_harness::ConfigBuilder::build_section(section.first,
section.second) +
"\n";
}
std::pair<std::string, std::map<std::string, std::string>>
metadata_cache_routing_section(uint16_t router_port, const std::string &role,
const std::string &strategy,
const std::string &mode = "") {
std::map<std::string, std::string> options{
{"bind_port", std::to_string(router_port)},
{"destinations", "metadata-cache://test/default?role=" + role},
{"protocol", "classic"},
};
if (!strategy.empty()) options["routing_strategy"] = strategy;
if (!mode.empty()) options["mode"] = mode;
return {"routing:test_default", options};
}
std::string get_metadata_cache_routing_section(uint16_t router_port,
const std::string &role,
const std::string &strategy,
const std::string &mode = "") {
auto section =
metadata_cache_routing_section(router_port, role, strategy, mode);
return mysql_harness::ConfigBuilder::build_section(section.first,
section.second) +
"\n";
}
auto &launch_router(const std::string &temp_test_dir,
const std::string &metadata_cache_section,
const std::string &routing_section,
const std::string &state_file_path,
const int expected_errorcode = EXIT_SUCCESS,
std::chrono::milliseconds wait_for_notify = 5s) {
const std::string masterkey_file =
Path(temp_test_dir).join("master.key").str();
const std::string keyring_file = Path(temp_test_dir).join("keyring").str();
mysql_harness::init_keyring(keyring_file, masterkey_file, true);
mysql_harness::Keyring *keyring = mysql_harness::get_keyring();
keyring->store("mysql_router1_user", "password", "root");
mysql_harness::flush_keyring();
mysql_harness::reset_keyring();
// launch the router with metadata-cache configuration
auto default_section = get_DEFAULT_defaults();
default_section["keyring_path"] = keyring_file;
default_section["master_key_path"] = masterkey_file;
default_section["dynamic_state"] = state_file_path;
const std::string conf_file = create_config_file(
temp_test_dir, metadata_cache_section + routing_section,
&default_section);
auto &router = ProcessManager::launch_router(
{"-c", conf_file}, expected_errorcode, /*catch_stderr=*/true,
/*with_sudo=*/false,
/*wait_for_notify_ready=*/wait_for_notify);
return router;
}
bool wait_log_file_contains(ProcessWrapper &router,
const std::string &expected_entry,
std::chrono::milliseconds max_wait_time) {
auto kRetrySleep = 100ms;
if (getenv("WITH_VALGRIND")) {
max_wait_time *= 50;
kRetrySleep *= 10;
}
do {
const auto log_content = router.get_logfile_content();
if (log_content.find(expected_entry) != std::string::npos) return true;
std::this_thread::sleep_for(kRetrySleep);
if (max_wait_time <= kRetrySleep) return false;
max_wait_time -= kRetrySleep;
} while (true);
}
std::string create_state_file_content(
const std::string &cluster_id,
const std::vector<uint16_t> &metadata_servers_ports,
const std::string &hostname = "127.0.0.1") {
std::string metadata_servers;
for (std::size_t i = 0; i < metadata_servers_ports.size(); i++) {
metadata_servers += R"("mysql://)" + hostname + ":" +
std::to_string(metadata_servers_ports[i]) + "\"";
if (i < metadata_servers_ports.size() - 1) metadata_servers += ",";
}
// clang-format off
const std::string result =
"{"
R"("version": "1.0.0",)"
R"("metadata-cache": {)"
R"("group-replication-id": ")" + cluster_id + R"(",)"
R"("cluster-metadata-servers": [)" + metadata_servers + "]"
"}"
"}";
// clang-format on
return result;
}
};
//////////////////////////////////////////////////////////////////////////
class StateFileDynamicChangesTest : public StateFileTest {
protected:
void kill_server(ProcessWrapper *server) { EXPECT_NO_THROW(server->kill()); }
};
struct StateFileTestParam {
public:
std::string description;
std::string trace_file;
ClusterType cluster_type;
bool ipv6{false};
};
auto get_test_description(
const ::testing::TestParamInfo<StateFileTestParam> &info) {
return info.param.description;
}
class StateFileMetadataServersChangedInRuntimeTest
: public StateFileDynamicChangesTest,
public ::testing::WithParamInterface<StateFileTestParam> {};
/**
* @test
* Verify that changes in the cluster topology are reflected in the state
* file in the runtime.
*/
TEST_P(StateFileMetadataServersChangedInRuntimeTest,
MetadataServersChangedInRuntime) {
const auto param = GetParam();
const std::string kGroupId = "3a0be5af-0022-11e8-9655-0800279e6a88";
TempDirectory temp_test_dir;
const unsigned CLUSTER_NODES = 3;
std::vector<ProcessWrapper *> cluster_nodes;
std::vector<uint16_t> cluster_nodes_ports;
std::vector<uint16_t> cluster_http_ports;
for (unsigned i = 0; i < CLUSTER_NODES; ++i) {
cluster_nodes_ports.push_back(port_pool_.get_next_available());
cluster_http_ports.push_back(port_pool_.get_next_available());
}
const std::string node_host = param.ipv6 ? "[::1]" : "127.0.0.1";
const std::string bind_address = param.ipv6 ? "::" : "0.0.0.0";
SCOPED_TRACE(
"// Launch 3 server mocks that will act as our metadata servers");
const auto trace_file = get_data_dir().join(param.trace_file).str();
for (unsigned i = 0; i < CLUSTER_NODES; ++i) {
cluster_nodes.push_back(&ProcessManager::launch_mysql_server_mock(
trace_file, cluster_nodes_ports[i], EXIT_SUCCESS, false,
cluster_http_ports[i], 0, "", bind_address));
try {
ASSERT_NO_FATAL_FAILURE(check_port_ready(
*cluster_nodes[i], cluster_nodes_ports[i], kDefaultPortReadyTimeout,
param.ipv6 ? "::1" : "127.0.0.1"));
} catch (const std::system_error &e) {
// the only expected system-error is "address-no-available" in case of
// trying to bind to ipv6 when ipv6 is disabled on the host
ASSERT_EQ(e.code(),
make_error_condition(std::errc::address_not_available));
// there is no good synchronization point for waiting for the mock's
// signal handler to be setup
//
// - nothing is written to the log
std::this_thread::sleep_for(100ms);
return;
}
ASSERT_TRUE(MockServerRestClient(cluster_http_ports[i])
.wait_for_rest_endpoint_ready());
SCOPED_TRACE(
"// Make our metadata server to return single node as a cluster "
"member (meaning single metadata server)");
set_mock_metadata(cluster_http_ports[i], kGroupId, {cluster_nodes_ports[i]},
i, {cluster_nodes_ports[i]}, 0, 0, false, node_host);
}
SCOPED_TRACE("// Create a router state file with a single metadata server");
const std::string state_file = create_state_file(
temp_test_dir.name(),
create_state_file_content(kGroupId, {cluster_nodes_ports[0]}, node_host));
SCOPED_TRACE(
"// Create a configuration file sections with low ttl so that any "
"changes we make in the mock server via http port were refreshed "
"quickly");
const std::string metadata_cache_section =
get_metadata_cache_section(0, kTTL, param.cluster_type);
const uint16_t router_port = port_pool_.get_next_available();
const std::string routing_section = get_metadata_cache_routing_section(
router_port, "PRIMARY", "first-available");
SCOPED_TRACE("// Launch ther router with the initial state file");
launch_router(temp_test_dir.name(), metadata_cache_section, routing_section,
state_file);
SCOPED_TRACE(
"// Check our state file content, it should not change yet, there is "
"single metadata server reported as initially");
check_state_file(state_file, param.cluster_type, kGroupId,
{cluster_nodes_ports[0]}, 0, node_host);
SCOPED_TRACE(
"// Now change the response from the metadata server to return 3 gr "
"nodes (metadata servers)");
for (unsigned i = 0; i < CLUSTER_NODES; ++i) {
set_mock_metadata(cluster_http_ports[i], kGroupId,
classic_ports_to_gr_nodes(cluster_nodes_ports), i,
classic_ports_to_cluster_nodes(cluster_nodes_ports), 0, 0,
false, node_host);
}
SCOPED_TRACE(
"// Check our state file content, it should now contain 3 metadata "
"servers");
check_state_file(
state_file, param.cluster_type, kGroupId,
{cluster_nodes_ports[0], cluster_nodes_ports[1], cluster_nodes_ports[2]},
0, node_host);
///////////////////////////////////////////////////
SCOPED_TRACE(
"// We have 3 nodes now, let's make a few connections, we have single "
"primary configured so they should be directed to the same first server");
std::string out_port;
for (unsigned i = 0; i < 3; ++i) {
EXPECT_NO_THROW(connect_client_and_query_port(router_port, out_port,
/*should_fail=*/false));
EXPECT_EQ(out_port, std::to_string(cluster_nodes_ports[0]));
}
///////////////////////////////////////////////////
SCOPED_TRACE(
"// Instrument the second and third metadata servers to return 2 "
"servers: second and third");
set_mock_metadata(cluster_http_ports[1], kGroupId,
{cluster_nodes_ports[1], cluster_nodes_ports[2]}, 1,
{cluster_nodes_ports[1], cluster_nodes_ports[2]}, 0, 0,
false, node_host);
set_mock_metadata(cluster_http_ports[2], kGroupId,
{cluster_nodes_ports[1], cluster_nodes_ports[2]}, 2,
{cluster_nodes_ports[1], cluster_nodes_ports[2]}, 0, 0,
false, node_host);
SCOPED_TRACE("// Kill first metada server");
kill_server(cluster_nodes[0]);
SCOPED_TRACE(
"// Check our state file content, it should now contain 2 metadata "
"servers reported by the second metadata server");
check_state_file(state_file, param.cluster_type, kGroupId,
{cluster_nodes_ports[1], cluster_nodes_ports[2]}, 0,
node_host, 10000ms);
}
INSTANTIATE_TEST_SUITE_P(
MetadataServersChangedInRuntime,
StateFileMetadataServersChangedInRuntimeTest,
::testing::Values(
StateFileTestParam{"gr", "metadata_dynamic_nodes.js",
ClusterType::GR_V1},
StateFileTestParam{"gr_v2", "metadata_dynamic_nodes_v2_gr.js",
ClusterType::GR_V2},
StateFileTestParam{"ar_v2", "metadata_dynamic_nodes_v2_ar.js",
ClusterType::RS_V2},
StateFileTestParam{"gr_ipv6", "metadata_dynamic_nodes.js",
ClusterType::GR_V1, /*ipv6=*/true},
StateFileTestParam{"gr_v2_ipv6", "metadata_dynamic_nodes_v2_gr.js",
ClusterType::GR_V2, /*ipv6=*/true},
StateFileTestParam{"ar_v2_ipv6", "metadata_dynamic_nodes_v2_ar.js",
ClusterType::RS_V2, /*ipv6=*/true}),
get_test_description);
class StateFileMetadataServersInaccessibleTest
: public StateFileDynamicChangesTest,
public ::testing::WithParamInterface<StateFileTestParam> {};
/* @test
* Verify that if not metadata server can't be accessed the list of the
* server does not get cleared.
*/
TEST_P(StateFileMetadataServersInaccessibleTest, MetadataServersInaccessible) {
const auto param = GetParam();
const std::string kGroupId = "3a0be5af-0022-11e8-9655-0800279e6a88";
TempDirectory temp_test_dir;
uint16_t cluster_node_port = port_pool_.get_next_available();
uint16_t cluster_http_port = port_pool_.get_next_available();
SCOPED_TRACE(
"// Launch single server mock that will act as our metadata server");
const auto trace_file = get_data_dir().join(param.trace_file).str();
auto &cluster_node(ProcessManager::launch_mysql_server_mock(
trace_file, cluster_node_port, EXIT_SUCCESS, false, cluster_http_port));
ASSERT_NO_FATAL_FAILURE(check_port_ready(cluster_node, cluster_node_port));
ASSERT_TRUE(
MockServerRestClient(cluster_http_port).wait_for_rest_endpoint_ready());
SCOPED_TRACE(
"// Make our metadata server return single node as a cluster "
"member (meaning single metadata server)");
set_mock_metadata(cluster_http_port, kGroupId, {cluster_node_port}, 0,
{cluster_node_port});
SCOPED_TRACE("// Create a router state file with a single metadata server");
const std::string state_file = create_state_file(
temp_test_dir.name(),
create_state_file_content(kGroupId, {cluster_node_port}));
SCOPED_TRACE("// Create a configuration file with low ttl");
const std::string metadata_cache_section =
get_metadata_cache_section(0, kTTL, param.cluster_type);
const uint16_t router_port = port_pool_.get_next_available();
const std::string routing_section = get_metadata_cache_routing_section(
router_port, "PRIMARY", "first-available");
SCOPED_TRACE("// Launch ther router with the initial state file");
auto &router = launch_router(temp_test_dir.name(), metadata_cache_section,
routing_section, state_file);
ASSERT_NO_FATAL_FAILURE(check_port_ready(router, router_port));
// kill our single instance server
kill_server(&cluster_node);
SCOPED_TRACE(
"// Check our state file content, it should still contain out metadata "
"server");
check_state_file(state_file, param.cluster_type, kGroupId,
{cluster_node_port}, 0, "127.0.0.1", 10s);
router.send_shutdown_event();
EXPECT_EQ(0, router.wait_for_exit());
}
INSTANTIATE_TEST_SUITE_P(
MetadataServersInaccessible, StateFileMetadataServersInaccessibleTest,
::testing::Values(StateFileTestParam{"gr", "metadata_dynamic_nodes.js",
ClusterType::GR_V1},
StateFileTestParam{"gr_v2",
"metadata_dynamic_nodes_v2_gr.js",
ClusterType::GR_V2},
StateFileTestParam{"ar_v2",
"metadata_dynamic_nodes_v2_ar.js",
ClusterType::RS_V2}),
get_test_description);
class StateFileGroupReplicationIdDiffersTest
: public StateFileDynamicChangesTest,
public ::testing::WithParamInterface<StateFileTestParam> {};
/**
* @test
* Verify that if the metadata servers do not know about the replication
* group id that was bootstrapped against, Router does not use metadata for
* routing, logs an error but does not change the metadata servers list in the
* dynamic state file.
*/
TEST_P(StateFileGroupReplicationIdDiffersTest, GroupReplicationIdDiffers) {
const auto param = GetParam();
constexpr const char kStateFileGroupId[] =
"3a0be5af-0022-11e8-0000-0800279e6a88";
constexpr const char kClusterFileGroupId[] =
"3a0be5af-0022-11e8-0000-0800279e6a89";
TempDirectory temp_test_dir;
auto cluster_node_port = port_pool_.get_next_available();
auto cluster_http_port = port_pool_.get_next_available();
SCOPED_TRACE("// Launch server mock that will act as our metadata server");
const auto trace_file = get_data_dir().join(param.trace_file).str();
/*auto &cluster_node =*/ProcessManager::launch_mysql_server_mock(
trace_file, cluster_node_port, EXIT_SUCCESS, false, cluster_http_port);
SCOPED_TRACE(
"// Make our metadata server to return single node as a cluster "
"member (meaning single metadata server)");
set_mock_metadata(cluster_http_port, kClusterFileGroupId, {cluster_node_port},
0, {cluster_node_port});
SCOPED_TRACE(
"// Create a router state file with a single metadata server and "
"group-replication-id different than the one reported by the "
"mock-server");
const std::string state_file = create_state_file(
temp_test_dir.name(),
create_state_file_content(kStateFileGroupId, {cluster_node_port}));
SCOPED_TRACE(
"// Create a configuration file sections with low ttl so that any "
"changes we make in the mock server via http port were refreshed "
"quickly");
const std::string metadata_cache_section =
get_metadata_cache_section(0, kTTL, param.cluster_type);
const uint16_t router_port = port_pool_.get_next_available();
const std::string routing_section = get_metadata_cache_routing_section(
router_port, "PRIMARY", "first-available");
SCOPED_TRACE("// Launch ther router with the initial state file");
auto &router = launch_router(temp_test_dir.name(), metadata_cache_section,
routing_section, state_file, EXIT_SUCCESS, -1s);
SCOPED_TRACE(
"// Check our state file content, it should not change. "
"We did not found the data for our replication group on any of the "
"servers so we do not update the metadata srever list.");
check_state_file(state_file, param.cluster_type, kStateFileGroupId,
{cluster_node_port});
SCOPED_TRACE("// We expect an error in the logfile");
EXPECT_TRUE(wait_log_file_contains(
router, "Failed fetching metadata from any of the 1 metadata servers",
5s));
// now try to connect to the router port, we expect error 2003
std::string out_port_unused;
EXPECT_NO_THROW(connect_client_and_query_port(router_port, out_port_unused,
/*should_fail=*/true));
}
INSTANTIATE_TEST_SUITE_P(
GroupReplicationIdDiffers, StateFileGroupReplicationIdDiffersTest,
::testing::Values(StateFileTestParam{"gr", "metadata_dynamic_nodes.js",
ClusterType::GR_V1},
StateFileTestParam{"gr_v2",
"metadata_dynamic_nodes_v2_gr.js",
ClusterType::GR_V2},
StateFileTestParam{"ar_v2",
"metadata_dynamic_nodes_v2_ar.js",
ClusterType::RS_V2}),
get_test_description);
class StateFileSplitBrainScenarioTest
: public StateFileDynamicChangesTest,
public ::testing::WithParamInterface<StateFileTestParam> {};
/**
* @test
* Verify that if the split brain scenario the list of the metadata servers
* gets updated properly in the state file.
*/
TEST_P(StateFileSplitBrainScenarioTest, SplitBrainScenario) {
const auto param = GetParam();
const std::string kClusterGroupId = "3a0be5af-0022-11e8-0000-0800279e6a88";
const unsigned kNodesNum = 3; // number of nodes in the cluster
// TcpPortPool
// cluster_ports_pool; // currently TcpPortPool supports max 10 ports so
// // we create dedicated one for our cluster
TempDirectory temp_test_dir;
std::vector<ProcessWrapper *> cluster_nodes;
std::vector<std::pair<uint16_t, uint16_t>>
cluster_node_ports; // pair of connection and http port
for (unsigned i = 0; i < kNodesNum; i++) {
cluster_node_ports.push_back(
{port_pool_.get_next_available(), port_pool_.get_next_available()});
}
SCOPED_TRACE("// Launch server mocks that play as our split brain cluster");
const auto trace_file = get_data_dir().join(param.trace_file).str();
for (unsigned i = 0; i < kNodesNum; i++) {
const auto port_connect = cluster_node_ports[i].first;
const auto port_http = cluster_node_ports[i].second;
cluster_nodes.push_back(&ProcessManager::launch_mysql_server_mock(
trace_file, port_connect, EXIT_SUCCESS, false, port_http));
ASSERT_NO_FATAL_FAILURE(check_port_ready(*cluster_nodes[i], port_connect));
ASSERT_TRUE(MockServerRestClient(port_http).wait_for_rest_endpoint_ready());
}
SCOPED_TRACE(
"// let's configure the metadata so that there are 2 groups that do not "
"know about each other (split brain)");
std::vector<uint16_t> fist_group{cluster_node_ports[0].first,
cluster_node_ports[1].first};
for (unsigned i = 0; i < 1; i++) {
const auto port_http = cluster_node_ports[i].second;
set_mock_metadata(port_http, kClusterGroupId,
classic_ports_to_gr_nodes(fist_group), i,
classic_ports_to_cluster_nodes(fist_group));
}
std::vector<uint16_t> second_group{cluster_node_ports[2].first};
for (unsigned i = 2; i < kNodesNum; i++) {
const auto port_http = cluster_node_ports[i].second;
set_mock_metadata(port_http, kMockServerGlobalsRestUri,
classic_ports_to_gr_nodes(second_group), i - 2,
classic_ports_to_cluster_nodes(second_group));
}
SCOPED_TRACE(
"// Create a router state file with all the nodes as a "
"cluster-metadata-servers ");
std::vector<uint16_t> cluster_ports;
for (const auto &port : cluster_node_ports)
cluster_ports.push_back(port.first);
const std::string state_file = create_state_file(
temp_test_dir.name(),
create_state_file_content(kClusterGroupId, cluster_ports));
SCOPED_TRACE(
"// Create a configuration file sections with low ttl so that any "
"changes we make in the mock server via http port were refreshed "
"quickly");
const std::string metadata_cache_section =
get_metadata_cache_section(0, kTTL, param.cluster_type);
const uint16_t router_port = port_pool_.get_next_available();
const std::string routing_section = get_metadata_cache_routing_section(
router_port, "PRIMARY", "first-available");
SCOPED_TRACE("// Launch ther router with the initial state file");
launch_router(temp_test_dir.name(), metadata_cache_section, routing_section,
state_file);
SCOPED_TRACE(
"// Check our state file content, it should now contain only the nodes "
"from the first group.");
std::vector<uint16_t> node_ports;
for (unsigned i = 0; i < 2; ++i) {
node_ports.push_back(cluster_node_ports[i].first);
}
check_state_file(state_file, param.cluster_type, kClusterGroupId, node_ports);
SCOPED_TRACE(
"// Try to connect to the router port, we expect first port from the "
"first group.");
std::string port_connected;
EXPECT_NO_THROW(connect_client_and_query_port(router_port, port_connected,
/*should_fail=*/false));
EXPECT_STREQ(std::to_string(cluster_node_ports[0].first).c_str(),
port_connected.c_str());
}
INSTANTIATE_TEST_SUITE_P(
SplitBrainScenario, StateFileSplitBrainScenarioTest,
::testing::Values(StateFileTestParam{"gr", "metadata_dynamic_nodes.js",
ClusterType::GR_V1},
StateFileTestParam{"gr_v2",
"metadata_dynamic_nodes_v2_gr.js",
ClusterType::GR_V2}),
get_test_description);
/**
* @test
* Verify that in case of empty metada-server-address list in the state
* file the Router logs proper error and exits.
*/
TEST_F(StateFileDynamicChangesTest, EmptyMetadataServersList) {
constexpr const char kGroupId[] = "3a0be5af-0022-11e8-9655-0800279e6a88";
TempDirectory temp_test_dir;
SCOPED_TRACE("// Create a router state file with empty server list");
const std::string state_file = create_state_file(
temp_test_dir.name(), create_state_file_content(kGroupId, {}));
SCOPED_TRACE(
"// Create a configuration file sections with low ttl so that any "
"changes we make in the mock server via http port were refreshed "
"quickly");
const std::string metadata_cache_section =
get_metadata_cache_section(0, kTTL);
const uint16_t router_port = port_pool_.get_next_available();
const std::string routing_section = get_metadata_cache_routing_section(
router_port, "PRIMARY", "first-available");
SCOPED_TRACE("// Launch ther router with the initial state file");
auto &router = launch_router(temp_test_dir.name(), metadata_cache_section,
routing_section, state_file, EXIT_FAILURE, -1s);
// wait for shutdown before checking the logfile.
EXPECT_EQ(router.wait_for_exit(), EXIT_FAILURE);
// proper error should get logged
EXPECT_TRUE(wait_log_file_contains(
router,
"'bootstrap_server_addresses' in the configuration file is empty "
"or not set and list of 'cluster-metadata-servers' in "
"'dynamic_config'-file is empty, too.",
3 * kTTL));
}
//////////////////////////////////////////////////////////////////////////
struct StateFileSchemaTestParams {
std::string state_file_content;
std::vector<std::string> expected_errors_in_log;
bool create_state_file_from_content{true};
std::string state_file_path{""};
bool use_static_server_list{false};
ClusterType cluster_type{ClusterType::GR_V2};
};
::std::ostream &operator<<(::std::ostream &os,
const StateFileSchemaTestParams &sfp) {
os << "state_file_content = " << sfp.state_file_content
<< "\n, expected_errors = [";
for (const auto &err : sfp.expected_errors_in_log) {
os << err << "\n";
}
os << "]\n";
return os;
}
class StateFileSchemaTest
: public StateFileTest,
public ::testing::WithParamInterface<StateFileSchemaTestParams> {
protected:
void SetUp() override { StateFileTest::SetUp(); }
};
/**
* @test
* Verify that the proper error gets logged and the Router shuts down in
* case of various configuration mismatches.
*/
TEST_P(StateFileSchemaTest, ParametrizedStateFileSchemaTest) {
auto test_params = GetParam();
TempDirectory temp_test_dir;
const uint16_t router_port = port_pool_.get_next_available();
const std::string state_file =
test_params.create_state_file_from_content
? create_state_file(temp_test_dir.name(),
test_params.state_file_content)
: test_params.state_file_path;
auto writer =
config_writer(temp_test_dir.name())
.section(metadata_cache_section(test_params.use_static_server_list
? port_pool_.get_next_available()
: 0,
kTTL, test_params.cluster_type))
.section(metadata_cache_routing_section(router_port, "PRIMARY",
"first-available"));
auto &default_section = writer.sections()["DEFAULT"];
init_keyring(default_section, temp_test_dir.name());
default_section["dynamic_state"] = state_file;
auto &router =
router_spawner()
.expected_exit_code(EXIT_FAILURE)
.wait_for_sync_point(ProcessManager::Spawner::SyncPoint::RUNNING)
.spawn({"-c", writer.write()});
// the router should close with non-0 return value
ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
// proper log should get logged
auto log_content = router.get_logfile_content();
for (const auto &expeted_in_log : test_params.expected_errors_in_log) {
EXPECT_TRUE(log_content.find(expeted_in_log) != std::string::npos);
}
}
INSTANTIATE_TEST_SUITE_P(
StateFileTests, StateFileSchemaTest,
::testing::Values(
// state file does not exits
StateFileSchemaTestParams{
"",
{"Could not open dynamic state file 'non-existing.json' "
"for "
"reading"},
false, /* = don't create state file, use the path given */
"non-existing.json"},
// state file path empty
StateFileSchemaTestParams{
"", {"Could not open dynamic state file '' for reading"},
false, /* = don't create state file, use the empty path given */
""},
// state file containing invalid non-json data
StateFileSchemaTestParams{"some invalid, non-json content",
{"Error parsing file dynamic state file",
"Parsing JSON failed at offset 0"}},
// state file content is not an object
StateFileSchemaTestParams{"[]",
{"Invalid json structure: not an object"}},
// version field missing
StateFileSchemaTestParams{
// clang-format off
"{"
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Invalid json structure: missing field: version"}},
// version field is not a string
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": 1,"
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Invalid json structure: field version "
"should be a string type"}},
// version field is non numeric string
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"str\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Invalid version field format, expected MAJOR.MINOR.PATCH, "
"found: str"}},
// version field has wrong number of numeric values
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"1.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Invalid version field format, expected MAJOR.MINOR.PATCH, "
"found: 1.0"}},
// major version does not match (GR cluster)
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"2.0.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Unsupported state file version, "
"expected: 1.1.0, found: 2.0.0"}},
// major version does not match (AR cluster)
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"2.0.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Unsupported state file version, "
"expected: 1.1.0, found: 2.0.0"}, true, "", false,
ClusterType::RS_V2},
// minor version does not match
StateFileSchemaTestParams{ // clang-format off
"{"
"\"version\": \"1.2.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"Unsupported state file version, "
"expected: 1.1.0, found: 1.2.0"}},
// both bootstrap_server_addresses and dynamic_state configured
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"1.0.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\","
"\"cluster-metadata-servers\": ["
"\"mysql://localhost:5000\","
"\"mysql://127.0.0.1:5001\""
"]"
"}"
"}",
// clang-format on
{"bootstrap_server_addresses is not allowed when dynamic "
"state file is used"},
true, "",
true /*use static bootstrap_server_addresses in static conf. file*/},
// group-replication-id filed missing
// no longer required
// StateFileSchemaTestParams{
// // clang-format off
// "{"
// "\"version\": \"1.0.0\","
// "\"metadata-cache\": {"
// "\"cluster-metadata-servers\": ["
// "\"mysql://localhost:5000\","
// "\"mysql://127.0.0.1:5001\""
// "]"
// "}"
// "}",
// // clang-format on
// {"JSON file failed validation against JSON schema: Failed schema "
// "directive: #/properties/metadata-cache",
// "Failed schema keyword: required",
// "Failure location in validated document: #/metadata-cache"}},
// cluster-metadata-servers filed missing (GR cluster)
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"1.0.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\""
"}"
"}",
// clang-format on
{"JSON file failed validation against JSON schema: Failed schema "
"directive: #/properties/metadata-cache",
"Failed schema keyword: required",
"Failure location in validated document: #/metadata-cache"}},
// cluster-metadata-servers filed missing (AR cluster)
StateFileSchemaTestParams{
// clang-format off
"{"
"\"version\": \"1.0.0\","
"\"metadata-cache\": {"
"\"group-replication-id\": \"3a0be5af-994c-11e8-9655-0800279e6a88\""
"}"
"}",
// clang-format on
{"JSON file failed validation against JSON schema: Failed schema "
"directive: #/properties/metadata-cache",
"Failed schema keyword: required",
"Failure location in validated document: #/metadata-cache"}},
// both bootstrap_server_addresses and dynamic_state configured
// dynamic_state file not existing
StateFileSchemaTestParams{
"",
{"bootstrap_server_addresses is not allowed when dynamic "
"state file is used"},
false, /* = don't create state file, use the path given */
"non-existing.json",
true /*use static bootstrap_server_addresses in static conf. file*/}));
////////////////////////////////////////////
// Test for state file right access
////////////////////////////////////////////
#ifndef _WIN32
struct StateFileAccessRightsTestParams {
bool read_access;
bool write_access;
std::string expected_error;
StateFileAccessRightsTestParams(bool read_access_, bool write_access_,
const std::string &expected_error_)
: read_access(read_access_),
write_access(write_access_),
expected_error(expected_error_) {}
};
class StateFileAccessRightsTest
: public StateFileTest,
public ::testing::WithParamInterface<StateFileAccessRightsTestParams> {
protected:
void SetUp() override { StateFileTest::SetUp(); }
};
/**
* @test
* Verify that the proper error gets logged and the Router shuts down in
* case of state file access rights problems.
*/
TEST_P(StateFileAccessRightsTest, ParametrizedStateFileSchemaTest) {
auto test_params = GetParam();
TempDirectory temp_test_dir;
const uint16_t router_port = port_pool_.get_next_available();
// launch the router with static metadata-cache configuration and
// dynamic state file configured via test parameter
const std::string state_file = create_state_file(
temp_test_dir.name(), create_state_file_content("000-000", {10000}));
mode_t file_mode = 0;
if (test_params.read_access) file_mode |= S_IRUSR;
if (test_params.write_access) file_mode |= S_IWUSR;
chmod(state_file.c_str(), file_mode);
auto writer = config_writer(temp_test_dir.name())
.section(metadata_cache_section())
.section(metadata_cache_routing_section(
router_port, "PRIMARY", "first-available"));
auto &default_section = writer.sections()["DEFAULT"];
init_keyring(default_section, temp_test_dir.name());
default_section["dynamic_state"] = state_file;
auto &router =
router_spawner()
.expected_exit_code(EXIT_FAILURE)
.wait_for_sync_point(ProcessManager::Spawner::SyncPoint::NONE)
.spawn({"-c", writer.write()});
// the router should close with non-0 return value
ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_FAILURE));
// proper error should get logged
EXPECT_TRUE(wait_log_file_contains(router, test_params.expected_error, 1ms));
}
INSTANTIATE_TEST_SUITE_P(
StateFileTests, StateFileAccessRightsTest,
::testing::Values(
// no read, nor write access
StateFileAccessRightsTestParams(false, false,
"Could not open dynamic state file"),
// read access, no write access
StateFileAccessRightsTestParams(true, false,
"Could not open dynamic state file")));
#endif
////////////////////////////////////////////
// Bootstrap tests
////////////////////////////////////////////
class StateFileDirectoryBootstrapTest : public StateFileTest {
void SetUp() override { StateFileTest::SetUp(); }
};
/**
* @test
* Verify that state file gets correctly created with proper access rights
* in case of directory bootstrap.
*/
TEST_F(StateFileDirectoryBootstrapTest, DirectoryBootstrapTest) {
TempDirectory temp_test_dir;
SCOPED_TRACE("// Launch our metadata server we bootsrtap against");
const auto trace_file = get_data_dir().join("bootstrap_gr.js").str();
const auto metadata_server_port = port_pool_.get_next_available();
auto &md_server = ProcessManager::launch_mysql_server_mock(
trace_file, metadata_server_port, EXIT_SUCCESS, false);
ASSERT_NO_FATAL_FAILURE(check_port_ready(md_server, metadata_server_port));
SCOPED_TRACE("// Bootstrap against our metadata server");
std::vector<std::string> router_cmdline{
"--bootstrap=localhost:" + std::to_string(metadata_server_port), "-d",
temp_test_dir.name()};
auto &router = launch_router_for_bootstrap(router_cmdline, EXIT_SUCCESS);
ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS));
// check the state file that was produced, if it contains
// what the bootstrap server has reported
const std::string state_file = temp_test_dir.name() + "/data/state.json";
check_state_file(state_file, ClusterType::GR_V1, "cluster-specific-id",
{5500, 5510, 5520}, 0, "localhost");
// check that static file has a proper reference to the dynamic file
const std::string conf_content =
get_file_output("mysqlrouter.conf", temp_test_dir.name());
const std::vector<std::string> lines =
mysql_harness::split_string(conf_content, '\n');
ASSERT_THAT(lines,
::testing::Contains(::testing::HasSubstr(
"dynamic_state=" + Path(state_file).real_path().str())));
}
/*
* These tests are executed only for STANDALONE layout and are not executed for
* Windows. Bootstrap for layouts different than STANDALONE use directories to
* which tests don't have access (see install_layout.cmake).
*/
#ifndef SKIP_BOOTSTRAP_SYSTEM_DEPLOYMENT_TESTS
class StateFileSystemBootstrapTest : public StateFileTest,
public RouterSystemLayout {
void SetUp() override {
StateFileTest::SetUp();
RouterSystemLayout::init_system_layout_dir(get_mysqlrouter_exec(),
ProcessManager::get_origin());
set_mysqlrouter_exec(Path(exec_file_));
}
void TearDown() override { RouterSystemLayout::cleanup_system_layout(); }
};
/**
* @test
* Verify that state file gets correctly created with proper access rights
* in case of system (non-directory) bootstrap.
*/
TEST_F(StateFileSystemBootstrapTest, SystemBootstrapTest) {
SCOPED_TRACE("// Launch our metadata server we bootsrtap against");
const auto trace_file = get_data_dir().join("bootstrap_gr.js").str();
const auto metadata_server_port = port_pool_.get_next_available();
auto &md_server = ProcessManager::launch_mysql_server_mock(
trace_file, metadata_server_port, EXIT_SUCCESS, false);
ASSERT_NO_FATAL_FAILURE(check_port_ready(md_server, metadata_server_port));
SCOPED_TRACE("// Bootstrap against our metadata server");
std::vector<std::string> router_cmdline{"--bootstrap=localhost:" +
std::to_string(metadata_server_port)};
auto &router = launch_router_for_bootstrap(router_cmdline, EXIT_SUCCESS);
ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS));
// check the state file that was produced, if it contains
// what the bootstrap server has reported
const std::string state_file =
RouterSystemLayout::tmp_dir_ + "/stage/var/lib/mysqlrouter/state.json";
check_state_file(state_file, ClusterType::GR_V1, "cluster-specific-id",
{5500, 5510, 5520}, 0, "localhost");
}
#endif // SKIP_BOOTSTRAP_SYSTEM_DEPLOYMENT_TESTS
int main(int argc, char *argv[]) {
init_windows_sockets();
g_origin_path = Path(argv[0]).dirname();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|