| 1 | /* Copyright (c) 2011, 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 <gtest/gtest.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #define FRIEND_OF_GTID_SET class GroupTest_Group_containers_Test |
| 28 | #define FRIEND_OF_GROUP_CACHE class GroupTest_Group_containers_Test |
| 29 | #define FRIEND_OF_GROUP_LOG_STATE class GroupTest_Group_containers_Test |
| 30 | #define NON_DISABLED_UNITTEST_GTID |
| 31 | |
| 32 | #include "binlog.h" |
| 33 | #include "my_thread.h" |
| 34 | #include "rpl_gtid.h" |
| 35 | #include "sql_class.h" |
| 36 | |
| 37 | #define N_SIDS 16 |
| 38 | |
| 39 | #define ASSERT_OK(X) ASSERT_EQ(RETURN_STATUS_OK, X) |
| 40 | #define EXPECT_OK(X) EXPECT_EQ(RETURN_STATUS_OK, X) |
| 41 | #define EXPECT_NOK(X) EXPECT_NE(RETURN_STATUS_OK, X) |
| 42 | |
| 43 | class GroupTest : public ::testing::Test { |
| 44 | public: |
| 45 | static const char *uuids[16]; |
| 46 | rpl_sid sids[16]; |
| 47 | unsigned int seed; |
| 48 | |
| 49 | void SetUp() override { |
| 50 | seed = (unsigned int)time(nullptr); |
| 51 | printf("# seed = %u\n", seed); |
| 52 | srand(seed); |
| 53 | for (int i = 0; i < 16; i++) sids[i].parse(uuids[i]); |
| 54 | |
| 55 | verbose = false; |
| 56 | errtext_stack_pos = 0; |
| 57 | errtext_stack[0] = 0; |
| 58 | append_errtext(__LINE__, "seed=%d", seed); |
| 59 | my_delete("sid-map-0", MYF(0)); |
| 60 | my_delete("sid-map-1", MYF(0)); |
| 61 | my_delete("sid-map-2", MYF(0)); |
| 62 | } |
| 63 | |
| 64 | void TearDown() { |
| 65 | my_delete("sid-map-0", MYF(0)); |
| 66 | my_delete("sid-map-1", MYF(0)); |
| 67 | my_delete("sid-map-2", MYF(0)); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | Test that different, equivalent ways to construct a Gtid_set give |
| 72 | the same resulting Gtid_set. This is used to test Gtid_set, |
| 73 | Sid_map, Group_cache, Group_log_state, and Owned_groups. |
| 74 | |
| 75 | We will generate sets of groups in *stages*. Each stage is |
| 76 | divided into a number of *sub-stages* (the number of substages is |
| 77 | taken uniformly at random from the set 1, 2, ..., 200). In each |
| 78 | sub-stage, we randomly sample one sub-group from a fixed set of |
| 79 | groups. The fixed set of groups consists of groups from 16 |
| 80 | different SIDs. For the Nth SID (1 <= N <= 16), the fixed set of |
| 81 | groups contains all GNOS from the closed interval [N, N - 1 + N * |
| 82 | N]. The stage consists of the set of groups from all the |
| 83 | sub-stages. |
| 84 | */ |
| 85 | |
| 86 | #define BEGIN_SUBSTAGE_LOOP(group_test, stage, do_errtext) \ |
| 87 | (group_test)->push_errtext(); \ |
| 88 | for (int substage_i = 0; substage_i < (stage)->n_substages; substage_i++) { \ |
| 89 | Substage &substage = (stage)->substages[substage_i]; \ |
| 90 | if (do_errtext) \ |
| 91 | (group_test) \ |
| 92 | ->append_errtext(__LINE__, "sidno=%d group=%s substage_i=%d", \ |
| 93 | substage.sidno, substage.gtid_str, substage_i); |
| 94 | #define END_SUBSTAGE_LOOP(group_test) \ |
| 95 | } \ |
| 96 | group_test->pop_errtext() |
| 97 | |
| 98 | /** |
| 99 | A substage, i.e., one of the randomly generated groups. |
| 100 | */ |
| 101 | struct Substage { |
| 102 | rpl_sidno sidno; |
| 103 | rpl_gno gno; |
| 104 | const rpl_sid *sid; |
| 105 | char sid_str[binary_log::Uuid::TEXT_LENGTH + 1]; |
| 106 | char gtid_str[binary_log::Uuid::TEXT_LENGTH + 1 + MAX_GNO_TEXT_LENGTH + 1]; |
| 107 | bool is_first, is_last, is_auto; |
| 108 | #ifndef NO_DBUG |
| 109 | void print() const { |
| 110 | printf("%d/%s [first=%d last=%d auto=%d]", sidno, gtid_str, is_first, |
| 111 | is_last, is_auto); |
| 112 | } |
| 113 | #endif |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | A stage, i.e., the sequence of randomly generated groups. |
| 118 | */ |
| 119 | struct Stage { |
| 120 | class GroupTest *group_test; |
| 121 | Sid_map *sid_map; |
| 122 | |
| 123 | // List of groups added in the present stage. |
| 124 | static const int MAX_SUBSTAGES = 200; |
| 125 | Substage substages[MAX_SUBSTAGES]; |
| 126 | int n_substages; |
| 127 | |
| 128 | // Set of groups added in the present stage. |
| 129 | Gtid_set set; |
| 130 | int str_len; |
| 131 | char *str; |
| 132 | |
| 133 | // The subset of groups that can be added as automatic groups. |
| 134 | Gtid_set automatic_groups; |
| 135 | // The subset of groups that cannot be added as automatic groups. |
| 136 | Gtid_set non_automatic_groups; |
| 137 | |
| 138 | Stage(class GroupTest *gt, Sid_map *sm) |
| 139 | : group_test(gt), |
| 140 | sid_map(sm), |
| 141 | set(sm), |
| 142 | str_len(0), |
| 143 | str(nullptr), |
| 144 | automatic_groups(sm), |
| 145 | non_automatic_groups(sm) { |
| 146 | init(sm); |
| 147 | } |
| 148 | |
| 149 | void init(Sid_map *sm) { |
| 150 | rpl_sidno max_sidno = sm->get_max_sidno(); |
| 151 | ASSERT_OK(set.ensure_sidno(max_sidno)); |
| 152 | ASSERT_OK(automatic_groups.ensure_sidno(max_sidno)); |
| 153 | ASSERT_OK(non_automatic_groups.ensure_sidno(max_sidno)); |
| 154 | } |
| 155 | |
| 156 | ~Stage() { free(str); } |
| 157 | |
| 158 | void print() const { |
| 159 | printf("%d substages = {\n", n_substages); |
| 160 | for (int i = 0; i < n_substages; i++) { |
| 161 | printf(" substage[%d]: ", i); |
| 162 | substages[i].print(); |
| 163 | printf("\n"); |
| 164 | } |
| 165 | printf("\n"); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | Generate the the random groups that constitute a stage. |
| 170 | |
| 171 | @param done_groups The set of all groups added in previous |
| 172 | stages. |
| 173 | @param other_sm Sid_map to which groups should be added. |
| 174 | */ |
| 175 | void new_stage(const Gtid_set *done_groups, Sid_map *other_sm) { |
| 176 | set.clear(); |
| 177 | automatic_groups.clear(); |
| 178 | non_automatic_groups.clear(); |
| 179 | |
| 180 | n_substages = 1 + (rand() % MAX_SUBSTAGES); |
| 181 | BEGIN_SUBSTAGE_LOOP(group_test, this, false) { |
| 182 | // generate random GTID |
| 183 | substage.sidno = 1 + (rand() % N_SIDS); |
| 184 | substage.gno = 1 + (rand() % (substage.sidno * substage.sidno)); |
| 185 | // compute alternative forms |
| 186 | substage.sid = sid_map->sidno_to_sid(substage.sidno); |
| 187 | ASSERT_NE((rpl_sid *)nullptr, substage.sid) << group_test->errtext; |
| 188 | substage.sid->to_string(substage.sid_str); |
| 189 | substage.sid->to_string(substage.gtid_str); |
| 190 | substage.gtid_str[rpl_sid.TEXT_LENGTH] = ':'; |
| 191 | format_gno(substage.gtid_str + rpl_sid.TEXT_LENGTH + 1, substage.gno); |
| 192 | |
| 193 | ASSERT_LE(1, other_sm->add_permanent(substage.sid)) |
| 194 | << group_test->errtext; |
| 195 | |
| 196 | // check if this group could be added as an 'automatic' group |
| 197 | Gtid_set::Const_interval_iterator ivit(done_groups, substage.sidno); |
| 198 | const Gtid_set::Interval *iv = ivit.get(); |
| 199 | substage.is_auto = |
| 200 | !set.contains_group(substage.sidno, substage.gno) && |
| 201 | ((iv == nullptr || iv->start > 1) ? substage.gno == 1 |
| 202 | : substage.gno == iv->end); |
| 203 | |
| 204 | // check if this sub-group is the first in its group in this |
| 205 | // stage, and add it to the set |
| 206 | substage.is_first = !set.contains_group(substage.sidno, substage.gno); |
| 207 | if (substage.is_first) ASSERT_OK(set.add(substage.gtid_str)); |
| 208 | } |
| 209 | END_SUBSTAGE_LOOP(group_test); |
| 210 | |
| 211 | // Iterate backwards so that we can detect when a subgroup is |
| 212 | // the last subgroup of its group. |
| 213 | set.clear(); |
| 214 | for (int substage_i = n_substages - 1; substage_i >= 0; substage_i--) { |
| 215 | Substage &substage = substages[substage_i]; |
| 216 | substage.is_last = !set.contains_group(substage.sidno, substage.gno); |
| 217 | if (substage.is_last) ASSERT_OK(set.add(substage.gtid_str)); |
| 218 | } |
| 219 | |
| 220 | str_len = set.get_string_length(); |
| 221 | str = (char *)realloc(str, str_len + 1); |
| 222 | set.to_string(str); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | /* |
| 227 | We maintain a text that contains the state of the test. We print |
| 228 | this text when a test assertion fails. The text is updated each |
| 229 | iteration of each loop, so that we can easier track the exact |
| 230 | point in time when an error occurs. Since loops may be nested, we |
| 231 | maintain a stack of offsets in the error text: before a new loop |
| 232 | is entered, the position of the end of the string is pushed to the |
| 233 | stack and the text appended in each iteration is added to that |
| 234 | position. |
| 235 | */ |
| 236 | char errtext[1000]; |
| 237 | int errtext_stack[100]; |
| 238 | int errtext_stack_pos; |
| 239 | bool verbose; |
| 240 | |
| 241 | void append_errtext(int line, const char *fmt, ...) |
| 242 | MY_ATTRIBUTE((format(printf, 3, 4))) { |
| 243 | va_list argp; |
| 244 | va_start(argp, fmt); |
| 245 | vsprintf(errtext + errtext_stack[errtext_stack_pos], fmt, argp); |
| 246 | if (verbose) printf("@line %d: %s\n", line, errtext); |
| 247 | va_end(argp); |
| 248 | } |
| 249 | |
| 250 | void push_errtext() { |
| 251 | int old_len = errtext_stack[errtext_stack_pos]; |
| 252 | int len = old_len + strlen(errtext + old_len); |
| 253 | strcpy(errtext + len, " | "); |
| 254 | errtext_stack[++errtext_stack_pos] = len + 3; |
| 255 | } |
| 256 | |
| 257 | void pop_errtext() { errtext[errtext_stack[errtext_stack_pos--] - 3] = 0; } |
| 258 | |
| 259 | void group_subset(Gtid_set *sub, Gtid_set *super, bool outcome, int line, |
| 260 | const char *desc) { |
| 261 | append_errtext(line, "%s", desc); |
| 262 | // check using is_subset |
| 263 | EXPECT_EQ(outcome, sub->is_subset(super)) << errtext; |
| 264 | // check using set subtraction |
| 265 | enum_return_status status; |
| 266 | Gtid_set sub_minus_super(sub, &status); |
| 267 | ASSERT_OK(status) << errtext; |
| 268 | ASSERT_OK(sub_minus_super.remove(super)) << errtext; |
| 269 | ASSERT_EQ(outcome, sub_minus_super.is_empty()) << errtext; |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | const char *GroupTest::uuids[16] = { |
| 274 | "00000000-0000-0000-0000-000000000000", |
| 275 | "11111111-1111-1111-1111-111111111111", |
| 276 | "22222222-2222-2222-2222-222222222222", |
| 277 | "33333333-3333-3333-3333-333333333333", |
| 278 | "44444444-4444-4444-4444-444444444444", |
| 279 | "55555555-5555-5555-5555-555555555555", |
| 280 | "66666666-6666-6666-6666-666666666666", |
| 281 | "77777777-7777-7777-7777-777777777777", |
| 282 | "88888888-8888-8888-8888-888888888888", |
| 283 | "99999999-9999-9999-9999-999999999999", |
| 284 | "aaaaAAAA-aaaa-AAAA-aaaa-aAaAaAaAaaaa", |
| 285 | "bbbbBBBB-bbbb-BBBB-bbbb-bBbBbBbBbbbb", |
| 286 | "ccccCCCC-cccc-CCCC-cccc-cCcCcCcCcccc", |
| 287 | "ddddDDDD-dddd-DDDD-dddd-dDdDdDdDdddd", |
| 288 | "eeeeEEEE-eeee-EEEE-eeee-eEeEeEeEeeee", |
| 289 | "ffffFFFF-ffff-FFFF-ffff-fFfFfFfFffff", |
| 290 | }; |
| 291 | |
| 292 | TEST_F(GroupTest, Uuid) { |
| 293 | Uuid u; |
| 294 | char buf[100]; |
| 295 | |
| 296 | // check that we get back the same UUID after parse + print |
| 297 | for (int i = 0; i < N_SIDS; i++) { |
| 298 | EXPECT_OK(u.parse(uuids[i])) << "i=" << i; |
| 299 | u.to_string(buf); |
| 300 | EXPECT_STRCASEEQ(uuids[i], buf) << "i=" << i; |
| 301 | } |
| 302 | // check error cases |
| 303 | EXPECT_OK(u.parse("ffffFFFF-ffff-FFFF-ffff-ffffffffFFFFf")); |
| 304 | EXPECT_NOK(u.parse("ffffFFFF-ffff-FFFF-ffff-ffffffffFFFg")); |
| 305 | EXPECT_NOK(u.parse("ffffFFFF-ffff-FFFF-ffff-ffffffffFFF")); |
| 306 | EXPECT_NOK(u.parse("ffffFFFF-ffff-FFFF-fff-fffffffffFFFF")); |
| 307 | EXPECT_NOK(u.parse("ffffFFFF-ffff-FFFF-ffff-ffffffffFFF-")); |
| 308 | EXPECT_NOK(u.parse(" ffffFFFF-ffff-FFFF-ffff-ffffffffFFFF")); |
| 309 | EXPECT_NOK(u.parse("ffffFFFFfffff-FFFF-ffff-ffffffffFFFF")); |
| 310 | } |
| 311 | |
| 312 | TEST_F(GroupTest, Sid_map) { |
| 313 | Checkable_rwlock lock; |
| 314 | Sid_map sm(&lock); |
| 315 | |
| 316 | lock.rdlock(); |
| 317 | ASSERT_OK(sm.open("sid-map-0")); |
| 318 | |
| 319 | // Add a random SID until we have N_SID SIDs in the map. |
| 320 | while (sm.get_max_sidno() < N_SIDS) |
| 321 | ASSERT_LE(1, sm.add_permanent(&sids[rand() % N_SIDS])) << errtext; |
| 322 | |
| 323 | // Check that all N_SID SIDs are in the map, and that |
| 324 | // get_sorted_sidno() has the correct order. This implies that no |
| 325 | // SID was added twice. |
| 326 | for (int i = 0; i < N_SIDS; i++) { |
| 327 | rpl_sidno sidno = sm.get_sorted_sidno(i); |
| 328 | const rpl_sid *sid; |
| 329 | char buf[100]; |
| 330 | EXPECT_NE((rpl_sid *)nullptr, sid = sm.sidno_to_sid(sidno)) << errtext; |
| 331 | const int max_len = binary_log::Uuid::TEXT_LENGTH; |
| 332 | EXPECT_EQ(max_len, sid->to_string(buf)) << errtext; |
| 333 | EXPECT_STRCASEEQ(uuids[i], buf) << errtext; |
| 334 | EXPECT_EQ(sidno, sm.sid_to_sidno(sid)) << errtext; |
| 335 | } |
| 336 | lock.unlock(); |
| 337 | lock.assert_no_lock(); |
| 338 | } |
| 339 | |
| 340 | TEST_F(GroupTest, Group_containers) { |
| 341 | /* |
| 342 | In this test, we maintain 298 Gtid_sets. We add groups to these |
| 343 | Gtid_sets in stages, as described above. We add the groups to |
| 344 | each of the 298 Gtid_sets in different ways, as described below. |
| 345 | At the end of each stage, we check that all the 298 resulting |
| 346 | Gtid_sets are mutually equal. |
| 347 | |
| 348 | We add groups in the two ways: |
| 349 | |
| 350 | A. Test Gtid_sets and Sid_maps. We vary two parameters: |
| 351 | |
| 352 | Parameter 1: vary the way that groups are added: |
| 353 | 0. Add one group at a time, using add(sidno, gno). |
| 354 | 1. Add one group at a time, using add(text). |
| 355 | 2. Add all new groups at once, using add(gs_new). |
| 356 | 3. add all new groups at once, using add(gs_new.to_string()). |
| 357 | 4. Maintain a string that contains the concatenation of all |
| 358 | gs_new.to_string(). in each stage, we set gs[4] to a new |
| 359 | Gtid_set created from this string. |
| 360 | |
| 361 | Parameter 2: vary the Sid_map object: |
| 362 | 0. Use a Sid_map that has all the SIDs in order. |
| 363 | 1. Use a Sid_map where SIDs are added in the order they appear. |
| 364 | |
| 365 | We vary these parameters in all combinations; thus we construct |
| 366 | 10 Gtid_sets. |
| 367 | */ |
| 368 | enum enum_sets_method { |
| 369 | METHOD_SIDNO_GNO = 0, |
| 370 | METHOD_GROUP_TEXT, |
| 371 | METHOD_GTID_SET, |
| 372 | METHOD_GTID_SET_TEXT, |
| 373 | METHOD_ALL_TEXTS_CONCATENATED, |
| 374 | MAX_METHOD |
| 375 | }; |
| 376 | enum enum_sets_sid_map { SID_MAP_0 = 0, SID_MAP_1, MAX_SID_MAP }; |
| 377 | const int N_COMBINATIONS_SETS = MAX_METHOD * MAX_SID_MAP; |
| 378 | /* |
| 379 | B. Test Group_cache, Group_log_state, and Owned_groups. All |
| 380 | sub-groups for the stage are added to the Group_cache, the |
| 381 | Group_cache is flushed to the Group_log_state, and the |
| 382 | Gtid_set is extracted from the Group_log_state. We vary the |
| 383 | following parameters. |
| 384 | |
| 385 | Parameter 1: type of statement: |
| 386 | 0. Transactional replayed statement: add all groups to the |
| 387 | transaction group cache (which is flushed to a |
| 388 | Group_log_state at the end of the stage). Set |
| 389 | GTID_NEXT_LIST to the list of all groups in the stage. |
| 390 | 1. Non-transactional replayed statement: add all groups to the |
| 391 | stmt group cache (which is flushed to the Group_log_state |
| 392 | at the end of each sub-stage). Set GTID_NEXT_LIST = NULL. |
| 393 | 2. Randomize: for each sub-stage, choose 0 or 1 with 50% |
| 394 | chance. Set GTID_NEXT_LIST to the list of all groups in |
| 395 | the stage. |
| 396 | 3. Automatic groups: add all groups to the stmt group cache, |
| 397 | but make the group automatic if possible, i.e., if the SID |
| 398 | and GNO are unlogged and there is no smaller unlogged GNO |
| 399 | for this SID. Set GTID_NEXT_LIST = NULL. |
| 400 | |
| 401 | Parameter 2: ended or non-ended sub-groups: |
| 402 | 0. All sub-groups are unended (except automatic sub-groups). |
| 403 | 1. For each group, the last sub-group of the group in the |
| 404 | stage is ended. Don't add groups that are already ended in the |
| 405 | Group_log_state. |
| 406 | 2. For each group in the stage, choose 0 or 1 with 50% chance. |
| 407 | |
| 408 | Parameter 3: empty or normal sub-group: |
| 409 | 0. Generate only normal (and possibly automatic) sub-groups. |
| 410 | 1. Generate only empty (and possibly automatic) sub-groups. |
| 411 | 2. Generate only empty (and possibly automatic) sub-groups. |
| 412 | Add the sub-groups implicitly: do not call |
| 413 | add_empty_subgroup(); instead rely on |
| 414 | gtid_before_flush_trx_cache() to add empty subgroups. |
| 415 | 3. Choose 0 or 1 with 33% chance. |
| 416 | |
| 417 | Parameter 4: insert anonymous sub-groups or not: |
| 418 | 0. Do not generate anonymous sub-groups. |
| 419 | 1. Generate an anomous sub-group before each sub-group with |
| 420 | 50% chance and an anonymous group after each sub-group with |
| 421 | 50% chance. |
| 422 | |
| 423 | We vary these parameters in all combinations; thus we construct |
| 424 | 4*3*4*2=96 Gtid_sets. |
| 425 | */ |
| 426 | enum enum_caches_type { |
| 427 | TYPE_TRX = 0, |
| 428 | TYPE_NONTRX, |
| 429 | TYPE_RANDOMIZE, |
| 430 | TYPE_AUTO, |
| 431 | MAX_TYPE |
| 432 | }; |
| 433 | enum enum_caches_end { END_OFF = 0, END_ON, END_RANDOMIZE, MAX_END }; |
| 434 | enum enum_caches_empty { |
| 435 | EMPTY_OFF = 0, |
| 436 | EMPTY_ON, |
| 437 | EMPTY_IMPLICIT, |
| 438 | EMPTY_RANDOMIZE, |
| 439 | MAX_EMPTY |
| 440 | }; |
| 441 | enum enum_caches_anon { ANON_OFF = 0, ANON_ON, MAX_ANON }; |
| 442 | const int N_COMBINATIONS_CACHES = MAX_TYPE * MAX_END * MAX_EMPTY * MAX_ANON; |
| 443 | const int N_COMBINATIONS = N_COMBINATIONS_SETS + N_COMBINATIONS_CACHES; |
| 444 | |
| 445 | // Auxiliary macros to loop through all combinations of parameters. |
| 446 | #define BEGIN_LOOP_A \ |
| 447 | push_errtext(); \ |
| 448 | for (int method_i = 0, combination_i = 0; method_i < MAX_METHOD; \ |
| 449 | method_i++) { \ |
| 450 | for (int sid_map_i = 0; sid_map_i < MAX_SID_MAP; \ |
| 451 | sid_map_i++, combination_i++) { \ |
| 452 | Gtid_set >id_set [[maybe_unused]] = \ |
| 453 | containers[combination_i]->gtid_set; \ |
| 454 | Sid_map *&sid_map [[maybe_unused]] = sid_maps[sid_map_i]; \ |
| 455 | append_errtext(__LINE__, "sid_map_i=%d method_i=%d combination_i=%d", \ |
| 456 | sid_map_i, method_i, combination_i); |
| 457 | |
| 458 | #define END_LOOP_A \ |
| 459 | } \ |
| 460 | } \ |
| 461 | pop_errtext() |
| 462 | |
| 463 | #define BEGIN_LOOP_B \ |
| 464 | push_errtext(); \ |
| 465 | for (int type_i = 0, combination_i = N_COMBINATIONS_SETS; type_i < MAX_TYPE; \ |
| 466 | type_i++) { \ |
| 467 | for (int end_i = 0; end_i < MAX_END; end_i++) { \ |
| 468 | for (int empty_i = 0; empty_i < MAX_EMPTY; empty_i++) { \ |
| 469 | for (int anon_i = 0; anon_i < MAX_ANON; anon_i++, combination_i++) { \ |
| 470 | Gtid_set >id_set [[maybe_unused]] = \ |
| 471 | containers[combination_i]->gtid_set; \ |
| 472 | Group_cache &stmt_cache [[maybe_unused]] = \ |
| 473 | containers[combination_i]->stmt_cache; \ |
| 474 | Group_cache &trx_cache [[maybe_unused]] = \ |
| 475 | containers[combination_i]->trx_cache; \ |
| 476 | Group_log_state &group_log_state [[maybe_unused]] = \ |
| 477 | containers[combination_i]->group_log_state; \ |
| 478 | append_errtext(__LINE__, \ |
| 479 | "type_i=%d end_i=%d empty_i=%d " \ |
| 480 | "anon_i=%d combination_i=%d", \ |
| 481 | type_i, end_i, empty_i, anon_i, combination_i); \ |
| 482 | // verbose= (combination_i == 108); /*todo*/ |
| 483 | |
| 484 | #define END_LOOP_B \ |
| 485 | } \ |
| 486 | } \ |
| 487 | } \ |
| 488 | } \ |
| 489 | pop_errtext() |
| 490 | |
| 491 | // Do not generate warnings (because that causes segfault when done |
| 492 | // from a unittest). |
| 493 | global_system_variables.log_error_verbosity = 1; |
| 494 | |
| 495 | mysql_bin_log.server_uuid_sidno = 1; |
| 496 | |
| 497 | // Create Sid_maps. |
| 498 | Checkable_rwlock &lock = mysql_bin_log.sid_lock; |
| 499 | Sid_map **sid_maps = new Sid_map *[2]; |
| 500 | sid_maps[0] = &mysql_bin_log.sid_map; |
| 501 | sid_maps[1] = new Sid_map(&lock); |
| 502 | |
| 503 | lock.rdlock(); |
| 504 | ASSERT_OK(sid_maps[0]->open("sid-map-1")); |
| 505 | ASSERT_OK(sid_maps[1]->open("sid-map-2")); |
| 506 | /* |
| 507 | Make sid_maps[0] and sid_maps[1] different: sid_maps[0] is |
| 508 | generated in order; sid_maps[1] is generated in the order that |
| 509 | SIDS are inserted in the Gtid_set. |
| 510 | */ |
| 511 | for (int i = 0; i < N_SIDS; i++) |
| 512 | ASSERT_LE(1, sid_maps[0]->add_permanent(&sids[i])) << errtext; |
| 513 | |
| 514 | // Create list of container objects. These are the objects that we |
| 515 | // test. |
| 516 | struct Containers { |
| 517 | Gtid_set gtid_set; |
| 518 | Group_cache stmt_cache; |
| 519 | Group_cache trx_cache; |
| 520 | Group_log_state group_log_state; |
| 521 | Containers(Checkable_rwlock *lock, Sid_map *sm) |
| 522 | : gtid_set(sm), group_log_state(lock, sm) { |
| 523 | init(); |
| 524 | } |
| 525 | void init() { ASSERT_OK(group_log_state.ensure_sidno()); }; |
| 526 | }; |
| 527 | Containers **containers = new Containers *[N_COMBINATIONS]; |
| 528 | BEGIN_LOOP_A { containers[combination_i] = new Containers(&lock, sid_map); } |
| 529 | END_LOOP_A; |
| 530 | BEGIN_LOOP_B { |
| 531 | containers[combination_i] = new Containers(&lock, sid_maps[0]); |
| 532 | } |
| 533 | END_LOOP_B; |
| 534 | |
| 535 | /* |
| 536 | Construct a Gtid_set that contains the set of all groups from |
| 537 | which we sample. |
| 538 | */ |
| 539 | static char all_groups_str[100 * 100]; |
| 540 | char *s = all_groups_str; |
| 541 | s += sprintf(s, "%s:1", uuids[0]); |
| 542 | for (rpl_sidno sidno = 2; sidno <= N_SIDS; sidno++) |
| 543 | s += sprintf(s, ",\n%s:1-%d", uuids[sidno - 1], sidno * sidno); |
| 544 | enum_return_status status; |
| 545 | Gtid_set all_groups(sid_maps[0], all_groups_str, &status); |
| 546 | ASSERT_OK(status) << errtext; |
| 547 | |
| 548 | // The set of groups that were added in some previous stage. |
| 549 | Gtid_set done_groups(sid_maps[0]); |
| 550 | ASSERT_OK(done_groups.ensure_sidno(sid_maps[0]->get_max_sidno())); |
| 551 | |
| 552 | /* |
| 553 | Iterate through stages. In each stage, create the "stage group |
| 554 | set" by generating up to 200 subgroups. Add this stage group set |
| 555 | to each of the group sets in different ways. Stop when the union |
| 556 | of all stage group sets is equal to the full set from which we |
| 557 | took the samples. |
| 558 | */ |
| 559 | char *done_str = nullptr; |
| 560 | int done_str_len = 0; |
| 561 | Stage stage(this, sid_maps[0]); |
| 562 | int stage_i = 0; |
| 563 | |
| 564 | /* |
| 565 | We need a THD object only to read THD::variables.gtid_next, |
| 566 | THD::variables.gtid_end, THD::variables.gtid_next_list, |
| 567 | THD::thread_id, THD::server_status. We don't want to invoke the |
| 568 | THD constructor because that would require setting up mutexes, |
| 569 | etc. Hence we use malloc instead of new. |
| 570 | */ |
| 571 | THD *thd = (THD *)malloc(sizeof(THD)); |
| 572 | ASSERT_NE((THD *)nullptr, thd) << errtext; |
| 573 | Gtid_specification *gtid_next = &thd->variables.gtid_next; |
| 574 | thd->set_new_thread_id(); |
| 575 | gtid_next->type = Gtid_specification::AUTOMATIC; |
| 576 | bool >id_end = thd->variables.gtid_end; |
| 577 | bool >id_commit = thd->variables.gtid_commit; |
| 578 | thd->server_status = 0; |
| 579 | thd->system_thread = NON_SYSTEM_THREAD; |
| 580 | thd->variables.gtid_next_list.gtid_set = &stage.set; |
| 581 | |
| 582 | push_errtext(); |
| 583 | while (!all_groups.equals(&done_groups)) { |
| 584 | stage_i++; |
| 585 | append_errtext(__LINE__, "stage_i=%d", stage_i); |
| 586 | stage.new_stage(&done_groups, sid_maps[1]); |
| 587 | |
| 588 | if (verbose) { |
| 589 | printf("======== stage %d ========\n", stage_i); |
| 590 | stage.print(); |
| 591 | } |
| 592 | |
| 593 | // Create a string that contains all previous stage.str, |
| 594 | // concatenated. |
| 595 | done_str = (char *)realloc(done_str, done_str_len + 1 + stage.str_len + 1); |
| 596 | ASSERT_NE((char *)nullptr, done_str) << errtext; |
| 597 | done_str_len += sprintf(done_str + done_str_len, ",%s", stage.str); |
| 598 | |
| 599 | // Add groups to Gtid_sets. |
| 600 | BEGIN_LOOP_A { |
| 601 | switch (method_i) { |
| 602 | case METHOD_SIDNO_GNO: |
| 603 | BEGIN_SUBSTAGE_LOOP(this, &stage, true) { |
| 604 | rpl_sidno sidno_1 = sid_map->sid_to_sidno(substage.sid); |
| 605 | ASSERT_LE(1, sidno_1) << errtext; |
| 606 | ASSERT_OK(gtid_set.ensure_sidno(sidno_1)); |
| 607 | ASSERT_OK(gtid_set._add(sidno_1, substage.gno)); |
| 608 | } |
| 609 | END_SUBSTAGE_LOOP(this); |
| 610 | break; |
| 611 | case METHOD_GROUP_TEXT: |
| 612 | BEGIN_SUBSTAGE_LOOP(this, &stage, true) { |
| 613 | ASSERT_OK(gtid_set.add(substage.gtid_str)); |
| 614 | } |
| 615 | END_SUBSTAGE_LOOP(this); |
| 616 | break; |
| 617 | case METHOD_GTID_SET: |
| 618 | ASSERT_OK(gtid_set.add(&stage.set)) << errtext; |
| 619 | break; |
| 620 | case METHOD_GTID_SET_TEXT: |
| 621 | ASSERT_OK(gtid_set.add(stage.str)) << errtext; |
| 622 | break; |
| 623 | case METHOD_ALL_TEXTS_CONCATENATED: |
| 624 | gtid_set.clear(); |
| 625 | ASSERT_OK(gtid_set.add(done_str)) << errtext; |
| 626 | case MAX_METHOD: |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | END_LOOP_A; |
| 631 | |
| 632 | // Add groups to Group_caches. |
| 633 | BEGIN_LOOP_B { |
| 634 | if (verbose) { |
| 635 | printf("======== stage=%d combination=%d ========\n", stage_i, |
| 636 | combination_i); |
| 637 | #ifndef NDEBUG |
| 638 | printf("group log state:\n"); |
| 639 | group_log_state.print(); |
| 640 | printf("trx cache:\n"); |
| 641 | trx_cache.print(sid_maps[0]); |
| 642 | printf("stmt cache:\n"); |
| 643 | stmt_cache.print(sid_maps[0]); |
| 644 | #endif // ifdef NDEBUG |
| 645 | } |
| 646 | |
| 647 | Gtid_set ended_groups(sid_maps[0]); |
| 648 | bool trx_contains_logged_subgroup = false; |
| 649 | bool stmt_contains_logged_subgroup = false; |
| 650 | BEGIN_SUBSTAGE_LOOP(this, &stage, true) { |
| 651 | int type_j; |
| 652 | if (type_i == TYPE_RANDOMIZE) |
| 653 | type_j = rand() % 2; |
| 654 | else if (type_i == TYPE_AUTO && !substage.is_auto) |
| 655 | type_j = TYPE_NONTRX; |
| 656 | else |
| 657 | type_j = type_i; |
| 658 | int end_j; |
| 659 | if (substage.is_first && |
| 660 | ((end_i == END_RANDOMIZE && (rand() % 2)) || end_i == END_ON)) { |
| 661 | ASSERT_OK(ended_groups.ensure_sidno(substage.sidno)); |
| 662 | ASSERT_OK(ended_groups._add(substage.sidno, substage.gno)); |
| 663 | } |
| 664 | end_j = substage.is_last && |
| 665 | ended_groups.contains_group(substage.sidno, substage.gno); |
| 666 | |
| 667 | /* |
| 668 | In EMPTY_RANDOMIZE mode, we have to determine once *per |
| 669 | group* (not substage) if we use EMPTY_END or not. So we |
| 670 | determine this for the first subgroup of the group, and then |
| 671 | we memoize which groups use EMPTY_END using the Gtid_set |
| 672 | empty_end. |
| 673 | */ |
| 674 | int empty_j; |
| 675 | if (empty_i == EMPTY_RANDOMIZE) |
| 676 | empty_j = rand() % 3; |
| 677 | else |
| 678 | empty_j = empty_i; |
| 679 | int anon_j1, anon_j2; |
| 680 | if (type_j != TYPE_TRX || anon_i == ANON_OFF) |
| 681 | anon_j1 = anon_j2 = ANON_OFF; |
| 682 | else { |
| 683 | anon_j1 = rand() % 2; |
| 684 | anon_j2 = rand() % 2; |
| 685 | } |
| 686 | if (verbose) |
| 687 | printf("type_j=%d end_j=%d empty_j=%d anon_j1=%d anon_j2=%d\n", |
| 688 | type_j, end_j, empty_j, anon_j1, anon_j2); |
| 689 | |
| 690 | thd->variables.gtid_next_list.is_non_null = |
| 691 | (type_i == TYPE_NONTRX || type_i == TYPE_AUTO) ? 0 : 1; |
| 692 | gtid_commit = (substage_i == stage.n_substages - 1) || |
| 693 | !thd->variables.gtid_next_list.is_non_null; |
| 694 | |
| 695 | if (type_j == TYPE_AUTO) { |
| 696 | gtid_next->type = Gtid_specification::AUTOMATIC; |
| 697 | gtid_next->group.sidno = substage.sidno; |
| 698 | gtid_next->group.gno = 0; |
| 699 | gtid_end = false; |
| 700 | lock.unlock(); |
| 701 | lock.assert_no_lock(); |
| 702 | gtid_before_statement(thd, &lock, &group_log_state, &stmt_cache, |
| 703 | &trx_cache); |
| 704 | lock.rdlock(); |
| 705 | stmt_cache.add_logged_subgroup(thd, 20 + rand() % 100 /*binlog_len*/); |
| 706 | stmt_contains_logged_subgroup = true; |
| 707 | } else { |
| 708 | Group_cache &cache = type_j == TYPE_TRX ? trx_cache : stmt_cache; |
| 709 | |
| 710 | if (anon_j1) { |
| 711 | gtid_next->type = Gtid_specification::ANONYMOUS; |
| 712 | gtid_next->group.sidno = 0; |
| 713 | gtid_next->group.gno = 0; |
| 714 | gtid_end = false; |
| 715 | lock.unlock(); |
| 716 | lock.assert_no_lock(); |
| 717 | gtid_before_statement(thd, &lock, &group_log_state, &stmt_cache, |
| 718 | &trx_cache); |
| 719 | lock.rdlock(); |
| 720 | cache.add_logged_subgroup(thd, 20 + rand() % 100 /*binlog_len*/); |
| 721 | trx_contains_logged_subgroup = true; |
| 722 | } |
| 723 | |
| 724 | gtid_next->type = Gtid_specification::GTID; |
| 725 | gtid_next->group.sidno = substage.sidno; |
| 726 | gtid_next->group.gno = substage.gno; |
| 727 | gtid_end = (end_j == END_ON) ? true : false; |
| 728 | lock.unlock(); |
| 729 | lock.assert_no_lock(); |
| 730 | gtid_before_statement(thd, &lock, &group_log_state, &stmt_cache, |
| 731 | &trx_cache); |
| 732 | lock.rdlock(); |
| 733 | if (!group_log_state.is_ended(substage.sidno, substage.gno)) { |
| 734 | switch (empty_j) { |
| 735 | case EMPTY_OFF: |
| 736 | cache.add_logged_subgroup(thd, |
| 737 | 20 + rand() % 100 /*binlog_len*/); |
| 738 | if (type_j == TYPE_TRX) |
| 739 | trx_contains_logged_subgroup = true; |
| 740 | else |
| 741 | stmt_contains_logged_subgroup = true; |
| 742 | break; |
| 743 | case EMPTY_ON: |
| 744 | cache.add_empty_subgroup(substage.sidno, substage.gno, |
| 745 | end_j ? true : false); |
| 746 | break; |
| 747 | case EMPTY_IMPLICIT: |
| 748 | break; // do nothing |
| 749 | default: |
| 750 | assert(0); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (anon_j2) { |
| 755 | gtid_next->type = Gtid_specification::ANONYMOUS; |
| 756 | gtid_next->group.sidno = 0; |
| 757 | gtid_next->group.gno = 0; |
| 758 | gtid_end = false; |
| 759 | lock.unlock(); |
| 760 | lock.assert_no_lock(); |
| 761 | gtid_before_statement(thd, &lock, &group_log_state, &stmt_cache, |
| 762 | &trx_cache); |
| 763 | lock.rdlock(); |
| 764 | cache.add_logged_subgroup(thd, 20 + rand() % 100 /*binlog_len*/); |
| 765 | trx_contains_logged_subgroup = true; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | #ifndef NDEBUG |
| 770 | if (verbose) { |
| 771 | printf("stmt_cache:\n"); |
| 772 | stmt_cache.print(sid_maps[0]); |
| 773 | } |
| 774 | #endif // ifndef NDEBUG |
| 775 | if (!stmt_cache.is_empty()) |
| 776 | gtid_flush_group_cache( |
| 777 | thd, &lock, &group_log_state, nullptr /*group log*/, &stmt_cache, |
| 778 | &trx_cache, 1 /*binlog_no*/, 1 /*binlog_pos*/, |
| 779 | stmt_contains_logged_subgroup ? 20 + rand() % 99 : -1 |
| 780 | /*offset_after_last_statement*/); |
| 781 | stmt_contains_logged_subgroup = false; |
| 782 | gtid_before_flush_trx_cache(thd, &lock, &group_log_state, &trx_cache); |
| 783 | if (gtid_commit) { |
| 784 | // simulate gtid_after_flush_trx_cache() but don't |
| 785 | // execute a COMMIT statement |
| 786 | thd->variables.gtid_has_ongoing_super_group = 0; |
| 787 | |
| 788 | #ifndef NDEBUG |
| 789 | if (verbose) { |
| 790 | printf("trx_cache:\n"); |
| 791 | trx_cache.print(sid_maps[0]); |
| 792 | printf( |
| 793 | "trx_cache.is_empty=%d n_subgroups=%d " |
| 794 | "trx_contains_logged_subgroup=%d\n", |
| 795 | trx_cache.is_empty(), trx_cache.get_n_subgroups(), |
| 796 | trx_contains_logged_subgroup); |
| 797 | } |
| 798 | #endif // ifndef NDEBUG |
| 799 | |
| 800 | if (!trx_cache.is_empty()) |
| 801 | gtid_flush_group_cache( |
| 802 | thd, &lock, &group_log_state, nullptr /*group log*/, &trx_cache, |
| 803 | &trx_cache, 1 /*binlog_no*/, 1 /*binlog_pos*/, |
| 804 | trx_contains_logged_subgroup ? 20 + rand() % 99 : -1 |
| 805 | /*offset_after_last_statement*/); |
| 806 | trx_contains_logged_subgroup = false; |
| 807 | } |
| 808 | } |
| 809 | END_SUBSTAGE_LOOP(this); |
| 810 | |
| 811 | gtid_set.clear(); |
| 812 | ASSERT_OK(group_log_state.owned_groups.get_partial_groups(>id_set)); |
| 813 | ASSERT_OK(gtid_set.add(&group_log_state.ended_groups)); |
| 814 | } |
| 815 | END_LOOP_B; |
| 816 | |
| 817 | // add stage.set to done_groups |
| 818 | Gtid_set old_done_groups(&done_groups, &status); |
| 819 | ASSERT_OK(status); |
| 820 | ASSERT_OK(done_groups.add(&stage.set)); |
| 821 | |
| 822 | // check the Gtid_set::remove and Gtid_set::is_subset functions |
| 823 | Gtid_set diff(&done_groups, &status); |
| 824 | ASSERT_OK(status); |
| 825 | ASSERT_OK(diff.remove(&old_done_groups)); |
| 826 | Gtid_set not_new(&stage.set, &status); |
| 827 | ASSERT_OK(status); |
| 828 | ASSERT_OK(not_new.remove(&diff)); |
| 829 | |
| 830 | #define GROUP_SUBSET(gs1, gs2, outcome) \ |
| 831 | group_subset(&gs1, &gs2, outcome, __LINE__, #gs1 " <= " #gs2); |
| 832 | push_errtext(); |
| 833 | GROUP_SUBSET(not_new, not_new, true); |
| 834 | GROUP_SUBSET(not_new, diff, not_new.is_empty()); |
| 835 | GROUP_SUBSET(not_new, stage.set, true); |
| 836 | GROUP_SUBSET(not_new, done_groups, true); |
| 837 | GROUP_SUBSET(not_new, old_done_groups, true); |
| 838 | |
| 839 | GROUP_SUBSET(diff, not_new, diff.is_empty()); |
| 840 | GROUP_SUBSET(diff, diff, true); |
| 841 | GROUP_SUBSET(diff, stage.set, true); |
| 842 | GROUP_SUBSET(diff, done_groups, true); |
| 843 | GROUP_SUBSET(diff, old_done_groups, diff.is_empty()); |
| 844 | |
| 845 | GROUP_SUBSET(stage.set, not_new, diff.is_empty()); |
| 846 | GROUP_SUBSET(stage.set, diff, not_new.is_empty()); |
| 847 | GROUP_SUBSET(stage.set, stage.set, true); |
| 848 | GROUP_SUBSET(stage.set, done_groups, true); |
| 849 | GROUP_SUBSET(stage.set, old_done_groups, diff.is_empty()); |
| 850 | |
| 851 | // GROUP_SUBSET(done_groups, not_new, ???); |
| 852 | GROUP_SUBSET(done_groups, diff, old_done_groups.is_empty()); |
| 853 | GROUP_SUBSET(done_groups, stage.set, done_groups.equals(&stage.set)); |
| 854 | GROUP_SUBSET(done_groups, done_groups, true); |
| 855 | GROUP_SUBSET(done_groups, old_done_groups, diff.is_empty()); |
| 856 | |
| 857 | GROUP_SUBSET(old_done_groups, not_new, old_done_groups.equals(¬_new)); |
| 858 | GROUP_SUBSET(old_done_groups, diff, old_done_groups.is_empty()); |
| 859 | // GROUP_SUBSET(old_done_groups, stage.set, ???); |
| 860 | GROUP_SUBSET(old_done_groups, done_groups, true); |
| 861 | GROUP_SUBSET(old_done_groups, old_done_groups, true); |
| 862 | pop_errtext(); |
| 863 | |
| 864 | /* |
| 865 | Verify that all group sets are equal. We test both a.equals(b) |
| 866 | and b.equals(a) and a.equals(a), because we want to verify that |
| 867 | Gtid_set::equals is correct too. We compare both the sets |
| 868 | using Gtid_set::equals, and the output of to_string() using |
| 869 | EXPECT_STREQ. |
| 870 | */ |
| 871 | BEGIN_LOOP_A { |
| 872 | char *buf1 = new char[gtid_set.get_string_length() + 1]; |
| 873 | gtid_set.to_string(buf1); |
| 874 | for (int i = 0; i < N_COMBINATIONS_SETS; i++) { |
| 875 | Gtid_set >id_set_2 = containers[i]->gtid_set; |
| 876 | if (combination_i < i) { |
| 877 | char *buf2 = new char[gtid_set_2.get_string_length() + 1]; |
| 878 | gtid_set_2.to_string(buf2); |
| 879 | EXPECT_STREQ(buf1, buf2) << errtext << " i=" << i; |
| 880 | delete buf2; |
| 881 | } |
| 882 | EXPECT_EQ(true, gtid_set.equals(>id_set_2)) << errtext << " i=" << i; |
| 883 | } |
| 884 | delete buf1; |
| 885 | } |
| 886 | END_LOOP_A; |
| 887 | BEGIN_LOOP_B { |
| 888 | EXPECT_EQ(true, containers[combination_i]->gtid_set.equals(&done_groups)) |
| 889 | << errtext; |
| 890 | } |
| 891 | END_LOOP_B; |
| 892 | } |
| 893 | pop_errtext(); |
| 894 | |
| 895 | // Finally, verify that the string representations of |
| 896 | // done_groups is as expected |
| 897 | static char buf[100 * 100]; |
| 898 | done_groups.to_string(buf); |
| 899 | EXPECT_STRCASEEQ(all_groups_str, buf) << errtext; |
| 900 | lock.unlock(); |
| 901 | lock.assert_no_lock(); |
| 902 | |
| 903 | // Clean up. |
| 904 | free(done_str); |
| 905 | for (int i = 0; i < N_COMBINATIONS; i++) delete containers[i]; |
| 906 | delete containers; |
| 907 | delete sid_maps[1]; |
| 908 | delete sid_maps; |
| 909 | free(thd); |
| 910 | |
| 911 | mysql_bin_log.sid_lock.assert_no_lock(); |
| 912 | } |