| 1 | /* Copyright (c) 2017, 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 "univ.i" |
| 25 | |
| 26 | #include <atomic> |
| 27 | #include <iostream> |
| 28 | #include <map> |
| 29 | #include <string> |
| 30 | #include <thread> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include <gtest/gtest.h> |
| 34 | |
| 35 | #include "clone0api.h" /* clone_init(), clone_free() */ |
| 36 | #include "fil0fil.h" |
| 37 | #include "log0buf.h" |
| 38 | #include "log0chkp.h" |
| 39 | #include "log0recv.h" |
| 40 | #include "log0test.h" |
| 41 | #include "log0write.h" |
| 42 | #include "srv0srv.h" |
| 43 | #include "srv0start.h" /* srv_is_being_started */ |
| 44 | #include "ut0byte.h" |
| 45 | #include "ut0new.h" |
| 46 | #include "ut0rnd.h" |
| 47 | |
| 48 | #include <mysql/components/minimal_chassis.h> /* minimal_chassis_init() */ |
| 49 | #include <mysql/components/service.h> /* SERVICE_TYPE_NO_CONST */ |
| 50 | |
| 51 | static std::map<std::string, std::vector<std::string>> log_sync_points = { |
| 52 | {"log_buffer_exclussive_access", |
| 53 | {"log_buffer_x_lock_enter_before_lock", |
| 54 | "log_buffer_x_lock_enter_after_lock", |
| 55 | "log_buffer_x_lock_exit_before_unlock", |
| 56 | "log_buffer_x_lock_exit_after_unlock"}}, |
| 57 | |
| 58 | {"log_wait_for_space", |
| 59 | {"log_wfs_after_reserving_before_buf_size_1", |
| 60 | "log_wfs_after_reserving_before_buf_size_2", |
| 61 | "log_wait_for_space_in_buf_middle"}}, |
| 62 | |
| 63 | {"log_buffer_reserve", |
| 64 | {"log_buffer_reserve_before_buf_limit_sn", |
| 65 | "log_buffer_reserve_before_confirmation"}}, |
| 66 | |
| 67 | {"log_buffer_write", |
| 68 | {"log_buffer_write_before_memcpy", |
| 69 | "log_buffer_write_completed_before_store", |
| 70 | "log_buffer_write_completed_dpa_before_store", |
| 71 | "log_buffer_set_first_record_group_before_update"}}, |
| 72 | |
| 73 | {"log_writer", |
| 74 | {"log_advance_ready_for_write_before_reclaim", |
| 75 | "log_advance_ready_for_write_before_update", "log_writer_write_begin", |
| 76 | "log_writer_before_write_from_log_buffer", |
| 77 | "log_writer_before_copy_to_write_ahead_buffer", |
| 78 | "log_writer_before_write_new_incomplete_block", |
| 79 | "log_writer_before_write_ahead", "log_writer_after_checkpoint_check", |
| 80 | "log_writer_after_archiver_check", "log_writer_before_lsn_update", |
| 81 | "log_writer_before_buf_limit_update", "log_writer_write_end"}}, |
| 82 | |
| 83 | {"log_closer", |
| 84 | {"log_advance_dpa_before_reclaim", "log_advance_dpa_before_update"}}, |
| 85 | |
| 86 | {"log_checkpointer", |
| 87 | {"log_get_available_for_chkp_lsn_before_buf_pool", |
| 88 | "log_get_available_for_chkp_lsn_before_dpa", |
| 89 | "log_checkpointer_before_consider_checkpoint", |
| 90 | "log_before_checkpoint_write", "log_before_checkpoint_flush", |
| 91 | "log_before_checkpoint_lsn_update", "log_before_checkpoint_limits_update", |
| 92 | "log_before_checkpoint_data_flush"}}, |
| 93 | |
| 94 | {"log_users", |
| 95 | {"log_wait_for_flush_before_flushed_to_disk_lsn", |
| 96 | "log_wait_for_flush_before_wait"}}, |
| 97 | |
| 98 | {"log_flusher", |
| 99 | {"log_flusher_before_should_flush", "log_flush_before_fsync", |
| 100 | "log_flush_before_flushed_to_disk_lsn", "log_flush_before_users_notify", |
| 101 | "log_flush_before_notifier_notify"}}, |
| 102 | |
| 103 | {"log_write_notifier", |
| 104 | {"log_write_notifier_before_check", "log_write_notifier_after_event_reset", |
| 105 | "log_write_notifier_before_wait", "log_write_notifier_before_write_lsn", |
| 106 | "log_write_notifier_before_notify"}}, |
| 107 | |
| 108 | {"log_flush_notifier", |
| 109 | {"log_flush_notifier_before_check", "log_flush_notifier_after_event_reset", |
| 110 | "log_flush_notifier_before_wait", |
| 111 | "log_flush_notifier_before_flushed_to_disk_lsn", |
| 112 | "log_flush_notifier_before_notify"}}}; |
| 113 | |
| 114 | constexpr size_t LOG_TEST_N_THREADS = 8; |
| 115 | |
| 116 | constexpr int LOG_TEST_N_STEPS = 20; |
| 117 | |
| 118 | fil_space_t *log_space; |
| 119 | |
| 120 | extern SERVICE_TYPE_NO_CONST(registry) * srv_registry; |
| 121 | |
| 122 | extern ulong srv_log_checkpoint_every; |
| 123 | extern ulong srv_log_wait_for_flush_timeout; |
| 124 | |
| 125 | char log_group_home_dir[1] = {'\0'}; |
| 126 | |
| 127 | static bool log_test_general_init() { |
| 128 | ut_new_boot_safe(); |
| 129 | |
| 130 | /* It would be better to ask for default values of all sys vars. |
| 131 | However, I did not know how to achieve that. */ |
| 132 | |
| 133 | srv_log_write_events = 64; |
| 134 | srv_log_flush_events = 64; |
| 135 | srv_log_recent_written_size = 4 * 4096; |
| 136 | srv_log_recent_closed_size = 4 * 4096; |
| 137 | srv_log_writer_threads = true; |
| 138 | srv_log_wait_for_write_spin_delay = 0; |
| 139 | srv_log_wait_for_flush_timeout = 100000; |
| 140 | srv_log_write_max_size = 4096; |
| 141 | srv_log_writer_spin_delay = 25000; |
| 142 | srv_log_checkpoint_every = INNODB_LOG_CHECKPOINT_EVERY_DEFAULT; |
| 143 | srv_log_flusher_spin_delay = 25000; |
| 144 | srv_log_write_notifier_spin_delay = 0; |
| 145 | srv_log_flush_notifier_spin_delay = 0; |
| 146 | srv_log_spin_cpu_abs_lwm = 0; |
| 147 | srv_log_spin_cpu_pct_hwm = 100; |
| 148 | srv_log_wait_for_flush_spin_hwm = 1000; |
| 149 | srv_log_group_home_dir = log_group_home_dir; |
| 150 | srv_max_n_threads = 1000; |
| 151 | srv_redo_log_capacity = srv_redo_log_capacity_used = 8 * 1024 * 1024; |
| 152 | srv_log_buffer_size = 256 * 1024; |
| 153 | srv_log_write_ahead_size = 4096; |
| 154 | srv_buf_pool_instances = 0; |
| 155 | log_checksum_algorithm_ptr.store(log_block_calc_checksum_none); |
| 156 | srv_n_read_io_threads = 1; |
| 157 | srv_n_write_io_threads = 1; |
| 158 | |
| 159 | os_event_global_init(); |
| 160 | sync_check_init(srv_max_n_threads); |
| 161 | recv_sys_var_init(); |
| 162 | os_thread_open(); |
| 163 | ut_crc32_init(); |
| 164 | os_create_block_cache(); |
| 165 | clone_init(); |
| 166 | |
| 167 | if (!os_aio_init(srv_n_read_io_threads, srv_n_write_io_threads)) { |
| 168 | std::cerr << "Cannot initialize aio system" << std::endl; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | const size_t max_n_open_files = 1000; |
| 173 | |
| 174 | /* Below function will initialize the srv_registry variable which is |
| 175 | required for the mysql_plugin_registry_acquire() */ |
| 176 | minimal_chassis_init(&srv_registry, nullptr); |
| 177 | fil_init(max_n_open_files); |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | static bool log_test_init() { |
| 183 | if (!log_test_general_init()) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | Log_files_context log_files_ctx{srv_log_group_home_dir, |
| 188 | Log_files_ruleset::CURRENT}; |
| 189 | |
| 190 | const auto remove_ret = log_remove_files(log_files_ctx); |
| 191 | |
| 192 | const auto remove_unused_ret = log_remove_unused_files(log_files_ctx); |
| 193 | |
| 194 | srv_is_being_started = true; |
| 195 | |
| 196 | lsn_t flushed_lsn = LOG_START_LSN + LOG_BLOCK_HDR_SIZE; |
| 197 | |
| 198 | dberr_t err = log_sys_init(true, flushed_lsn, flushed_lsn); |
| 199 | ut_a(err == DB_SUCCESS); |
| 200 | |
| 201 | ut_a(log_sys != nullptr); |
| 202 | log_t &log = *log_sys; |
| 203 | |
| 204 | fil_open_system_tablespace_files(); |
| 205 | |
| 206 | err = log_start(log, flushed_lsn, flushed_lsn); |
| 207 | ut_a(err == DB_SUCCESS); |
| 208 | |
| 209 | log_start_background_threads(log); |
| 210 | |
| 211 | srv_is_being_started = false; |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | static bool log_test_recovery() { |
| 216 | srv_is_being_started = true; |
| 217 | recv_sys_create(); |
| 218 | |
| 219 | /** DBLWR directory is the current directory. */ |
| 220 | recv_sys_init(); |
| 221 | |
| 222 | lsn_t flushed_lsn = LOG_START_LSN + LOG_BLOCK_HDR_SIZE; |
| 223 | lsn_t new_files_lsn; |
| 224 | |
| 225 | dberr_t err = log_sys_init(false, flushed_lsn, new_files_lsn); |
| 226 | ut_a(err == DB_SUCCESS); |
| 227 | |
| 228 | EXPECT_EQ(0, new_files_lsn); |
| 229 | |
| 230 | ut_a(log_sys != nullptr); |
| 231 | log_t &log = *log_sys; |
| 232 | |
| 233 | err = recv_recovery_from_checkpoint_start(log, LOG_START_LSN); |
| 234 | |
| 235 | srv_is_being_started = false; |
| 236 | |
| 237 | if (err == DB_SUCCESS) { |
| 238 | auto *ret = recv_recovery_from_checkpoint_finish(true); |
| 239 | EXPECT_EQ(nullptr, ret); |
| 240 | |
| 241 | } else { |
| 242 | srv_shutdown_state = SRV_SHUTDOWN_FLUSH_PHASE; |
| 243 | |
| 244 | /* XXX: Shouldn't this be guaranteed within log0recv.cc ? */ |
| 245 | while (srv_thread_is_active(srv_threads.m_recv_writer)) { |
| 246 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | recv_sys_close(); |
| 251 | |
| 252 | fil_flush_file_spaces(); |
| 253 | |
| 254 | return err == DB_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | template <typename TFunctor> |
| 258 | static void run_threads(TFunctor f, size_t n_threads) { |
| 259 | std::atomic_bool started{false}; |
| 260 | std::atomic_size_t ready{0}; |
| 261 | std::vector<std::thread> threads; |
| 262 | |
| 263 | for (size_t i = 0; i < n_threads; ++i) { |
| 264 | threads.emplace_back( |
| 265 | [&f, &ready, &started](size_t thread_no) { |
| 266 | ++ready; |
| 267 | while (!started) { |
| 268 | std::this_thread::yield(); |
| 269 | } |
| 270 | f(thread_no); |
| 271 | }, |
| 272 | i); |
| 273 | } |
| 274 | |
| 275 | while (ready < n_threads) { |
| 276 | std::this_thread::yield(); |
| 277 | } |
| 278 | started = true; |
| 279 | |
| 280 | for (size_t i = 0; i < n_threads; ++i) { |
| 281 | threads[i].join(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static lsn_t write_single_mlog_test(Log_test::Key key) { |
| 286 | byte record[MLOG_TEST_MAX_REC_LEN]; |
| 287 | byte *record_end; |
| 288 | log_t &log = *log_sys; |
| 289 | |
| 290 | record_end = log_test->create_mlog_rec(record, key, MLOG_TEST_VALUE); |
| 291 | |
| 292 | record[0] |= MLOG_SINGLE_REC_FLAG; |
| 293 | |
| 294 | const size_t rec_len = record_end - record; |
| 295 | |
| 296 | auto handle = log_buffer_reserve(log, rec_len); |
| 297 | |
| 298 | mach_write_to_8(record_end - 16, handle.start_lsn); |
| 299 | |
| 300 | mach_write_to_8(record_end - 8, handle.end_lsn); |
| 301 | |
| 302 | const lsn_t end_lsn = |
| 303 | log_buffer_write(log, record, rec_len, handle.start_lsn); |
| 304 | |
| 305 | ut_a(end_lsn == handle.end_lsn); |
| 306 | |
| 307 | if (handle.start_lsn / OS_FILE_LOG_BLOCK_SIZE != |
| 308 | end_lsn / OS_FILE_LOG_BLOCK_SIZE) { |
| 309 | log_buffer_set_first_record_group(log, end_lsn); |
| 310 | } |
| 311 | |
| 312 | log_buffer_write_completed(log, handle.start_lsn, end_lsn); |
| 313 | |
| 314 | log_wait_for_space_in_log_recent_closed(log, handle.start_lsn); |
| 315 | |
| 316 | Log_test::Page page; |
| 317 | page.key = key; |
| 318 | page.value = MLOG_TEST_VALUE; |
| 319 | page.oldest_modification = handle.start_lsn; |
| 320 | page.newest_modification = handle.end_lsn; |
| 321 | |
| 322 | log_test->add_dirty_page(page); |
| 323 | |
| 324 | log_buffer_close(log, handle); |
| 325 | |
| 326 | return end_lsn; |
| 327 | } |
| 328 | |
| 329 | static lsn_t write_multi_mlog_tests(Log_test::Key key, size_t n) { |
| 330 | constexpr size_t MLOG_TEST_MAX_GROUP_LEN = |
| 331 | MLOG_TEST_MAX_REC_LEN * MLOG_TEST_GROUP_MAX_REC_N + 1; |
| 332 | |
| 333 | byte buf[MLOG_TEST_MAX_GROUP_LEN]; |
| 334 | byte *ptr = buf; |
| 335 | std::queue<uchar *> record_end; |
| 336 | Log_test::Value value_left = 0; |
| 337 | log_t &log = *log_sys; |
| 338 | |
| 339 | /* We need to prepare all log records first, because we need to know |
| 340 | how much space we have to reserve in redo log. */ |
| 341 | for (size_t i = n; i > 0; --i) { |
| 342 | Log_test::Value value; |
| 343 | if (i == n) { |
| 344 | value = 0; |
| 345 | value_left = MLOG_TEST_VALUE; |
| 346 | } else if (i > 1) { |
| 347 | value = ut::random_from_interval(1, 100); |
| 348 | ut_a(value < value_left); |
| 349 | value_left -= value; |
| 350 | } else { |
| 351 | value = value_left; |
| 352 | value_left = 0; |
| 353 | } |
| 354 | |
| 355 | ptr = log_test->create_mlog_rec(ptr, key, value); |
| 356 | record_end.push(ptr); |
| 357 | ut_a(ptr < buf + MLOG_TEST_MAX_GROUP_LEN); |
| 358 | } |
| 359 | |
| 360 | mach_write_to_1(ptr, MLOG_MULTI_REC_END); |
| 361 | ptr++; |
| 362 | |
| 363 | const size_t group_len = ptr - buf; |
| 364 | |
| 365 | size_t left_to_write = group_len; |
| 366 | |
| 367 | /* Reserve space in redo log. */ |
| 368 | const auto handle = log_buffer_reserve(log, group_len); |
| 369 | |
| 370 | const lsn_t group_start_lsn = handle.start_lsn; |
| 371 | |
| 372 | const sn_t group_start_sn = log_translate_lsn_to_sn(group_start_lsn); |
| 373 | |
| 374 | const lsn_t group_end_lsn = |
| 375 | log_translate_sn_to_lsn(group_start_sn + group_len); |
| 376 | |
| 377 | lsn_t start_lsn = group_start_lsn; |
| 378 | |
| 379 | lsn_t end_lsn = group_start_lsn; |
| 380 | |
| 381 | ptr = buf; |
| 382 | |
| 383 | /* Copy log records to the reserved space in log buffer. */ |
| 384 | while (!record_end.empty()) { |
| 385 | byte *end = record_end.front(); |
| 386 | |
| 387 | record_end.pop(); |
| 388 | |
| 389 | mach_write_to_8(end - 16, group_start_lsn); |
| 390 | mach_write_to_8(end - 8, group_end_lsn); |
| 391 | |
| 392 | size_t rec_len = end - ptr; |
| 393 | |
| 394 | if (left_to_write == rec_len + 1) { |
| 395 | /* Last record has MLOG_MULTI_REC_END attached. */ |
| 396 | ++rec_len; |
| 397 | ++end; |
| 398 | } |
| 399 | |
| 400 | end_lsn = log_buffer_write(log, ptr, rec_len, start_lsn); |
| 401 | |
| 402 | left_to_write -= rec_len; |
| 403 | ptr = end; |
| 404 | ut_a(ptr <= buf + MLOG_TEST_MAX_GROUP_LEN); |
| 405 | |
| 406 | if (left_to_write == 0 && group_start_lsn / OS_FILE_LOG_BLOCK_SIZE != |
| 407 | end_lsn / OS_FILE_LOG_BLOCK_SIZE) { |
| 408 | log_buffer_set_first_record_group(log, end_lsn); |
| 409 | } |
| 410 | |
| 411 | log_buffer_write_completed(log, start_lsn, end_lsn); |
| 412 | |
| 413 | start_lsn = end_lsn; |
| 414 | } |
| 415 | |
| 416 | ut_a(group_end_lsn == end_lsn); |
| 417 | ut_a(left_to_write == 0); |
| 418 | |
| 419 | log_wait_for_space_in_log_recent_closed(log, handle.start_lsn); |
| 420 | |
| 421 | Log_test::Page page; |
| 422 | page.key = key; |
| 423 | page.value = MLOG_TEST_VALUE; |
| 424 | page.oldest_modification = group_start_lsn; |
| 425 | page.newest_modification = group_end_lsn; |
| 426 | |
| 427 | log_test->add_dirty_page(page); |
| 428 | |
| 429 | log_buffer_close(log, handle); |
| 430 | |
| 431 | return group_end_lsn; |
| 432 | } |
| 433 | |
| 434 | static void log_test_run() { |
| 435 | const lsn_t max_dirty_page_age = 10 * 1024; |
| 436 | |
| 437 | run_threads( |
| 438 | [max_dirty_page_age](size_t thread_no) { |
| 439 | static_cast<void>(max_dirty_page_age); // clang -Wunused-lambda-capture |
| 440 | log_t &log = *log_sys; |
| 441 | |
| 442 | for (int j = 0; j < LOG_TEST_N_STEPS; ++j) { |
| 443 | int64_t value; |
| 444 | lsn_t end_lsn; |
| 445 | |
| 446 | value = int64_t{j} * LOG_TEST_N_THREADS + thread_no; |
| 447 | |
| 448 | const auto ppb = ut::random_from_interval(0, 100); |
| 449 | |
| 450 | if (ppb <= 50) { |
| 451 | /* Single MLOG_TEST */ |
| 452 | end_lsn = write_single_mlog_test(value); |
| 453 | |
| 454 | } else if (ppb <= 90) { |
| 455 | /* Two MLOG_TEST (group of 2 records). */ |
| 456 | end_lsn = write_multi_mlog_tests(value, 2); |
| 457 | |
| 458 | } else { |
| 459 | /* Random number of more than 2 MLOG_TEST */ |
| 460 | size_t n; |
| 461 | |
| 462 | n = ut::random_from_interval(3, MLOG_TEST_GROUP_MAX_REC_N); |
| 463 | |
| 464 | end_lsn = write_multi_mlog_tests(value, n); |
| 465 | } |
| 466 | |
| 467 | if (j % log_test->flush_every() == 0) { |
| 468 | log_test->purge(max_dirty_page_age); |
| 469 | |
| 470 | log_write_up_to(log, end_lsn, true); |
| 471 | } |
| 472 | } |
| 473 | }, |
| 474 | LOG_TEST_N_THREADS); |
| 475 | } |
| 476 | |
| 477 | static void log_test_general_close() { |
| 478 | clone_free(); |
| 479 | |
| 480 | undo_spaces_deinit(); |
| 481 | |
| 482 | os_aio_free(); |
| 483 | |
| 484 | fil_close_all_files(); |
| 485 | |
| 486 | fil_close(); |
| 487 | |
| 488 | minimal_chassis_deinit(srv_registry, nullptr); |
| 489 | |
| 490 | os_thread_close(); |
| 491 | |
| 492 | sync_check_close(); |
| 493 | |
| 494 | os_event_global_destroy(); |
| 495 | |
| 496 | srv_shutdown_state = SRV_SHUTDOWN_NONE; |
| 497 | } |
| 498 | |
| 499 | static void log_test_close() { |
| 500 | log_t &log = *log_sys; |
| 501 | |
| 502 | srv_shutdown_state = SRV_SHUTDOWN_FLUSH_PHASE; |
| 503 | |
| 504 | log_stop_background_threads(log); |
| 505 | |
| 506 | if (log_test->verbosity() > 0) { |
| 507 | std::cout << "Finally: lsn = " << log_get_lsn(log) |
| 508 | << " checkpoint = " << log.last_checkpoint_lsn |
| 509 | << " oldest_modification_approx = " |
| 510 | << log_test->oldest_modification_approx() << std::endl; |
| 511 | } |
| 512 | |
| 513 | log_sys_close(); |
| 514 | |
| 515 | fil_close_all_files(); |
| 516 | fil_close(); |
| 517 | const size_t max_n_open_files = 1000; |
| 518 | fil_init(max_n_open_files); |
| 519 | |
| 520 | log_test_recovery(); |
| 521 | |
| 522 | if (log_test->verbosity() > 0) { |
| 523 | std::cout << "Flushed " << log_test->flushed().size() << " pages." |
| 524 | << std::endl; |
| 525 | |
| 526 | std::cout << "Found " << log_test->recovered().size() |
| 527 | << " MLOG_TEST log records." << std::endl; |
| 528 | } |
| 529 | |
| 530 | Log_test::Pages pages; |
| 531 | |
| 532 | for (auto flushed : log_test->flushed()) { |
| 533 | pages[flushed.first] = flushed.second; |
| 534 | } |
| 535 | |
| 536 | for (auto recovered : log_test->recovered()) { |
| 537 | auto &p = pages[recovered.first]; |
| 538 | |
| 539 | if (recovered.second.newest_modification > p.newest_modification) { |
| 540 | p = recovered.second; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | const size_t n_pages = LOG_TEST_N_THREADS * LOG_TEST_N_STEPS; |
| 545 | |
| 546 | EXPECT_EQ(n_pages, pages.size()); |
| 547 | |
| 548 | bool found_missing = false; |
| 549 | for (size_t i = 0; i < n_pages; ++i) { |
| 550 | if (pages.find(i) == pages.end()) { |
| 551 | std::cerr << "Missing page for key: " << i |
| 552 | << " (thread: " << i % LOG_TEST_N_THREADS |
| 553 | << ", step: " << i / LOG_TEST_N_THREADS << ")" << std::endl; |
| 554 | |
| 555 | found_missing = true; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | log_sys_close(); |
| 560 | |
| 561 | log_test_general_close(); |
| 562 | |
| 563 | ASSERT_FALSE(found_missing); |
| 564 | } |
| 565 | |
| 566 | static void execute_test() { |
| 567 | ASSERT_TRUE(log_test_init()); |
| 568 | log_test_run(); |
| 569 | log_test_close(); |
| 570 | } |
| 571 | |
| 572 | static void configure_delay_for_sync_point(const std::string &sync_point_name, |
| 573 | uint64_t min_us, uint64_t max_us) { |
| 574 | /* NOTE: On Windows sleeps smaller than minimum resolution of |
| 575 | timer, would anyway be equivalent to sleep(0) which only asks |
| 576 | for context switch. However that's good enough for now. */ |
| 577 | #ifdef WIN32 |
| 578 | min_us = max_us = 0; |
| 579 | #endif |
| 580 | |
| 581 | using Sync_point = Log_test::Sync_point; |
| 582 | |
| 583 | class Random_delay : public Sync_point { |
| 584 | public: |
| 585 | Random_delay(uint64_t min_us, uint64_t max_us) |
| 586 | : m_min_us(min_us), m_max_us(max_us) {} |
| 587 | |
| 588 | void sync() override { |
| 589 | if (m_max_us == 0) { |
| 590 | std::this_thread::yield(); |
| 591 | } else { |
| 592 | std::this_thread::sleep_for(std::chrono::microseconds( |
| 593 | ut::random_from_interval(m_min_us, m_max_us))); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | uint64_t m_min_us, m_max_us; |
| 599 | }; |
| 600 | |
| 601 | std::unique_ptr<Sync_point> sync_point_handler; |
| 602 | |
| 603 | sync_point_handler.reset(new Random_delay{min_us, max_us}); |
| 604 | |
| 605 | log_test->register_sync_point_handler(sync_point_name, |
| 606 | std::move(sync_point_handler)); |
| 607 | } |
| 608 | |
| 609 | static void test_single(const std::string &group) { |
| 610 | const uint64_t max_delay = |
| 611 | std::max(uint64_t{10}, uint64_t{500 / log_sync_points[group].size()}); |
| 612 | |
| 613 | for (auto pt : log_sync_points[group]) { |
| 614 | log_test.reset(new Log_test); |
| 615 | |
| 616 | configure_delay_for_sync_point(pt, 10, max_delay); |
| 617 | |
| 618 | execute_test(); |
| 619 | } |
| 620 | |
| 621 | log_test.reset(new Log_test); |
| 622 | |
| 623 | for (auto pt : log_sync_points[group]) { |
| 624 | configure_delay_for_sync_point(pt, 0, 500); |
| 625 | } |
| 626 | |
| 627 | execute_test(); |
| 628 | } |
| 629 | |
| 630 | class Log_test_disturber { |
| 631 | public: |
| 632 | virtual ~Log_test_disturber() = default; |
| 633 | |
| 634 | virtual void disturb() = 0; |
| 635 | }; |
| 636 | |
| 637 | class Log_background_disturber { |
| 638 | public: |
| 639 | explicit Log_background_disturber( |
| 640 | std::unique_ptr<Log_test_disturber> disturber) |
| 641 | : m_disturber(std::move(disturber)), m_is_active(false) {} |
| 642 | |
| 643 | void start(); |
| 644 | void stop(); |
| 645 | |
| 646 | private: |
| 647 | void run(); |
| 648 | |
| 649 | std::unique_ptr<Log_test_disturber> m_disturber; |
| 650 | std::thread m_thread; |
| 651 | std::atomic_bool m_is_active; |
| 652 | }; |
| 653 | |
| 654 | void Log_background_disturber::start() { |
| 655 | ut_a(!m_is_active); |
| 656 | |
| 657 | m_is_active = true; |
| 658 | |
| 659 | m_thread = std::thread{std::bind(&Log_background_disturber::run, this)}; |
| 660 | } |
| 661 | |
| 662 | void Log_background_disturber::stop() { |
| 663 | ut_a(m_is_active); |
| 664 | |
| 665 | m_is_active = false; |
| 666 | |
| 667 | m_thread.join(); |
| 668 | } |
| 669 | |
| 670 | void Log_background_disturber::run() { |
| 671 | while (m_is_active) { |
| 672 | m_disturber->disturb(); |
| 673 | |
| 674 | const auto sleep_time = ut::random_from_interval(0, 300 * 1000); |
| 675 | |
| 676 | std::this_thread::sleep_for(std::chrono::microseconds(sleep_time)); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | class Log_buf_resizer : public Log_test_disturber { |
| 681 | public: |
| 682 | Log_buf_resizer(size_t min_size, size_t max_size) |
| 683 | : m_min_size(min_size), m_max_size(max_size) {} |
| 684 | |
| 685 | void disturb() override { |
| 686 | size_t new_size = ut::random_from_interval(m_min_size, m_max_size); |
| 687 | |
| 688 | new_size = ut_uint64_align_down(new_size, OS_FILE_LOG_BLOCK_SIZE); |
| 689 | |
| 690 | log_buffer_resize(*log_sys, new_size); |
| 691 | } |
| 692 | |
| 693 | private: |
| 694 | size_t m_min_size, m_max_size; |
| 695 | }; |
| 696 | |
| 697 | class Log_write_ahead_resizer : public Log_test_disturber { |
| 698 | public: |
| 699 | Log_write_ahead_resizer(size_t min_k, size_t max_k) |
| 700 | : m_min_k(min_k), m_max_k(max_k) {} |
| 701 | |
| 702 | void disturb() override { |
| 703 | const auto new_size = 1 << ut::random_from_interval(m_min_k, m_max_k); |
| 704 | |
| 705 | log_write_ahead_resize(*log_sys, new_size); |
| 706 | } |
| 707 | |
| 708 | private: |
| 709 | size_t m_min_k, m_max_k; |
| 710 | }; |
| 711 | |
| 712 | static void execute_disturbed_test( |
| 713 | std::unique_ptr<Log_test_disturber> &&disturber) { |
| 714 | log_test.reset(new Log_test); |
| 715 | ASSERT_TRUE(log_test_init()); |
| 716 | Log_background_disturber bg{std::move(disturber)}; |
| 717 | bg.start(); |
| 718 | log_test_run(); |
| 719 | bg.stop(); |
| 720 | log_test_close(); |
| 721 | } |
| 722 | |
| 723 | TEST(log0log, log_random_disturb) { |
| 724 | std::unique_ptr<Log_test_disturber> disturber; |
| 725 | |
| 726 | disturber.reset(new Log_buf_resizer{256 * 1024, 1024 * 1024}); |
| 727 | execute_disturbed_test(std::move(disturber)); |
| 728 | |
| 729 | disturber.reset(new Log_write_ahead_resizer{ |
| 730 | 9, ut_2_log(INNODB_LOG_WRITE_AHEAD_SIZE_MAX)}); |
| 731 | execute_disturbed_test(std::move(disturber)); |
| 732 | } |
| 733 | |
| 734 | TEST(log0log, log_buffer_reserve) { test_single("log_buffer_reserve"); } |
| 735 | |
| 736 | TEST(log0log, log_buffer_write) { test_single("log_buffer_write"); } |
| 737 | |
| 738 | TEST(log0log, log_writer) { test_single("log_writer"); } |
| 739 | |
| 740 | TEST(log0log, log_flusher) { test_single("log_flusher"); } |
| 741 | |
| 742 | TEST(log0log, log_write_notifier) { test_single("log_write_notifier"); } |
| 743 | |
| 744 | TEST(log0log, log_flush_notifier) { test_single("log_flush_notifier"); } |
| 745 | |
| 746 | TEST(log0log, log_users) { test_single("log_users"); } |
| 747 | |
| 748 | TEST(log0log, log_closer) { test_single("log_closer"); } |
| 749 | |
| 750 | TEST(log0log, log_checkpointer) { test_single("log_checkpointer"); } |