| 1 | /* Copyright (c) 2014, 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 | /* |
| 25 | Unit tests for MDL subsystem which require DEBUG_SYNC facility |
| 26 | (mostly because they are very sensitive to thread scheduling). |
| 27 | */ |
| 28 | |
| 29 | #include "my_config.h" |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | #include "sql/debug_sync.h" |
| 34 | #include "sql/mdl.h" |
| 35 | #include "sql/sql_base.h" |
| 36 | #include "sql/sql_class.h" |
| 37 | #include "unittest/gunit/test_utils.h" |
| 38 | #include "unittest/gunit/thread_utils.h" |
| 39 | |
| 40 | #if defined(ENABLED_DEBUG_SYNC) |
| 41 | namespace mdl_sync_unittest { |
| 42 | |
| 43 | using my_testing::Mock_error_handler; |
| 44 | using my_testing::Server_initializer; |
| 45 | using thread::Notification; |
| 46 | using thread::Thread; |
| 47 | |
| 48 | class MDLSyncTest : public ::testing::Test { |
| 49 | protected: |
| 50 | MDLSyncTest() = default; |
| 51 | |
| 52 | void SetUp() override { |
| 53 | /* Set debug sync timeout of 60 seconds. */ |
| 54 | opt_debug_sync_timeout = 60; |
| 55 | debug_sync_init(); |
| 56 | /* Force immediate destruction of unused MDL_lock objects. */ |
| 57 | mdl_locks_unused_locks_low_water = 0; |
| 58 | max_write_lock_count = ULONG_MAX; |
| 59 | mdl_init(); |
| 60 | |
| 61 | m_initializer.SetUp(); |
| 62 | m_thd = m_initializer.thd(); |
| 63 | table_def_init(); |
| 64 | } |
| 65 | |
| 66 | void TearDown() override { |
| 67 | m_initializer.TearDown(); |
| 68 | mdl_destroy(); |
| 69 | debug_sync_end(); |
| 70 | opt_debug_sync_timeout = 0; |
| 71 | mdl_locks_unused_locks_low_water = MDL_LOCKS_UNUSED_LOCKS_LOW_WATER_DEFAULT; |
| 72 | table_def_free(); |
| 73 | } |
| 74 | |
| 75 | Server_initializer m_initializer; |
| 76 | THD *m_thd; |
| 77 | }; |
| 78 | |
| 79 | /** Wrapper function which simplify usage of debug_sync_set_action(). */ |
| 80 | |
| 81 | static bool debug_sync_set_action(THD *thd, const char *sync) { |
| 82 | return debug_sync_set_action(thd, sync, strlen(sync)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | Set sync point and acquires and then releases the specified type of lock |
| 87 | on the table. |
| 88 | Allows to pause thread execution after lock acquisition and before its |
| 89 | release by using notifications. |
| 90 | */ |
| 91 | |
| 92 | class MDLSyncThread : public Thread { |
| 93 | public: |
| 94 | MDLSyncThread(const char *main_table_arg, enum_mdl_type main_mdl_type_arg, |
| 95 | const char *sync_arg, Notification *grabbed_arg, |
| 96 | Notification *release_arg) |
| 97 | : m_main_table(main_table_arg), |
| 98 | m_main_mdl_type(main_mdl_type_arg), |
| 99 | m_sync(sync_arg), |
| 100 | m_lock_grabbed(grabbed_arg), |
| 101 | m_lock_release(release_arg), |
| 102 | m_pre_table(nullptr), |
| 103 | m_pre_mdl_type(MDL_INTENTION_EXCLUSIVE), |
| 104 | m_pre_sync(nullptr) {} |
| 105 | |
| 106 | MDLSyncThread(const char *main_table_arg, enum_mdl_type main_mdl_type_arg, |
| 107 | const char *sync_arg, Notification *grabbed_arg, |
| 108 | Notification *release_arg, const char *pre_table_arg, |
| 109 | enum_mdl_type pre_mdl_type_arg, const char *pre_sync_arg) |
| 110 | : m_main_table(main_table_arg), |
| 111 | m_main_mdl_type(main_mdl_type_arg), |
| 112 | m_sync(sync_arg), |
| 113 | m_lock_grabbed(grabbed_arg), |
| 114 | m_lock_release(release_arg), |
| 115 | m_pre_table(pre_table_arg), |
| 116 | m_pre_mdl_type(pre_mdl_type_arg), |
| 117 | m_pre_sync(pre_sync_arg) {} |
| 118 | |
| 119 | virtual void run() { |
| 120 | m_initializer.SetUp(); |
| 121 | m_thd = m_initializer.thd(); |
| 122 | m_mdl_context = &m_thd->mdl_context; |
| 123 | |
| 124 | if (m_pre_table) { |
| 125 | Mock_error_handler error_handler(m_thd, 0); |
| 126 | |
| 127 | if (m_pre_sync) { |
| 128 | EXPECT_FALSE(debug_sync_set_action(m_thd, m_pre_sync)); |
| 129 | } |
| 130 | |
| 131 | /* Pre-acquire lock on auxiliary table if requested. */ |
| 132 | MDL_request request; |
| 133 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, "db", m_pre_table, |
| 134 | m_pre_mdl_type, MDL_TRANSACTION); |
| 135 | |
| 136 | EXPECT_FALSE(m_mdl_context->acquire_lock(&request, 3600)); |
| 137 | |
| 138 | /* The above should not generate any warnings (e.g. about timeouts). */ |
| 139 | EXPECT_EQ(0, error_handler.handle_called()); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | Use a block to ensure that Mock_error_handler dtor is called |
| 144 | before m_initalizer.TearDown(). |
| 145 | */ |
| 146 | { |
| 147 | Mock_error_handler error_handler(m_thd, 0); |
| 148 | |
| 149 | if (m_sync) { |
| 150 | EXPECT_FALSE(debug_sync_set_action(m_thd, m_sync)); |
| 151 | } |
| 152 | |
| 153 | MDL_request request; |
| 154 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, "db", m_main_table, |
| 155 | m_main_mdl_type, MDL_TRANSACTION); |
| 156 | |
| 157 | EXPECT_FALSE(m_mdl_context->acquire_lock(&request, 3600)); |
| 158 | EXPECT_TRUE(m_mdl_context->owns_equal_or_stronger_lock( |
| 159 | MDL_key::TABLE, "db", m_main_table, m_main_mdl_type)); |
| 160 | |
| 161 | if (m_lock_grabbed) m_lock_grabbed->notify(); |
| 162 | if (m_lock_release) m_lock_release->wait_for_notification(); |
| 163 | |
| 164 | m_mdl_context->release_transactional_locks(); |
| 165 | |
| 166 | /* The above should not generate any warnings (e.g. about timeouts). */ |
| 167 | EXPECT_EQ(0, error_handler.handle_called()); |
| 168 | } |
| 169 | |
| 170 | m_initializer.TearDown(); |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | Server_initializer m_initializer; |
| 175 | THD *m_thd; |
| 176 | MDL_context *m_mdl_context; |
| 177 | const char *m_main_table; |
| 178 | enum_mdl_type m_main_mdl_type; |
| 179 | const char *m_sync; |
| 180 | Notification *m_lock_grabbed; |
| 181 | Notification *m_lock_release; |
| 182 | const char *m_pre_table; |
| 183 | enum_mdl_type m_pre_mdl_type; |
| 184 | const char *m_pre_sync; |
| 185 | }; |
| 186 | |
| 187 | /* |
| 188 | Checks that "fast path" lock acquisition correctly handles MDL_lock objects |
| 189 | which already have been marked as destroyed but still present in MDL_map. |
| 190 | */ |
| 191 | |
| 192 | TEST_F(MDLSyncTest, IsDestroyedFastPath) { |
| 193 | MDLSyncThread thread1("table", MDL_SHARED, |
| 194 | "mdl_remove_random_unused_after_is_destroyed_set " |
| 195 | "SIGNAL marked WAIT_FOR resume_removal", |
| 196 | nullptr, nullptr); |
| 197 | MDLSyncThread thread2("table", MDL_SHARED, |
| 198 | "mdl_acquire_lock_is_destroyed_fast_path " |
| 199 | "SIGNAL resume_removal", |
| 200 | nullptr, nullptr); |
| 201 | |
| 202 | /* |
| 203 | Start the first thread which acquires S lock on a table and immediately |
| 204 | releases it. As result the MDL_lock object for the table becomes unused |
| 205 | and attempt to destroy this unused object is made. MDL_lock is marked |
| 206 | as destroyed, but the thread hits sync point before the object is removed |
| 207 | from the MDL_map hash. |
| 208 | */ |
| 209 | thread1.start(); |
| 210 | |
| 211 | /* Wait until MDL_lock object is marked as destroyed. */ |
| 212 | Mock_error_handler error_handler(m_thd, 0); |
| 213 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR marked")); |
| 214 | /* The above should not generate warnings about timeouts. */ |
| 215 | EXPECT_EQ(0, error_handler.handle_called()); |
| 216 | |
| 217 | /* |
| 218 | Start the second thread which will try to acquire S lock on the same |
| 219 | table. It should notice that corresponding MDL_lock object is marked |
| 220 | as destroyed. Sync point in code responsible for handling this situation |
| 221 | will emit signal which should unblock the first thread. |
| 222 | */ |
| 223 | thread2.start(); |
| 224 | |
| 225 | /* Check that both threads finish. */ |
| 226 | thread1.join(); |
| 227 | thread2.join(); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | Checks that "slow path" lock acquisition correctly handles MDL_lock objects |
| 232 | which already have been marked as destroyed but still present in MDL_map. |
| 233 | */ |
| 234 | |
| 235 | TEST_F(MDLSyncTest, IsDestroyedSlowPath) { |
| 236 | MDLSyncThread thread1("table", MDL_SHARED, |
| 237 | "mdl_remove_random_unused_after_is_destroyed_set " |
| 238 | "SIGNAL marked WAIT_FOR resume_removal", |
| 239 | nullptr, nullptr); |
| 240 | MDLSyncThread thread2("table", MDL_SHARED_NO_READ_WRITE, |
| 241 | "mdl_acquire_lock_is_destroyed_slow_path " |
| 242 | "SIGNAL resume_removal", |
| 243 | nullptr, nullptr); |
| 244 | |
| 245 | /* |
| 246 | Start the first thread which acquires S lock on a table and immediately |
| 247 | releases it. As result the MDL_lock object for the table becomes unused |
| 248 | and attempt to destroy this unused object is made. MDL_lock is marked |
| 249 | as destroyed, but the thread hits sync point before the object is removed |
| 250 | from the MDL_map hash. |
| 251 | */ |
| 252 | thread1.start(); |
| 253 | |
| 254 | /* Wait until MDL_lock object is marked as destroyed. */ |
| 255 | Mock_error_handler error_handler(m_thd, 0); |
| 256 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR marked")); |
| 257 | /* The above should not generate warnings about timeouts. */ |
| 258 | EXPECT_EQ(0, error_handler.handle_called()); |
| 259 | |
| 260 | /* |
| 261 | Start the second thread which will try to acquire SNRW lock on the same |
| 262 | table. It should notice that corresponding MDL_lock object is marked |
| 263 | as destroyed. Sync point in code responsible for handling this situation |
| 264 | will emit signal which should unblock the first thread. |
| 265 | */ |
| 266 | thread2.start(); |
| 267 | |
| 268 | /* Check that both threads finish. */ |
| 269 | thread1.join(); |
| 270 | thread2.join(); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | Checks that code responsible for destroying of random unused MDL_lock |
| 275 | object correctly handles situation then it fails to find such an object. |
| 276 | */ |
| 277 | |
| 278 | TEST_F(MDLSyncTest, DoubleDestroyTakeOne) { |
| 279 | MDLSyncThread thread1("table", MDL_SHARED, |
| 280 | "mdl_remove_random_unused_before_search " |
| 281 | "SIGNAL before_search WAIT_FOR start_search", |
| 282 | nullptr, nullptr); |
| 283 | MDLSyncThread thread2("table", MDL_SHARED, nullptr, nullptr, nullptr); |
| 284 | |
| 285 | /* |
| 286 | Start the first thread which acquires S lock on a table and immediately |
| 287 | releases it. As result the MDL_lock object for the table becomes unused |
| 288 | and attempt to destroy random unused object is made. Thread hits sync |
| 289 | point before it starts search for unused object. |
| 290 | */ |
| 291 | thread1.start(); |
| 292 | |
| 293 | /* Wait until thread stops before searching for unused object. */ |
| 294 | Mock_error_handler error_handler(m_thd, 0); |
| 295 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR before_search")); |
| 296 | /* The above should not generate warnings about timeouts. */ |
| 297 | EXPECT_EQ(0, error_handler.handle_called()); |
| 298 | |
| 299 | /* |
| 300 | Start the second thread which will acquire and release S lock. |
| 301 | As result MDL_lock object will become unused and destroyed. |
| 302 | There should be no unused MDL_lock objects after its completion. |
| 303 | */ |
| 304 | thread2.start(); |
| 305 | thread2.join(); |
| 306 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 307 | |
| 308 | /* |
| 309 | Resume the first thread. At this point it should find no unused objects. |
| 310 | Check that it finishes succesfully. |
| 311 | */ |
| 312 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now SIGNAL start_search")); |
| 313 | thread1.join(); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | Checks that code responsible for destroying of random unused MDL_lock |
| 318 | object correctly handles situation then it discovers that object |
| 319 | which was chosen for destruction is already destroyed. |
| 320 | */ |
| 321 | |
| 322 | TEST_F(MDLSyncTest, DoubleDestroyTakeTwo) { |
| 323 | MDLSyncThread thread1("table", MDL_SHARED, |
| 324 | "mdl_remove_random_unused_after_search " |
| 325 | "SIGNAL found WAIT_FOR resume_destroy", |
| 326 | nullptr, nullptr); |
| 327 | MDLSyncThread thread2("table", MDL_SHARED, |
| 328 | "mdl_remove_random_unused_after_is_destroyed_set " |
| 329 | "SIGNAL resume_destroy", |
| 330 | nullptr, nullptr); |
| 331 | |
| 332 | /* |
| 333 | Start the first thread which acquires S lock on a table and immediately |
| 334 | releases it. As result the MDL_lock object for the table becomes unused |
| 335 | and attempt to destroy random unused object is made. Thread hits sync |
| 336 | point after it has found the only unused MDL_lock object, but before |
| 337 | it has acquired its MDL_lock::m_rwlock. |
| 338 | */ |
| 339 | thread1.start(); |
| 340 | |
| 341 | /* Wait until thread finds unused object. */ |
| 342 | Mock_error_handler error_handler(m_thd, 0); |
| 343 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR found")); |
| 344 | /* The above should not generate warnings about timeouts. */ |
| 345 | EXPECT_EQ(0, error_handler.handle_called()); |
| 346 | |
| 347 | /* |
| 348 | Start the second thread which will acquire and release S lock. |
| 349 | As result MDL_lock object will become unused and we will try |
| 350 | to destroy it again. |
| 351 | Thread will hit the sync point once the object is marked as destroyed. |
| 352 | As result signal unblocking the first thread will be emitted. |
| 353 | |
| 354 | */ |
| 355 | thread2.start(); |
| 356 | |
| 357 | /* |
| 358 | The first thread should discover that the only unused MDL_lock object |
| 359 | already has been marked as destroyed and still complete successfully. |
| 360 | */ |
| 361 | thread1.join(); |
| 362 | |
| 363 | /* So does the second thread. */ |
| 364 | thread2.join(); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | Checks that code responsible for destroying of random unused MDL_lock |
| 369 | object correctly handles situation then it discovers that object |
| 370 | which was chosen for destruction is re-used again and can't be destroyed. |
| 371 | */ |
| 372 | |
| 373 | TEST_F(MDLSyncTest, DestroyUsed) { |
| 374 | Notification lock_grabbed, lock_release; |
| 375 | MDLSyncThread thread1("table", MDL_SHARED, |
| 376 | "mdl_remove_random_unused_after_search " |
| 377 | "SIGNAL found WAIT_FOR resume_destroy", |
| 378 | nullptr, nullptr); |
| 379 | MDLSyncThread thread2("table", MDL_SHARED, nullptr, &lock_grabbed, |
| 380 | &lock_release); |
| 381 | |
| 382 | /* |
| 383 | Start the first thread which acquires S lock on a table and immediately |
| 384 | releases it. As result the MDL_lock object for the table becomes unused |
| 385 | and attempt to destroy random unused object is made. Thread hits sync |
| 386 | point after it has found the only unused MDL_lock object, but before |
| 387 | it has acquired its MDL_lock::m_rwlock. |
| 388 | */ |
| 389 | thread1.start(); |
| 390 | |
| 391 | /* Wait until thread finds unused object. */ |
| 392 | Mock_error_handler error_handler(m_thd, 0); |
| 393 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR found")); |
| 394 | /* The above should not generate warnings about timeouts. */ |
| 395 | EXPECT_EQ(0, error_handler.handle_called()); |
| 396 | |
| 397 | /* |
| 398 | Start the second thread which will acquire S lock and re-use |
| 399 | the same MDL_lock object. |
| 400 | */ |
| 401 | thread2.start(); |
| 402 | /* Wait until lock is acquired. */ |
| 403 | lock_grabbed.wait_for_notification(); |
| 404 | |
| 405 | /* |
| 406 | Resume the first thread. It should discover that the MDL_lock object |
| 407 | it has found is used again and can't be destroyed. |
| 408 | */ |
| 409 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now SIGNAL resume_destroy")); |
| 410 | |
| 411 | /* Still it should complete successfully.*/ |
| 412 | thread1.join(); |
| 413 | |
| 414 | /* So does the second thread after it releases S lock. */ |
| 415 | lock_release.notify(); |
| 416 | thread2.join(); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | Test that shows that reschedule of waiters in cases when we acquire |
| 421 | "hog" locks without waiting and this changes priority matrice is |
| 422 | important. |
| 423 | */ |
| 424 | |
| 425 | TEST_F(MDLSyncTest, PriorityDeadlock) { |
| 426 | Notification first_grabbed, first_release; |
| 427 | MDLSyncThread thread1("alpha", MDL_SHARED_READ, nullptr, &first_grabbed, |
| 428 | &first_release); |
| 429 | MDLSyncThread thread2("alpha", MDL_SHARED_NO_READ_WRITE, |
| 430 | "mdl_acquire_lock_wait SIGNAL snrw_blocked ", nullptr, |
| 431 | nullptr); |
| 432 | MDLSyncThread thread3("alpha", MDL_SHARED_READ, |
| 433 | "mdl_acquire_lock_wait SIGNAL sr_blocked " |
| 434 | "WAIT_FOR sr_resume", |
| 435 | nullptr, nullptr, "beta", MDL_SHARED_WRITE, nullptr); |
| 436 | MDLSyncThread thread4("beta", MDL_SHARED_NO_WRITE, nullptr, nullptr, nullptr, |
| 437 | "alpha", MDL_SHARED_NO_WRITE, nullptr); |
| 438 | |
| 439 | /* |
| 440 | Set "max_write_lock_count" to low value to ensure that switch of priority |
| 441 | matrices happens early. |
| 442 | */ |
| 443 | max_write_lock_count = 1; |
| 444 | |
| 445 | /* Start the first thread which acquires SR lock on "alpha". */ |
| 446 | thread1.start(); |
| 447 | first_grabbed.wait_for_notification(); |
| 448 | |
| 449 | /* Start the second thread which will try to acquire SNRW on "alpha". */ |
| 450 | thread2.start(); |
| 451 | /* Wait until the thread gets blocked creating pending SNRW lock. */ |
| 452 | { |
| 453 | Mock_error_handler error_handler(m_thd, 0); |
| 454 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR snrw_blocked")); |
| 455 | EXPECT_EQ(0, error_handler.handle_called()); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | Start the third thread. It will acquire SW lock on table "beta", |
| 460 | and will try to acquire SR lock on "alpha". It will be blocked |
| 461 | because of pending SNRW lock on it. |
| 462 | */ |
| 463 | thread3.start(); |
| 464 | /* |
| 465 | Pause execution of the thread after it get blocked, but before |
| 466 | deadlock detection is done. |
| 467 | */ |
| 468 | { |
| 469 | Mock_error_handler error_handler(m_thd, 0); |
| 470 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR sr_blocked")); |
| 471 | EXPECT_EQ(0, error_handler.handle_called()); |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | Start the fourth thread. It will acquire SNW lock on "alpha". |
| 476 | This will switch priority matrice for this table and unblock pending |
| 477 | SR lock request (because max_write_lock_count will be reached). |
| 478 | After that it will try to acquire SNW lock on "beta" and block |
| 479 | due to active SW lock from thread 3. |
| 480 | */ |
| 481 | thread4.start(); |
| 482 | |
| 483 | /* |
| 484 | Resume thread 3. |
| 485 | It should not wait because priority matrice has been changed and SR lock |
| 486 | can now be granted (Reporting deadlock is acceptable outcome as well). |
| 487 | */ |
| 488 | { |
| 489 | Mock_error_handler error_handler(m_thd, 0); |
| 490 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now SIGNAL sr_resume")); |
| 491 | EXPECT_EQ(0, error_handler.handle_called()); |
| 492 | } |
| 493 | thread3.join(); |
| 494 | |
| 495 | /* |
| 496 | After SR lock has been acquired on "alpha" by thread 3. |
| 497 | It will release all locks including SW lock on "beta". |
| 498 | So thread 4 should be able to proceed. |
| 499 | */ |
| 500 | thread4.join(); |
| 501 | |
| 502 | /* Wrap-up. */ |
| 503 | first_release.notify(); |
| 504 | thread1.join(); |
| 505 | thread2.join(); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | Test that shows that reschedule is also important when priority |
| 510 | matrice is changed due to "hog" lock being granted after wait. |
| 511 | */ |
| 512 | |
| 513 | TEST_F(MDLSyncTest, PriorityDeadlock2) { |
| 514 | Notification first_grabbed, first_release, second_grabbed, second_release; |
| 515 | MDLSyncThread thread1("alpha", MDL_SHARED_WRITE, nullptr, &first_grabbed, |
| 516 | &first_release); |
| 517 | MDLSyncThread thread2("alpha", MDL_SHARED_READ, nullptr, &second_grabbed, |
| 518 | &second_release); |
| 519 | MDLSyncThread thread3("alpha", MDL_SHARED_NO_READ_WRITE, |
| 520 | "mdl_acquire_lock_wait SIGNAL snrw_blocked ", nullptr, |
| 521 | nullptr); |
| 522 | MDLSyncThread thread4("alpha", MDL_SHARED_READ, |
| 523 | "mdl_acquire_lock_wait SIGNAL sr_blocked " |
| 524 | "WAIT_FOR sr_resume", |
| 525 | nullptr, nullptr, "beta", MDL_SHARED_WRITE, nullptr); |
| 526 | MDLSyncThread thread5("beta", MDL_SHARED_NO_WRITE, nullptr, nullptr, nullptr, |
| 527 | "alpha", MDL_SHARED_NO_WRITE, |
| 528 | "mdl_acquire_lock_wait SIGNAL snw_blocked"); |
| 529 | |
| 530 | /* |
| 531 | Set "max_write_lock_count" to low value to ensure that switch of priority |
| 532 | matrices happens early. |
| 533 | */ |
| 534 | max_write_lock_count = 1; |
| 535 | |
| 536 | /* Start the first thread which acquires SW lock on "alpha". */ |
| 537 | thread1.start(); |
| 538 | first_grabbed.wait_for_notification(); |
| 539 | /* Start the second thread which acquires SR lock on "alpha". */ |
| 540 | thread2.start(); |
| 541 | second_grabbed.wait_for_notification(); |
| 542 | |
| 543 | /* Start the third thread which will try to acquire SNRW on "alpha". */ |
| 544 | thread3.start(); |
| 545 | /* Wait until the thread gets blocked creating pending SNRW lock. */ |
| 546 | { |
| 547 | Mock_error_handler error_handler(m_thd, 0); |
| 548 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR snrw_blocked")); |
| 549 | EXPECT_EQ(0, error_handler.handle_called()); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | Start the fourth thread. It will acquire SW lock on table "beta", |
| 554 | and will try to acquire SR lock on "alpha". It will be blocked |
| 555 | because of pending SNRW lock on it. |
| 556 | */ |
| 557 | thread4.start(); |
| 558 | /* |
| 559 | Pause execution of the thread after it get blocked, but before |
| 560 | deadlock detection is done. |
| 561 | */ |
| 562 | { |
| 563 | Mock_error_handler error_handler(m_thd, 0); |
| 564 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR sr_blocked")); |
| 565 | EXPECT_EQ(0, error_handler.handle_called()); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | Start the fifth thread. It will try to acquire SNW lock on "alpha". |
| 570 | It will get blocked because of active SW lock. |
| 571 | */ |
| 572 | thread5.start(); |
| 573 | |
| 574 | /* |
| 575 | Wait until the thread gets blocked and SNW request is added to waiters |
| 576 | queue. |
| 577 | */ |
| 578 | { |
| 579 | Mock_error_handler error_handler(m_thd, 0); |
| 580 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR snw_blocked")); |
| 581 | EXPECT_EQ(0, error_handler.handle_called()); |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | Release SW lock. This will unblock the fifth thread. SNW lock will |
| 586 | be granted. |
| 587 | |
| 588 | This will switch priority matrice for this table and unblock pending |
| 589 | SR lock request (because max_write_lock_count will be reached). |
| 590 | After that it will try to acquire SNW lock on "beta" and block |
| 591 | due to active SW lock from thread 3. |
| 592 | */ |
| 593 | first_release.notify(); |
| 594 | thread1.join(); |
| 595 | |
| 596 | /* |
| 597 | Resume thread 4. |
| 598 | It should not wait because priority matrice has been changed and SR lock |
| 599 | can now be granted (Reporting deadlock is acceptable outcome as well). |
| 600 | */ |
| 601 | { |
| 602 | Mock_error_handler error_handler(m_thd, 0); |
| 603 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now SIGNAL sr_resume")); |
| 604 | EXPECT_EQ(0, error_handler.handle_called()); |
| 605 | } |
| 606 | thread4.join(); |
| 607 | |
| 608 | /* |
| 609 | After SR lock has been acquired on "alpha" by thread 4. |
| 610 | It will release all locks including SW lock on "beta". |
| 611 | So thread 5 should be able to proceed. |
| 612 | */ |
| 613 | thread5.join(); |
| 614 | |
| 615 | /* Wrap-up. */ |
| 616 | second_release.notify(); |
| 617 | thread2.join(); |
| 618 | thread3.join(); |
| 619 | } |
| 620 | |
| 621 | /** Dummy MDL_context_visitor which does nothing. */ |
| 622 | |
| 623 | class MDLSyncContextVisitor : public MDL_context_visitor { |
| 624 | public: |
| 625 | virtual void visit_context(const MDL_context *) {} |
| 626 | }; |
| 627 | |
| 628 | /* |
| 629 | Checks that MDL_context::find_lock_owner() correctly handles MDL_lock |
| 630 | objects which already have been marked as destroyed but still present |
| 631 | in MDL_map. |
| 632 | */ |
| 633 | |
| 634 | TEST_F(MDLSyncTest, IsDestroyedFindLockOwner) { |
| 635 | MDLSyncThread thread1("table", MDL_EXCLUSIVE, |
| 636 | "mdl_remove_random_unused_after_is_destroyed_set " |
| 637 | "SIGNAL marked WAIT_FOR resume_removal", |
| 638 | nullptr, nullptr); |
| 639 | MDL_key mdl_key(MDL_key::TABLE, "db", "table"); |
| 640 | MDLSyncContextVisitor dummy_visitor; |
| 641 | |
| 642 | /* |
| 643 | Start a thread which acquires X lock on a table and immediately releases |
| 644 | it. As result the MDL_lock object for the table becomes unused and |
| 645 | attempt to destroy this unused object is made. MDL_lock is marked |
| 646 | as destroyed, but the thread hits sync point before the object is |
| 647 | removed from the MDL_map hash. |
| 648 | */ |
| 649 | thread1.start(); |
| 650 | |
| 651 | /* Wait until MDL_lock object is marked as destroyed. */ |
| 652 | Mock_error_handler error_handler(m_thd, 0); |
| 653 | EXPECT_FALSE(debug_sync_set_action(m_thd, "now WAIT_FOR marked")); |
| 654 | /* The above should not generate warnings about timeouts. */ |
| 655 | EXPECT_EQ(0, error_handler.handle_called()); |
| 656 | |
| 657 | /* |
| 658 | Ensure that once MDL_context::find_lock_owner() sees object marked |
| 659 | as destroyed it resumes the above thread. |
| 660 | */ |
| 661 | EXPECT_FALSE(debug_sync_set_action( |
| 662 | m_thd, "mdl_find_lock_owner_is_destroyed SIGNAL resume_removal")); |
| 663 | EXPECT_EQ(0, error_handler.handle_called()); |
| 664 | |
| 665 | /* |
| 666 | MDL_context::find_lock_owner() should see MDL_lock object which is |
| 667 | marked as destroyed and unblock thread performing its destruction. |
| 668 | */ |
| 669 | EXPECT_FALSE(m_thd->mdl_context.find_lock_owner(&mdl_key, &dummy_visitor)); |
| 670 | |
| 671 | /* Check that thread performing destruction of MDL_lock object has finished. |
| 672 | */ |
| 673 | thread1.join(); |
| 674 | } |
| 675 | |
| 676 | } // namespace mdl_sync_unittest |
| 677 | #endif |