| 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 "gcs_base_test.h" |
| 25 | |
| 26 | #include <array> |
| 27 | #include <cstring> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | #include "app_data.h" |
| 31 | #include "get_synode_app_data.h" |
| 32 | #include "pax_msg.h" |
| 33 | #include "xcom/site_struct.h" |
| 34 | #include "xcom_base.h" |
| 35 | #include "xcom_cache.h" |
| 36 | #include "xcom_memory.h" |
| 37 | #include "xcom_transport.h" |
| 38 | |
| 39 | extern void recompute_node_set(node_set const *old_set, |
| 40 | node_list const *old_nodes, node_set *new_set, |
| 41 | node_list const *new_nodes); |
| 42 | |
| 43 | extern void recompute_timestamps(detector_state const old_timestamp, |
| 44 | node_list const *old_nodes, |
| 45 | detector_state new_timestamp, |
| 46 | node_list const *new_nodes); |
| 47 | |
| 48 | extern void analyze_leaders(site_def *site); |
| 49 | extern int is_active_leader(node_no x, site_def *site); |
| 50 | extern node_no found_active_leaders(site_def *site); |
| 51 | extern bool unsafe_leaders(app_data *a); |
| 52 | extern bool_t handle_set_leaders(app_data *a); |
| 53 | extern bool_t handle_leaders(app_data *a); |
| 54 | |
| 55 | namespace xcom_base_unittest { |
| 56 | |
| 57 | uint32_t constexpr const test_group_id{0xbaadcafe}; |
| 58 | |
| 59 | class XcomSingleWriter : public GcsBaseTest { |
| 60 | protected: |
| 61 | XcomSingleWriter() {} |
| 62 | ~XcomSingleWriter() override {} |
| 63 | }; |
| 64 | |
| 65 | auto xprintf = [](std::ostream &os, auto... args) { |
| 66 | int dummy[] = {(os << args, 0)...}; |
| 67 | (void)dummy; |
| 68 | }; |
| 69 | |
| 70 | TEST_F(XcomSingleWriter, test_xcom_client_set_max_leaders) { |
| 71 | std::array<uint32_t, 4> numbers = {std::numeric_limits<uint32_t>::min(), 0, 1, |
| 72 | std::numeric_limits<uint32_t>::max()}; |
| 73 | for (auto n : numbers) { // Should always return 0 because of nullptr |
| 74 | ASSERT_EQ(0, xcom_client_set_max_leaders(nullptr, n, test_group_id)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | TEST_F(XcomSingleWriter, test_xcom_client_set_leaders) { |
| 79 | std::array<char const *, 5> a = {nullptr, "hostname", "xxfunnyxx.nohost.foo", |
| 80 | "localhost", "127.0.0.1"}; |
| 81 | for (auto name : a) { // Should always return 0 because of nullptr |
| 82 | ASSERT_EQ(0, xcom_client_set_leaders(nullptr, 1, &name, test_group_id)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | struct test_node_address : public node_address { |
| 87 | test_node_address(char const *s) { |
| 88 | address = strdup(s); |
| 89 | uuid = {}; |
| 90 | proto = {}; |
| 91 | services = 0; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | TEST_F(XcomSingleWriter, test_recompute_node_set) { |
| 96 | node_set old_set{}; |
| 97 | node_set new_set{}; |
| 98 | test_node_address node_a{"node_a:12345"}; |
| 99 | test_node_address node_b{"node_b:67890"}; |
| 100 | test_node_address list_1[]{node_a, node_b}; |
| 101 | test_node_address list_2[]{node_b, node_a}; |
| 102 | node_list old_nodes{2, list_1}; |
| 103 | node_list new_nodes{2, list_2}; |
| 104 | bool failed{false}; |
| 105 | |
| 106 | // Deallocate on scope exit |
| 107 | auto cleanup{[&](auto p) { |
| 108 | (void)p; |
| 109 | xdr_free((xdrproc_t)xdr_node_address, (char *)&node_a); |
| 110 | xdr_free((xdrproc_t)xdr_node_address, (char *)&node_b); |
| 111 | xdr_free((xdrproc_t)xdr_node_set, (char *)&old_set); |
| 112 | xdr_free((xdrproc_t)xdr_node_set, (char *)&new_set); |
| 113 | }}; |
| 114 | std::unique_ptr<node_set, decltype(cleanup)> guard(&old_set, cleanup); |
| 115 | |
| 116 | // Note failure and print message |
| 117 | auto fail{[&failed](auto... args) { |
| 118 | xprintf(std::cerr, "test_recompute_node_set FAILED: ", args..., "\n"); |
| 119 | failed = true; |
| 120 | ASSERT_FALSE(failed); |
| 121 | }}; |
| 122 | |
| 123 | // Two nodes, one alive,and one not |
| 124 | init_node_set(&old_set, 2); |
| 125 | init_node_set(&new_set, 2); |
| 126 | add_node(old_set, 0); // Node 0 (node_a) is alive |
| 127 | |
| 128 | auto assert_set{[&](u_int i) { |
| 129 | if (!is_set(new_set, i)) |
| 130 | fail("new_set[", i, "] should be set after ::recompute_node_set"); |
| 131 | }}; |
| 132 | |
| 133 | auto assert_not_set{[&](u_int i) { |
| 134 | if (is_set(new_set, i)) |
| 135 | fail("new_set[", i, "] should not be set after ::recompute_node_set"); |
| 136 | }}; |
| 137 | |
| 138 | // Recompute node set after having swapped position of a and b in node list |
| 139 | ::recompute_node_set(&old_set, &old_nodes, &new_set, &new_nodes); |
| 140 | assert_not_set(0); |
| 141 | assert_set(1); |
| 142 | |
| 143 | // Recompute node set after having removed node a |
| 144 | new_nodes.node_list_len = 1; |
| 145 | new_set.node_set_len = 1; |
| 146 | ::recompute_node_set(&old_set, &old_nodes, &new_set, &new_nodes); |
| 147 | assert_not_set(0); |
| 148 | |
| 149 | // Recompute node set after having removed node b |
| 150 | new_nodes.node_list_val[0] = node_a; |
| 151 | ::recompute_node_set(&old_set, &old_nodes, &new_set, &new_nodes); |
| 152 | assert_set(0); |
| 153 | } |
| 154 | |
| 155 | TEST_F(XcomSingleWriter, test_recompute_timestamps) { |
| 156 | detector_state const old_ts{1.0, 2.0}; |
| 157 | detector_state new_ts{}; |
| 158 | test_node_address node_a{"node_a:12345"}; |
| 159 | test_node_address node_b{"node_b:67890"}; |
| 160 | node_address list_1[]{node_a, node_b}; |
| 161 | node_address list_2[]{node_b, node_a}; |
| 162 | node_list old_nodes{2, list_1}; |
| 163 | node_list new_nodes{2, list_2}; |
| 164 | bool failed{false}; |
| 165 | |
| 166 | // Deallocate on scope exit |
| 167 | auto cleanup{[&](auto p) { |
| 168 | (void)p; |
| 169 | xdr_free((xdrproc_t)xdr_node_address, (char *)&node_a); |
| 170 | xdr_free((xdrproc_t)xdr_node_address, (char *)&node_b); |
| 171 | }}; |
| 172 | std::unique_ptr<node_address, decltype(cleanup)> guard(&node_a, cleanup); |
| 173 | |
| 174 | // Note failure and print message |
| 175 | auto fail{[&failed](auto... args) { |
| 176 | xprintf(std::cerr, "test_recompute_timestamps FAILED: ", args..., "\n"); |
| 177 | failed = true; |
| 178 | ASSERT_FALSE(failed); |
| 179 | }}; |
| 180 | |
| 181 | auto assert_ts{[&](u_int i, double ts) { |
| 182 | if (!(new_ts[i] == ts)) |
| 183 | fail("new_ts[", i, "] should be ", ts, " after recompute_timestamps"); |
| 184 | }}; |
| 185 | |
| 186 | // Recompute timestamps after having swapped position of a and b in node list |
| 187 | ::recompute_timestamps(old_ts, &old_nodes, new_ts, &new_nodes); |
| 188 | assert_ts(0, 2.0); |
| 189 | assert_ts(1, 1.0); |
| 190 | |
| 191 | // Recompute node ts after having removed node a |
| 192 | new_nodes.node_list_len = 1; |
| 193 | ::recompute_timestamps(old_ts, &old_nodes, new_ts, &new_nodes); |
| 194 | assert_ts(0, 2.0); |
| 195 | |
| 196 | // Recompute node ts after having removed node b |
| 197 | new_nodes.node_list_val[0] = node_a; |
| 198 | ::recompute_timestamps(old_ts, &old_nodes, new_ts, &new_nodes); |
| 199 | assert_ts(0, 1.0); |
| 200 | } |
| 201 | |
| 202 | /* Simple multiplicative hash */ |
| 203 | static uint32_t mhash(unsigned char const *buf, size_t length) { |
| 204 | size_t i = 0; |
| 205 | uint32_t sum = 0; |
| 206 | for (i = 0; i < length; i++) { |
| 207 | sum += 0x811c9dc5 * (uint32_t)buf[i]; |
| 208 | } |
| 209 | return sum; |
| 210 | } |
| 211 | |
| 212 | static blob uuid_blob(char const *arg) { |
| 213 | blob uuid_tmp; |
| 214 | unsigned int hash = mhash((unsigned char const *)arg, strlen(arg)); |
| 215 | G_MESSAGE("hash %x", hash); |
| 216 | uuid_tmp.data.data_len = sizeof(hash); |
| 217 | uuid_tmp.data.data_val = (char *)calloc(1, uuid_tmp.data.data_len); |
| 218 | memcpy(uuid_tmp.data.data_val, &hash, uuid_tmp.data.data_len); |
| 219 | return uuid_tmp; |
| 220 | } |
| 221 | |
| 222 | /* Initialize address list from command line */ |
| 223 | static void init_me(node_list *nl, char const *arg) { |
| 224 | /* Synthesize (hopefully) unique identifier and add to address list. */ |
| 225 | /* The uuid may be anything, it is not used by xcom */ |
| 226 | u_int j = 0; |
| 227 | blob uuid_tmp = uuid_blob(arg); |
| 228 | nl->node_list_len = 1; |
| 229 | nl->node_list_val = new_node_address_uuid(nl->node_list_len, &arg, &uuid_tmp); |
| 230 | GET_GOUT; |
| 231 | ADD_GOUT("init_me uuid"); |
| 232 | for (j = 0; j < uuid_tmp.data.data_len; j++) { |
| 233 | ADD_F_GOUT(" %x", *((unsigned char *)&uuid_tmp.data.data_val[j])); |
| 234 | } |
| 235 | ADD_GOUT(" services"); |
| 236 | ADD_F_GOUT(" %x", nl->node_list_val->services); |
| 237 | PRINT_GOUT; |
| 238 | FREE_GOUT; |
| 239 | free(uuid_tmp.data.data_val); |
| 240 | } |
| 241 | |
| 242 | TEST_F(XcomSingleWriter, test_analyze_leaders) { |
| 243 | bool failed{false}; |
| 244 | char const *node0{"iamthegreatest:12345"}; |
| 245 | char const *node1{"node1:12346"}; |
| 246 | blob uuid{uuid_blob(node1)}; |
| 247 | site_def site; |
| 248 | |
| 249 | // Deallocate on scope exit |
| 250 | auto cleanup{[&](auto p) { |
| 251 | uuid.data.data_len = 0; |
| 252 | free(uuid.data.data_val); |
| 253 | free_site_def_body(p); |
| 254 | }}; |
| 255 | std::unique_ptr<site_def, decltype(cleanup)> guard(&site, cleanup); |
| 256 | |
| 257 | // Note failure and print message |
| 258 | auto fail{[&failed](auto... args) { |
| 259 | xprintf(std::cerr, "test_analyze_leaders FAILED: ", args..., "\n"); |
| 260 | failed = true; |
| 261 | ASSERT_FALSE(failed); |
| 262 | }}; |
| 263 | |
| 264 | auto assert_cached_leaders{[&]() { |
| 265 | if (!site.cached_leaders) |
| 266 | fail("site.cached_leaders should be set if max_active_leaders == 1"); |
| 267 | }}; |
| 268 | |
| 269 | auto assert_no_cached_leaders{[&]() { |
| 270 | if (site.cached_leaders) |
| 271 | fail( |
| 272 | "site.cached_leaders should not be set if max_active_leaders == " |
| 273 | "active_leaders_all"); |
| 274 | }}; |
| 275 | |
| 276 | auto assert_leader{[&](node_no n, char const *why) { |
| 277 | if (!site.active_leader[n]) |
| 278 | fail("site.active_leader[", n, "] should be set if ", why); |
| 279 | if (!::is_active_leader(n, &site)) |
| 280 | fail("::is_active_leader(", n, ") should return 1 if ", why); |
| 281 | }}; |
| 282 | |
| 283 | auto assert_not_leader{[&](node_no n, char const *why) { |
| 284 | if (site.active_leader[n]) |
| 285 | fail("site.active_leader[", n, "] should not be set if ", why); |
| 286 | if (::is_active_leader(n, &site)) |
| 287 | fail("::is_active_leader(", n, ") should return 0 if ", why); |
| 288 | }}; |
| 289 | |
| 290 | auto assert_found_leaders{[&](node_no n) { |
| 291 | if (::found_active_leaders(&site) != n) |
| 292 | fail("::found_active_leaders(site) should return ", n); |
| 293 | }}; |
| 294 | |
| 295 | // Completely empty config with all nodes as leaders |
| 296 | site.max_active_leaders = 0; |
| 297 | ::analyze_leaders(&site); |
| 298 | assert_no_cached_leaders(); |
| 299 | assert_found_leaders(0); |
| 300 | |
| 301 | // Single writer, but no nodes in global node set |
| 302 | site.nodeno = 0; // I am node 0 |
| 303 | site.max_active_leaders = 1; |
| 304 | init_me(&site.nodes, node0); |
| 305 | init_node_set(&site.global_node_set, 1); |
| 306 | |
| 307 | ::analyze_leaders(&site); |
| 308 | assert_cached_leaders(); |
| 309 | // Node 0 wil be leader if node set empty |
| 310 | assert_leader(0, "no nodes in global node set"); |
| 311 | assert_found_leaders(1); |
| 312 | |
| 313 | // Single writer, all nodes in global node set |
| 314 | site.cached_leaders = 0; |
| 315 | set_node_set(&site.global_node_set); |
| 316 | |
| 317 | ::analyze_leaders(&site); |
| 318 | assert_cached_leaders(); |
| 319 | assert_leader(0, "all nodes in global node set"); |
| 320 | assert_found_leaders(1); |
| 321 | |
| 322 | // Add second node as leader |
| 323 | node_address *addr = new_node_address_uuid(1, &node1, &uuid); |
| 324 | add_site_def(1, addr, &site); |
| 325 | delete_node_address(1, addr); |
| 326 | site.leaders = alloc_leader_array(1); |
| 327 | site.leaders.leader_array_val[0].address = strdup(node1); |
| 328 | |
| 329 | site.cached_leaders = 0; |
| 330 | reset_node_set(&site.global_node_set); // Mark all as down |
| 331 | |
| 332 | ::analyze_leaders(&site); |
| 333 | assert_cached_leaders(); |
| 334 | assert_not_leader(1, "all are down"); |
| 335 | assert_leader(0, "all are down"); |
| 336 | assert_found_leaders(1); |
| 337 | |
| 338 | site.cached_leaders = 0; |
| 339 | set_node_set(&site.global_node_set); // Mark all as present |
| 340 | |
| 341 | ::analyze_leaders(&site); |
| 342 | assert_cached_leaders(); |
| 343 | assert_not_leader(0, "node1 is leader"); |
| 344 | assert_leader(1, "node1 is leader"); |
| 345 | assert_found_leaders(1); |
| 346 | |
| 347 | // Remove leader from global node set |
| 348 | site.global_node_set.node_set_val[1] = 0; |
| 349 | site.cached_leaders = 0; |
| 350 | |
| 351 | ::analyze_leaders(&site); |
| 352 | assert_cached_leaders(); |
| 353 | assert_leader(0, "node1 is down"); |
| 354 | assert_not_leader(1, "node1 is down"); |
| 355 | assert_found_leaders(1); |
| 356 | } |
| 357 | |
| 358 | TEST_F(XcomSingleWriter, test_unsafe_leaders) { |
| 359 | bool failed = false; |
| 360 | char const *node0 = "iamthegreatest:12345"; |
| 361 | app_data a; |
| 362 | memset(&a, 0, sizeof a); |
| 363 | site_def *site = new_site_def(); |
| 364 | char const *node1{"node1:12346"}; |
| 365 | blob uuid{uuid_blob(node1)}; |
| 366 | |
| 367 | // Deallocate on scope exit |
| 368 | auto cleanup{[&](auto p) { |
| 369 | (void)p; |
| 370 | uuid.data.data_len = 0; |
| 371 | free(uuid.data.data_val); |
| 372 | free_site_defs(); |
| 373 | xdr_free((xdrproc_t)xdr_app_data, (char *)&a); |
| 374 | }}; |
| 375 | std::unique_ptr<site_def, decltype(cleanup)> guard(site, cleanup); |
| 376 | |
| 377 | // Note failure and print message |
| 378 | auto fail{[&failed](auto... args) { |
| 379 | xprintf(std::cerr, "test_unsafe_leaders FAILED: ", args..., "\n"); |
| 380 | failed = true; |
| 381 | ASSERT_FALSE(failed); |
| 382 | }}; |
| 383 | |
| 384 | // Null app_data |
| 385 | a.body.c_t = add_node_type; |
| 386 | |
| 387 | if (::unsafe_leaders(&a)) fail("empty app_data should be all right"); |
| 388 | |
| 389 | // Single node, compatible protocol version and max_leaders == all |
| 390 | node_list &nl = a.body.app_u_u.nodes; |
| 391 | init_me(&nl, node0); |
| 392 | push_site_def(site); |
| 393 | if (::unsafe_leaders(&a)) |
| 394 | fail("Compatible protocol version and max_active_leaders == all"); |
| 395 | |
| 396 | // Single node, compatible protocol version and max_active_leaders == 1 |
| 397 | site->max_active_leaders = 1; |
| 398 | if (::unsafe_leaders(&a)) |
| 399 | fail("Compatible protocol version and max_active_leaders == 1"); |
| 400 | |
| 401 | // Single node, incompatible protocol version and max_active_leaders == all |
| 402 | site->max_active_leaders = active_leaders_all; |
| 403 | nl.node_list_val[0].proto.max_proto = x_1_8; |
| 404 | if (::unsafe_leaders(&a)) |
| 405 | fail("Incompatible protocol version and max_active_leaders == all"); |
| 406 | |
| 407 | // Single node, incompatible protocol version and max_active_leaders == 1 |
| 408 | site->max_active_leaders = 1; |
| 409 | if (!::unsafe_leaders(&a)) |
| 410 | fail("Incompatible protocol version and max_active_leaders == 1"); |
| 411 | |
| 412 | // Add second node with compatible protocol version |
| 413 | node_address *addr = new_node_address_uuid(1, &node1, &uuid); |
| 414 | add_site_def(1, addr, site); |
| 415 | delete_node_address(1, addr); |
| 416 | if (!::unsafe_leaders(&a)) |
| 417 | fail( |
| 418 | "Two nodes, 1 incompatible protocol version and max_active_leaders == " |
| 419 | "1"); |
| 420 | |
| 421 | // Switch to max_active_leaders == all |
| 422 | site->max_active_leaders = active_leaders_all; |
| 423 | if (::unsafe_leaders(&a)) |
| 424 | fail( |
| 425 | "Two nodes, 1 incompatible protocol version and max_active_leaders == " |
| 426 | "all"); |
| 427 | |
| 428 | // Make both protocol versions compatible |
| 429 | nl.node_list_val[0].proto.max_proto = x_1_9; |
| 430 | if (::unsafe_leaders(&a)) |
| 431 | fail( |
| 432 | "Two nodes, compatible protocol versions and max_active_leaders == " |
| 433 | "all"); |
| 434 | |
| 435 | // Make both protocol versions compatible |
| 436 | site->max_active_leaders = 1; |
| 437 | if (::unsafe_leaders(&a)) |
| 438 | fail("Two nodes, compatible protocol versions and max_active_leaders == 1"); |
| 439 | } |
| 440 | |
| 441 | TEST_F(XcomSingleWriter, test_handle_max_leaders) { |
| 442 | bool failed = false; |
| 443 | char const *node0 = "iamthegreatest:12345"; |
| 444 | app_data a; |
| 445 | memset(&a, 0, sizeof a); |
| 446 | site_def *site = new_site_def(); |
| 447 | char const *node1{"node1:12346"}; |
| 448 | blob uuid{uuid_blob(node1)}; |
| 449 | |
| 450 | // Deallocate on scope exit |
| 451 | auto cleanup{[&](auto p) { |
| 452 | (void)p; |
| 453 | uuid.data.data_len = 0; |
| 454 | free(uuid.data.data_val); |
| 455 | free_site_defs(); |
| 456 | xdr_free((xdrproc_t)xdr_app_data, (char *)&a); |
| 457 | }}; |
| 458 | std::unique_ptr<site_def, decltype(cleanup)> guard(site, cleanup); |
| 459 | |
| 460 | // Note failure and print message |
| 461 | auto fail{[&failed](auto... args) { |
| 462 | xprintf(std::cerr, "test_handle_max_leaders FAILED: ", args..., "\n"); |
| 463 | failed = true; |
| 464 | ASSERT_FALSE(failed); |
| 465 | }}; |
| 466 | |
| 467 | // Single node, compatible protocol version and max_leaders == all |
| 468 | init_me(&site->nodes, node0); |
| 469 | alloc_node_set(&site->global_node_set, 1); |
| 470 | alloc_node_set(&site->local_node_set, 1); |
| 471 | site->nodeno = 0; |
| 472 | site->event_horizon = EVENT_HORIZON_MIN; |
| 473 | site_install_action(site, unified_boot_type); |
| 474 | init_set_max_leaders(test_group_id, &a, active_leaders_all); |
| 475 | if (!handle_max_leaders(&a)) |
| 476 | fail("Compatible protocol version and new max_leaders == all"); |
| 477 | |
| 478 | // Single node, compatible protocol version and max_leaders == 1 |
| 479 | a.body.app_u_u.max_leaders = 1; |
| 480 | if (!handle_max_leaders(&a)) |
| 481 | fail("Compatible protocol version and new max_leaders == 1"); |
| 482 | |
| 483 | init_set_max_leaders(test_group_id, &a, 2); |
| 484 | if (handle_max_leaders(&a)) |
| 485 | fail("Compatible protocol version and new max_leaders == 2"); |
| 486 | |
| 487 | // Add second node with incompatible protocol version, max_leaders == all |
| 488 | site = clone_site_def(get_site_def()); |
| 489 | node_address *addr = new_node_address_uuid(1, &node1, &uuid); |
| 490 | addr->proto.max_proto = x_1_8; |
| 491 | add_site_def(1, addr, site); |
| 492 | site_install_action(site, add_node_type); |
| 493 | delete_node_address(1, addr); |
| 494 | init_set_max_leaders(test_group_id, &a, active_leaders_all); |
| 495 | if (!handle_max_leaders(&a)) |
| 496 | fail("Incompatible protocol version and max_leaders == all"); |
| 497 | |
| 498 | // Two nodes, incompatible protocol version and max_leaders == 1 |
| 499 | init_set_max_leaders(test_group_id, &a, 1); |
| 500 | if (handle_max_leaders(&a)) |
| 501 | fail("Incompatible protocol version and max_leaders == 1"); |
| 502 | |
| 503 | // Two nodes, incompatible protocol version and max_leaders == 2 |
| 504 | init_set_max_leaders(test_group_id, &a, 2); |
| 505 | if (handle_max_leaders(&a)) |
| 506 | fail("Incompatible protocol version and max_leaders == 2"); |
| 507 | |
| 508 | // Two nodes, incompatible protocol version and max_leaders == 3 |
| 509 | init_set_max_leaders(test_group_id, &a, 3); |
| 510 | if (handle_max_leaders(&a)) |
| 511 | fail("Incompatible protocol version and max_leaders == 3"); |
| 512 | } |
| 513 | |
| 514 | TEST_F(XcomSingleWriter, test_handle_set_leaders) { |
| 515 | bool failed = false; |
| 516 | char const *nodes[]{"iamthegreatest:12345", "node1:12346"}; |
| 517 | app_data a; |
| 518 | memset(&a, 0, sizeof a); |
| 519 | site_def *site = new_site_def(); |
| 520 | blob uuid{uuid_blob(nodes[1])}; |
| 521 | |
| 522 | auto free_app{[&]() { xdr_free((xdrproc_t)xdr_app_data, (char *)&a); }}; |
| 523 | |
| 524 | // Deallocate on scope exit |
| 525 | auto cleanup{[&](auto p) { |
| 526 | (void)p; |
| 527 | uuid.data.data_len = 0; |
| 528 | free(uuid.data.data_val); |
| 529 | free_site_defs(); |
| 530 | free_app(); |
| 531 | }}; |
| 532 | std::unique_ptr<site_def, decltype(cleanup)> guard(site, cleanup); |
| 533 | |
| 534 | // Note failure and print message |
| 535 | auto fail{[&](auto... args) { |
| 536 | xprintf(std::cerr, "test_handle_set_leaders FAILED: ", args..., "\n"); |
| 537 | failed = true; |
| 538 | ASSERT_FALSE(failed); |
| 539 | }}; |
| 540 | |
| 541 | init_me(&site->nodes, nodes[0]); |
| 542 | alloc_node_set(&site->global_node_set, 1); |
| 543 | alloc_node_set(&site->local_node_set, 1); |
| 544 | site->nodeno = 0; |
| 545 | site->event_horizon = EVENT_HORIZON_MIN; |
| 546 | site_install_action(site, unified_boot_type); |
| 547 | |
| 548 | // Single node, compatible protocol version and leaders node0 |
| 549 | init_set_leaders(test_group_id, &a, 1, nodes); |
| 550 | if (!handle_set_leaders(&a)) |
| 551 | fail("Compatible protocol version and leaders node0"); |
| 552 | free_app(); |
| 553 | |
| 554 | // Compatible protocol version and leaders == node0, node1 |
| 555 | init_set_leaders(test_group_id, &a, 2, nodes); |
| 556 | if (!handle_set_leaders(&a)) |
| 557 | fail("Compatible protocol version and leaders node0, node1"); |
| 558 | free_app(); |
| 559 | |
| 560 | // Add second node with incompatible protocol version |
| 561 | site = clone_site_def(get_site_def()); |
| 562 | node_address *addr = new_node_address_uuid(1, &nodes[1], &uuid); |
| 563 | addr->proto.max_proto = x_1_8; |
| 564 | add_site_def(1, addr, site); |
| 565 | site_install_action(site, add_node_type); |
| 566 | delete_node_address(1, addr); |
| 567 | |
| 568 | init_set_leaders(test_group_id, &a, 1, nodes); |
| 569 | if (handle_set_leaders(&a)) |
| 570 | fail("Both compatible and incompatible protocol versions"); |
| 571 | free_app(); |
| 572 | |
| 573 | init_set_leaders(test_group_id, &a, 2, nodes); |
| 574 | if (handle_set_leaders(&a)) |
| 575 | fail("Both compatible and incompatible protocol versions"); |
| 576 | free_app(); |
| 577 | |
| 578 | // Two nodes, incompatible protocol version and leaders == 3 |
| 579 | a.body.app_u_u.max_leaders = 3; |
| 580 | if (handle_set_leaders(&a)) |
| 581 | fail("Incompatible protocol version and leaders == 3"); |
| 582 | } |
| 583 | |
| 584 | TEST_F(XcomSingleWriter, test_handle_leaders) { |
| 585 | bool failed = false; |
| 586 | char const *nodes[]{"iamthegreatest:12345", "node1:12346"}; |
| 587 | app_data leader_app; |
| 588 | memset(&leader_app, 0, sizeof leader_app); |
| 589 | app_data max_app; |
| 590 | memset(&max_app, 0, sizeof max_app); |
| 591 | site_def *site = nullptr; |
| 592 | blob uuid{uuid_blob(nodes[1])}; |
| 593 | |
| 594 | auto free_app{[&]() { |
| 595 | // leader_app and max_app have been linked, so unlink |
| 596 | // to avoid deallocating the stack objects. |
| 597 | leader_app.next = nullptr; |
| 598 | max_app.next = nullptr; |
| 599 | xdr_free((xdrproc_t)xdr_app_data, (char *)&leader_app); |
| 600 | xdr_free((xdrproc_t)xdr_app_data, (char *)&max_app); |
| 601 | }}; |
| 602 | |
| 603 | // Deallocate on scope exit |
| 604 | auto cleanup{[&](auto p) { |
| 605 | (void)p; |
| 606 | uuid.data.data_len = 0; |
| 607 | free(uuid.data.data_val); |
| 608 | free_site_defs(); |
| 609 | free_app(); |
| 610 | }}; |
| 611 | |
| 612 | // Note failure and print message |
| 613 | auto fail{[&](auto... args) { |
| 614 | xprintf(std::cerr, "test_handle_leaders FAILED: ", args..., "\n"); |
| 615 | failed = true; |
| 616 | ASSERT_FALSE(failed); |
| 617 | }}; |
| 618 | |
| 619 | site = new_site_def(); |
| 620 | std::unique_ptr<site_def, decltype(cleanup)> guard(site, cleanup); |
| 621 | |
| 622 | init_me(&site->nodes, nodes[0]); |
| 623 | alloc_node_set(&site->global_node_set, 1); |
| 624 | alloc_node_set(&site->local_node_set, 1); |
| 625 | site->nodeno = 0; |
| 626 | site->event_horizon = EVENT_HORIZON_MIN; |
| 627 | site_install_action(site, unified_boot_type); |
| 628 | |
| 629 | // Single node, compatible protocol version and leaders node0 |
| 630 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 1); |
| 631 | if (!::handle_leaders(&leader_app)) |
| 632 | fail("Compatible protocol version and leaders node0, max_leaders == 1"); |
| 633 | free_app(); |
| 634 | |
| 635 | // Compatible protocol version and leaders == node0, node1 |
| 636 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 1); |
| 637 | if (!::handle_leaders(&leader_app)) |
| 638 | fail( |
| 639 | "Compatible protocol version and leaders node0, node1, max_leaders == " |
| 640 | "1"); |
| 641 | free_app(); |
| 642 | |
| 643 | // Single node, compatible protocol version and leaders node0 |
| 644 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 2); |
| 645 | if (::handle_leaders(&leader_app)) |
| 646 | fail("Compatible protocol version and leaders node0, max_leaders == 2"); |
| 647 | free_app(); |
| 648 | |
| 649 | // Compatible protocol version and leaders == node0, node1 |
| 650 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 2); |
| 651 | if (::handle_leaders(&leader_app)) |
| 652 | fail( |
| 653 | "Compatible protocol version and leaders node0, node1, max_leaders == " |
| 654 | "2"); |
| 655 | free_app(); |
| 656 | |
| 657 | // Single node, compatible protocol version and leaders node0 |
| 658 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 3); |
| 659 | if (::handle_leaders(&leader_app)) |
| 660 | fail("Compatible protocol version and leaders node0, max_leaders == 3"); |
| 661 | free_app(); |
| 662 | |
| 663 | // Compatible protocol version and leaders == node0, node1 |
| 664 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 3); |
| 665 | if (::handle_leaders(&leader_app)) |
| 666 | fail( |
| 667 | "Compatible protocol version and leaders node0, node1, max_leaders == " |
| 668 | "3"); |
| 669 | free_app(); |
| 670 | |
| 671 | // Add second node with incompatible protocol version |
| 672 | site = clone_site_def(get_site_def()); |
| 673 | node_address *addr = new_node_address_uuid(1, &nodes[1], &uuid); |
| 674 | addr->proto.max_proto = x_1_8; |
| 675 | add_site_def(1, addr, site); |
| 676 | site_install_action(site, add_node_type); |
| 677 | delete_node_address(1, addr); |
| 678 | |
| 679 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 1); |
| 680 | if (::handle_leaders(&leader_app)) |
| 681 | fail( |
| 682 | "Both compatible and incompatible protocol versions, max_leaders == 1"); |
| 683 | free_app(); |
| 684 | |
| 685 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 1); |
| 686 | if (::handle_leaders(&leader_app)) |
| 687 | fail( |
| 688 | "Both compatible and incompatible protocol versions, max_leaders == 1"); |
| 689 | free_app(); |
| 690 | |
| 691 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 2); |
| 692 | if (::handle_leaders(&leader_app)) |
| 693 | fail( |
| 694 | "Both compatible and incompatible protocol versions, max_leaders == 2"); |
| 695 | free_app(); |
| 696 | |
| 697 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 2); |
| 698 | if (::handle_leaders(&leader_app)) |
| 699 | fail( |
| 700 | "Both compatible and incompatible protocol versions, max_leaders == 2"); |
| 701 | free_app(); |
| 702 | |
| 703 | init_set_leaders(test_group_id, &leader_app, 1, nodes, &max_app, 3); |
| 704 | if (::handle_leaders(&leader_app)) |
| 705 | fail( |
| 706 | "Both compatible and incompatible protocol versions, max_leaders == 3"); |
| 707 | free_app(); |
| 708 | |
| 709 | init_set_leaders(test_group_id, &leader_app, 2, nodes, &max_app, 3); |
| 710 | if (::handle_leaders(&leader_app)) |
| 711 | fail( |
| 712 | "Both compatible and incompatible protocol versions, max_leaders == 3"); |
| 713 | free_app(); |
| 714 | } |
| 715 | |
| 716 | } // namespace xcom_base_unittest |