| 1 | /* Copyright (c) 2009, 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 | This is a unit test for the 'meta data locking' classes. |
| 26 | It is written to illustrate how we can use Google Test for unit testing |
| 27 | of MySQL code. |
| 28 | For documentation on Google Test, see http://code.google.com/p/googletest/ |
| 29 | and the contained wiki pages GoogleTestPrimer and GoogleTestAdvancedGuide. |
| 30 | The code below should hopefully be (mostly) self-explanatory. |
| 31 | */ |
| 32 | |
| 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include <gtest/gtest.h> |
| 38 | #include <stddef.h> |
| 39 | #include <sys/types.h> |
| 40 | |
| 41 | #include "my_dbug.h" |
| 42 | #include "my_inttypes.h" |
| 43 | #include "mysqld_error.h" |
| 44 | #include "sql/mdl.h" |
| 45 | #include "sql/mysqld.h" |
| 46 | #include "sql/thr_malloc.h" |
| 47 | #include "unittest/gunit/benchmark.h" |
| 48 | #include "unittest/gunit/test_mdl_context_owner.h" |
| 49 | #include "unittest/gunit/thread_utils.h" |
| 50 | |
| 51 | /* |
| 52 | Mock thd_wait_begin/end functions |
| 53 | */ |
| 54 | |
| 55 | void thd_wait_begin(THD *, int) {} |
| 56 | |
| 57 | void thd_wait_end(THD *) {} |
| 58 | |
| 59 | /* |
| 60 | A mock error handler. |
| 61 | */ |
| 62 | static uint expected_error = 0; |
| 63 | |
| 64 | // This is needed to verify that an error was indeed reported. If |
| 65 | // my_error() is NOT called as expected, test_error_handler_hook will |
| 66 | // not be called, so the EXPECT_EQ below will not fire, even if |
| 67 | // expected_error has been assigned a non-zero value. |
| 68 | static uint reported_error = 0; |
| 69 | extern "C" void test_error_handler_hook(uint err, const char *str, myf) { |
| 70 | // Record the error reported, so that it becomes possible to verify |
| 71 | // that an error was actually reported |
| 72 | reported_error = err; |
| 73 | EXPECT_EQ(expected_error, err) << str; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | Mock away this global function. |
| 78 | We don't need DEBUG_SYNC functionality in a unit test. |
| 79 | */ |
| 80 | void debug_sync(THD *, const char *sync_point_name [[maybe_unused]], size_t) { |
| 81 | DBUG_PRINT("debug_sync_point", ("hit: '%s'", sync_point_name)); |
| 82 | FAIL() << "Not yet implemented."; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | Putting everything in a namespace prevents any (unintentional) |
| 87 | name clashes with the code under test. |
| 88 | */ |
| 89 | namespace mdl_unittest { |
| 90 | |
| 91 | bool test_drive_fix_pins(MDL_context *cp) { return cp->fix_pins(); } |
| 92 | |
| 93 | using thread::Notification; |
| 94 | using thread::Thread; |
| 95 | |
| 96 | const char db_name[] = "some_database"; |
| 97 | const char table_name1[] = "some_table1"; |
| 98 | const char table_name2[] = "some_table2"; |
| 99 | const char table_name3[] = "some_table3"; |
| 100 | const char table_name4[] = "some_table4"; |
| 101 | const ulong zero_timeout = 0; |
| 102 | const ulong long_timeout = (ulong)3600L * 24L * 365L; |
| 103 | |
| 104 | class MDLTest : public ::testing::Test, public Test_MDL_context_owner { |
| 105 | protected: |
| 106 | MDLTest() : m_null_ticket(nullptr), m_null_request(nullptr) {} |
| 107 | |
| 108 | static void SetUpTestCase() { |
| 109 | /* Save original and install our custom error hook. */ |
| 110 | m_old_error_handler_hook = error_handler_hook; |
| 111 | error_handler_hook = test_error_handler_hook; |
| 112 | } |
| 113 | |
| 114 | static void TearDownTestCase() { |
| 115 | error_handler_hook = m_old_error_handler_hook; |
| 116 | } |
| 117 | |
| 118 | void SetUp() override { |
| 119 | expected_error = 0; |
| 120 | reported_error = 0; |
| 121 | |
| 122 | mdl_locks_unused_locks_low_water = MDL_LOCKS_UNUSED_LOCKS_LOW_WATER_DEFAULT; |
| 123 | max_write_lock_count = ULONG_MAX; |
| 124 | mdl_init(); |
| 125 | m_mdl_context.init(this); |
| 126 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 127 | m_charset = system_charset_info; |
| 128 | system_charset_info = &my_charset_utf8mb3_bin; |
| 129 | EXPECT_TRUE(system_charset_info != nullptr); |
| 130 | |
| 131 | MDL_REQUEST_INIT(&m_global_request, MDL_key::GLOBAL, "", "", |
| 132 | MDL_INTENTION_EXCLUSIVE, MDL_TRANSACTION); |
| 133 | } |
| 134 | |
| 135 | void TearDown() override { |
| 136 | // Verify that the error handling hook has indeed been called if an error |
| 137 | // was expected. |
| 138 | EXPECT_TRUE((expected_error == 0 || reported_error > 0)); |
| 139 | system_charset_info = m_charset; |
| 140 | m_mdl_context.destroy(); |
| 141 | mdl_destroy(); |
| 142 | } |
| 143 | |
| 144 | void notify_shared_lock(MDL_context_owner *in_use, |
| 145 | bool needs_thr_lock_abort) override { |
| 146 | in_use->notify_shared_lock(nullptr, needs_thr_lock_abort); |
| 147 | } |
| 148 | |
| 149 | // A utility member for testing single lock requests. |
| 150 | void test_one_simple_shared_lock(enum_mdl_type lock_type); |
| 151 | |
| 152 | const MDL_ticket *m_null_ticket; |
| 153 | const MDL_request *m_null_request; |
| 154 | MDL_context m_mdl_context; |
| 155 | MDL_request m_request; |
| 156 | MDL_request m_global_request; |
| 157 | MDL_request_list m_request_list; |
| 158 | |
| 159 | private: |
| 160 | CHARSET_INFO *m_charset; |
| 161 | MDLTest(MDLTest const &) = delete; |
| 162 | MDLTest &operator=(MDLTest const &) = delete; |
| 163 | |
| 164 | static ErrorHandlerFunctionPointer m_old_error_handler_hook; |
| 165 | }; |
| 166 | |
| 167 | ErrorHandlerFunctionPointer MDLTest::m_old_error_handler_hook; |
| 168 | |
| 169 | /* |
| 170 | Will grab a lock on table_name of given type in the run() function. |
| 171 | The two notifications are for synchronizing with the main thread. |
| 172 | Does *not* take ownership of the notifications. |
| 173 | */ |
| 174 | class MDL_thread : public Thread, public Test_MDL_context_owner { |
| 175 | public: |
| 176 | MDL_thread(const char *table_name, enum_mdl_type mdl_type, |
| 177 | Notification *lock_grabbed, Notification *release_locks, |
| 178 | Notification *lock_blocked, Notification *lock_released) |
| 179 | : m_table_name(table_name), |
| 180 | m_mdl_type(mdl_type), |
| 181 | m_lock_grabbed(lock_grabbed), |
| 182 | m_release_locks(release_locks), |
| 183 | m_lock_blocked(lock_blocked), |
| 184 | m_lock_released(lock_released), |
| 185 | m_enable_release_on_notify(false) { |
| 186 | m_mdl_context.init(this); |
| 187 | } |
| 188 | |
| 189 | ~MDL_thread() override { m_mdl_context.destroy(); } |
| 190 | |
| 191 | void run() override; |
| 192 | void enable_release_on_notify() { m_enable_release_on_notify = true; } |
| 193 | |
| 194 | void notify_shared_lock(MDL_context_owner *in_use, |
| 195 | bool needs_thr_lock_abort) override { |
| 196 | if (in_use) |
| 197 | in_use->notify_shared_lock(nullptr, needs_thr_lock_abort); |
| 198 | else if (m_enable_release_on_notify && m_release_locks) |
| 199 | m_release_locks->notify(); |
| 200 | } |
| 201 | |
| 202 | void enter_cond(mysql_cond_t *cond, mysql_mutex_t *mutex, |
| 203 | const PSI_stage_info *stage, PSI_stage_info *old_stage, |
| 204 | const char *src_function, const char *src_file, |
| 205 | int src_line) override { |
| 206 | Test_MDL_context_owner::enter_cond(cond, mutex, stage, old_stage, |
| 207 | src_function, src_file, src_line); |
| 208 | |
| 209 | /* |
| 210 | No extra checks needed here since MDL uses enter_con only when thread |
| 211 | is blocked. |
| 212 | */ |
| 213 | if (m_lock_blocked) m_lock_blocked->notify(); |
| 214 | |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | MDL_context &get_mdl_context() { return m_mdl_context; } |
| 219 | |
| 220 | private: |
| 221 | const char *m_table_name; |
| 222 | enum_mdl_type m_mdl_type; |
| 223 | Notification *m_lock_grabbed; |
| 224 | Notification *m_release_locks; |
| 225 | Notification *m_lock_blocked; |
| 226 | Notification *m_lock_released; |
| 227 | bool m_enable_release_on_notify; |
| 228 | MDL_context m_mdl_context; |
| 229 | }; |
| 230 | |
| 231 | void MDL_thread::run() { |
| 232 | MDL_request request; |
| 233 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, m_table_name, m_mdl_type, |
| 234 | MDL_TRANSACTION); |
| 235 | |
| 236 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 237 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 238 | MDL_key::TABLE, db_name, m_table_name, m_mdl_type)); |
| 239 | |
| 240 | // Tell the main thread that we have grabbed our locks. |
| 241 | if (m_lock_grabbed) m_lock_grabbed->notify(); |
| 242 | |
| 243 | // Hold on to locks until we are told to release them |
| 244 | if (m_release_locks) m_release_locks->wait_for_notification(); |
| 245 | |
| 246 | m_mdl_context.release_transactional_locks(); |
| 247 | |
| 248 | // Tell the main thread that grabbed lock is released. |
| 249 | if (m_lock_released) m_lock_released->notify(); |
| 250 | } |
| 251 | |
| 252 | // Google Test recommends DeathTest suffix for classes use in death tests. |
| 253 | typedef MDLTest MDLDeathTest; |
| 254 | |
| 255 | /* |
| 256 | Verifies that we die with a assert if we destry a non-empty MDL_context. |
| 257 | */ |
| 258 | #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 259 | TEST_F(MDLDeathTest, DieWhenMTicketsNonempty) { |
| 260 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 261 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 262 | MDL_TRANSACTION); |
| 263 | |
| 264 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 265 | EXPECT_DEATH(m_mdl_context.destroy(), |
| 266 | ".*Assertion.*m_ticket_store.*is_empty.*"); |
| 267 | m_mdl_context.release_transactional_locks(); |
| 268 | } |
| 269 | #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 270 | |
| 271 | /* |
| 272 | The most basic test: just construct and destruct our test fixture. |
| 273 | */ |
| 274 | TEST_F(MDLTest, ConstructAndDestruct) {} |
| 275 | |
| 276 | void MDLTest::test_one_simple_shared_lock(enum_mdl_type lock_type) { |
| 277 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, lock_type, |
| 278 | MDL_TRANSACTION); |
| 279 | |
| 280 | EXPECT_EQ(lock_type, m_request.type); |
| 281 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 282 | |
| 283 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 284 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 285 | EXPECT_TRUE(m_mdl_context.has_locks()); |
| 286 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 287 | MDL_key::TABLE, db_name, table_name1, lock_type)); |
| 288 | |
| 289 | MDL_request request_2; |
| 290 | MDL_REQUEST_INIT_BY_KEY(&request_2, &m_request.key, lock_type, |
| 291 | MDL_TRANSACTION); |
| 292 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2)); |
| 293 | EXPECT_EQ(m_request.ticket, request_2.ticket); |
| 294 | |
| 295 | m_mdl_context.release_transactional_locks(); |
| 296 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | Acquires one lock of type MDL_SHARED. |
| 301 | */ |
| 302 | TEST_F(MDLTest, OneShared) { test_one_simple_shared_lock(MDL_SHARED); } |
| 303 | |
| 304 | /* |
| 305 | Acquires one lock of type MDL_SHARED_HIGH_PRIO. |
| 306 | */ |
| 307 | TEST_F(MDLTest, OneSharedHighPrio) { |
| 308 | test_one_simple_shared_lock(MDL_SHARED_HIGH_PRIO); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | Acquires one lock of type MDL_SHARED_READ. |
| 313 | */ |
| 314 | TEST_F(MDLTest, OneSharedRead) { test_one_simple_shared_lock(MDL_SHARED_READ); } |
| 315 | |
| 316 | /* |
| 317 | Acquires one lock of type MDL_SHARED_WRITE. |
| 318 | */ |
| 319 | TEST_F(MDLTest, OneSharedWrite) { |
| 320 | test_one_simple_shared_lock(MDL_SHARED_WRITE); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | Acquires one lock of type MDL_EXCLUSIVE. |
| 325 | */ |
| 326 | TEST_F(MDLTest, OneExclusive) { |
| 327 | const enum_mdl_type lock_type = MDL_EXCLUSIVE; |
| 328 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, lock_type, |
| 329 | MDL_TRANSACTION); |
| 330 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 331 | |
| 332 | m_request_list.push_front(&m_request); |
| 333 | m_request_list.push_front(&m_global_request); |
| 334 | |
| 335 | EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout)); |
| 336 | |
| 337 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 338 | EXPECT_NE(m_null_ticket, m_global_request.ticket); |
| 339 | EXPECT_TRUE(m_mdl_context.has_locks()); |
| 340 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 341 | MDL_key::TABLE, db_name, table_name1, lock_type)); |
| 342 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 343 | MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE)); |
| 344 | EXPECT_TRUE(m_request.ticket->is_upgradable_or_exclusive()); |
| 345 | |
| 346 | m_mdl_context.release_transactional_locks(); |
| 347 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | Acquires two locks, on different tables, of type MDL_SHARED. |
| 352 | Verifies that they are independent. |
| 353 | */ |
| 354 | TEST_F(MDLTest, TwoShared) { |
| 355 | MDL_request request_2; |
| 356 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 357 | MDL_EXPLICIT); |
| 358 | MDL_REQUEST_INIT(&request_2, MDL_key::TABLE, db_name, table_name2, MDL_SHARED, |
| 359 | MDL_EXPLICIT); |
| 360 | |
| 361 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 362 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2)); |
| 363 | EXPECT_TRUE(m_mdl_context.has_locks()); |
| 364 | ASSERT_NE(m_null_ticket, m_request.ticket); |
| 365 | ASSERT_NE(m_null_ticket, request_2.ticket); |
| 366 | |
| 367 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 368 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 369 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 370 | MDL_key::TABLE, db_name, table_name2, MDL_SHARED)); |
| 371 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 372 | MDL_key::TABLE, db_name, table_name3, MDL_SHARED)); |
| 373 | |
| 374 | m_mdl_context.release_lock(m_request.ticket); |
| 375 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 376 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 377 | EXPECT_TRUE(m_mdl_context.has_locks()); |
| 378 | |
| 379 | m_mdl_context.release_lock(request_2.ticket); |
| 380 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 381 | MDL_key::TABLE, db_name, table_name2, MDL_SHARED)); |
| 382 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | Verifies that two different contexts can acquire a shared lock |
| 387 | on the same table. |
| 388 | */ |
| 389 | TEST_F(MDLTest, SharedLocksBetweenContexts) { |
| 390 | MDL_context mdl_context2; |
| 391 | mdl_context2.init(this); |
| 392 | MDL_request request_2; |
| 393 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 394 | MDL_TRANSACTION); |
| 395 | MDL_REQUEST_INIT(&request_2, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 396 | MDL_TRANSACTION); |
| 397 | |
| 398 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 399 | EXPECT_FALSE(mdl_context2.try_acquire_lock(&request_2)); |
| 400 | |
| 401 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 402 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 403 | EXPECT_TRUE(mdl_context2.owns_equal_or_stronger_lock( |
| 404 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 405 | |
| 406 | m_mdl_context.release_transactional_locks(); |
| 407 | mdl_context2.release_transactional_locks(); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | Verifies that we can upgrade a shared lock to exclusive. |
| 412 | */ |
| 413 | TEST_F(MDLTest, UpgradeSharedUpgradable) { |
| 414 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 415 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 416 | |
| 417 | m_request_list.push_front(&m_request); |
| 418 | m_request_list.push_front(&m_global_request); |
| 419 | |
| 420 | EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout)); |
| 421 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 422 | MDL_EXCLUSIVE, long_timeout)); |
| 423 | EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type()); |
| 424 | |
| 425 | // Another upgrade should be a no-op. |
| 426 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 427 | MDL_EXCLUSIVE, long_timeout)); |
| 428 | EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type()); |
| 429 | |
| 430 | m_mdl_context.release_transactional_locks(); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | Verfies that locks are released when we roll back to a savepoint. |
| 435 | */ |
| 436 | TEST_F(MDLTest, SavePoint) { |
| 437 | MDL_request request_2; |
| 438 | MDL_request request_3; |
| 439 | MDL_request request_4; |
| 440 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 441 | MDL_TRANSACTION); |
| 442 | MDL_REQUEST_INIT(&request_2, MDL_key::TABLE, db_name, table_name2, MDL_SHARED, |
| 443 | MDL_TRANSACTION); |
| 444 | MDL_REQUEST_INIT(&request_3, MDL_key::TABLE, db_name, table_name3, MDL_SHARED, |
| 445 | MDL_TRANSACTION); |
| 446 | MDL_REQUEST_INIT(&request_4, MDL_key::TABLE, db_name, table_name4, MDL_SHARED, |
| 447 | MDL_TRANSACTION); |
| 448 | |
| 449 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 450 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2)); |
| 451 | MDL_savepoint savepoint = m_mdl_context.mdl_savepoint(); |
| 452 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_3)); |
| 453 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_4)); |
| 454 | |
| 455 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 456 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 457 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 458 | MDL_key::TABLE, db_name, table_name2, MDL_SHARED)); |
| 459 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 460 | MDL_key::TABLE, db_name, table_name3, MDL_SHARED)); |
| 461 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 462 | MDL_key::TABLE, db_name, table_name4, MDL_SHARED)); |
| 463 | |
| 464 | m_mdl_context.rollback_to_savepoint(savepoint); |
| 465 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 466 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 467 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 468 | MDL_key::TABLE, db_name, table_name2, MDL_SHARED)); |
| 469 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 470 | MDL_key::TABLE, db_name, table_name3, MDL_SHARED)); |
| 471 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 472 | MDL_key::TABLE, db_name, table_name4, MDL_SHARED)); |
| 473 | |
| 474 | m_mdl_context.release_transactional_locks(); |
| 475 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 476 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 477 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 478 | MDL_key::TABLE, db_name, table_name2, MDL_SHARED)); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | Verifies that we can grab shared locks concurrently, in different threads. |
| 483 | */ |
| 484 | TEST_F(MDLTest, ConcurrentShared) { |
| 485 | Notification lock_grabbed; |
| 486 | Notification release_locks; |
| 487 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks, |
| 488 | nullptr, nullptr); |
| 489 | mdl_thread.start(); |
| 490 | lock_grabbed.wait_for_notification(); |
| 491 | |
| 492 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 493 | MDL_TRANSACTION); |
| 494 | |
| 495 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 496 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 497 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED)); |
| 498 | |
| 499 | release_locks.notify(); |
| 500 | mdl_thread.join(); |
| 501 | |
| 502 | m_mdl_context.release_transactional_locks(); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | Verifies that we cannot grab an exclusive lock on something which |
| 507 | is locked with a shared lock in a different thread. |
| 508 | */ |
| 509 | TEST_F(MDLTest, ConcurrentSharedExclusive) { |
| 510 | expected_error = ER_LOCK_WAIT_TIMEOUT; |
| 511 | |
| 512 | Notification lock_grabbed; |
| 513 | Notification release_locks; |
| 514 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks, |
| 515 | nullptr, nullptr); |
| 516 | mdl_thread.start(); |
| 517 | lock_grabbed.wait_for_notification(); |
| 518 | |
| 519 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 520 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 521 | |
| 522 | m_request_list.push_front(&m_request); |
| 523 | m_request_list.push_front(&m_global_request); |
| 524 | |
| 525 | // We should *not* be able to grab the lock here. |
| 526 | EXPECT_TRUE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout)); |
| 527 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 528 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 529 | |
| 530 | release_locks.notify(); |
| 531 | mdl_thread.join(); |
| 532 | |
| 533 | // Now we should be able to grab the lock. |
| 534 | EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout)); |
| 535 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 536 | |
| 537 | m_mdl_context.release_transactional_locks(); |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | Verifies that we cannot we cannot grab a shared lock on something which |
| 542 | is locked exlusively in a different thread. |
| 543 | */ |
| 544 | TEST_F(MDLTest, ConcurrentExclusiveShared) { |
| 545 | Notification lock_grabbed; |
| 546 | Notification release_locks; |
| 547 | MDL_thread mdl_thread(table_name1, MDL_EXCLUSIVE, &lock_grabbed, |
| 548 | &release_locks, nullptr, nullptr); |
| 549 | mdl_thread.start(); |
| 550 | lock_grabbed.wait_for_notification(); |
| 551 | |
| 552 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 553 | MDL_TRANSACTION); |
| 554 | |
| 555 | // We should *not* be able to grab the lock here. |
| 556 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 557 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 558 | |
| 559 | release_locks.notify(); |
| 560 | |
| 561 | // The other thread should eventually release its locks. |
| 562 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 563 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 564 | |
| 565 | mdl_thread.join(); |
| 566 | m_mdl_context.release_transactional_locks(); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | Verifies the following scenario: |
| 571 | Thread 1: grabs a shared upgradable lock. |
| 572 | Thread 2: grabs a shared lock. |
| 573 | Thread 1: asks for an upgrade to exclusive (needs to wait for thread 2) |
| 574 | Thread 2: gets notified, and releases lock. |
| 575 | Thread 1: gets the exclusive lock. |
| 576 | */ |
| 577 | TEST_F(MDLTest, ConcurrentUpgrade) { |
| 578 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 579 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 580 | m_request_list.push_front(&m_request); |
| 581 | m_request_list.push_front(&m_global_request); |
| 582 | |
| 583 | EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout)); |
| 584 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 585 | MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE)); |
| 586 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 587 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 588 | |
| 589 | Notification lock_grabbed; |
| 590 | Notification release_locks; |
| 591 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks, |
| 592 | nullptr, nullptr); |
| 593 | mdl_thread.enable_release_on_notify(); |
| 594 | mdl_thread.start(); |
| 595 | lock_grabbed.wait_for_notification(); |
| 596 | |
| 597 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 598 | MDL_EXCLUSIVE, long_timeout)); |
| 599 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 600 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 601 | |
| 602 | mdl_thread.join(); |
| 603 | m_mdl_context.release_transactional_locks(); |
| 604 | } |
| 605 | |
| 606 | TEST_F(MDLTest, UpgradableConcurrency) { |
| 607 | MDL_request request_2; |
| 608 | MDL_request_list request_list; |
| 609 | Notification lock_grabbed; |
| 610 | Notification release_locks; |
| 611 | MDL_thread mdl_thread(table_name1, MDL_SHARED_UPGRADABLE, &lock_grabbed, |
| 612 | &release_locks, nullptr, nullptr); |
| 613 | mdl_thread.start(); |
| 614 | lock_grabbed.wait_for_notification(); |
| 615 | |
| 616 | // We should be able to take a SW lock. |
| 617 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 618 | MDL_SHARED_WRITE, MDL_TRANSACTION); |
| 619 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 620 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 621 | |
| 622 | // But SHARED_UPGRADABLE is not compatible with itself |
| 623 | expected_error = ER_LOCK_WAIT_TIMEOUT; |
| 624 | MDL_REQUEST_INIT(&request_2, MDL_key::TABLE, db_name, table_name1, |
| 625 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 626 | request_list.push_front(&m_global_request); |
| 627 | request_list.push_front(&request_2); |
| 628 | EXPECT_TRUE(m_mdl_context.acquire_locks(&request_list, zero_timeout)); |
| 629 | EXPECT_EQ(m_null_ticket, request_2.ticket); |
| 630 | |
| 631 | release_locks.notify(); |
| 632 | |
| 633 | mdl_thread.join(); |
| 634 | m_mdl_context.release_transactional_locks(); |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | Test compatibility matrice for MDL_SHARED_WRITE_LOW_PRIO lock. |
| 639 | */ |
| 640 | |
| 641 | TEST_F(MDLTest, SharedWriteLowPrioCompatibility) { |
| 642 | enum_mdl_type compatible[] = { |
| 643 | MDL_SHARED, MDL_SHARED_HIGH_PRIO, MDL_SHARED_READ, |
| 644 | MDL_SHARED_WRITE, MDL_SHARED_WRITE_LOW_PRIO, MDL_SHARED_UPGRADABLE}; |
| 645 | enum_mdl_type incompatible[] = {MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE, |
| 646 | MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE}; |
| 647 | enum_mdl_type higher_prio[] = {MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE, |
| 648 | MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE}; |
| 649 | Notification lock_grabbed; |
| 650 | Notification release_lock; |
| 651 | MDL_thread mdl_thread(table_name1, MDL_SHARED_WRITE_LOW_PRIO, &lock_grabbed, |
| 652 | &release_lock, nullptr, nullptr); |
| 653 | uint i; |
| 654 | |
| 655 | // Start thread which will acquire SWLP lock and pause. |
| 656 | mdl_thread.start(); |
| 657 | lock_grabbed.wait_for_notification(); |
| 658 | |
| 659 | // We should be able to take all locks from compatible list |
| 660 | for (i = 0; i < sizeof(compatible) / sizeof(enum_mdl_type); ++i) { |
| 661 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 662 | compatible[i], MDL_TRANSACTION); |
| 663 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 664 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 665 | m_mdl_context.release_transactional_locks(); |
| 666 | } |
| 667 | |
| 668 | // But none of the locks from incompatible list |
| 669 | for (i = 0; i < sizeof(incompatible) / sizeof(enum_mdl_type); ++i) { |
| 670 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 671 | incompatible[i], MDL_TRANSACTION); |
| 672 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 673 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 674 | } |
| 675 | |
| 676 | release_lock.notify(); |
| 677 | mdl_thread.join(); |
| 678 | |
| 679 | // Check that SWLP lock can be acquired when any of compatible locks is active |
| 680 | for (i = 0; i < sizeof(compatible) / sizeof(enum_mdl_type); ++i) { |
| 681 | Notification second_grabbed; |
| 682 | Notification second_release; |
| 683 | MDL_thread mdl_thread2(table_name1, compatible[i], &second_grabbed, |
| 684 | &second_release, nullptr, nullptr); |
| 685 | |
| 686 | // Start thread that will acquire one of locks from compatible list |
| 687 | mdl_thread2.start(); |
| 688 | second_grabbed.wait_for_notification(); |
| 689 | |
| 690 | // Acquisition of SWLP should succeed |
| 691 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 692 | MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION); |
| 693 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 694 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 695 | m_mdl_context.release_transactional_locks(); |
| 696 | |
| 697 | second_release.notify(); |
| 698 | mdl_thread2.join(); |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | Check that SWLP lock can't be acquired when any of incompatible locks |
| 703 | is active. |
| 704 | */ |
| 705 | for (i = 0; i < sizeof(incompatible) / sizeof(enum_mdl_type); ++i) { |
| 706 | Notification third_grabbed; |
| 707 | Notification third_release; |
| 708 | MDL_thread mdl_thread3(table_name1, incompatible[i], &third_grabbed, |
| 709 | &third_release, nullptr, nullptr); |
| 710 | |
| 711 | // Start thread that will acquire one of locks from incompatible list |
| 712 | mdl_thread3.start(); |
| 713 | third_grabbed.wait_for_notification(); |
| 714 | |
| 715 | // Acquisition of SWLP should fail. |
| 716 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 717 | MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION); |
| 718 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 719 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 720 | |
| 721 | third_release.notify(); |
| 722 | mdl_thread3.join(); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | Check that SWLP lock can't be acquired if one of higher-prio locks is |
| 727 | pending. |
| 728 | */ |
| 729 | for (i = 0; i < sizeof(higher_prio) / sizeof(enum_mdl_type); ++i) { |
| 730 | Notification fourth_grabbed; |
| 731 | Notification fourth_release; |
| 732 | Notification fifth_blocked; |
| 733 | MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE, &fourth_grabbed, |
| 734 | &fourth_release, nullptr, nullptr); |
| 735 | MDL_thread mdl_thread5(table_name1, higher_prio[i], nullptr, nullptr, |
| 736 | &fifth_blocked, nullptr); |
| 737 | |
| 738 | // Acquire SW lock on the table. |
| 739 | mdl_thread4.start(); |
| 740 | fourth_grabbed.wait_for_notification(); |
| 741 | |
| 742 | // Ensure that there is pending high-prio lock. |
| 743 | mdl_thread5.start(); |
| 744 | fifth_blocked.wait_for_notification(); |
| 745 | |
| 746 | // Acquisition of SWLP should fail because there is pending high-prio lock. |
| 747 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 748 | MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION); |
| 749 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 750 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 751 | |
| 752 | fourth_release.notify(); |
| 753 | mdl_thread4.join(); |
| 754 | mdl_thread5.join(); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | Check that higher-prio locks can be acquired even if there |
| 759 | is pending SWLP lock. |
| 760 | */ |
| 761 | for (i = 0; i < sizeof(higher_prio) / sizeof(enum_mdl_type); ++i) { |
| 762 | Notification sixth_grabbed; |
| 763 | Notification sixth_release; |
| 764 | Notification seventh_blocked; |
| 765 | Notification seventh_grabbed; |
| 766 | Notification eighth_blocked; |
| 767 | Notification eighth_release; |
| 768 | MDL_thread mdl_thread6(table_name1, MDL_EXCLUSIVE, &sixth_grabbed, |
| 769 | &sixth_release, nullptr, nullptr); |
| 770 | MDL_thread mdl_thread7(table_name1, higher_prio[i], &seventh_grabbed, |
| 771 | nullptr, &seventh_blocked, nullptr); |
| 772 | MDL_thread mdl_thread8(table_name1, MDL_SHARED_WRITE_LOW_PRIO, nullptr, |
| 773 | &eighth_release, &eighth_blocked, nullptr); |
| 774 | |
| 775 | // Acquire X lock on the table. |
| 776 | mdl_thread6.start(); |
| 777 | sixth_grabbed.wait_for_notification(); |
| 778 | |
| 779 | // Ensure that there is pending high-prio lock. |
| 780 | mdl_thread7.start(); |
| 781 | seventh_blocked.wait_for_notification(); |
| 782 | |
| 783 | // Ensure that there is pending SWLP after it. |
| 784 | mdl_thread8.start(); |
| 785 | eighth_blocked.wait_for_notification(); |
| 786 | |
| 787 | // Release X lock. |
| 788 | sixth_release.notify(); |
| 789 | mdl_thread6.join(); |
| 790 | |
| 791 | /* |
| 792 | This should unblock high-prio lock and not SWLP (otherwise we will |
| 793 | wait for SWLP release. |
| 794 | */ |
| 795 | seventh_grabbed.wait_for_notification(); |
| 796 | mdl_thread7.join(); |
| 797 | |
| 798 | // After this SWLP lock will be granted and can be released |
| 799 | eighth_release.notify(); |
| 800 | mdl_thread8.join(); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | Test compatibility matrice for MDL_SHARED_READ_ONLY lock. |
| 806 | */ |
| 807 | |
| 808 | TEST_F(MDLTest, SharedReadOnlyCompatibility) { |
| 809 | enum_mdl_type compatible[] = {MDL_SHARED, MDL_SHARED_HIGH_PRIO, |
| 810 | MDL_SHARED_READ, MDL_SHARED_UPGRADABLE, |
| 811 | MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE}; |
| 812 | enum_mdl_type incompatible[] = {MDL_SHARED_WRITE, MDL_SHARED_WRITE_LOW_PRIO, |
| 813 | MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE}; |
| 814 | enum_mdl_type higher_prio[] = {MDL_SHARED_WRITE, MDL_SHARED_NO_READ_WRITE, |
| 815 | MDL_EXCLUSIVE}; |
| 816 | Notification lock_grabbed; |
| 817 | Notification release_lock; |
| 818 | MDL_thread mdl_thread(table_name1, MDL_SHARED_READ_ONLY, &lock_grabbed, |
| 819 | &release_lock, nullptr, nullptr); |
| 820 | uint i; |
| 821 | |
| 822 | // Start thread which will acquire SRO lock and pause. |
| 823 | mdl_thread.start(); |
| 824 | lock_grabbed.wait_for_notification(); |
| 825 | |
| 826 | // We should be able to take all locks from compatible list |
| 827 | for (i = 0; i < sizeof(compatible) / sizeof(enum_mdl_type); ++i) { |
| 828 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 829 | compatible[i], MDL_TRANSACTION); |
| 830 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 831 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 832 | m_mdl_context.release_transactional_locks(); |
| 833 | } |
| 834 | |
| 835 | // But none of the locks from incompatible list |
| 836 | for (i = 0; i < sizeof(incompatible) / sizeof(enum_mdl_type); ++i) { |
| 837 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 838 | incompatible[i], MDL_TRANSACTION); |
| 839 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 840 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 841 | } |
| 842 | |
| 843 | release_lock.notify(); |
| 844 | mdl_thread.join(); |
| 845 | |
| 846 | // Check that SRO lock can be acquired when any of compatible locks is active |
| 847 | for (i = 0; i < sizeof(compatible) / sizeof(enum_mdl_type); ++i) { |
| 848 | Notification second_grabbed; |
| 849 | Notification second_release; |
| 850 | MDL_thread mdl_thread2(table_name1, compatible[i], &second_grabbed, |
| 851 | &second_release, nullptr, nullptr); |
| 852 | |
| 853 | // Start thread that will acquire one of locks from compatible list |
| 854 | mdl_thread2.start(); |
| 855 | second_grabbed.wait_for_notification(); |
| 856 | |
| 857 | // Acquisition of SRO should succeed |
| 858 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 859 | MDL_SHARED_READ_ONLY, MDL_TRANSACTION); |
| 860 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 861 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 862 | m_mdl_context.release_transactional_locks(); |
| 863 | |
| 864 | second_release.notify(); |
| 865 | mdl_thread2.join(); |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | Check that SRO lock can't be acquired when any of incompatible locks |
| 870 | is active. |
| 871 | */ |
| 872 | for (i = 0; i < sizeof(incompatible) / sizeof(enum_mdl_type); ++i) { |
| 873 | Notification third_grabbed; |
| 874 | Notification third_release; |
| 875 | MDL_thread mdl_thread3(table_name1, incompatible[i], &third_grabbed, |
| 876 | &third_release, nullptr, nullptr); |
| 877 | |
| 878 | // Start thread that will acquire one of locks from incompatible list |
| 879 | mdl_thread3.start(); |
| 880 | third_grabbed.wait_for_notification(); |
| 881 | |
| 882 | // Acquisition of SRO should fail. |
| 883 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 884 | MDL_SHARED_READ_ONLY, MDL_TRANSACTION); |
| 885 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 886 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 887 | |
| 888 | third_release.notify(); |
| 889 | mdl_thread3.join(); |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | Check that SRO lock can't be acquired if one of higher-prio locks is |
| 894 | pending. |
| 895 | */ |
| 896 | for (i = 0; i < sizeof(higher_prio) / sizeof(enum_mdl_type); ++i) { |
| 897 | Notification fourth_grabbed; |
| 898 | Notification fourth_release; |
| 899 | Notification fifth_blocked; |
| 900 | MDL_thread mdl_thread4(table_name1, MDL_SHARED_READ_ONLY, &fourth_grabbed, |
| 901 | &fourth_release, nullptr, nullptr); |
| 902 | MDL_thread mdl_thread5(table_name1, higher_prio[i], nullptr, nullptr, |
| 903 | &fifth_blocked, nullptr); |
| 904 | |
| 905 | // Acquire SRO lock on the table. |
| 906 | mdl_thread4.start(); |
| 907 | fourth_grabbed.wait_for_notification(); |
| 908 | |
| 909 | // Ensure that there is pending high-prio lock. |
| 910 | mdl_thread5.start(); |
| 911 | fifth_blocked.wait_for_notification(); |
| 912 | |
| 913 | // Acquisition of SRO should fail because there is pending high-prio lock. |
| 914 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 915 | MDL_SHARED_READ_ONLY, MDL_TRANSACTION); |
| 916 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 917 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 918 | |
| 919 | fourth_release.notify(); |
| 920 | mdl_thread4.join(); |
| 921 | mdl_thread5.join(); |
| 922 | } |
| 923 | |
| 924 | // Check that SRO lock can be acquired if there is pending SWLP request. |
| 925 | Notification sixth_grabbed; |
| 926 | Notification sixth_release; |
| 927 | Notification seventh_blocked; |
| 928 | MDL_thread mdl_thread6(table_name1, MDL_SHARED_READ_ONLY, &sixth_grabbed, |
| 929 | &sixth_release, nullptr, nullptr); |
| 930 | MDL_thread mdl_thread7(table_name1, MDL_SHARED_WRITE_LOW_PRIO, nullptr, |
| 931 | nullptr, &seventh_blocked, nullptr); |
| 932 | |
| 933 | // Acquire SRO lock on the table. |
| 934 | mdl_thread6.start(); |
| 935 | sixth_grabbed.wait_for_notification(); |
| 936 | |
| 937 | // Ensure that there is pending SWLP lock. |
| 938 | mdl_thread7.start(); |
| 939 | seventh_blocked.wait_for_notification(); |
| 940 | |
| 941 | // Acquisition of SRO should succeed despite pending SWLP is present |
| 942 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 943 | MDL_SHARED_READ_ONLY, MDL_TRANSACTION); |
| 944 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 945 | EXPECT_NE(m_null_ticket, m_request.ticket); |
| 946 | m_mdl_context.release_transactional_locks(); |
| 947 | |
| 948 | sixth_release.notify(); |
| 949 | mdl_thread6.join(); |
| 950 | mdl_thread7.join(); |
| 951 | |
| 952 | /* |
| 953 | Check that higher-prio locks can be acquired even if there |
| 954 | is pending SRO lock. |
| 955 | */ |
| 956 | for (i = 0; i < sizeof(higher_prio) / sizeof(enum_mdl_type); ++i) { |
| 957 | Notification eighth_grabbed; |
| 958 | Notification eighth_release; |
| 959 | Notification nineth_blocked; |
| 960 | Notification nineth_grabbed; |
| 961 | Notification tenth_blocked; |
| 962 | Notification tenth_release; |
| 963 | MDL_thread mdl_thread8(table_name1, MDL_EXCLUSIVE, &eighth_grabbed, |
| 964 | &eighth_release, nullptr, nullptr); |
| 965 | MDL_thread mdl_thread9(table_name1, higher_prio[i], &nineth_grabbed, |
| 966 | nullptr, &nineth_blocked, nullptr); |
| 967 | MDL_thread mdl_thread10(table_name1, MDL_SHARED_READ_ONLY, nullptr, |
| 968 | &tenth_release, &tenth_blocked, nullptr); |
| 969 | |
| 970 | // Acquire X lock on the table. |
| 971 | mdl_thread8.start(); |
| 972 | eighth_grabbed.wait_for_notification(); |
| 973 | |
| 974 | // Ensure that there is pending high-prio lock. |
| 975 | mdl_thread9.start(); |
| 976 | nineth_blocked.wait_for_notification(); |
| 977 | |
| 978 | // Ensure that there is pending SRO after it. |
| 979 | mdl_thread10.start(); |
| 980 | tenth_blocked.wait_for_notification(); |
| 981 | |
| 982 | // Release X lock. |
| 983 | eighth_release.notify(); |
| 984 | mdl_thread8.join(); |
| 985 | |
| 986 | /* |
| 987 | This should unblock high-prio lock and not SRO (otherwise we will |
| 988 | wait for SRO release. |
| 989 | */ |
| 990 | nineth_grabbed.wait_for_notification(); |
| 991 | mdl_thread9.join(); |
| 992 | |
| 993 | // After this SRO lock will be granted and can be released |
| 994 | tenth_release.notify(); |
| 995 | mdl_thread10.join(); |
| 996 | } |
| 997 | |
| 998 | // Check that SWLP lock can't be acquired if there is pending SRO request. |
| 999 | Notification eleventh_grabbed; |
| 1000 | Notification eleventh_release; |
| 1001 | Notification twelveth_blocked; |
| 1002 | MDL_thread mdl_thread11(table_name1, MDL_SHARED_WRITE, &eleventh_grabbed, |
| 1003 | &eleventh_release, nullptr, nullptr); |
| 1004 | MDL_thread mdl_thread12(table_name1, MDL_SHARED_READ_ONLY, nullptr, nullptr, |
| 1005 | &twelveth_blocked, nullptr); |
| 1006 | |
| 1007 | // Acquire SW lock on the table. |
| 1008 | mdl_thread11.start(); |
| 1009 | eleventh_grabbed.wait_for_notification(); |
| 1010 | |
| 1011 | // Ensure that there is pending SRO lock. |
| 1012 | mdl_thread12.start(); |
| 1013 | twelveth_blocked.wait_for_notification(); |
| 1014 | |
| 1015 | // Acquisition of SWLP should fail. |
| 1016 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 1017 | MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION); |
| 1018 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 1019 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 1020 | |
| 1021 | eleventh_release.notify(); |
| 1022 | mdl_thread11.join(); |
| 1023 | mdl_thread12.join(); |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | Verifies following scenario, |
| 1028 | Low priority lock requests starvation. Lock is granted to high priority |
| 1029 | lock request in wait queue always as max_write_lock_count is a large value. |
| 1030 | - max_write_lock_count == default value i.e ~(ulong)0L |
| 1031 | - THREAD 1: Acquires X lock on the table. |
| 1032 | - THREAD 2: Requests for SR lock on the table. |
| 1033 | - THREAD 3: Requests for SW lock on the table. |
| 1034 | - THREAD 4: Requests for SNRW on the table. |
| 1035 | - THREAD 1: Releases X lock. |
| 1036 | - THREAD 5: Requests for SNRW lock on the table. |
| 1037 | - THREAD 4: Releases SNRW lock. |
| 1038 | - THREAD 2,3: Check whether THREADs got lock on the table. |
| 1039 | Though, THREAD 2,3 requested lock before THREAD 4's SNRW lock and |
| 1040 | THREAD 5's SNRW lock, lock is granted for THREAD 4 and 5. |
| 1041 | */ |
| 1042 | TEST_F(MDLTest, HogLockTest1) { |
| 1043 | Notification thd_lock_grabbed[5]; |
| 1044 | Notification thd_release_locks[5]; |
| 1045 | Notification thd_lock_blocked[5]; |
| 1046 | Notification thd_lock_released[5]; |
| 1047 | |
| 1048 | /* Locks taken by the threads */ |
| 1049 | enum { THD1_X, THD2_SR, THD3_SW, THD4_SNRW, THD5_SNRW }; |
| 1050 | |
| 1051 | /* |
| 1052 | THREAD1: Acquiring X lock on table. |
| 1053 | Lock Wait Queue: <empty> |
| 1054 | Lock granted: <empty> |
| 1055 | */ |
| 1056 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1057 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1058 | &thd_lock_released[THD1_X]); |
| 1059 | mdl_thread1.start(); |
| 1060 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1061 | |
| 1062 | /* |
| 1063 | THREAD2: Requesting SR lock on table. |
| 1064 | Lock Wait Queue: SR |
| 1065 | Lock granted: X |
| 1066 | */ |
| 1067 | MDL_thread mdl_thread2( |
| 1068 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD2_SR], |
| 1069 | &thd_release_locks[THD2_SR], &thd_lock_blocked[THD2_SR], |
| 1070 | &thd_lock_released[THD2_SR]); |
| 1071 | mdl_thread2.start(); |
| 1072 | thd_lock_blocked[THD2_SR].wait_for_notification(); |
| 1073 | |
| 1074 | /* |
| 1075 | THREAD3: Requesting SW lock on table. |
| 1076 | Lock Wait Queue: SR<--SW |
| 1077 | Lock granted: X |
| 1078 | */ |
| 1079 | MDL_thread mdl_thread3( |
| 1080 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD3_SW], |
| 1081 | &thd_release_locks[THD3_SW], &thd_lock_blocked[THD3_SW], |
| 1082 | &thd_lock_released[THD3_SW]); |
| 1083 | mdl_thread3.start(); |
| 1084 | thd_lock_blocked[THD3_SW].wait_for_notification(); |
| 1085 | |
| 1086 | /* |
| 1087 | THREAD4: Requesting SNRW lock on table. |
| 1088 | Lock Wait Queue: SR<--SW<--SNRW |
| 1089 | Lock granted: X |
| 1090 | */ |
| 1091 | MDL_thread mdl_thread4( |
| 1092 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD4_SNRW], |
| 1093 | &thd_release_locks[THD4_SNRW], &thd_lock_blocked[THD4_SNRW], |
| 1094 | &thd_lock_released[THD4_SNRW]); |
| 1095 | mdl_thread4.start(); |
| 1096 | thd_lock_blocked[THD4_SNRW].wait_for_notification(); |
| 1097 | |
| 1098 | /* THREAD 1: Release X lock. */ |
| 1099 | thd_release_locks[THD1_X].notify(); |
| 1100 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1101 | |
| 1102 | /* |
| 1103 | Lock Wait Queue: SR<--SW |
| 1104 | Lock granted: SNRW |
| 1105 | */ |
| 1106 | thd_lock_grabbed[THD4_SNRW].wait_for_notification(); |
| 1107 | |
| 1108 | /* |
| 1109 | THREAD 5: Requests SNRW lock on the table. |
| 1110 | Lock Wait Queue: SR<--SW<--SNRW |
| 1111 | Lock granted: SNRW |
| 1112 | */ |
| 1113 | MDL_thread mdl_thread5( |
| 1114 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD5_SNRW], |
| 1115 | &thd_release_locks[THD5_SNRW], &thd_lock_blocked[THD5_SNRW], |
| 1116 | &thd_lock_released[THD5_SNRW]); |
| 1117 | mdl_thread5.start(); |
| 1118 | thd_lock_blocked[THD5_SNRW].wait_for_notification(); |
| 1119 | |
| 1120 | /* THREAD 4: Release SNRW lock */ |
| 1121 | thd_release_locks[THD4_SNRW].notify(); |
| 1122 | thd_lock_released[THD4_SNRW].wait_for_notification(); |
| 1123 | |
| 1124 | /* THREAD 2: Is Lock granted to me? */ |
| 1125 | EXPECT_FALSE((mdl_thread2.get_mdl_context()) |
| 1126 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1127 | table_name1, MDL_SHARED_READ)); |
| 1128 | /* THREAD 3: Is Lock granted to me? */ |
| 1129 | EXPECT_FALSE((mdl_thread3.get_mdl_context()) |
| 1130 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1131 | table_name1, MDL_SHARED_WRITE)); |
| 1132 | /* |
| 1133 | THREAD 5: Lock is granted to THREAD 5 as priority is higher. |
| 1134 | Lock Wait Queue: SR<--SW |
| 1135 | Lock granted: SNRW |
| 1136 | */ |
| 1137 | thd_lock_grabbed[THD5_SNRW].wait_for_notification(); |
| 1138 | thd_release_locks[THD5_SNRW].notify(); |
| 1139 | thd_lock_released[THD5_SNRW].wait_for_notification(); |
| 1140 | |
| 1141 | /*CLEANUP*/ |
| 1142 | thd_lock_grabbed[THD2_SR].wait_for_notification(); |
| 1143 | thd_release_locks[THD2_SR].notify(); |
| 1144 | thd_lock_released[THD2_SR].wait_for_notification(); |
| 1145 | |
| 1146 | thd_lock_grabbed[THD3_SW].wait_for_notification(); |
| 1147 | thd_release_locks[THD3_SW].notify(); |
| 1148 | thd_lock_released[THD3_SW].wait_for_notification(); |
| 1149 | |
| 1150 | mdl_thread1.join(); |
| 1151 | mdl_thread2.join(); |
| 1152 | mdl_thread3.join(); |
| 1153 | mdl_thread4.join(); |
| 1154 | mdl_thread5.join(); |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | Verifies following scenario, |
| 1159 | After granting max_write_lock_count(=1) number of times for high priority |
| 1160 | lock request, lock is granted to starving low priority lock request |
| 1161 | in wait queue. |
| 1162 | - max_write_lock_count= 1 |
| 1163 | - THREAD 1: Acquires X lock on the table. |
| 1164 | - THREAD 2: Requests for SR lock on the table. |
| 1165 | - THREAD 3: Requests for SW lock on the table. |
| 1166 | - THREAD 4: Requests for SNRW on the table. |
| 1167 | - THREAD 1: Releases X lock. m_hog_lock_count= 1 |
| 1168 | - THREAD 5: Requests for SNRW lock on the table. |
| 1169 | - THREAD 4: Releases SNRW lock. |
| 1170 | - THREAD 2,3: Release lock. |
| 1171 | While releasing X held by THREAD-1, m_hog_lock_count becomes 1 and while |
| 1172 | releasing SNRW lock in THREAD 4, lock is granted to starving low priority |
| 1173 | locks as m_hog_lock_count == max_write_lock_count. |
| 1174 | So THREAD 2, 3 gets lock here instead of THREAD 5. |
| 1175 | */ |
| 1176 | TEST_F(MDLTest, HogLockTest2) { |
| 1177 | Notification thd_lock_grabbed[5]; |
| 1178 | Notification thd_release_locks[5]; |
| 1179 | Notification thd_lock_blocked[5]; |
| 1180 | Notification thd_lock_released[5]; |
| 1181 | |
| 1182 | /* Locks taken by the threads */ |
| 1183 | enum { THD1_X, THD2_SR, THD3_SW, THD4_SNRW, THD5_SNRW }; |
| 1184 | |
| 1185 | max_write_lock_count = 1; |
| 1186 | |
| 1187 | /* |
| 1188 | THREAD1: Acquiring X lock on table. |
| 1189 | Lock Wait Queue: <empty> |
| 1190 | Lock Granted: <empty> |
| 1191 | */ |
| 1192 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1193 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1194 | &thd_lock_released[THD1_X]); |
| 1195 | mdl_thread1.start(); |
| 1196 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1197 | |
| 1198 | /* |
| 1199 | THREAD2: Requesting SR lock on table. |
| 1200 | Lock Wait Queue: SR |
| 1201 | Lock Granted: X |
| 1202 | */ |
| 1203 | MDL_thread mdl_thread2( |
| 1204 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD2_SR], |
| 1205 | &thd_release_locks[THD2_SR], &thd_lock_blocked[THD2_SR], |
| 1206 | &thd_lock_released[THD2_SR]); |
| 1207 | mdl_thread2.start(); |
| 1208 | thd_lock_blocked[THD2_SR].wait_for_notification(); |
| 1209 | |
| 1210 | /* |
| 1211 | THREAD3: Requesting SW lock on table. |
| 1212 | Lock Wait Queue: SR<--SW |
| 1213 | Lock Granted: X |
| 1214 | */ |
| 1215 | MDL_thread mdl_thread3( |
| 1216 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD3_SW], |
| 1217 | &thd_release_locks[THD3_SW], &thd_lock_blocked[THD3_SW], |
| 1218 | &thd_lock_released[THD3_SW]); |
| 1219 | mdl_thread3.start(); |
| 1220 | thd_lock_blocked[THD3_SW].wait_for_notification(); |
| 1221 | |
| 1222 | /* |
| 1223 | THREAD4: Requesting SNRW lock on table. |
| 1224 | Lock Wait Queue: SR<--SW<--SNRW |
| 1225 | Lock Granted: X |
| 1226 | */ |
| 1227 | MDL_thread mdl_thread4( |
| 1228 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD4_SNRW], |
| 1229 | &thd_release_locks[THD4_SNRW], &thd_lock_blocked[THD4_SNRW], |
| 1230 | &thd_lock_released[THD4_SNRW]); |
| 1231 | mdl_thread4.start(); |
| 1232 | thd_lock_blocked[THD4_SNRW].wait_for_notification(); |
| 1233 | |
| 1234 | /* |
| 1235 | THREAD 1: Release X lock. |
| 1236 | Lock Wait Queue: SR<--SW |
| 1237 | Lock Granted: SNRW |
| 1238 | m_hog_lock_count= 1 |
| 1239 | */ |
| 1240 | thd_release_locks[THD1_X].notify(); |
| 1241 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1242 | |
| 1243 | /* Lock is granted to THREAD 4 */ |
| 1244 | thd_lock_grabbed[THD4_SNRW].wait_for_notification(); |
| 1245 | |
| 1246 | /* |
| 1247 | THREAD 5: Requests SNRW lock on the table. |
| 1248 | Lock Wait Queue: SR<--SW<--SNRW |
| 1249 | Lock Granted: SNRW |
| 1250 | */ |
| 1251 | MDL_thread mdl_thread5( |
| 1252 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD5_SNRW], |
| 1253 | &thd_release_locks[THD5_SNRW], &thd_lock_blocked[THD5_SNRW], |
| 1254 | &thd_lock_released[THD5_SNRW]); |
| 1255 | mdl_thread5.start(); |
| 1256 | thd_lock_blocked[THD5_SNRW].wait_for_notification(); |
| 1257 | |
| 1258 | /* THREAD 4: Release SNRW lock */ |
| 1259 | thd_release_locks[THD4_SNRW].notify(); |
| 1260 | thd_lock_released[THD4_SNRW].wait_for_notification(); |
| 1261 | |
| 1262 | /* |
| 1263 | THREAD 2: Since max_write_lock_count == m_hog_lock_count, Lock is granted to |
| 1264 | THREAD 2 and 3 instead of THREAD 5. |
| 1265 | Lock Wait Queue: SNRW |
| 1266 | Lock Granted: SR, SW |
| 1267 | */ |
| 1268 | thd_lock_grabbed[THD2_SR].wait_for_notification(); |
| 1269 | thd_lock_grabbed[THD3_SW].wait_for_notification(); |
| 1270 | |
| 1271 | thd_release_locks[THD2_SR].notify(); |
| 1272 | thd_lock_released[THD2_SR].wait_for_notification(); |
| 1273 | |
| 1274 | thd_release_locks[THD3_SW].notify(); |
| 1275 | thd_lock_released[THD3_SW].wait_for_notification(); |
| 1276 | |
| 1277 | /* Cleanup */ |
| 1278 | thd_lock_grabbed[THD5_SNRW].wait_for_notification(); |
| 1279 | thd_release_locks[THD5_SNRW].notify(); |
| 1280 | thd_lock_released[THD5_SNRW].wait_for_notification(); |
| 1281 | |
| 1282 | mdl_thread1.join(); |
| 1283 | mdl_thread2.join(); |
| 1284 | mdl_thread3.join(); |
| 1285 | mdl_thread4.join(); |
| 1286 | mdl_thread5.join(); |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | Verifies locks priorities, |
| 1291 | X has priority over--> S, SR, SW, SU, (SNW, SNRW) |
| 1292 | SNRW has priority over--> SR, SW |
| 1293 | SNW has priority over--> SW |
| 1294 | |
| 1295 | - max_write_lock_count contains default value i.e ~(ulong)0L |
| 1296 | - THREAD 1: Acquires X lock on the table. |
| 1297 | - THREAD 2: Requests for S lock on the table. |
| 1298 | - THREAD 3: Requests for SR lock on the table. |
| 1299 | - THREAD 4: Requests for SW lock on the table. |
| 1300 | - THREAD 5: Requests for SU lock on the table. |
| 1301 | - THREAD 6: Requests for SNRW on the table. |
| 1302 | - THREAD 1: Releases X lock. |
| 1303 | Lock is granted THREAD 2, THREAD 5. |
| 1304 | - THREAD 5: RELEASE SU lock. |
| 1305 | Lock is granted to THREAD 6. |
| 1306 | - THREAD 7: Requests for SNW lock on the table. |
| 1307 | - THREAD 6: Releases SNRW lock. |
| 1308 | Lock is granted to THREAD 4 & THREAD 7. |
| 1309 | - THREAD 4: Check whether THREAD got lock on the table. |
| 1310 | At each locks release, locks of equal priorities are granted. |
| 1311 | At the end only SW will be in wait queue as lock is granted to SNW |
| 1312 | lock request. |
| 1313 | */ |
| 1314 | TEST_F(MDLTest, LockPriorityTest) { |
| 1315 | Notification thd_lock_grabbed[7]; |
| 1316 | Notification thd_release_locks[7]; |
| 1317 | Notification thd_lock_blocked[7]; |
| 1318 | Notification thd_lock_released[7]; |
| 1319 | |
| 1320 | /* Locks taken by the threads */ |
| 1321 | enum { THD1_X, THD2_S, THD3_SR, THD4_SW, THD5_SU, THD6_SNRW, THD7_SNW }; |
| 1322 | |
| 1323 | /*THREAD1: Acquiring X lock on table */ |
| 1324 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1325 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1326 | &thd_lock_released[THD1_X]); |
| 1327 | mdl_thread1.start(); |
| 1328 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1329 | |
| 1330 | /* |
| 1331 | THREAD2: Requesting S lock on table. |
| 1332 | Lock Wait Queue: S |
| 1333 | Lock Granted: X |
| 1334 | */ |
| 1335 | MDL_thread mdl_thread2(table_name1, MDL_SHARED, &thd_lock_grabbed[THD2_S], |
| 1336 | &thd_release_locks[THD2_S], &thd_lock_blocked[THD2_S], |
| 1337 | &thd_lock_released[THD2_S]); |
| 1338 | mdl_thread2.start(); |
| 1339 | thd_lock_blocked[THD2_S].wait_for_notification(); |
| 1340 | |
| 1341 | /* |
| 1342 | THREAD3: Requesting SR lock on table. |
| 1343 | Lock Wait Queue: S<--SR |
| 1344 | Lock Granted: X |
| 1345 | */ |
| 1346 | MDL_thread mdl_thread3( |
| 1347 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD3_SR], |
| 1348 | &thd_release_locks[THD3_SR], &thd_lock_blocked[THD3_SR], |
| 1349 | &thd_lock_released[THD3_SR]); |
| 1350 | mdl_thread3.start(); |
| 1351 | thd_lock_blocked[THD3_SR].wait_for_notification(); |
| 1352 | |
| 1353 | /* |
| 1354 | THREAD4: Requesting SW lock on table. |
| 1355 | Lock Wait Queue: S<--SR<--SW |
| 1356 | Lock Granted: X |
| 1357 | */ |
| 1358 | MDL_thread mdl_thread4( |
| 1359 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD4_SW], |
| 1360 | &thd_release_locks[THD4_SW], &thd_lock_blocked[THD4_SW], |
| 1361 | &thd_lock_released[THD4_SW]); |
| 1362 | mdl_thread4.start(); |
| 1363 | thd_lock_blocked[THD4_SW].wait_for_notification(); |
| 1364 | |
| 1365 | /* |
| 1366 | THREAD5: Requesting SU lock on table |
| 1367 | Lock Wait Queue: S<--SR<--SW<--SU |
| 1368 | Lock Granted: X |
| 1369 | */ |
| 1370 | MDL_thread mdl_thread5( |
| 1371 | table_name1, MDL_SHARED_UPGRADABLE, &thd_lock_grabbed[THD5_SU], |
| 1372 | &thd_release_locks[THD5_SU], &thd_lock_blocked[THD5_SU], |
| 1373 | &thd_lock_released[THD5_SU]); |
| 1374 | mdl_thread5.start(); |
| 1375 | thd_lock_blocked[THD5_SU].wait_for_notification(); |
| 1376 | |
| 1377 | /* |
| 1378 | THREAD6: Requesting SNRW lock on table |
| 1379 | Lock Wait Queue: S<--SR<--SW<--SU<--SNRW |
| 1380 | Lock Granted: X |
| 1381 | */ |
| 1382 | MDL_thread mdl_thread6( |
| 1383 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD6_SNRW], |
| 1384 | &thd_release_locks[THD6_SNRW], &thd_lock_blocked[THD6_SNRW], |
| 1385 | &thd_lock_released[THD6_SNRW]); |
| 1386 | mdl_thread6.start(); |
| 1387 | thd_lock_blocked[THD6_SNRW].wait_for_notification(); |
| 1388 | |
| 1389 | /* |
| 1390 | Lock wait Queue status: S<--SR<--SW<--SU<--SNRW |
| 1391 | THREAD 1: Release X lock. |
| 1392 | */ |
| 1393 | thd_release_locks[THD1_X].notify(); |
| 1394 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1395 | |
| 1396 | /* |
| 1397 | THREAD 5: Verify and Release lock. |
| 1398 | Lock wait Queue status: SR<--SW<--SNRW |
| 1399 | Lock Granted: S, SU |
| 1400 | */ |
| 1401 | thd_lock_grabbed[THD2_S].wait_for_notification(); |
| 1402 | thd_release_locks[THD2_S].notify(); |
| 1403 | thd_lock_released[THD2_S].wait_for_notification(); |
| 1404 | |
| 1405 | thd_lock_grabbed[THD5_SU].wait_for_notification(); |
| 1406 | thd_release_locks[THD5_SU].notify(); |
| 1407 | thd_lock_released[THD5_SU].wait_for_notification(); |
| 1408 | |
| 1409 | /* Now Lock Granted to THREAD 6 SNRW lock type request*/ |
| 1410 | thd_lock_grabbed[THD6_SNRW].wait_for_notification(); |
| 1411 | |
| 1412 | /* |
| 1413 | THREAD 7: Requests SNW lock on the table. |
| 1414 | Lock wait Queue status: SR<--SW<--SNW |
| 1415 | Lock Granted: SNRW |
| 1416 | */ |
| 1417 | MDL_thread mdl_thread7( |
| 1418 | table_name1, MDL_SHARED_NO_WRITE, &thd_lock_grabbed[THD7_SNW], |
| 1419 | &thd_release_locks[THD7_SNW], &thd_lock_blocked[THD7_SNW], |
| 1420 | &thd_lock_released[THD7_SNW]); |
| 1421 | mdl_thread7.start(); |
| 1422 | thd_lock_blocked[THD7_SNW].wait_for_notification(); |
| 1423 | |
| 1424 | /* THREAD 6: Release SNRW lock */ |
| 1425 | thd_release_locks[THD6_SNRW].notify(); |
| 1426 | thd_lock_released[THD6_SNRW].wait_for_notification(); |
| 1427 | |
| 1428 | /* Now lock is granted to THREAD 3 & 7 */ |
| 1429 | thd_lock_grabbed[THD7_SNW].wait_for_notification(); |
| 1430 | thd_lock_grabbed[THD3_SR].wait_for_notification(); |
| 1431 | |
| 1432 | /* |
| 1433 | THREAD 3: Release SR lock |
| 1434 | Lock wait Queue status: SW |
| 1435 | Lock Granted: SR, SNW |
| 1436 | */ |
| 1437 | thd_release_locks[THD3_SR].notify(); |
| 1438 | thd_lock_released[THD3_SR].wait_for_notification(); |
| 1439 | |
| 1440 | /* THREAD 4: Verify whether lock is granted or not*/ |
| 1441 | EXPECT_FALSE((mdl_thread4.get_mdl_context()) |
| 1442 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1443 | table_name1, MDL_SHARED_WRITE)); |
| 1444 | |
| 1445 | /*CLEANUP*/ |
| 1446 | thd_release_locks[THD7_SNW].notify(); |
| 1447 | thd_lock_released[THD7_SNW].wait_for_notification(); |
| 1448 | |
| 1449 | thd_lock_grabbed[THD4_SW].wait_for_notification(); |
| 1450 | thd_release_locks[THD4_SW].notify(); |
| 1451 | thd_lock_released[THD4_SW].wait_for_notification(); |
| 1452 | |
| 1453 | mdl_thread1.join(); |
| 1454 | mdl_thread2.join(); |
| 1455 | mdl_thread3.join(); |
| 1456 | mdl_thread4.join(); |
| 1457 | mdl_thread5.join(); |
| 1458 | mdl_thread6.join(); |
| 1459 | mdl_thread7.join(); |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | Verifies locks priorities when max_write_lock_count= 1 |
| 1464 | X has priority over--> S, SR, SW, SU, (SNW, SNRW) |
| 1465 | SNRW has priority over--> SR, SW |
| 1466 | SNW has priority over--> SW |
| 1467 | |
| 1468 | - max_write_lock_count= 1 |
| 1469 | - THREAD 1: Acquires X lock on the table. |
| 1470 | - THREAD 2: Requests for S lock on the table. |
| 1471 | - THREAD 3: Requests for SR lock on the table. |
| 1472 | - THREAD 4: Requests for SW lock on the table. |
| 1473 | - THREAD 5: Requests for SU lock on the table. |
| 1474 | - THREAD 6: Requests for X on the table. |
| 1475 | - THREAD 1: Releases X lock. |
| 1476 | Lock is granted THREAD 6. |
| 1477 | - THREAD 7: Requests SNRW lock. |
| 1478 | - THREAD 6: Releases X lock. |
| 1479 | Lock is granted to THREAD 2,3,4,5. |
| 1480 | - THREAD 7: Check Whether lock is granted or not. |
| 1481 | */ |
| 1482 | TEST_F(MDLTest, HogLockTest3) { |
| 1483 | Notification thd_lock_grabbed[7]; |
| 1484 | Notification thd_release_locks[7]; |
| 1485 | Notification thd_lock_blocked[7]; |
| 1486 | Notification thd_lock_released[7]; |
| 1487 | |
| 1488 | enum { THD1_X, THD2_S, THD3_SR, THD4_SW, THD5_SU, THD6_X, THD7_SNRW }; |
| 1489 | |
| 1490 | max_write_lock_count = 1; |
| 1491 | |
| 1492 | /* THREAD1: Acquiring X lock on table. */ |
| 1493 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1494 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1495 | &thd_lock_released[THD1_X]); |
| 1496 | mdl_thread1.start(); |
| 1497 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1498 | |
| 1499 | /* |
| 1500 | THREAD2: Requesting S lock on table. |
| 1501 | Lock Wait Queue: S |
| 1502 | Lock Granted: X |
| 1503 | */ |
| 1504 | MDL_thread mdl_thread2(table_name1, MDL_SHARED, &thd_lock_grabbed[THD2_S], |
| 1505 | &thd_release_locks[THD2_S], &thd_lock_blocked[THD2_S], |
| 1506 | &thd_lock_released[THD2_S]); |
| 1507 | mdl_thread2.start(); |
| 1508 | thd_lock_blocked[THD2_S].wait_for_notification(); |
| 1509 | |
| 1510 | /* |
| 1511 | THREAD3: Requesting SR lock on table. |
| 1512 | Lock Wait Queue: S<--SR |
| 1513 | Lock Granted: X |
| 1514 | */ |
| 1515 | MDL_thread mdl_thread3( |
| 1516 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD3_SR], |
| 1517 | &thd_release_locks[THD3_SR], &thd_lock_blocked[THD3_SR], |
| 1518 | &thd_lock_released[THD3_SR]); |
| 1519 | mdl_thread3.start(); |
| 1520 | thd_lock_blocked[THD3_SR].wait_for_notification(); |
| 1521 | |
| 1522 | /* |
| 1523 | THREAD4: Requesting SW lock on table. |
| 1524 | Lock Wait Queue: S<--SR<--SW. |
| 1525 | Lock Granted: X |
| 1526 | */ |
| 1527 | MDL_thread mdl_thread4( |
| 1528 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD4_SW], |
| 1529 | &thd_release_locks[THD4_SW], &thd_lock_blocked[THD4_SW], |
| 1530 | &thd_lock_released[THD4_SW]); |
| 1531 | mdl_thread4.start(); |
| 1532 | thd_lock_blocked[THD4_SW].wait_for_notification(); |
| 1533 | |
| 1534 | /* |
| 1535 | THREAD5: Requesting SU lock on table. |
| 1536 | Lock Wait Queue: S<--SR<--SW<--SU |
| 1537 | Lock Granted: X |
| 1538 | */ |
| 1539 | MDL_thread mdl_thread5( |
| 1540 | table_name1, MDL_SHARED_UPGRADABLE, &thd_lock_grabbed[THD5_SU], |
| 1541 | &thd_release_locks[THD5_SU], &thd_lock_blocked[THD5_SU], |
| 1542 | &thd_lock_released[THD5_SU]); |
| 1543 | mdl_thread5.start(); |
| 1544 | thd_lock_blocked[THD5_SU].wait_for_notification(); |
| 1545 | |
| 1546 | /* |
| 1547 | THREAD6: Requesting X lock on table |
| 1548 | Lock Wait Queue: S<--SR<--SW<--SU<--X |
| 1549 | Lock Granted: X |
| 1550 | */ |
| 1551 | MDL_thread mdl_thread6(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD6_X], |
| 1552 | &thd_release_locks[THD6_X], &thd_lock_blocked[THD6_X], |
| 1553 | &thd_lock_released[THD6_X]); |
| 1554 | mdl_thread6.start(); |
| 1555 | thd_lock_blocked[THD6_X].wait_for_notification(); |
| 1556 | |
| 1557 | /* |
| 1558 | Lock wait Queue status: S<--SR<--SW<--SU<--X |
| 1559 | Lock Granted: X |
| 1560 | THREAD 1: Release X lock. |
| 1561 | */ |
| 1562 | thd_release_locks[THD1_X].notify(); |
| 1563 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1564 | |
| 1565 | /* Lock is granted to THREAD 6*/ |
| 1566 | thd_lock_grabbed[THD6_X].wait_for_notification(); |
| 1567 | |
| 1568 | /* |
| 1569 | THREAD7: Requesting SNRW lock on table |
| 1570 | Lock wait Queue status: S<--SR<--SW<--SU |
| 1571 | Lock Granted: X |
| 1572 | */ |
| 1573 | MDL_thread mdl_thread7( |
| 1574 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD7_SNRW], |
| 1575 | &thd_release_locks[THD7_SNRW], &thd_lock_blocked[THD7_SNRW], |
| 1576 | &thd_lock_released[THD7_SNRW]); |
| 1577 | mdl_thread7.start(); |
| 1578 | thd_lock_blocked[THD7_SNRW].wait_for_notification(); |
| 1579 | |
| 1580 | /* THREAD 6: Release X lock. */ |
| 1581 | thd_release_locks[THD6_X].notify(); |
| 1582 | thd_lock_released[THD6_X].wait_for_notification(); |
| 1583 | |
| 1584 | /* Lock is granted to THREAD 2, 3, 4, 5*/ |
| 1585 | thd_lock_grabbed[THD2_S].wait_for_notification(); |
| 1586 | thd_lock_grabbed[THD3_SR].wait_for_notification(); |
| 1587 | thd_lock_grabbed[THD4_SW].wait_for_notification(); |
| 1588 | thd_lock_grabbed[THD5_SU].wait_for_notification(); |
| 1589 | |
| 1590 | /* |
| 1591 | Lock wait Queue status: <empty> |
| 1592 | Lock Granted: <empty> |
| 1593 | THREAD 7: high priority SNRW lock is still waiting. |
| 1594 | */ |
| 1595 | EXPECT_FALSE((mdl_thread7.get_mdl_context()) |
| 1596 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1597 | table_name1, |
| 1598 | MDL_SHARED_NO_READ_WRITE)); |
| 1599 | |
| 1600 | /* CLEAN UP */ |
| 1601 | thd_release_locks[THD2_S].notify(); |
| 1602 | thd_lock_released[THD2_S].wait_for_notification(); |
| 1603 | |
| 1604 | thd_release_locks[THD3_SR].notify(); |
| 1605 | thd_lock_released[THD3_SR].wait_for_notification(); |
| 1606 | |
| 1607 | thd_release_locks[THD4_SW].notify(); |
| 1608 | thd_lock_released[THD4_SW].wait_for_notification(); |
| 1609 | |
| 1610 | thd_release_locks[THD5_SU].notify(); |
| 1611 | thd_lock_released[THD5_SU].wait_for_notification(); |
| 1612 | |
| 1613 | thd_lock_grabbed[THD7_SNRW].wait_for_notification(); |
| 1614 | thd_release_locks[THD7_SNRW].notify(); |
| 1615 | thd_lock_released[THD7_SNRW].wait_for_notification(); |
| 1616 | |
| 1617 | mdl_thread1.join(); |
| 1618 | mdl_thread2.join(); |
| 1619 | mdl_thread3.join(); |
| 1620 | mdl_thread4.join(); |
| 1621 | mdl_thread5.join(); |
| 1622 | mdl_thread6.join(); |
| 1623 | mdl_thread7.join(); |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | Verifies whether m_hog_lock_count is resets or not, |
| 1628 | when there are no low priority lock request. |
| 1629 | |
| 1630 | - max_write_lock_count= 1 |
| 1631 | - THREAD 1: Acquires X lock on the table. |
| 1632 | - THREAD 2: Requests for SU lock on the table. |
| 1633 | - THREAD 3: Requests for X lock on the table. |
| 1634 | - THREAD 1: Releases X lock. |
| 1635 | Lock is granted to THREAD 3 |
| 1636 | m_hog_lock_count= 1; |
| 1637 | - THREAD 3: Releases X lock. |
| 1638 | Lock is granted to THRED 2. |
| 1639 | m_hog_lock_count= 0; |
| 1640 | - THREAD 4: Requests for SNRW lock. |
| 1641 | - THREAD 5: Requests for R lock. |
| 1642 | - THREAD 2: Releases SU lock. |
| 1643 | Lock is granted to THREAD 4. |
| 1644 | */ |
| 1645 | TEST_F(MDLTest, HogLockTest4) { |
| 1646 | Notification thd_lock_grabbed[5]; |
| 1647 | Notification thd_release_locks[5]; |
| 1648 | Notification thd_lock_blocked[5]; |
| 1649 | Notification thd_lock_released[5]; |
| 1650 | |
| 1651 | /* Locks taken by the threads */ |
| 1652 | enum { THD1_X, THD2_SU, THD3_X, THD4_SNRW, THD5_SR }; |
| 1653 | |
| 1654 | max_write_lock_count = 1; |
| 1655 | |
| 1656 | /* THREAD1: Acquiring X lock on table */ |
| 1657 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1658 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1659 | &thd_lock_released[THD1_X]); |
| 1660 | mdl_thread1.start(); |
| 1661 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1662 | |
| 1663 | /* THREAD2: Requesting SU lock on table */ |
| 1664 | MDL_thread mdl_thread2( |
| 1665 | table_name1, MDL_SHARED_UPGRADABLE, &thd_lock_grabbed[THD2_SU], |
| 1666 | &thd_release_locks[THD2_SU], &thd_lock_blocked[THD2_SU], |
| 1667 | &thd_lock_released[THD2_SU]); |
| 1668 | mdl_thread2.start(); |
| 1669 | thd_lock_blocked[THD2_SU].wait_for_notification(); |
| 1670 | |
| 1671 | /* THREAD3: Requesting X lock on table */ |
| 1672 | MDL_thread mdl_thread3(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD3_X], |
| 1673 | &thd_release_locks[THD3_X], &thd_lock_blocked[THD3_X], |
| 1674 | &thd_lock_released[THD3_X]); |
| 1675 | mdl_thread3.start(); |
| 1676 | thd_lock_blocked[THD3_X].wait_for_notification(); |
| 1677 | |
| 1678 | /* |
| 1679 | THREAD1: Release X lock. |
| 1680 | Lock Request Queue: SU<--X |
| 1681 | Lock Grant: X |
| 1682 | m_hog_lock_count= 1 |
| 1683 | */ |
| 1684 | thd_release_locks[THD1_X].notify(); |
| 1685 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1686 | /* Lock is granted to THREAD 3 */ |
| 1687 | thd_lock_grabbed[THD3_X].wait_for_notification(); |
| 1688 | |
| 1689 | /* |
| 1690 | THREAD3: Release X lock. |
| 1691 | Lock Request Queue: <empty> |
| 1692 | Lock Grant: SU |
| 1693 | m_hog_lock_count= 0 |
| 1694 | */ |
| 1695 | thd_release_locks[THD3_X].notify(); |
| 1696 | thd_lock_released[THD3_X].wait_for_notification(); |
| 1697 | /*Lock is granted to THREAD 2 */ |
| 1698 | thd_lock_grabbed[THD2_SU].wait_for_notification(); |
| 1699 | |
| 1700 | /* |
| 1701 | THREAD4: Requesting SNRW lock on table. |
| 1702 | Lock Request Queue: SNRW |
| 1703 | Lock Grant: SU |
| 1704 | */ |
| 1705 | MDL_thread mdl_thread4( |
| 1706 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD4_SNRW], |
| 1707 | &thd_release_locks[THD4_SNRW], &thd_lock_blocked[THD4_SNRW], |
| 1708 | &thd_lock_released[THD4_SNRW]); |
| 1709 | mdl_thread4.start(); |
| 1710 | thd_lock_blocked[THD4_SNRW].wait_for_notification(); |
| 1711 | |
| 1712 | /* |
| 1713 | THREAD5: Requesting SR lock on table. |
| 1714 | Lock Request Queue: SNRW<--SR |
| 1715 | Lock Grant: SU |
| 1716 | */ |
| 1717 | MDL_thread mdl_thread5( |
| 1718 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD5_SR], |
| 1719 | &thd_release_locks[THD5_SR], &thd_lock_blocked[THD5_SR], |
| 1720 | &thd_lock_released[THD5_SR]); |
| 1721 | mdl_thread5.start(); |
| 1722 | thd_lock_blocked[THD5_SR].wait_for_notification(); |
| 1723 | |
| 1724 | /* THREAD 2: Release lock. */ |
| 1725 | thd_release_locks[THD2_SU].notify(); |
| 1726 | thd_lock_released[THD2_SU].wait_for_notification(); |
| 1727 | |
| 1728 | /* |
| 1729 | Lock Request Queue: SR |
| 1730 | Lock Grant: SNRW |
| 1731 | Lock is granted to THREAD 5 if m_hog_lock_count is not reset. |
| 1732 | */ |
| 1733 | thd_lock_grabbed[THD4_SNRW].wait_for_notification(); |
| 1734 | |
| 1735 | /* THREAD5: Lock is not granted */ |
| 1736 | EXPECT_FALSE((mdl_thread5.get_mdl_context()) |
| 1737 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1738 | table_name1, MDL_SHARED_READ)); |
| 1739 | |
| 1740 | /* CLEAN UP */ |
| 1741 | thd_release_locks[THD4_SNRW].notify(); |
| 1742 | thd_lock_released[THD4_SNRW].wait_for_notification(); |
| 1743 | |
| 1744 | thd_lock_grabbed[THD5_SR].wait_for_notification(); |
| 1745 | thd_release_locks[THD5_SR].notify(); |
| 1746 | thd_lock_released[THD5_SR].wait_for_notification(); |
| 1747 | |
| 1748 | mdl_thread1.join(); |
| 1749 | mdl_thread2.join(); |
| 1750 | mdl_thread3.join(); |
| 1751 | mdl_thread4.join(); |
| 1752 | mdl_thread5.join(); |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | Verifies resetting of m_hog_lock_count when only few of |
| 1757 | the waiting low priority locks are granted and queue has |
| 1758 | some more low priority lock requests in queue. |
| 1759 | m_hog_lock_count should not be reset to 0 when few low priority |
| 1760 | lock requests are granted. |
| 1761 | |
| 1762 | - max_write_lock_count= 1 |
| 1763 | - THREAD 1: Acquires X lock on the table. |
| 1764 | - THREAD 2: Requests for SNW lock on the table. |
| 1765 | - THREAD 3: Requests for SR lock on the table. |
| 1766 | - THREAD 4: Requests for SW lock on the table. |
| 1767 | - THREAD 5: Requests for SU lock on the table. |
| 1768 | - THREAD 1: Releases X lock. |
| 1769 | Lock is granted THREAD 2, 3 as they are of same priority. |
| 1770 | - THREAD 6: Requests for SNRW lock. |
| 1771 | - THREAD 2: Releases SNW lock. |
| 1772 | Lock shoule be granted to THREAD 4, 5 as |
| 1773 | m_hog_lock_count == max_write_lock_count. |
| 1774 | - THREAD 3: Check Whether lock is granted or not. |
| 1775 | */ |
| 1776 | TEST_F(MDLTest, HogLockTest5) { |
| 1777 | Notification thd_lock_grabbed[6]; |
| 1778 | Notification thd_release_locks[6]; |
| 1779 | Notification thd_lock_blocked[6]; |
| 1780 | Notification thd_lock_released[6]; |
| 1781 | |
| 1782 | /* Locks taken by the threads */ |
| 1783 | enum { THD1_X, THD2_SNW, THD3_SR, THD4_SW, THD5_SU, THD6_SNRW }; |
| 1784 | max_write_lock_count = 1; |
| 1785 | |
| 1786 | /* THREAD1: Acquiring X lock on table. */ |
| 1787 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X], |
| 1788 | &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X], |
| 1789 | &thd_lock_released[THD1_X]); |
| 1790 | mdl_thread1.start(); |
| 1791 | thd_lock_grabbed[THD1_X].wait_for_notification(); |
| 1792 | |
| 1793 | /* THREAD2: Requesting SNW lock on table. */ |
| 1794 | MDL_thread mdl_thread2( |
| 1795 | table_name1, MDL_SHARED_NO_WRITE, &thd_lock_grabbed[THD2_SNW], |
| 1796 | &thd_release_locks[THD2_SNW], &thd_lock_blocked[THD2_SNW], |
| 1797 | &thd_lock_released[THD2_SNW]); |
| 1798 | mdl_thread2.start(); |
| 1799 | thd_lock_blocked[THD2_SNW].wait_for_notification(); |
| 1800 | |
| 1801 | /* THREAD3: Requesting SR lock on table. */ |
| 1802 | MDL_thread mdl_thread3( |
| 1803 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD3_SR], |
| 1804 | &thd_release_locks[THD3_SR], &thd_lock_blocked[THD3_SR], |
| 1805 | &thd_lock_released[THD3_SR]); |
| 1806 | mdl_thread3.start(); |
| 1807 | thd_lock_blocked[THD3_SR].wait_for_notification(); |
| 1808 | |
| 1809 | /* THREAD4: Requesting SW lock on table. */ |
| 1810 | MDL_thread mdl_thread4( |
| 1811 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD4_SW], |
| 1812 | &thd_release_locks[THD4_SW], &thd_lock_blocked[THD4_SW], |
| 1813 | &thd_lock_released[THD4_SW]); |
| 1814 | mdl_thread4.start(); |
| 1815 | thd_lock_blocked[THD4_SW].wait_for_notification(); |
| 1816 | |
| 1817 | /* THREAD5: Requesting SNW lock on table. */ |
| 1818 | MDL_thread mdl_thread5( |
| 1819 | table_name1, MDL_SHARED_UPGRADABLE, &thd_lock_grabbed[THD5_SU], |
| 1820 | &thd_release_locks[THD5_SU], &thd_lock_blocked[THD5_SU], |
| 1821 | &thd_lock_released[THD5_SU]); |
| 1822 | mdl_thread5.start(); |
| 1823 | thd_lock_blocked[THD5_SU].wait_for_notification(); |
| 1824 | |
| 1825 | /* |
| 1826 | Lock wait Queue status: SNW<--SR<--SW<--SU |
| 1827 | Lock Granted: X |
| 1828 | THREAD 1: Release X lock. |
| 1829 | */ |
| 1830 | thd_release_locks[THD1_X].notify(); |
| 1831 | thd_lock_released[THD1_X].wait_for_notification(); |
| 1832 | |
| 1833 | /* |
| 1834 | Lock wait Queue status: SW<--SU |
| 1835 | Lock Granted: SR, SNW |
| 1836 | Lock is granted for Thread 2, 3 |
| 1837 | */ |
| 1838 | thd_lock_grabbed[THD2_SNW].wait_for_notification(); |
| 1839 | thd_lock_grabbed[THD3_SR].wait_for_notification(); |
| 1840 | |
| 1841 | /* |
| 1842 | THREAD5: Requesting SNRW lock on table. |
| 1843 | Lock wait Queue status: SW<--SU<--SNRW |
| 1844 | Lock Granted: SR, SNW |
| 1845 | */ |
| 1846 | MDL_thread mdl_thread6( |
| 1847 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD6_SNRW], |
| 1848 | &thd_release_locks[THD6_SNRW], &thd_lock_blocked[THD6_SNRW], |
| 1849 | &thd_lock_released[THD6_SNRW]); |
| 1850 | mdl_thread6.start(); |
| 1851 | thd_lock_blocked[THD6_SNRW].wait_for_notification(); |
| 1852 | |
| 1853 | /* Thread 2: Release SNW lock */ |
| 1854 | thd_release_locks[THD2_SNW].notify(); |
| 1855 | thd_lock_released[THD2_SNW].wait_for_notification(); |
| 1856 | |
| 1857 | /* |
| 1858 | Lock wait Queue status: SNRW |
| 1859 | Lock Granted: SR, SW, SU |
| 1860 | Lock is granted to Thread 4,5 instead of Thread 6 |
| 1861 | THREAD6: Lock is not granted |
| 1862 | */ |
| 1863 | EXPECT_FALSE((mdl_thread6.get_mdl_context()) |
| 1864 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 1865 | table_name1, |
| 1866 | MDL_SHARED_NO_READ_WRITE)); |
| 1867 | |
| 1868 | thd_lock_grabbed[THD4_SW].wait_for_notification(); |
| 1869 | thd_release_locks[THD4_SW].notify(); |
| 1870 | thd_lock_released[THD4_SW].wait_for_notification(); |
| 1871 | |
| 1872 | thd_lock_grabbed[THD5_SU].wait_for_notification(); |
| 1873 | thd_release_locks[THD5_SU].notify(); |
| 1874 | thd_lock_released[THD5_SU].wait_for_notification(); |
| 1875 | |
| 1876 | /* CLEANUP */ |
| 1877 | thd_release_locks[THD3_SR].notify(); |
| 1878 | thd_lock_released[THD3_SR].wait_for_notification(); |
| 1879 | thd_lock_grabbed[THD6_SNRW].wait_for_notification(); |
| 1880 | thd_release_locks[THD6_SNRW].notify(); |
| 1881 | thd_lock_released[THD6_SNRW].wait_for_notification(); |
| 1882 | |
| 1883 | mdl_thread1.join(); |
| 1884 | mdl_thread2.join(); |
| 1885 | mdl_thread3.join(); |
| 1886 | mdl_thread4.join(); |
| 1887 | mdl_thread5.join(); |
| 1888 | mdl_thread6.join(); |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | Verifies that pending "obtrusive" lock correctly blocks later |
| 1893 | "unobtrusive" lock, which can't use "fast path" in this case. |
| 1894 | |
| 1895 | Also verifies that release of "fast path" "unobtrusive" lock |
| 1896 | unblocks waiting "obtrusive" lock and release of "obtrusive" lock |
| 1897 | unblocks pending "unobtrusive" lock. |
| 1898 | */ |
| 1899 | |
| 1900 | TEST_F(MDLTest, ConcurrentSharedExclusiveShared) { |
| 1901 | Notification first_shared_grabbed; |
| 1902 | Notification first_shared_release; |
| 1903 | Notification exclusive_blocked; |
| 1904 | Notification exclusive_grabbed; |
| 1905 | Notification exclusive_release; |
| 1906 | Notification second_shared_grabbed; |
| 1907 | Notification second_shared_blocked; |
| 1908 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &first_shared_grabbed, |
| 1909 | &first_shared_release, nullptr, nullptr); |
| 1910 | MDL_thread mdl_thread2(table_name1, MDL_EXCLUSIVE, &exclusive_grabbed, |
| 1911 | &exclusive_release, &exclusive_blocked, nullptr); |
| 1912 | MDL_thread mdl_thread3(table_name1, MDL_SHARED, &second_shared_grabbed, |
| 1913 | nullptr, &second_shared_blocked, nullptr); |
| 1914 | |
| 1915 | /* Start thread which will acquire S lock. */ |
| 1916 | mdl_thread1.start(); |
| 1917 | first_shared_grabbed.wait_for_notification(); |
| 1918 | |
| 1919 | /* Start thread which will try to acquire X lock and will block. */ |
| 1920 | mdl_thread2.start(); |
| 1921 | exclusive_blocked.wait_for_notification(); |
| 1922 | |
| 1923 | /* |
| 1924 | Start thread which will try to acquire another S lock. |
| 1925 | It should not use "fast path" because of pending X lock |
| 1926 | and should be blocked. |
| 1927 | */ |
| 1928 | mdl_thread3.start(); |
| 1929 | second_shared_blocked.wait_for_notification(); |
| 1930 | |
| 1931 | /* |
| 1932 | Unblock 2nd thread by releasing the first S lock. |
| 1933 | Here S lock acquiring on "fast path" should unblock |
| 1934 | pending "obtrusive" - X lock. |
| 1935 | */ |
| 1936 | first_shared_release.notify(); |
| 1937 | exclusive_grabbed.wait_for_notification(); |
| 1938 | |
| 1939 | /* |
| 1940 | Release X lock and thus unblock the second S lock. |
| 1941 | This tests that release of X lock which uses "slow path" |
| 1942 | unblocks pending locks. |
| 1943 | */ |
| 1944 | exclusive_release.notify(); |
| 1945 | second_shared_grabbed.wait_for_notification(); |
| 1946 | |
| 1947 | /* Wrap-up. */ |
| 1948 | mdl_thread1.join(); |
| 1949 | mdl_thread2.join(); |
| 1950 | mdl_thread3.join(); |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | Verify that active X lock will block incoming X lock. |
| 1955 | This also covers general situation when "obtrusive" lock is |
| 1956 | acquired when other "obtrusive" lock is active. |
| 1957 | Also covers case when release of "obtrusive" lock |
| 1958 | unblocks another "obtrusive" lock. |
| 1959 | */ |
| 1960 | |
| 1961 | TEST_F(MDLTest, ConcurrentExclusiveExclusive) { |
| 1962 | Notification first_exclusive_grabbed; |
| 1963 | Notification first_exclusive_release; |
| 1964 | Notification second_exclusive_blocked; |
| 1965 | Notification second_exclusive_grabbed; |
| 1966 | MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &first_exclusive_grabbed, |
| 1967 | &first_exclusive_release, nullptr, nullptr); |
| 1968 | MDL_thread mdl_thread2(table_name1, MDL_EXCLUSIVE, &second_exclusive_grabbed, |
| 1969 | nullptr, &second_exclusive_blocked, nullptr); |
| 1970 | |
| 1971 | /* Start thread which will acquire X lock. */ |
| 1972 | mdl_thread1.start(); |
| 1973 | first_exclusive_grabbed.wait_for_notification(); |
| 1974 | |
| 1975 | /* Start thread which will try to acquire X lock and will block. */ |
| 1976 | mdl_thread2.start(); |
| 1977 | second_exclusive_blocked.wait_for_notification(); |
| 1978 | |
| 1979 | /* Releasing the first lock. This should unblock the second X request.*/ |
| 1980 | first_exclusive_release.notify(); |
| 1981 | second_exclusive_grabbed.wait_for_notification(); |
| 1982 | |
| 1983 | /* Wrap-up. */ |
| 1984 | mdl_thread1.join(); |
| 1985 | mdl_thread2.join(); |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | Verify that during rescheduling we correctly handle |
| 1990 | situation when still there are conflicting "fast path" |
| 1991 | lock (i.e. test that MDL_lock::can_grant_lock() correctly |
| 1992 | works in this case). |
| 1993 | */ |
| 1994 | |
| 1995 | TEST_F(MDLTest, ConcurrentSharedSharedExclusive) { |
| 1996 | Notification first_shared_grabbed; |
| 1997 | Notification first_shared_release; |
| 1998 | Notification second_shared_grabbed; |
| 1999 | Notification second_shared_release; |
| 2000 | Notification exclusive_blocked; |
| 2001 | Notification exclusive_grabbed; |
| 2002 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &first_shared_grabbed, |
| 2003 | &first_shared_release, nullptr, nullptr); |
| 2004 | MDL_thread mdl_thread2(table_name1, MDL_SHARED, &second_shared_grabbed, |
| 2005 | &second_shared_release, nullptr, nullptr); |
| 2006 | MDL_thread mdl_thread3(table_name1, MDL_EXCLUSIVE, &exclusive_grabbed, |
| 2007 | nullptr, &exclusive_blocked, nullptr); |
| 2008 | |
| 2009 | /* Start two threads which will acquire S locks. */ |
| 2010 | mdl_thread1.start(); |
| 2011 | first_shared_grabbed.wait_for_notification(); |
| 2012 | |
| 2013 | mdl_thread2.start(); |
| 2014 | second_shared_grabbed.wait_for_notification(); |
| 2015 | |
| 2016 | /* Start 3rd thread which will try to acquire X lock and will block. */ |
| 2017 | mdl_thread3.start(); |
| 2018 | exclusive_blocked.wait_for_notification(); |
| 2019 | |
| 2020 | /* Release one S lock. */ |
| 2021 | first_shared_release.notify(); |
| 2022 | |
| 2023 | /* |
| 2024 | Rescheduling which happens in this case should still see that X lock |
| 2025 | is waiting. |
| 2026 | */ |
| 2027 | EXPECT_FALSE((mdl_thread3.get_mdl_context()) |
| 2028 | .owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, |
| 2029 | table_name1, MDL_EXCLUSIVE)); |
| 2030 | |
| 2031 | /* Release the second S lock to unblock request for X lock. */ |
| 2032 | second_shared_release.notify(); |
| 2033 | |
| 2034 | /* Wrap-up. */ |
| 2035 | mdl_thread1.join(); |
| 2036 | mdl_thread2.join(); |
| 2037 | mdl_thread3.join(); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | Verify that we correctly handle situation when one thread tries |
| 2042 | consequitively to acquire locks which conflict with each other |
| 2043 | (this is again to check that MDL_lock::can_grant_lock() handles |
| 2044 | such situations correctly). |
| 2045 | */ |
| 2046 | |
| 2047 | TEST_F(MDLTest, SelfConflict) { |
| 2048 | /* The first scenario: "unobtrusive" lock first, then "obtrusive". */ |
| 2049 | |
| 2050 | /* Acquire S lock, it will be acquired using "fast path" algorithm. */ |
| 2051 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 2052 | MDL_TRANSACTION); |
| 2053 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2054 | |
| 2055 | /* Acquire global IX lock to be able acquire X lock later. */ |
| 2056 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2057 | |
| 2058 | /* |
| 2059 | Acquire X lock on the same table. MDL subsystem should be able to detect |
| 2060 | that conflicting S lock belongs to the same context even though it was |
| 2061 | was acquired using "fast path". |
| 2062 | */ |
| 2063 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2064 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 2065 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2066 | |
| 2067 | m_mdl_context.release_transactional_locks(); |
| 2068 | |
| 2069 | /* |
| 2070 | The second scenario: "obtrusive" lock first then "unobtrusive". |
| 2071 | |
| 2072 | Let us try to acquire global S lock. |
| 2073 | */ |
| 2074 | MDL_REQUEST_INIT(&m_global_request, MDL_key::GLOBAL, "", "", MDL_SHARED, |
| 2075 | MDL_TRANSACTION); |
| 2076 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2077 | |
| 2078 | /* |
| 2079 | Now let us acquire IX lock. Note that S lock is not exactly stronger |
| 2080 | than IX lock, so IX lock can't be acquired using find_ticket() |
| 2081 | optimization. |
| 2082 | */ |
| 2083 | MDL_REQUEST_INIT(&m_global_request, MDL_key::GLOBAL, "", "", |
| 2084 | MDL_INTENTION_EXCLUSIVE, MDL_TRANSACTION); |
| 2085 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2086 | |
| 2087 | m_mdl_context.release_transactional_locks(); |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | Verifies that we correctly account for "fast path" locks in |
| 2092 | clone_ticket() operation. |
| 2093 | */ |
| 2094 | |
| 2095 | TEST_F(MDLTest, CloneSharedExclusive) { |
| 2096 | MDL_ticket *initial_ticket; |
| 2097 | Notification lock_blocked; |
| 2098 | MDL_thread mdl_thread(table_name1, MDL_EXCLUSIVE, nullptr, nullptr, |
| 2099 | &lock_blocked, nullptr); |
| 2100 | |
| 2101 | /* Acquire SHARED lock, it will be acquired using "fast path" algorithm. */ |
| 2102 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 2103 | MDL_EXPLICIT); |
| 2104 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2105 | |
| 2106 | /* |
| 2107 | Save initial ticket and create its clone. |
| 2108 | This operation should correctly update MDL_lock's "fast path" |
| 2109 | counter. |
| 2110 | */ |
| 2111 | initial_ticket = m_request.ticket; |
| 2112 | EXPECT_FALSE(m_mdl_context.clone_ticket(&m_request)); |
| 2113 | |
| 2114 | /* Release the original lock. This will decrement "fast path" counter. */ |
| 2115 | m_mdl_context.release_lock(initial_ticket); |
| 2116 | |
| 2117 | /* |
| 2118 | Now try to acquire EXCLUSIVE lock. This request should be blocked |
| 2119 | because "fast path" counter is non-zero. |
| 2120 | */ |
| 2121 | mdl_thread.start(); |
| 2122 | lock_blocked.wait_for_notification(); |
| 2123 | |
| 2124 | /* Release remaining SHARED lock to unblock request for EXCLUSIVE lock. */ |
| 2125 | m_mdl_context.release_lock(m_request.ticket); |
| 2126 | |
| 2127 | mdl_thread.join(); |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | Verifies that we correctly account for "obtrusive" locks in |
| 2132 | clone_ticket() operation. |
| 2133 | */ |
| 2134 | |
| 2135 | TEST_F(MDLTest, CloneExclusiveShared) { |
| 2136 | MDL_ticket *initial_ticket; |
| 2137 | Notification lock_blocked; |
| 2138 | MDL_thread mdl_thread(table_name1, MDL_SHARED, nullptr, nullptr, |
| 2139 | &lock_blocked, nullptr); |
| 2140 | |
| 2141 | /* Acquire EXCLUSIVE lock, counter of "obtrusive" locks is increased. */ |
| 2142 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2143 | MDL_EXCLUSIVE, MDL_EXPLICIT); |
| 2144 | |
| 2145 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2146 | |
| 2147 | /* |
| 2148 | Save initial ticket and create its clone. |
| 2149 | This operation should correctly increase counter of "obtrusive" locks. |
| 2150 | */ |
| 2151 | initial_ticket = m_request.ticket; |
| 2152 | EXPECT_FALSE(m_mdl_context.clone_ticket(&m_request)); |
| 2153 | |
| 2154 | /* |
| 2155 | Release the original lock. This will decrement count of "obtrusive" locks. |
| 2156 | */ |
| 2157 | m_mdl_context.release_lock(initial_ticket); |
| 2158 | |
| 2159 | /* |
| 2160 | Now try to acquire SHARED lock. This request should be blocked |
| 2161 | because "obtrusive" counter still should be non-zero. |
| 2162 | */ |
| 2163 | mdl_thread.start(); |
| 2164 | lock_blocked.wait_for_notification(); |
| 2165 | |
| 2166 | /* Release remaining EXCLUSIVE lock to unblock request for SHARED lock. */ |
| 2167 | m_mdl_context.release_lock(m_request.ticket); |
| 2168 | |
| 2169 | mdl_thread.join(); |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | Verify that we correctly notify owners of "unobtrusive" locks when "obtrusive" |
| 2174 | locks are acquired, even though the former can be initially acquired |
| 2175 | using "fast path". |
| 2176 | */ |
| 2177 | TEST_F(MDLTest, NotifyScenarios) { |
| 2178 | /* |
| 2179 | The first scenario: Check that notification works properly if for |
| 2180 | "unobtrusive" lock owner set_needs_thr_lock_abort(true) |
| 2181 | was called after lock acquisition. |
| 2182 | */ |
| 2183 | Notification first_shared_grabbed, first_shared_release; |
| 2184 | Notification first_shared_released, first_exclusive_grabbed; |
| 2185 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &first_shared_grabbed, |
| 2186 | &first_shared_release, nullptr, |
| 2187 | &first_shared_released); |
| 2188 | MDL_thread mdl_thread2(table_name1, MDL_EXCLUSIVE, &first_exclusive_grabbed, |
| 2189 | nullptr, nullptr, nullptr); |
| 2190 | |
| 2191 | /* Acquire S lock which will be granted using "fast path". */ |
| 2192 | mdl_thread1.enable_release_on_notify(); |
| 2193 | mdl_thread1.start(); |
| 2194 | first_shared_grabbed.wait_for_notification(); |
| 2195 | |
| 2196 | /* |
| 2197 | In order for notification to work properly for such locks |
| 2198 | context should be marked as requiring lock abort. |
| 2199 | */ |
| 2200 | mdl_thread1.get_mdl_context().set_needs_thr_lock_abort(true); |
| 2201 | |
| 2202 | /* |
| 2203 | Now try to acquire X lock. This attemptshould notify owner of S lock. |
| 2204 | In our unit test such notification causes S lock release, so X lock |
| 2205 | should be successfully granted after that. |
| 2206 | */ |
| 2207 | mdl_thread2.start(); |
| 2208 | first_shared_released.wait_for_notification(); |
| 2209 | first_exclusive_grabbed.wait_for_notification(); |
| 2210 | |
| 2211 | /* Wrap-up of the first scenario. */ |
| 2212 | mdl_thread1.join(); |
| 2213 | mdl_thread2.join(); |
| 2214 | |
| 2215 | /* |
| 2216 | The second scenario: Check the same works fine when |
| 2217 | set_needs_thr_lock_abort(true) was called before |
| 2218 | lock acquisition. |
| 2219 | */ |
| 2220 | Notification second_shared_grabbed, second_shared_release; |
| 2221 | Notification second_shared_released, second_exclusive_grabbed; |
| 2222 | MDL_thread mdl_thread3(table_name1, MDL_SHARED, &second_shared_grabbed, |
| 2223 | &second_shared_release, nullptr, |
| 2224 | &second_shared_released); |
| 2225 | MDL_thread mdl_thread4(table_name1, MDL_EXCLUSIVE, &second_exclusive_grabbed, |
| 2226 | nullptr, nullptr, nullptr); |
| 2227 | |
| 2228 | /* |
| 2229 | In order for notification to work properly context should be marked |
| 2230 | as requiring lock abort. |
| 2231 | */ |
| 2232 | mdl_thread3.get_mdl_context().set_needs_thr_lock_abort(true); |
| 2233 | mdl_thread3.enable_release_on_notify(); |
| 2234 | /* Acquire S lock which will be granted using "fast path". */ |
| 2235 | mdl_thread3.start(); |
| 2236 | second_shared_grabbed.wait_for_notification(); |
| 2237 | |
| 2238 | /* |
| 2239 | Now try to acquire X lock. This attemptshould notify owner of S lock. |
| 2240 | In our unit test such notification causes S lock release, so X lock |
| 2241 | should be successfully granted after that. |
| 2242 | */ |
| 2243 | mdl_thread4.start(); |
| 2244 | second_shared_released.wait_for_notification(); |
| 2245 | second_exclusive_grabbed.wait_for_notification(); |
| 2246 | |
| 2247 | /* Wrap-up of the second scenario. */ |
| 2248 | mdl_thread3.join(); |
| 2249 | mdl_thread4.join(); |
| 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | Verify that upgrade to "obtrusive" lock doesn't leave any wrong traces |
| 2254 | in MDL subsystem. |
| 2255 | */ |
| 2256 | |
| 2257 | TEST_F(MDLTest, UpgradeScenarios) { |
| 2258 | /* |
| 2259 | The first scenario: Upgrade to X lock and then its release, should not |
| 2260 | leave traces blocking further S locks. |
| 2261 | */ |
| 2262 | |
| 2263 | /* Acquire S lock to be upgraded. */ |
| 2264 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 2265 | MDL_TRANSACTION); |
| 2266 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2267 | |
| 2268 | /* Acquire IX lock to be able to upgrade. */ |
| 2269 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2270 | |
| 2271 | /* Upgrade S lock to X lock. */ |
| 2272 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 2273 | MDL_EXCLUSIVE, long_timeout)); |
| 2274 | |
| 2275 | /* |
| 2276 | Ensure that there is pending S lock, so release of X lock won't destroy |
| 2277 | MDL_lock object. |
| 2278 | */ |
| 2279 | Notification first_blocked; |
| 2280 | Notification first_release; |
| 2281 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, nullptr, &first_release, |
| 2282 | &first_blocked, nullptr); |
| 2283 | mdl_thread1.start(); |
| 2284 | first_blocked.wait_for_notification(); |
| 2285 | |
| 2286 | /* |
| 2287 | Now release IX and X locks. |
| 2288 | The latter should clear "obtrusive" locks counter so it should be |
| 2289 | possible to acquire S lock using "fast path" after that. |
| 2290 | */ |
| 2291 | m_mdl_context.release_transactional_locks(); |
| 2292 | |
| 2293 | /* Check that we can acquire S lock. */ |
| 2294 | Notification second_grabbed; |
| 2295 | MDL_thread mdl_thread2(table_name1, MDL_SHARED, &second_grabbed, nullptr, |
| 2296 | nullptr, nullptr); |
| 2297 | mdl_thread2.start(); |
| 2298 | second_grabbed.wait_for_notification(); |
| 2299 | |
| 2300 | /* Wrap-up of the first scenario. */ |
| 2301 | first_release.notify(); |
| 2302 | mdl_thread1.join(); |
| 2303 | mdl_thread2.join(); |
| 2304 | |
| 2305 | /* |
| 2306 | The second scenario: Upgrade from S to X, should not block further X |
| 2307 | requests if upgraded lock is released. |
| 2308 | */ |
| 2309 | |
| 2310 | /* Acquire S lock to be upgraded. */ |
| 2311 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 2312 | MDL_TRANSACTION); |
| 2313 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2314 | |
| 2315 | /* Upgrade S lock to X lock. */ |
| 2316 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 2317 | MDL_EXCLUSIVE, long_timeout)); |
| 2318 | |
| 2319 | /* |
| 2320 | Try to acquire X lock, it will block. |
| 2321 | */ |
| 2322 | Notification third_blocked; |
| 2323 | Notification third_grabbed; |
| 2324 | MDL_thread mdl_thread3(table_name1, MDL_EXCLUSIVE, &third_grabbed, nullptr, |
| 2325 | &third_blocked, nullptr); |
| 2326 | mdl_thread3.start(); |
| 2327 | third_blocked.wait_for_notification(); |
| 2328 | |
| 2329 | /* |
| 2330 | Now release IX and X locks. Upgrade should not leave any traces |
| 2331 | from original S lock (e.g. no "fast path" counter), so this should |
| 2332 | unblock pending X lock. |
| 2333 | */ |
| 2334 | m_mdl_context.release_transactional_locks(); |
| 2335 | third_grabbed.wait_for_notification(); |
| 2336 | |
| 2337 | /* Wrap-up of the second scenario. */ |
| 2338 | mdl_thread3.join(); |
| 2339 | |
| 2340 | /* |
| 2341 | The third scenario: After upgrade from SU to X lock and release of the |
| 2342 | latter there should not be traces preventing "fast |
| 2343 | path" acquisition of S lock. Main difference from |
| 2344 | the first scenario is that we upgrade from "obtrusive" |
| 2345 | lock, so different path is triggered. |
| 2346 | */ |
| 2347 | /* Acquire SU lock to be upgraded. */ |
| 2348 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2349 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 2350 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2351 | |
| 2352 | /* Upgrade SU lock to X lock. */ |
| 2353 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(m_request.ticket, |
| 2354 | MDL_EXCLUSIVE, long_timeout)); |
| 2355 | |
| 2356 | /* |
| 2357 | Ensure that there is pending S lock, so release of X lock won't destroy |
| 2358 | MDL_lock object. |
| 2359 | */ |
| 2360 | Notification fourth_blocked; |
| 2361 | Notification fourth_release; |
| 2362 | MDL_thread mdl_thread4(table_name1, MDL_SHARED, nullptr, &fourth_release, |
| 2363 | &fourth_blocked, nullptr); |
| 2364 | mdl_thread4.start(); |
| 2365 | fourth_blocked.wait_for_notification(); |
| 2366 | |
| 2367 | /* |
| 2368 | Now release IX and X locks. |
| 2369 | The latter should clear "obtrusive" locks counter so it should be |
| 2370 | possible to acquire S lock using "fast path" after that. |
| 2371 | */ |
| 2372 | m_mdl_context.release_transactional_locks(); |
| 2373 | |
| 2374 | /* Check that we can acquire S lock. */ |
| 2375 | Notification fifth_grabbed; |
| 2376 | MDL_thread mdl_thread5(table_name1, MDL_SHARED, &fifth_grabbed, nullptr, |
| 2377 | nullptr, nullptr); |
| 2378 | mdl_thread5.start(); |
| 2379 | fifth_grabbed.wait_for_notification(); |
| 2380 | |
| 2381 | /* Wrap-up of the third scenario. */ |
| 2382 | fourth_release.notify(); |
| 2383 | mdl_thread4.join(); |
| 2384 | mdl_thread5.join(); |
| 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | Verify that MDL subsystem can correctly handle deadlock |
| 2389 | in which one of participated locks was initially granted |
| 2390 | using "fast path". |
| 2391 | */ |
| 2392 | |
| 2393 | TEST_F(MDLTest, Deadlock) { |
| 2394 | Notification lock_blocked; |
| 2395 | MDL_thread mdl_thread(table_name1, MDL_EXCLUSIVE, nullptr, nullptr, |
| 2396 | &lock_blocked, nullptr); |
| 2397 | |
| 2398 | /* Acquire SR lock which will be granted using "fast path". */ |
| 2399 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2400 | MDL_SHARED_READ, MDL_TRANSACTION); |
| 2401 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2402 | |
| 2403 | /* Now try to request X lock. This will blocked. */ |
| 2404 | mdl_thread.start(); |
| 2405 | lock_blocked.wait_for_notification(); |
| 2406 | |
| 2407 | /* |
| 2408 | Try to acquire SW lock. |
| 2409 | Because of pending X lock this will lead to deadlock. |
| 2410 | |
| 2411 | Which should be correctly detected and reported even though SR |
| 2412 | lock was originally granted using "fast path". |
| 2413 | */ |
| 2414 | expected_error = ER_LOCK_DEADLOCK; |
| 2415 | |
| 2416 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2417 | MDL_SHARED_WRITE, MDL_TRANSACTION); |
| 2418 | EXPECT_TRUE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2419 | |
| 2420 | /* Wrap-up. */ |
| 2421 | m_mdl_context.release_transactional_locks(); |
| 2422 | mdl_thread.join(); |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | Verify that downgrade correctly handles "obtrusive" locks by decrementing |
| 2427 | "obtrusive"-lock counter when necessary. |
| 2428 | */ |
| 2429 | |
| 2430 | TEST_F(MDLTest, DowngradeShared) { |
| 2431 | Notification lock_grabbed; |
| 2432 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, nullptr, |
| 2433 | nullptr, nullptr); |
| 2434 | |
| 2435 | /* Acquire global IX lock first to satisfy MDL asserts. */ |
| 2436 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2437 | |
| 2438 | /* Acquire "obtrusive" X lock. */ |
| 2439 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2440 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 2441 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 2442 | |
| 2443 | /* Downgrade lock to S lock. */ |
| 2444 | m_request.ticket->downgrade_lock(MDL_SHARED); |
| 2445 | |
| 2446 | /* |
| 2447 | It should be possible to acquire S lock now in another thread since |
| 2448 | downgrade operation should decrement counter of "obtrusive" locks. |
| 2449 | */ |
| 2450 | mdl_thread.start(); |
| 2451 | lock_grabbed.wait_for_notification(); |
| 2452 | |
| 2453 | /* Wrap-up. */ |
| 2454 | m_mdl_context.release_transactional_locks(); |
| 2455 | mdl_thread.join(); |
| 2456 | } |
| 2457 | |
| 2458 | /** |
| 2459 | Verify that rescheduling of "obtrusive" lock correctly handles |
| 2460 | "obtrusive" lock counter and further attempts to acquire "unobtrusive" |
| 2461 | locks are not blocked. |
| 2462 | */ |
| 2463 | |
| 2464 | TEST_F(MDLTest, RescheduleSharedNoWrite) { |
| 2465 | Notification shared_grabbed; |
| 2466 | Notification shared_release; |
| 2467 | Notification first_shared_write_grabbed; |
| 2468 | Notification first_shared_write_release; |
| 2469 | Notification shared_no_write_grabbed; |
| 2470 | Notification shared_no_write_blocked; |
| 2471 | Notification second_shared_write_grabbed; |
| 2472 | |
| 2473 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &shared_grabbed, |
| 2474 | &shared_release, nullptr, nullptr); |
| 2475 | MDL_thread mdl_thread2(table_name1, MDL_SHARED_WRITE, |
| 2476 | &first_shared_write_grabbed, |
| 2477 | &first_shared_write_release, nullptr, nullptr); |
| 2478 | MDL_thread mdl_thread3(table_name1, MDL_SHARED_NO_WRITE, |
| 2479 | &shared_no_write_grabbed, nullptr, |
| 2480 | &shared_no_write_blocked, nullptr); |
| 2481 | MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE, |
| 2482 | &second_shared_write_grabbed, nullptr, nullptr, |
| 2483 | nullptr); |
| 2484 | |
| 2485 | /* Start thread which will acquire S lock. */ |
| 2486 | mdl_thread1.start(); |
| 2487 | shared_grabbed.wait_for_notification(); |
| 2488 | |
| 2489 | /* Start thread which will acquire SW lock. */ |
| 2490 | mdl_thread2.start(); |
| 2491 | first_shared_write_grabbed.wait_for_notification(); |
| 2492 | |
| 2493 | /* Now start thread which will try to acquire SNW lock and will block. */ |
| 2494 | mdl_thread3.start(); |
| 2495 | shared_no_write_blocked.wait_for_notification(); |
| 2496 | |
| 2497 | /* |
| 2498 | Now we will release the first SW lock. This should schedule SNW lock. |
| 2499 | In the process or rescheduling counter of waiting and granted "obtrusive" |
| 2500 | locks should stay the same. So release of SNW, which happens after that, |
| 2501 | will allow to acquire more SW locks. |
| 2502 | */ |
| 2503 | first_shared_write_release.notify(); |
| 2504 | |
| 2505 | /* It should be possible to acquire SW lock without problems now. */ |
| 2506 | mdl_thread4.start(); |
| 2507 | second_shared_write_grabbed.wait_for_notification(); |
| 2508 | |
| 2509 | /* Wrap-up. */ |
| 2510 | shared_release.notify(); |
| 2511 | mdl_thread1.join(); |
| 2512 | mdl_thread2.join(); |
| 2513 | mdl_thread3.join(); |
| 2514 | mdl_thread4.join(); |
| 2515 | } |
| 2516 | |
| 2517 | /** |
| 2518 | Verify that try_acquire_lock() operation for lock correctly |
| 2519 | cleans up after itself. |
| 2520 | */ |
| 2521 | |
| 2522 | TEST_F(MDLTest, ConcurrentSharedTryExclusive) { |
| 2523 | Notification first_grabbed, second_grabbed, third_grabbed; |
| 2524 | Notification first_release, second_release; |
| 2525 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &first_grabbed, |
| 2526 | &first_release, nullptr, nullptr); |
| 2527 | MDL_thread mdl_thread2(table_name1, MDL_SHARED, &second_grabbed, |
| 2528 | &second_release, nullptr, nullptr); |
| 2529 | MDL_thread mdl_thread3(table_name1, MDL_SHARED, &third_grabbed, nullptr, |
| 2530 | nullptr, nullptr); |
| 2531 | |
| 2532 | /* Start the first thread which will acquire S lock. */ |
| 2533 | mdl_thread1.start(); |
| 2534 | first_grabbed.wait_for_notification(); |
| 2535 | |
| 2536 | /* Acquire global IX lock to satisfy asserts in MDL subsystem. */ |
| 2537 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_global_request, long_timeout)); |
| 2538 | EXPECT_NE(m_null_ticket, m_global_request.ticket); |
| 2539 | |
| 2540 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 2541 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 2542 | |
| 2543 | /* |
| 2544 | Attempt to acquire X lock on table should fail. |
| 2545 | But it should correctly cleanup after itself. |
| 2546 | */ |
| 2547 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 2548 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 2549 | |
| 2550 | first_release.notify(); |
| 2551 | mdl_thread1.join(); |
| 2552 | |
| 2553 | /* After S lock is released. MDL_lock should be counted as unused. */ |
| 2554 | EXPECT_EQ(1, mdl_get_unused_locks_count()); |
| 2555 | |
| 2556 | /* Start the second thread which will acquire S lock again. */ |
| 2557 | mdl_thread2.start(); |
| 2558 | second_grabbed.wait_for_notification(); |
| 2559 | |
| 2560 | /* Attempt to acquire X lock on table should fail again. */ |
| 2561 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request)); |
| 2562 | EXPECT_EQ(m_null_ticket, m_request.ticket); |
| 2563 | |
| 2564 | /* Grabbing another S lock after that should not be a problem. */ |
| 2565 | mdl_thread3.start(); |
| 2566 | third_grabbed.wait_for_notification(); |
| 2567 | |
| 2568 | second_release.notify(); |
| 2569 | mdl_thread2.join(); |
| 2570 | mdl_thread3.join(); |
| 2571 | |
| 2572 | m_mdl_context.release_transactional_locks(); |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | Basic test which checks that unused MDL_lock objects are freed at all. |
| 2577 | */ |
| 2578 | |
| 2579 | TEST_F(MDLTest, UnusedBasic) { |
| 2580 | mdl_locks_unused_locks_low_water = 0; |
| 2581 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2582 | test_one_simple_shared_lock(MDL_SHARED); |
| 2583 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | More complex test in which we test freeing couple of unused |
| 2588 | MDL_lock objects while having another one still used and around. |
| 2589 | */ |
| 2590 | |
| 2591 | TEST_F(MDLTest, UnusedConcurrentThree) { |
| 2592 | Notification first_grabbed, first_release, second_grabbed, second_release, |
| 2593 | third_grabbed, third_release; |
| 2594 | |
| 2595 | MDL_thread mdl_thread1(table_name1, MDL_SHARED, &first_grabbed, |
| 2596 | &first_release, nullptr, nullptr); |
| 2597 | MDL_thread mdl_thread2(table_name2, MDL_SHARED, &second_grabbed, |
| 2598 | &second_release, nullptr, nullptr); |
| 2599 | MDL_thread mdl_thread3(table_name3, MDL_SHARED_UPGRADABLE, &third_grabbed, |
| 2600 | &third_release, nullptr, nullptr); |
| 2601 | |
| 2602 | mdl_locks_unused_locks_low_water = 0; |
| 2603 | |
| 2604 | /* Start thread which will use one MDL_lock object and will keep it used. */ |
| 2605 | mdl_thread1.start(); |
| 2606 | first_grabbed.wait_for_notification(); |
| 2607 | |
| 2608 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2609 | |
| 2610 | /* |
| 2611 | Start thread which will acquire lock on another table, i.e. will use |
| 2612 | another MDL_lock object. |
| 2613 | */ |
| 2614 | mdl_thread2.start(); |
| 2615 | second_grabbed.wait_for_notification(); |
| 2616 | |
| 2617 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2618 | |
| 2619 | /* Now let 2nd thread to release its lock and thus unuse MDL_lock object. */ |
| 2620 | second_release.notify(); |
| 2621 | mdl_thread2.join(); |
| 2622 | |
| 2623 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2624 | |
| 2625 | /* |
| 2626 | Start 3rd thread which will acquire "slow path" lock and release it. |
| 2627 | By doing this we test freeing of unused on "slow path" release. |
| 2628 | */ |
| 2629 | mdl_thread3.start(); |
| 2630 | third_grabbed.wait_for_notification(); |
| 2631 | |
| 2632 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2633 | |
| 2634 | /* Now let 3nd thread to release its lock and thus unuse MDL_lock object. */ |
| 2635 | third_release.notify(); |
| 2636 | mdl_thread3.join(); |
| 2637 | |
| 2638 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2639 | |
| 2640 | /* |
| 2641 | Let the 1st thread to release its lock and unuse its MDL_lock object |
| 2642 | as well. |
| 2643 | */ |
| 2644 | first_release.notify(); |
| 2645 | mdl_thread1.join(); |
| 2646 | |
| 2647 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | Finally test which involves many threads using, unusing and |
| 2652 | freeing MDL_lock objects. |
| 2653 | */ |
| 2654 | |
| 2655 | TEST_F(MDLTest, UnusedConcurrentMany) { |
| 2656 | const uint THREADS = 50, TABLES = 10; |
| 2657 | const char *table_names_group_a[TABLES] = {"0", "1", "2", "3", "4", |
| 2658 | "5", "6", "7", "8", "9"}; |
| 2659 | const char *table_names_group_b[TABLES] = {"a", "b", "c", "d", "e", |
| 2660 | "f", "g", "h", "i", "j"}; |
| 2661 | MDL_thread *mdl_thread_group_a[THREADS]; |
| 2662 | MDL_thread *mdl_thread_group_b[THREADS]; |
| 2663 | Notification group_a_grabbed[THREADS], group_a_release, |
| 2664 | group_b_grabbed[THREADS], group_b_release; |
| 2665 | uint i; |
| 2666 | |
| 2667 | for (i = 0; i < THREADS; ++i) |
| 2668 | mdl_thread_group_a[i] = new MDL_thread( |
| 2669 | table_names_group_a[i % TABLES], |
| 2670 | /* |
| 2671 | To make things more interesting for the |
| 2672 | first 2 tables in the group on of threads |
| 2673 | will also acquire SU lock. |
| 2674 | */ |
| 2675 | ((i % TABLES < 2) && (i % TABLES == i)) ? MDL_SHARED_UPGRADABLE |
| 2676 | : MDL_SHARED, |
| 2677 | &group_a_grabbed[i], &group_a_release, nullptr, nullptr); |
| 2678 | |
| 2679 | for (i = 0; i < THREADS; ++i) |
| 2680 | mdl_thread_group_b[i] = new MDL_thread( |
| 2681 | table_names_group_b[i % TABLES], |
| 2682 | /* |
| 2683 | To make things more interesting for the |
| 2684 | first 2 tables in the group on of threads |
| 2685 | will also acquire SU lock. |
| 2686 | */ |
| 2687 | ((i % TABLES < 2) && (i % TABLES == i)) ? MDL_SHARED_UPGRADABLE |
| 2688 | : MDL_SHARED, |
| 2689 | &group_b_grabbed[i], &group_b_release, nullptr, nullptr); |
| 2690 | |
| 2691 | mdl_locks_unused_locks_low_water = 0; |
| 2692 | |
| 2693 | /* Start both groups of threads. */ |
| 2694 | for (i = 0; i < THREADS; ++i) mdl_thread_group_a[i]->start(); |
| 2695 | for (i = 0; i < THREADS; ++i) mdl_thread_group_b[i]->start(); |
| 2696 | |
| 2697 | /* Wait until both groups acquire their locks. */ |
| 2698 | for (i = 0; i < THREADS; ++i) group_a_grabbed[i].wait_for_notification(); |
| 2699 | for (i = 0; i < THREADS; ++i) group_b_grabbed[i].wait_for_notification(); |
| 2700 | |
| 2701 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2702 | |
| 2703 | /* |
| 2704 | Now let the second group to release its locks and free unused MDL_lock |
| 2705 | objects. The first group will keep its objects used. |
| 2706 | */ |
| 2707 | group_b_release.notify(); |
| 2708 | |
| 2709 | /* Wait until it is done. */ |
| 2710 | for (i = 0; i < THREADS; ++i) { |
| 2711 | mdl_thread_group_b[i]->join(); |
| 2712 | delete mdl_thread_group_b[i]; |
| 2713 | } |
| 2714 | |
| 2715 | /* |
| 2716 | At this point we should not have more than |
| 2717 | TABLES*MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO/(1-MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO |
| 2718 | unused objects. It is OK to have less. |
| 2719 | */ |
| 2720 | EXPECT_GE((TABLES * MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO / |
| 2721 | (1 - MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO)), |
| 2722 | mdl_get_unused_locks_count()); |
| 2723 | |
| 2724 | /* Now let the first group go as well. */ |
| 2725 | group_a_release.notify(); |
| 2726 | for (i = 0; i < THREADS; ++i) { |
| 2727 | mdl_thread_group_a[i]->join(); |
| 2728 | delete mdl_thread_group_a[i]; |
| 2729 | } |
| 2730 | |
| 2731 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2732 | } |
| 2733 | |
| 2734 | /** |
| 2735 | Test that unused locks low water threshold works as expected, |
| 2736 | i.e. that we don't free unused objects unless their number |
| 2737 | exceeds threshold. |
| 2738 | */ |
| 2739 | |
| 2740 | TEST_F(MDLTest, UnusedLowWater) { |
| 2741 | const uint TABLES = 10; |
| 2742 | const char *table_names[TABLES] = {"0", "1", "2", "3", "4", |
| 2743 | "5", "6", "7", "8", "9"}; |
| 2744 | MDL_request request; |
| 2745 | uint i; |
| 2746 | |
| 2747 | mdl_locks_unused_locks_low_water = 4; |
| 2748 | |
| 2749 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2750 | |
| 2751 | /* Acquire some locks and then release them all at once. */ |
| 2752 | |
| 2753 | for (i = 0; i < TABLES; ++i) { |
| 2754 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_names[i], |
| 2755 | MDL_SHARED, MDL_TRANSACTION); |
| 2756 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 2757 | } |
| 2758 | |
| 2759 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2760 | |
| 2761 | /* Release all locks. */ |
| 2762 | m_mdl_context.release_transactional_locks(); |
| 2763 | |
| 2764 | /* Number of unused lock objects should not go below threshold. */ |
| 2765 | EXPECT_EQ(4, mdl_get_unused_locks_count()); |
| 2766 | } |
| 2767 | |
| 2768 | /** |
| 2769 | Test that we don't free unused MDL_lock objects if unused/total objects |
| 2770 | ratio is below MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO. |
| 2771 | */ |
| 2772 | |
| 2773 | TEST_F(MDLTest, UnusedMinRatio) { |
| 2774 | const uint TABLES = 10; |
| 2775 | const char *table_names_a[TABLES] = {"0", "1", "2", "3", "4", |
| 2776 | "5", "6", "7", "8", "9"}; |
| 2777 | const char *table_names_b[TABLES] = {"0", "1", "2", "3", "4", |
| 2778 | "5", "6", "7", "8", "9"}; |
| 2779 | MDL_request request; |
| 2780 | uint i; |
| 2781 | |
| 2782 | mdl_locks_unused_locks_low_water = 0; |
| 2783 | |
| 2784 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2785 | |
| 2786 | /* Acquire some locks. */ |
| 2787 | |
| 2788 | for (i = 0; i < TABLES; ++i) { |
| 2789 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_names_a[i], |
| 2790 | MDL_SHARED, MDL_TRANSACTION); |
| 2791 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 2792 | } |
| 2793 | |
| 2794 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2795 | |
| 2796 | /* Take a savepoint to be able to release part of the locks in future. */ |
| 2797 | MDL_savepoint savepoint = m_mdl_context.mdl_savepoint(); |
| 2798 | |
| 2799 | /* Acquire a few more locks. */ |
| 2800 | for (i = 0; i < TABLES; ++i) { |
| 2801 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_names_b[i], |
| 2802 | MDL_SHARED, MDL_TRANSACTION); |
| 2803 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 2804 | } |
| 2805 | |
| 2806 | /* Now release part of the locks we hold. */ |
| 2807 | m_mdl_context.rollback_to_savepoint(savepoint); |
| 2808 | |
| 2809 | /* |
| 2810 | At this point we should not have more than |
| 2811 | TABLES*MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO/(1-MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO |
| 2812 | unused objects. |
| 2813 | |
| 2814 | This is equivalent to: |
| 2815 | "unused objects" <= (("used objects (i.e.TABLES)" + "unused objects") * |
| 2816 | MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO). |
| 2817 | */ |
| 2818 | EXPECT_GE((TABLES * MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO / |
| 2819 | (1 - MDL_LOCKS_UNUSED_LOCKS_MIN_RATIO)), |
| 2820 | mdl_get_unused_locks_count()); |
| 2821 | |
| 2822 | /* Release all locks. */ |
| 2823 | m_mdl_context.release_transactional_locks(); |
| 2824 | |
| 2825 | /* |
| 2826 | There should be no unused MDL_lock objects after this |
| 2827 | since low water threshold is zero. |
| 2828 | */ |
| 2829 | EXPECT_EQ(0, mdl_get_unused_locks_count()); |
| 2830 | } |
| 2831 | |
| 2832 | /* |
| 2833 | Verifies following scenario, |
| 2834 | After granting max_write_lock_count(=1) number of times for SW |
| 2835 | lock request, lock is granted to starving SRO lock request |
| 2836 | in wait queue. After SRO lock has been granted SW locks again |
| 2837 | get higher priority than SRO locks until max_write_lock_count(=1) |
| 2838 | SW lock requests has been satisfied. |
| 2839 | |
| 2840 | - max_write_lock_count= 1 |
| 2841 | - THREAD 1: Acquires SW lock on the table. |
| 2842 | - THREAD 2: Requests for SRO lock on the table, blocked. |
| 2843 | - THREAD 3: Acquires SW lock on the table (m_piglet_lock_count= 1). |
| 2844 | - THREAD 4: Requests for SW on the table, blocked. |
| 2845 | - THREAD 5: Requests for SRO lock on the table, blocked. |
| 2846 | - THREAD 1,3: Release SW locks. |
| 2847 | - THREAD 2,5: Get SRO locks, (m_piglet_lock_count= 0) |
| 2848 | THREAD 6: Requests SRO lock, blocked. |
| 2849 | - THREAD 2,5: Release SRO locks. |
| 2850 | - THREAD 4: Gets SW lock (m_piglet_lock_count= 1). |
| 2851 | - THREAD 7: Requests SW lock, blocked. |
| 2852 | - THREAD 4: Releases SW lock. |
| 2853 | - THREAD 6: Gets SRO lock. Releases it. |
| 2854 | - THREAD 7: Gets SW lock. |
| 2855 | */ |
| 2856 | |
| 2857 | TEST_F(MDLTest, PigletLockTest) { |
| 2858 | Notification thd_lock_grabbed[7]; |
| 2859 | Notification thd_release_locks[7]; |
| 2860 | Notification thd_lock_blocked[7]; |
| 2861 | Notification thd_lock_released[7]; |
| 2862 | |
| 2863 | /* Locks taken by the threads */ |
| 2864 | enum { THD1_SW, THD2_SRO, THD3_SW, THD4_SW, THD5_SRO, THD6_SRO, THD7_SW }; |
| 2865 | |
| 2866 | max_write_lock_count = 1; |
| 2867 | |
| 2868 | /* |
| 2869 | THREAD1: Acquires SW lock on table. |
| 2870 | Lock Wait Queue: <empty> |
| 2871 | Lock Granted: SW |
| 2872 | */ |
| 2873 | MDL_thread mdl_thread1( |
| 2874 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD1_SW], |
| 2875 | &thd_release_locks[THD1_SW], &thd_lock_blocked[THD1_SW], |
| 2876 | &thd_lock_released[THD1_SW]); |
| 2877 | mdl_thread1.start(); |
| 2878 | thd_lock_grabbed[THD1_SW].wait_for_notification(); |
| 2879 | |
| 2880 | /* |
| 2881 | THREAD2: Requesting SRO lock on table. |
| 2882 | Lock Wait Queue: SRO |
| 2883 | Lock Granted: SW |
| 2884 | */ |
| 2885 | MDL_thread mdl_thread2( |
| 2886 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD2_SRO], |
| 2887 | &thd_release_locks[THD2_SRO], &thd_lock_blocked[THD2_SRO], |
| 2888 | &thd_lock_released[THD2_SRO]); |
| 2889 | mdl_thread2.start(); |
| 2890 | thd_lock_blocked[THD2_SRO].wait_for_notification(); |
| 2891 | |
| 2892 | /* |
| 2893 | THREAD3: Acquires SW lock on table. m_piglet_lock_count becomes 1. |
| 2894 | Lock Wait Queue: SRO |
| 2895 | Lock Granted: SW, SW |
| 2896 | */ |
| 2897 | MDL_thread mdl_thread3( |
| 2898 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD3_SW], |
| 2899 | &thd_release_locks[THD3_SW], &thd_lock_blocked[THD3_SW], |
| 2900 | &thd_lock_released[THD3_SW]); |
| 2901 | mdl_thread3.start(); |
| 2902 | thd_lock_grabbed[THD3_SW].wait_for_notification(); |
| 2903 | |
| 2904 | /* |
| 2905 | THREAD4: Requesting SW lock on table. |
| 2906 | Blocks because m_piglet_lock_count == max_write_lock_count. |
| 2907 | |
| 2908 | Lock Wait Queue: SRO <-- SW |
| 2909 | Lock Granted: SW, SW |
| 2910 | m_piglet_lock_count == 1 |
| 2911 | */ |
| 2912 | MDL_thread mdl_thread4( |
| 2913 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD4_SW], |
| 2914 | &thd_release_locks[THD4_SW], &thd_lock_blocked[THD4_SW], |
| 2915 | &thd_lock_released[THD4_SW]); |
| 2916 | mdl_thread4.start(); |
| 2917 | thd_lock_blocked[THD4_SW].wait_for_notification(); |
| 2918 | |
| 2919 | /* |
| 2920 | THREAD 5: Requests SRO lock on the table. |
| 2921 | Lock Wait Queue: SRO <-- SW <--SRO |
| 2922 | Lock Granted: SW, SW |
| 2923 | m_piglet_lock_count == 1 |
| 2924 | */ |
| 2925 | MDL_thread mdl_thread5( |
| 2926 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD5_SRO], |
| 2927 | &thd_release_locks[THD5_SRO], &thd_lock_blocked[THD5_SRO], |
| 2928 | &thd_lock_released[THD5_SRO]); |
| 2929 | mdl_thread5.start(); |
| 2930 | thd_lock_blocked[THD5_SRO].wait_for_notification(); |
| 2931 | |
| 2932 | /* |
| 2933 | THREAD 1, 3: Release SW locks. Both SRO locks are granted since |
| 2934 | m_piglet_lock_count == max_write_lock_count. After that |
| 2935 | m_piglet_lock_count is reset to 0. |
| 2936 | |
| 2937 | Lock Wait Queue: SW |
| 2938 | Lock Granted: SRO, SRO |
| 2939 | */ |
| 2940 | thd_release_locks[THD1_SW].notify(); |
| 2941 | thd_lock_released[THD1_SW].wait_for_notification(); |
| 2942 | thd_release_locks[THD3_SW].notify(); |
| 2943 | thd_lock_released[THD3_SW].wait_for_notification(); |
| 2944 | |
| 2945 | /* Locks are granted to THREAD 2 and THREAD 5 */ |
| 2946 | thd_lock_grabbed[THD2_SRO].wait_for_notification(); |
| 2947 | thd_lock_grabbed[THD5_SRO].wait_for_notification(); |
| 2948 | |
| 2949 | /* |
| 2950 | THREAD 6: Requests SRO lock on the table. |
| 2951 | Blocked because m_piglet_lock_count == 0. |
| 2952 | Lock Wait Queue: SW <-- SRO |
| 2953 | Lock Granted: SRO, SRO |
| 2954 | */ |
| 2955 | MDL_thread mdl_thread6( |
| 2956 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD6_SRO], |
| 2957 | &thd_release_locks[THD6_SRO], &thd_lock_blocked[THD6_SRO], |
| 2958 | &thd_lock_released[THD6_SRO]); |
| 2959 | mdl_thread6.start(); |
| 2960 | thd_lock_blocked[THD6_SRO].wait_for_notification(); |
| 2961 | |
| 2962 | /* THREAD 2 and THREAD 5: Release SRO locks */ |
| 2963 | thd_release_locks[THD2_SRO].notify(); |
| 2964 | thd_lock_released[THD2_SRO].wait_for_notification(); |
| 2965 | thd_release_locks[THD5_SRO].notify(); |
| 2966 | thd_lock_released[THD5_SRO].wait_for_notification(); |
| 2967 | |
| 2968 | /* |
| 2969 | THREAD 4: Gets SW lock. |
| 2970 | Since there is pending SRO, m_piglet_lock_count becomes 1. |
| 2971 | Lock Wait Queue: SRO |
| 2972 | Lock Granted: SW |
| 2973 | */ |
| 2974 | thd_lock_grabbed[THD4_SW].wait_for_notification(); |
| 2975 | |
| 2976 | /* |
| 2977 | THREAD 7: Requests SW lock on the table. |
| 2978 | Blocked because m_piglet_lock_count == max_write_lock_count. |
| 2979 | Lock Wait Queue: SRO <-- SW |
| 2980 | Lock Granted: SW |
| 2981 | */ |
| 2982 | MDL_thread mdl_thread7( |
| 2983 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD7_SW], |
| 2984 | &thd_release_locks[THD7_SW], &thd_lock_blocked[THD7_SW], |
| 2985 | &thd_lock_released[THD7_SW]); |
| 2986 | mdl_thread7.start(); |
| 2987 | thd_lock_blocked[THD7_SW].wait_for_notification(); |
| 2988 | |
| 2989 | /* THREAD 4: Release SW lock */ |
| 2990 | thd_release_locks[THD4_SW].notify(); |
| 2991 | thd_lock_released[THD4_SW].wait_for_notification(); |
| 2992 | |
| 2993 | /* THREAD 6: Gets SRO lock. */ |
| 2994 | thd_lock_grabbed[THD6_SRO].wait_for_notification(); |
| 2995 | |
| 2996 | /* Cleanup */ |
| 2997 | thd_release_locks[THD6_SRO].notify(); |
| 2998 | thd_lock_released[THD6_SRO].wait_for_notification(); |
| 2999 | |
| 3000 | thd_lock_grabbed[THD7_SW].wait_for_notification(); |
| 3001 | thd_release_locks[THD7_SW].notify(); |
| 3002 | thd_lock_released[THD7_SW].wait_for_notification(); |
| 3003 | |
| 3004 | mdl_thread1.join(); |
| 3005 | mdl_thread2.join(); |
| 3006 | mdl_thread3.join(); |
| 3007 | mdl_thread4.join(); |
| 3008 | mdl_thread5.join(); |
| 3009 | mdl_thread6.join(); |
| 3010 | mdl_thread7.join(); |
| 3011 | } |
| 3012 | |
| 3013 | /* |
| 3014 | Verifies interaction of "piglet" and "hog" lock requests. |
| 3015 | |
| 3016 | Check situation when we first reach limit on successive grants |
| 3017 | of "piglet" and then "hog" locks. Notice that once both these |
| 3018 | limits are reached we give a way to SRO locks and reset both |
| 3019 | counters even though there are still pending SW locks. This is |
| 3020 | allows to avoid stream of concurrent SRO and SW starving out |
| 3021 | "hog" locks. |
| 3022 | |
| 3023 | - max_write_lock_count= 1 |
| 3024 | - THREAD 1: Acquires SW lock on the table. |
| 3025 | - THREAD 2: Requests for SRO lock on the table, blocked. |
| 3026 | - THREAD 3: Acquires SW lock on the table (m_piglet_lock_count= 1). |
| 3027 | - THREAD 4: Requests for SW on the table, blocked. |
| 3028 | - THREAD 5: Requests for SNRW lock on the table, blocked. |
| 3029 | - THREAD 6: Requests for SRO lock on the table, blocked. |
| 3030 | - THREAD 7: Requests for SNRW lock on the table, blocked. |
| 3031 | - THREAD 8: Requests for SW lock on the table, blocked. |
| 3032 | - THREAD 1, 3: Release SW locks. |
| 3033 | - THREAD 5: Gets SNRW lock on the table (m_hog_lock_count= 1). |
| 3034 | - THREAD 5: Releases SNRW lock. |
| 3035 | - THREAD 2, 6: Get SRO locks on the table. (m_piglet_lock_count= 0, |
| 3036 | m_hog_lock_count= 0) |
| 3037 | - THREAD 9: Requests for SRO lock on the table, blocked. |
| 3038 | - THREAD 2, 6: Release SRO locks on the table. |
| 3039 | - THREAD 7: Gets SNRW lock (m_hog_lock_count= 1). |
| 3040 | - THREAD 7: Releases SNRW lock. |
| 3041 | - THREAD 4: Gets SW lock on the table. (m_piglet_lock_count= 1) |
| 3042 | - THREAD 4: Releases SW lock. |
| 3043 | - THREAD 9: Gets SRO lock. (m_piglet_lock_count= 0, |
| 3044 | m_hog_lock_count= 0). |
| 3045 | - THREAD 9: Releases SRO lock. |
| 3046 | - THREAD 8: Gets SW lock. |
| 3047 | */ |
| 3048 | |
| 3049 | TEST_F(MDLTest, PigletThenHogLockTest) { |
| 3050 | Notification thd_lock_grabbed[9]; |
| 3051 | Notification thd_release_locks[9]; |
| 3052 | Notification thd_lock_blocked[9]; |
| 3053 | Notification thd_lock_released[9]; |
| 3054 | |
| 3055 | /* Locks taken by the threads */ |
| 3056 | enum { |
| 3057 | THD1_SW, |
| 3058 | THD2_SRO, |
| 3059 | THD3_SW, |
| 3060 | THD4_SW, |
| 3061 | THD5_SNRW, |
| 3062 | THD6_SRO, |
| 3063 | THD7_SNRW, |
| 3064 | THD8_SW, |
| 3065 | THD9_SRO |
| 3066 | }; |
| 3067 | |
| 3068 | max_write_lock_count = 1; |
| 3069 | |
| 3070 | /* |
| 3071 | THREAD1: Acquires SW lock on table. |
| 3072 | Lock Wait Queue: <empty> |
| 3073 | Lock Granted: SW |
| 3074 | */ |
| 3075 | MDL_thread mdl_thread1( |
| 3076 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD1_SW], |
| 3077 | &thd_release_locks[THD1_SW], &thd_lock_blocked[THD1_SW], |
| 3078 | &thd_lock_released[THD1_SW]); |
| 3079 | mdl_thread1.start(); |
| 3080 | thd_lock_grabbed[THD1_SW].wait_for_notification(); |
| 3081 | |
| 3082 | /* |
| 3083 | THREAD2: Requesting SRO lock on table. |
| 3084 | Lock Wait Queue: SRO |
| 3085 | Lock Granted: SW |
| 3086 | */ |
| 3087 | MDL_thread mdl_thread2( |
| 3088 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD2_SRO], |
| 3089 | &thd_release_locks[THD2_SRO], &thd_lock_blocked[THD2_SRO], |
| 3090 | &thd_lock_released[THD2_SRO]); |
| 3091 | mdl_thread2.start(); |
| 3092 | thd_lock_blocked[THD2_SRO].wait_for_notification(); |
| 3093 | |
| 3094 | /* |
| 3095 | THREAD3: Acquires SW lock on table. m_piglet_lock_count becomes 1. |
| 3096 | Lock Wait Queue: SRO |
| 3097 | Lock Granted: SW, SW |
| 3098 | */ |
| 3099 | MDL_thread mdl_thread3( |
| 3100 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD3_SW], |
| 3101 | &thd_release_locks[THD3_SW], &thd_lock_blocked[THD3_SW], |
| 3102 | &thd_lock_released[THD3_SW]); |
| 3103 | mdl_thread3.start(); |
| 3104 | thd_lock_grabbed[THD3_SW].wait_for_notification(); |
| 3105 | |
| 3106 | /* |
| 3107 | THREAD4: Requesting SW lock on table. |
| 3108 | Blocks because m_piglet_lock_count == max_write_lock_count. |
| 3109 | |
| 3110 | Lock Wait Queue: SRO <-- SW |
| 3111 | Lock Granted: SW, SW |
| 3112 | m_piglet_lock_count == 1 |
| 3113 | */ |
| 3114 | MDL_thread mdl_thread4( |
| 3115 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD4_SW], |
| 3116 | &thd_release_locks[THD4_SW], &thd_lock_blocked[THD4_SW], |
| 3117 | &thd_lock_released[THD4_SW]); |
| 3118 | mdl_thread4.start(); |
| 3119 | thd_lock_blocked[THD4_SW].wait_for_notification(); |
| 3120 | |
| 3121 | /* |
| 3122 | THREAD 5: Requests SNRW lock on the table. |
| 3123 | Lock Wait Queue: SRO <-- SW <-- SNRW |
| 3124 | Lock Granted: SW, SW |
| 3125 | m_piglet_lock_count == 1 |
| 3126 | */ |
| 3127 | MDL_thread mdl_thread5( |
| 3128 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD5_SNRW], |
| 3129 | &thd_release_locks[THD5_SNRW], &thd_lock_blocked[THD5_SNRW], |
| 3130 | &thd_lock_released[THD5_SNRW]); |
| 3131 | mdl_thread5.start(); |
| 3132 | thd_lock_blocked[THD5_SNRW].wait_for_notification(); |
| 3133 | |
| 3134 | /* |
| 3135 | THREAD 6: Requests SRO lock on the table. |
| 3136 | Blocked because m_hog_lock_count == 0. |
| 3137 | Lock Wait Queue: SRO <-- SW <-- SNRW <-- SRO |
| 3138 | Lock Granted: SW, SW |
| 3139 | m_piglet_lock_count == 1 |
| 3140 | */ |
| 3141 | MDL_thread mdl_thread6( |
| 3142 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD6_SRO], |
| 3143 | &thd_release_locks[THD6_SRO], &thd_lock_blocked[THD6_SRO], |
| 3144 | &thd_lock_released[THD6_SRO]); |
| 3145 | mdl_thread6.start(); |
| 3146 | thd_lock_blocked[THD6_SRO].wait_for_notification(); |
| 3147 | |
| 3148 | /* |
| 3149 | THREAD 7: Requests SNRW lock on the table. |
| 3150 | Lock Wait Queue: SRO <-- SW <-- SNRW <-- SRO <-- SNRW |
| 3151 | Lock Granted: SW, SW |
| 3152 | m_piglet_lock_count == 1 |
| 3153 | */ |
| 3154 | MDL_thread mdl_thread7( |
| 3155 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD7_SNRW], |
| 3156 | &thd_release_locks[THD7_SNRW], &thd_lock_blocked[THD7_SNRW], |
| 3157 | &thd_lock_released[THD7_SNRW]); |
| 3158 | mdl_thread7.start(); |
| 3159 | thd_lock_blocked[THD7_SNRW].wait_for_notification(); |
| 3160 | |
| 3161 | /* |
| 3162 | THREAD 8: Requests SW lock on the table. |
| 3163 | Blocked because of pending SRO and SNRW locks. |
| 3164 | Lock Wait Queue: SRO <-- SW <-- SNRW <-- SRO <-- SNRW <-- SW |
| 3165 | Lock Granted: SW, SW |
| 3166 | m_piglet_lock_count == 1 |
| 3167 | */ |
| 3168 | MDL_thread mdl_thread8( |
| 3169 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD8_SW], |
| 3170 | &thd_release_locks[THD8_SW], &thd_lock_blocked[THD8_SW], |
| 3171 | &thd_lock_released[THD8_SW]); |
| 3172 | mdl_thread8.start(); |
| 3173 | thd_lock_blocked[THD8_SW].wait_for_notification(); |
| 3174 | |
| 3175 | /* |
| 3176 | THREAD 1, 3: Release SW locks. The first SNRW lock is granted. |
| 3177 | |
| 3178 | Lock Wait Queue: SRO <-- SW <-- SRO <-- SNRW <-- SW |
| 3179 | Lock Granted: SNRW |
| 3180 | m_piglet_lock_count == 1 |
| 3181 | m_hog_lock_count == 1 |
| 3182 | */ |
| 3183 | thd_release_locks[THD1_SW].notify(); |
| 3184 | thd_lock_released[THD1_SW].wait_for_notification(); |
| 3185 | thd_release_locks[THD3_SW].notify(); |
| 3186 | thd_lock_released[THD3_SW].wait_for_notification(); |
| 3187 | |
| 3188 | /* Lock is granted to THREAD 5 */ |
| 3189 | thd_lock_grabbed[THD5_SNRW].wait_for_notification(); |
| 3190 | |
| 3191 | /* |
| 3192 | THREAD 5: Release SNRW lock. |
| 3193 | Since m_piglet_lock_count == 1 and m_hog_lock_count == 1 this |
| 3194 | ublocks SRO locks. |
| 3195 | */ |
| 3196 | thd_release_locks[THD5_SNRW].notify(); |
| 3197 | thd_lock_released[THD5_SNRW].wait_for_notification(); |
| 3198 | |
| 3199 | /* |
| 3200 | THREAD 2,6: Get SRO locks. |
| 3201 | m_piglet_lock_count and m_hog_lock_count are reset to 0. |
| 3202 | |
| 3203 | Lock Wait Queue: SW <-- SNRW <-- SW |
| 3204 | Lock Granted: SRO, SRO |
| 3205 | m_piglet_lock_count == 0 |
| 3206 | m_hog_lock_count == 0 |
| 3207 | */ |
| 3208 | thd_lock_grabbed[THD2_SRO].wait_for_notification(); |
| 3209 | thd_lock_grabbed[THD6_SRO].wait_for_notification(); |
| 3210 | |
| 3211 | /* |
| 3212 | THREAD 9: Requests SRO lock on the table. |
| 3213 | Blocked because m_piglet_lock_count == 0 and m_hog_lock_count == 0. |
| 3214 | Lock Wait Queue: SW <-- SNRW <-- SW <-- SRO |
| 3215 | Lock Granted: SRO, SRO |
| 3216 | m_piglet_lock_count == 0 |
| 3217 | m_hog_lock_count == 0 |
| 3218 | */ |
| 3219 | MDL_thread mdl_thread9( |
| 3220 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD9_SRO], |
| 3221 | &thd_release_locks[THD9_SRO], &thd_lock_blocked[THD9_SRO], |
| 3222 | &thd_lock_released[THD9_SRO]); |
| 3223 | mdl_thread9.start(); |
| 3224 | thd_lock_blocked[THD9_SRO].wait_for_notification(); |
| 3225 | |
| 3226 | /* |
| 3227 | THREAD 2,6: Release SRO locks. |
| 3228 | Since m_piglet_lock_count == 0 and m_hog_lock_count == 0 this |
| 3229 | unblocks SNRW lock request, |
| 3230 | */ |
| 3231 | thd_release_locks[THD2_SRO].notify(); |
| 3232 | thd_lock_released[THD2_SRO].wait_for_notification(); |
| 3233 | thd_release_locks[THD6_SRO].notify(); |
| 3234 | thd_lock_released[THD6_SRO].wait_for_notification(); |
| 3235 | |
| 3236 | /* |
| 3237 | THREAD 7: Gets SNRW lock. |
| 3238 | m_hog_lock_count is set to 1 since there is pending SW and SRO locks. |
| 3239 | |
| 3240 | Lock Wait Queue: SW <-- SW <-- SRO |
| 3241 | Lock Granted: SNW |
| 3242 | m_piglet_lock_count == 0 |
| 3243 | m_hog_lock_count == 1 |
| 3244 | */ |
| 3245 | thd_lock_grabbed[THD7_SNRW].wait_for_notification(); |
| 3246 | |
| 3247 | /* THREAD 7: Releases SNRW lock. This will unblock one of SW locks. */ |
| 3248 | thd_release_locks[THD7_SNRW].notify(); |
| 3249 | thd_lock_released[THD7_SNRW].wait_for_notification(); |
| 3250 | |
| 3251 | /* |
| 3252 | THREAD 4: Gets SW locks. |
| 3253 | m_piglet_lock_count is set to 1 since there is pending SRO lock. |
| 3254 | |
| 3255 | Lock Wait Queue: SW <-- SRO |
| 3256 | Lock Granted: SW |
| 3257 | m_piglet_lock_count == 1 |
| 3258 | m_hog_lock_count == 0 |
| 3259 | */ |
| 3260 | thd_lock_grabbed[THD4_SW].wait_for_notification(); |
| 3261 | |
| 3262 | /* |
| 3263 | THREAD 4: Release SW locks. |
| 3264 | Since m_piglet_lock_count == 1 this unblocks SRO lock request, |
| 3265 | */ |
| 3266 | thd_release_locks[THD4_SW].notify(); |
| 3267 | thd_lock_released[THD4_SW].wait_for_notification(); |
| 3268 | |
| 3269 | /* |
| 3270 | THREAD 9: Gets SRO lock. |
| 3271 | m_piglet_lock_count is reset to 0. |
| 3272 | |
| 3273 | Lock Wait Queue: SW |
| 3274 | Lock Granted: SRO |
| 3275 | m_piglet_lock_count == 0 |
| 3276 | m_hog_lock_count == 0 |
| 3277 | */ |
| 3278 | thd_lock_grabbed[THD9_SRO].wait_for_notification(); |
| 3279 | |
| 3280 | /* |
| 3281 | THREAD 9: Release SRO lock. |
| 3282 | Since m_hog_lock_count == 1 this unblocks SW lock request. |
| 3283 | */ |
| 3284 | thd_release_locks[THD9_SRO].notify(); |
| 3285 | thd_lock_released[THD9_SRO].wait_for_notification(); |
| 3286 | |
| 3287 | /* THREAD 8: Gets SW lock. */ |
| 3288 | thd_lock_grabbed[THD8_SW].wait_for_notification(); |
| 3289 | |
| 3290 | /* Cleanup */ |
| 3291 | thd_release_locks[THD8_SW].notify(); |
| 3292 | thd_lock_released[THD8_SW].wait_for_notification(); |
| 3293 | |
| 3294 | mdl_thread1.join(); |
| 3295 | mdl_thread2.join(); |
| 3296 | mdl_thread3.join(); |
| 3297 | mdl_thread4.join(); |
| 3298 | mdl_thread5.join(); |
| 3299 | mdl_thread6.join(); |
| 3300 | mdl_thread7.join(); |
| 3301 | mdl_thread8.join(); |
| 3302 | mdl_thread9.join(); |
| 3303 | } |
| 3304 | |
| 3305 | /* |
| 3306 | Another test for interaction of "piglet" and "hog" lock requests. |
| 3307 | |
| 3308 | Check situation when we first reach limit on successive grants |
| 3309 | of "hog" and then "piglet" locks. Again once both these limits |
| 3310 | are reached we give a way to SRO locks and reset both counters |
| 3311 | even though there are still pending SW locks in order to avoid |
| 3312 | starvation of "hog" requests. |
| 3313 | |
| 3314 | - max_write_lock_count= 1 |
| 3315 | - THREAD 1: Acquires SW lock on the table. |
| 3316 | - THREAD 2: Requests for SNRW lock on the table, blocked. |
| 3317 | - THREAD 3: Requests for SW on the table, blocked. |
| 3318 | - THREAD 1: Releases SW lock. |
| 3319 | - THREAD 2: Gets SNRW lock (m_hog_lock_count= 1). |
| 3320 | - THREAD 4: Requests for SNRW lock on the table, blocked. |
| 3321 | - THREAD 5: Requests for SRO lock on the table, blocked. |
| 3322 | - THREAD 2: Releases SNRW lock. |
| 3323 | - THREAD 3: Gets SW lock (m_piglet_lock_count= 1). |
| 3324 | - THREAD 6: Requests for SW lock on the table, blocked. |
| 3325 | - THREAD 3: Releases SW lock. |
| 3326 | - THREAD 5: Gets SRO lock (m_piglet_lock_count=0, m_hog_lock_count= 0). |
| 3327 | - THREAD 7: Requests SR lock on the table, blocked. |
| 3328 | - THREAD 5: Releases SRO lock. |
| 3329 | - THREAD 4: Gets SNRW lock (m_hog_lock_count= 1) |
| 3330 | - THREAD 4: Releases SNRW lock. |
| 3331 | - THREAD 6,7: Get SW and SR locks. |
| 3332 | */ |
| 3333 | |
| 3334 | TEST_F(MDLTest, HogThenPigletLockTest) { |
| 3335 | Notification thd_lock_grabbed[7]; |
| 3336 | Notification thd_release_locks[7]; |
| 3337 | Notification thd_lock_blocked[7]; |
| 3338 | Notification thd_lock_released[7]; |
| 3339 | |
| 3340 | /* Locks taken by the threads */ |
| 3341 | enum { THD1_SW, THD2_SNRW, THD3_SW, THD4_SNRW, THD5_SRO, THD6_SW, THD7_SR }; |
| 3342 | |
| 3343 | max_write_lock_count = 1; |
| 3344 | |
| 3345 | /* |
| 3346 | THREAD1: Acquires SW lock on table. |
| 3347 | |
| 3348 | Lock Wait Queue: <empty> |
| 3349 | Lock Granted: SW |
| 3350 | */ |
| 3351 | MDL_thread mdl_thread1( |
| 3352 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD1_SW], |
| 3353 | &thd_release_locks[THD1_SW], &thd_lock_blocked[THD1_SW], |
| 3354 | &thd_lock_released[THD1_SW]); |
| 3355 | mdl_thread1.start(); |
| 3356 | thd_lock_grabbed[THD1_SW].wait_for_notification(); |
| 3357 | |
| 3358 | /* |
| 3359 | THREAD2: Requesting SNRW lock on table. |
| 3360 | |
| 3361 | Lock Wait Queue: SNRW |
| 3362 | Lock Granted: SW |
| 3363 | */ |
| 3364 | MDL_thread mdl_thread2( |
| 3365 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD2_SNRW], |
| 3366 | &thd_release_locks[THD2_SNRW], &thd_lock_blocked[THD2_SNRW], |
| 3367 | &thd_lock_released[THD2_SNRW]); |
| 3368 | mdl_thread2.start(); |
| 3369 | thd_lock_blocked[THD2_SNRW].wait_for_notification(); |
| 3370 | |
| 3371 | /* |
| 3372 | THREAD3: Requesting SW lock on table. |
| 3373 | |
| 3374 | Lock Wait Queue: SNRW <-- SW |
| 3375 | Lock Granted: SW |
| 3376 | */ |
| 3377 | MDL_thread mdl_thread3( |
| 3378 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD3_SW], |
| 3379 | &thd_release_locks[THD3_SW], &thd_lock_blocked[THD3_SW], |
| 3380 | &thd_lock_released[THD3_SW]); |
| 3381 | mdl_thread3.start(); |
| 3382 | thd_lock_blocked[THD3_SW].wait_for_notification(); |
| 3383 | |
| 3384 | /* |
| 3385 | THREAD 1: Release SW lock. The SNRW lock is granted. |
| 3386 | m_hog_lock_count is set to 1. |
| 3387 | |
| 3388 | Lock Wait Queue: SW |
| 3389 | Lock Granted: SNRW |
| 3390 | m_piglet_lock_count == 0 |
| 3391 | m_hog_lock_count == 1 |
| 3392 | */ |
| 3393 | thd_release_locks[THD1_SW].notify(); |
| 3394 | thd_lock_released[THD1_SW].wait_for_notification(); |
| 3395 | |
| 3396 | /* THREAD 2: Gets SNRW. */ |
| 3397 | thd_lock_grabbed[THD2_SNRW].wait_for_notification(); |
| 3398 | |
| 3399 | /* |
| 3400 | THREAD4: Requesting SNRW lock on table. |
| 3401 | Blocks because m_hog_lock_count == 1. |
| 3402 | |
| 3403 | Lock Wait Queue: SW <-- SNRW |
| 3404 | Lock Granted: SNRW |
| 3405 | m_piglet_lock_count == 0 |
| 3406 | m_hog_lock_count == 1 |
| 3407 | */ |
| 3408 | MDL_thread mdl_thread4( |
| 3409 | table_name1, MDL_SHARED_NO_READ_WRITE, &thd_lock_grabbed[THD4_SNRW], |
| 3410 | &thd_release_locks[THD4_SNRW], &thd_lock_blocked[THD4_SNRW], |
| 3411 | &thd_lock_released[THD4_SNRW]); |
| 3412 | mdl_thread4.start(); |
| 3413 | thd_lock_blocked[THD4_SNRW].wait_for_notification(); |
| 3414 | |
| 3415 | /* |
| 3416 | THREAD 5: Requests SRO lock on the table. |
| 3417 | |
| 3418 | Lock Wait Queue: SW <-- SNRW <-- SRO |
| 3419 | Lock Granted: SNRW |
| 3420 | m_piglet_lock_count == 0 |
| 3421 | m_hog_lock_count == 1 |
| 3422 | */ |
| 3423 | MDL_thread mdl_thread5( |
| 3424 | table_name1, MDL_SHARED_READ_ONLY, &thd_lock_grabbed[THD5_SRO], |
| 3425 | &thd_release_locks[THD5_SRO], &thd_lock_blocked[THD5_SRO], |
| 3426 | &thd_lock_released[THD5_SRO]); |
| 3427 | mdl_thread5.start(); |
| 3428 | thd_lock_blocked[THD5_SRO].wait_for_notification(); |
| 3429 | |
| 3430 | /* |
| 3431 | THREAD 2: Releases SNRW lock. This unblocks SW lock |
| 3432 | since m_hog_lock_count == 1. |
| 3433 | */ |
| 3434 | thd_release_locks[THD2_SNRW].notify(); |
| 3435 | thd_lock_released[THD2_SNRW].wait_for_notification(); |
| 3436 | |
| 3437 | /* |
| 3438 | THREAD 3: Gets SW lock on the table. |
| 3439 | m_piglet_lock_count is set to 1. |
| 3440 | |
| 3441 | Lock Wait Queue: SNRW <-- SRO |
| 3442 | Lock Granted: SW |
| 3443 | m_piglet_lock_count == 1 |
| 3444 | m_hog_lock_count == 1 |
| 3445 | */ |
| 3446 | thd_lock_grabbed[THD3_SW].wait_for_notification(); |
| 3447 | |
| 3448 | /* |
| 3449 | THREAD 6: Requests SW lock on the table. |
| 3450 | Blocked because m_piglet_lock_count == 1. |
| 3451 | |
| 3452 | Lock Wait Queue: SNRW <-- SRO <-- SW |
| 3453 | Lock Granted: SW |
| 3454 | m_piglet_lock_count == 1 |
| 3455 | m_hog_lock_count == 1 |
| 3456 | */ |
| 3457 | MDL_thread mdl_thread6( |
| 3458 | table_name1, MDL_SHARED_WRITE, &thd_lock_grabbed[THD6_SW], |
| 3459 | &thd_release_locks[THD6_SW], &thd_lock_blocked[THD6_SW], |
| 3460 | &thd_lock_released[THD6_SW]); |
| 3461 | mdl_thread6.start(); |
| 3462 | thd_lock_blocked[THD6_SW].wait_for_notification(); |
| 3463 | |
| 3464 | /* |
| 3465 | THREAD 3: Releases SW lock. SRO lock is granted. |
| 3466 | Both m_piglet_lock_count and m_hog_lock_count are reset to 0. |
| 3467 | |
| 3468 | Lock Wait Queue: SNRW <-- SW |
| 3469 | Lock Granted: SRO |
| 3470 | m_piglet_lock_count == 0 |
| 3471 | m_hog_lock_count == 0 |
| 3472 | */ |
| 3473 | thd_release_locks[THD3_SW].notify(); |
| 3474 | thd_lock_released[THD3_SW].wait_for_notification(); |
| 3475 | |
| 3476 | /* THREAD 5: Gets SRO lock. */ |
| 3477 | thd_lock_grabbed[THD5_SRO].wait_for_notification(); |
| 3478 | |
| 3479 | /* |
| 3480 | THREAD 7: Requests SR lock on the table. Blocked. |
| 3481 | |
| 3482 | Lock Wait Queue: SNRW <-- SW <-- SR |
| 3483 | Lock Granted: SRO |
| 3484 | m_piglet_lock_count == 0 |
| 3485 | m_hog_lock_count == 0 |
| 3486 | */ |
| 3487 | MDL_thread mdl_thread7( |
| 3488 | table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD7_SR], |
| 3489 | &thd_release_locks[THD7_SR], &thd_lock_blocked[THD7_SR], |
| 3490 | &thd_lock_released[THD7_SR]); |
| 3491 | mdl_thread7.start(); |
| 3492 | thd_lock_blocked[THD7_SR].wait_for_notification(); |
| 3493 | |
| 3494 | /* THREAD 5: Releases SRO lock. */ |
| 3495 | thd_release_locks[THD5_SRO].notify(); |
| 3496 | thd_lock_released[THD5_SRO].wait_for_notification(); |
| 3497 | |
| 3498 | /* |
| 3499 | THREAD 4: Gets SNRW lock. m_hog_lock_count is set to 1. |
| 3500 | |
| 3501 | Lock Wait Queue: SW <-- SR |
| 3502 | Lock Granted: SNRW |
| 3503 | m_piglet_lock_count == 0 |
| 3504 | m_hog_lock_count == 1 |
| 3505 | */ |
| 3506 | thd_lock_grabbed[THD4_SNRW].wait_for_notification(); |
| 3507 | |
| 3508 | /* THREAD 4: Releases SNRW lock. */ |
| 3509 | thd_release_locks[THD4_SNRW].notify(); |
| 3510 | thd_lock_released[THD4_SNRW].wait_for_notification(); |
| 3511 | |
| 3512 | /* THREAD 6, 8: Get SW and SR locks. */ |
| 3513 | thd_lock_grabbed[THD6_SW].wait_for_notification(); |
| 3514 | thd_lock_grabbed[THD7_SR].wait_for_notification(); |
| 3515 | |
| 3516 | /* Cleanup */ |
| 3517 | thd_release_locks[THD6_SW].notify(); |
| 3518 | thd_lock_released[THD6_SW].wait_for_notification(); |
| 3519 | thd_release_locks[THD7_SR].notify(); |
| 3520 | thd_lock_released[THD7_SR].wait_for_notification(); |
| 3521 | |
| 3522 | mdl_thread1.join(); |
| 3523 | mdl_thread2.join(); |
| 3524 | mdl_thread3.join(); |
| 3525 | mdl_thread4.join(); |
| 3526 | mdl_thread5.join(); |
| 3527 | mdl_thread6.join(); |
| 3528 | mdl_thread7.join(); |
| 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | Auxiliary thread class which simulates connection which does |
| 3533 | LOCK TABLES t1 WRITE, t1 READ/UNLOCK TABLES in a loop. |
| 3534 | */ |
| 3535 | |
| 3536 | class MDL_SRO_SNRW_thread : public Thread, public Test_MDL_context_owner { |
| 3537 | public: |
| 3538 | MDL_SRO_SNRW_thread() { m_mdl_context.init(this); } |
| 3539 | |
| 3540 | ~MDL_SRO_SNRW_thread() override { m_mdl_context.destroy(); } |
| 3541 | |
| 3542 | void run() override; |
| 3543 | |
| 3544 | void notify_shared_lock(MDL_context_owner *, bool) override {} |
| 3545 | |
| 3546 | private: |
| 3547 | MDL_context m_mdl_context; |
| 3548 | }; |
| 3549 | |
| 3550 | void MDL_SRO_SNRW_thread::run() { |
| 3551 | for (int i = 0; i < 100; ++i) { |
| 3552 | MDL_request request1, request2; |
| 3553 | MDL_request_list request_list; |
| 3554 | |
| 3555 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 3556 | MDL_SHARED_NO_READ_WRITE, MDL_TRANSACTION); |
| 3557 | MDL_REQUEST_INIT(&request2, MDL_key::TABLE, db_name, table_name1, |
| 3558 | MDL_SHARED_READ_ONLY, MDL_TRANSACTION); |
| 3559 | /* |
| 3560 | Simulate LOCK TABLES t1 WRITE, t1 READ, by putting SRO lock |
| 3561 | to the front of the list and SNRW to its back. |
| 3562 | */ |
| 3563 | request_list.push_front(&request1); |
| 3564 | request_list.push_front(&request2); |
| 3565 | |
| 3566 | /* |
| 3567 | Lock acquisition should succeed and never deadlock. I.e. we should |
| 3568 | always acquire SNRW first and only then SRO. |
| 3569 | */ |
| 3570 | EXPECT_FALSE(m_mdl_context.acquire_locks(&request_list, long_timeout)); |
| 3571 | m_mdl_context.release_transactional_locks(); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | /* |
| 3576 | Check that MDL_context::acquire_locks() takes type of lock requested |
| 3577 | into account when it sorts requests in order to avoid/reduce number |
| 3578 | of deadlocks. |
| 3579 | Also provides additional coverage for case when we concurrently create |
| 3580 | and destroy MDL_lock instances for the same object. |
| 3581 | */ |
| 3582 | |
| 3583 | TEST_F(MDLTest, AcquireLocksTypeOrder) { |
| 3584 | MDL_SRO_SNRW_thread thread1, thread2; |
| 3585 | |
| 3586 | /* Force immediate destruction of unused MDL_lock object. */ |
| 3587 | mdl_locks_unused_locks_low_water = 0; |
| 3588 | |
| 3589 | thread1.start(); |
| 3590 | thread2.start(); |
| 3591 | thread1.join(); |
| 3592 | thread2.join(); |
| 3593 | } |
| 3594 | |
| 3595 | /** |
| 3596 | Thread class for testing MDL_context::set_force_dml_deadlock_weight() |
| 3597 | method. |
| 3598 | */ |
| 3599 | |
| 3600 | class MDL_weight_thread : public Thread, public Test_MDL_context_owner { |
| 3601 | public: |
| 3602 | MDL_weight_thread(Notification *first_grabbed, Notification *go_for_second, |
| 3603 | Notification *lock_blocked) |
| 3604 | : m_first_grabbed(first_grabbed), |
| 3605 | m_go_for_second(go_for_second), |
| 3606 | m_lock_blocked(lock_blocked) { |
| 3607 | m_mdl_context.init(this); |
| 3608 | } |
| 3609 | |
| 3610 | ~MDL_weight_thread() override { m_mdl_context.destroy(); } |
| 3611 | |
| 3612 | void run() override; |
| 3613 | |
| 3614 | void notify_shared_lock(MDL_context_owner *, bool) override {} |
| 3615 | |
| 3616 | void enter_cond(mysql_cond_t *cond, mysql_mutex_t *mutex, |
| 3617 | const PSI_stage_info *stage, PSI_stage_info *old_stage, |
| 3618 | const char *src_function, const char *src_file, |
| 3619 | int src_line) override { |
| 3620 | Test_MDL_context_owner::enter_cond(cond, mutex, stage, old_stage, |
| 3621 | src_function, src_file, src_line); |
| 3622 | |
| 3623 | m_lock_blocked->notify(); |
| 3624 | return; |
| 3625 | } |
| 3626 | |
| 3627 | private: |
| 3628 | MDL_context m_mdl_context; |
| 3629 | Notification *m_first_grabbed; |
| 3630 | Notification *m_go_for_second; |
| 3631 | Notification *m_lock_blocked; |
| 3632 | }; |
| 3633 | |
| 3634 | void MDL_weight_thread::run() { |
| 3635 | MDL_request request1, request2; |
| 3636 | |
| 3637 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 3638 | MDL_SHARED_NO_READ_WRITE, MDL_TRANSACTION); |
| 3639 | |
| 3640 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 3641 | |
| 3642 | m_first_grabbed->notify(); |
| 3643 | m_go_for_second->wait_for_notification(); |
| 3644 | |
| 3645 | MDL_REQUEST_INIT(&request2, MDL_key::TABLE, db_name, table_name2, |
| 3646 | MDL_SHARED_NO_READ_WRITE, MDL_TRANSACTION); |
| 3647 | |
| 3648 | /* Mark current thread as preferred deadlock victim. */ |
| 3649 | m_mdl_context.set_force_dml_deadlock_weight(true); |
| 3650 | |
| 3651 | /* |
| 3652 | Wait for the second table should end-up in a deadlock with |
| 3653 | thread being chosen as victim. |
| 3654 | */ |
| 3655 | expected_error = ER_LOCK_DEADLOCK; |
| 3656 | EXPECT_TRUE(m_mdl_context.acquire_lock(&request2, long_timeout)); |
| 3657 | |
| 3658 | m_mdl_context.release_transactional_locks(); |
| 3659 | } |
| 3660 | |
| 3661 | /** |
| 3662 | Test coverage for MDL_context::set_force_dml_deadlock_weight() method. |
| 3663 | */ |
| 3664 | |
| 3665 | TEST_F(MDLTest, ForceDMLDeadlockWeight) { |
| 3666 | Notification first_grabbed, go_for_second, second_blocked; |
| 3667 | MDL_weight_thread thread(&first_grabbed, &go_for_second, &second_blocked); |
| 3668 | |
| 3669 | /* |
| 3670 | Start the concurrent thread and wait until it acquires SNRW lock on |
| 3671 | table_name1 and gets suspended. |
| 3672 | */ |
| 3673 | thread.start(); |
| 3674 | first_grabbed.wait_for_notification(); |
| 3675 | |
| 3676 | /* Now let us grab SNRW lock table_name2. */ |
| 3677 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name2, |
| 3678 | MDL_SHARED_NO_READ_WRITE, MDL_TRANSACTION); |
| 3679 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 3680 | |
| 3681 | /* |
| 3682 | Resume the concurrent thread. It should try to acquire SNRW lock on |
| 3683 | table_name2. Wait for it to get blocked. |
| 3684 | */ |
| 3685 | go_for_second.notify(); |
| 3686 | second_blocked.wait_for_notification(); |
| 3687 | |
| 3688 | /* |
| 3689 | Now let us try to grab SNRW lock table_name1. |
| 3690 | This should lead to deadlock. |
| 3691 | |
| 3692 | Normally this context will be chosen as a victim since all waits |
| 3693 | happen for the same type request - SNRW and it has joined waiters |
| 3694 | graph last. |
| 3695 | |
| 3696 | But since another thread uses MDL_context::set_force_dml_deadlock_weight() |
| 3697 | method it will be chosen as a victim instead. |
| 3698 | */ |
| 3699 | MDL_REQUEST_INIT(&m_request, MDL_key::TABLE, db_name, table_name1, |
| 3700 | MDL_SHARED_NO_READ_WRITE, MDL_TRANSACTION); |
| 3701 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 3702 | |
| 3703 | m_mdl_context.release_transactional_locks(); |
| 3704 | |
| 3705 | thread.join(); |
| 3706 | } |
| 3707 | |
| 3708 | /** Simple MDL_context_visitor which stores pointer to context visited. */ |
| 3709 | |
| 3710 | class MDLTestContextVisitor : public MDL_context_visitor { |
| 3711 | public: |
| 3712 | MDLTestContextVisitor() : m_visited_ctx(nullptr) {} |
| 3713 | void visit_context(const MDL_context *ctx) override { m_visited_ctx = ctx; } |
| 3714 | const MDL_context *get_visited_ctx() { return m_visited_ctx; } |
| 3715 | |
| 3716 | private: |
| 3717 | const MDL_context *m_visited_ctx; |
| 3718 | }; |
| 3719 | |
| 3720 | /** |
| 3721 | Test coverage for MDL_context::find_lock_owner() method. |
| 3722 | */ |
| 3723 | |
| 3724 | TEST_F(MDLTest, FindLockOwner) { |
| 3725 | Notification first_grabbed, first_release; |
| 3726 | Notification second_blocked, second_grabbed, second_release; |
| 3727 | MDL_thread thread1(table_name1, MDL_EXCLUSIVE, &first_grabbed, &first_release, |
| 3728 | nullptr, nullptr); |
| 3729 | MDL_thread thread2(table_name1, MDL_EXCLUSIVE, &second_grabbed, |
| 3730 | &second_release, &second_blocked, nullptr); |
| 3731 | MDL_key mdl_key(MDL_key::TABLE, db_name, table_name1); |
| 3732 | |
| 3733 | /* There should be no lock owner before we have started any threads. */ |
| 3734 | MDLTestContextVisitor visitor1; |
| 3735 | EXPECT_FALSE(m_mdl_context.find_lock_owner(&mdl_key, &visitor1)); |
| 3736 | const MDL_context *null_context = nullptr; |
| 3737 | EXPECT_EQ(null_context, visitor1.get_visited_ctx()); |
| 3738 | |
| 3739 | /* |
| 3740 | Start the first thread and wait until it grabs the lock. |
| 3741 | This thread should be found as lock owner. |
| 3742 | */ |
| 3743 | thread1.start(); |
| 3744 | first_grabbed.wait_for_notification(); |
| 3745 | EXPECT_FALSE(m_mdl_context.find_lock_owner(&mdl_key, &visitor1)); |
| 3746 | EXPECT_EQ(&thread1.get_mdl_context(), visitor1.get_visited_ctx()); |
| 3747 | |
| 3748 | /* |
| 3749 | Start the second thread and wait unti it is blocked waiting for the lock. |
| 3750 | The first thread still should be reported as lock owner. |
| 3751 | */ |
| 3752 | MDLTestContextVisitor visitor2; |
| 3753 | thread2.start(); |
| 3754 | second_blocked.wait_for_notification(); |
| 3755 | EXPECT_FALSE(m_mdl_context.find_lock_owner(&mdl_key, &visitor2)); |
| 3756 | EXPECT_EQ(&thread1.get_mdl_context(), visitor2.get_visited_ctx()); |
| 3757 | |
| 3758 | /* |
| 3759 | Let the first thread to release lock and wait until the second thread |
| 3760 | acquires it. The second thread should be reported as lock owner now. |
| 3761 | */ |
| 3762 | MDLTestContextVisitor visitor3; |
| 3763 | first_release.notify(); |
| 3764 | second_grabbed.wait_for_notification(); |
| 3765 | EXPECT_FALSE(m_mdl_context.find_lock_owner(&mdl_key, &visitor3)); |
| 3766 | EXPECT_EQ(&thread2.get_mdl_context(), visitor3.get_visited_ctx()); |
| 3767 | |
| 3768 | /* Release the lock. Wait until both threads have stopped. */ |
| 3769 | second_release.notify(); |
| 3770 | thread1.join(); |
| 3771 | thread2.join(); |
| 3772 | |
| 3773 | /* There should be no lock owners again. */ |
| 3774 | MDLTestContextVisitor visitor4; |
| 3775 | EXPECT_FALSE(m_mdl_context.find_lock_owner(&mdl_key, &visitor4)); |
| 3776 | EXPECT_EQ(null_context, visitor4.get_visited_ctx()); |
| 3777 | } |
| 3778 | |
| 3779 | /** |
| 3780 | Verify that correct error is reported when the MDL system exhausts the LF |
| 3781 | Pinbox. |
| 3782 | */ |
| 3783 | TEST_F(MDLTest, ExhaustPinbox) { |
| 3784 | for (int i = 0; i < 65535; ++i) { |
| 3785 | MDL_context c; |
| 3786 | EXPECT_FALSE(test_drive_fix_pins(&c)); |
| 3787 | } |
| 3788 | MDL_context bad; |
| 3789 | expected_error = ER_MDL_OUT_OF_RESOURCES; |
| 3790 | EXPECT_TRUE(test_drive_fix_pins(&bad)); |
| 3791 | } |
| 3792 | |
| 3793 | /** Test class for SE notification testing. */ |
| 3794 | |
| 3795 | class MDLHtonNotifyTest : public MDLTest { |
| 3796 | protected: |
| 3797 | MDLHtonNotifyTest() = default; |
| 3798 | |
| 3799 | void SetUp() override { |
| 3800 | MDLTest::SetUp(); |
| 3801 | reset_counts_and_keys(); |
| 3802 | } |
| 3803 | |
| 3804 | void TearDown() override { MDLTest::TearDown(); } |
| 3805 | |
| 3806 | bool notify_hton_pre_acquire_exclusive(const MDL_key *mdl_key, |
| 3807 | bool *victimized) override { |
| 3808 | *victimized = false; |
| 3809 | m_pre_acquire_count++; |
| 3810 | m_pre_acquire_key.mdl_key_init(mdl_key); |
| 3811 | return m_refuse_acquire; |
| 3812 | } |
| 3813 | void notify_hton_post_release_exclusive(const MDL_key *mdl_key) override { |
| 3814 | m_post_release_key.mdl_key_init(mdl_key); |
| 3815 | m_post_release_count++; |
| 3816 | } |
| 3817 | |
| 3818 | uint pre_acquire_count() const { return m_pre_acquire_count; } |
| 3819 | |
| 3820 | uint post_release_count() const { return m_post_release_count; } |
| 3821 | |
| 3822 | const MDL_key &pre_acquire_key() const { return m_pre_acquire_key; } |
| 3823 | |
| 3824 | const MDL_key &post_release_key() const { return m_post_release_key; } |
| 3825 | |
| 3826 | void reset_counts_and_keys() { |
| 3827 | m_pre_acquire_count = 0; |
| 3828 | m_post_release_count = 0; |
| 3829 | m_pre_acquire_key.reset(); |
| 3830 | m_post_release_key.reset(); |
| 3831 | m_refuse_acquire = false; |
| 3832 | } |
| 3833 | |
| 3834 | void set_refuse_acquire() { m_refuse_acquire = true; } |
| 3835 | |
| 3836 | private: |
| 3837 | MDLHtonNotifyTest(MDLHtonNotifyTest const &) = delete; |
| 3838 | MDLHtonNotifyTest &operator=(MDLHtonNotifyTest const &) = delete; |
| 3839 | |
| 3840 | uint m_pre_acquire_count, m_post_release_count; |
| 3841 | MDL_key m_pre_acquire_key, m_post_release_key; |
| 3842 | bool m_refuse_acquire; |
| 3843 | }; |
| 3844 | |
| 3845 | /** |
| 3846 | Test that SE notification is performed only for prescribed namespaces and |
| 3847 | not others. |
| 3848 | */ |
| 3849 | |
| 3850 | TEST_F(MDLHtonNotifyTest, NotifyNamespaces) { |
| 3851 | bool notify_or_not[] = { |
| 3852 | false, // GLOBAL |
| 3853 | false, // BACKUP_LOCK |
| 3854 | true, // TABLESPACE |
| 3855 | true, // SCHEMA |
| 3856 | true, // TABLE |
| 3857 | true, // FUNCTION |
| 3858 | true, // PROCEDURE |
| 3859 | true, // TRIGGER |
| 3860 | true, // EVENT |
| 3861 | false, // COMMIT |
| 3862 | false, // USER_LEVEL_LOCK |
| 3863 | false, // LOCKING_SERVICE |
| 3864 | false, // SRID |
| 3865 | false, // ACL_CACHE |
| 3866 | false, // COLUMN_STATISTICS |
| 3867 | false, // RESOURCE_GROUPS |
| 3868 | false, // FOREIGN_KEY |
| 3869 | false // CHECK_CONSTRAINT |
| 3870 | }; |
| 3871 | static_assert( |
| 3872 | sizeof(notify_or_not) == MDL_key::NAMESPACE_END, |
| 3873 | "Initializer list for notify_or_not[] has the wrong number of elements!"); |
| 3874 | |
| 3875 | for (uint i = 0; i < static_cast<uint>(MDL_key::NAMESPACE_END); i++) { |
| 3876 | MDL_request request; |
| 3877 | if (static_cast<MDL_key::enum_mdl_namespace>(i) == MDL_key::FUNCTION || |
| 3878 | static_cast<MDL_key::enum_mdl_namespace>(i) == MDL_key::PROCEDURE || |
| 3879 | static_cast<MDL_key::enum_mdl_namespace>(i) == MDL_key::TRIGGER || |
| 3880 | static_cast<MDL_key::enum_mdl_namespace>(i) == MDL_key::EVENT || |
| 3881 | static_cast<MDL_key::enum_mdl_namespace>(i) == |
| 3882 | MDL_key::RESOURCE_GROUPS) { |
| 3883 | MDL_key mdl_key; |
| 3884 | mdl_key.mdl_key_init(static_cast<MDL_key::enum_mdl_namespace>(i), "", "", |
| 3885 | 0, ""); |
| 3886 | MDL_REQUEST_INIT_BY_KEY(&request, &mdl_key, MDL_EXCLUSIVE, |
| 3887 | MDL_TRANSACTION); |
| 3888 | } else { |
| 3889 | MDL_REQUEST_INIT(&request, static_cast<MDL_key::enum_mdl_namespace>(i), |
| 3890 | "", |
| 3891 | "", // To work with GLOBAL/COMMIT spaces |
| 3892 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 3893 | } |
| 3894 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 3895 | m_mdl_context.release_transactional_locks(); |
| 3896 | |
| 3897 | if (notify_or_not[i]) { |
| 3898 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3899 | EXPECT_EQ(1U, post_release_count()); |
| 3900 | reset_counts_and_keys(); |
| 3901 | } else { |
| 3902 | EXPECT_EQ(0U, pre_acquire_count()); |
| 3903 | EXPECT_EQ(0U, post_release_count()); |
| 3904 | } |
| 3905 | } |
| 3906 | } |
| 3907 | |
| 3908 | /** |
| 3909 | Test that SE notification is performed only for X requests and not others. |
| 3910 | */ |
| 3911 | |
| 3912 | TEST_F(MDLHtonNotifyTest, NotifyLockTypes) { |
| 3913 | MDL_request request; |
| 3914 | |
| 3915 | // IX type can only be used for scoped locks. Doesn't cause notification. |
| 3916 | MDL_REQUEST_INIT(&request, MDL_key::SCHEMA, db_name, "", |
| 3917 | MDL_INTENTION_EXCLUSIVE, MDL_TRANSACTION); |
| 3918 | |
| 3919 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 3920 | m_mdl_context.release_transactional_locks(); |
| 3921 | |
| 3922 | EXPECT_EQ(0U, pre_acquire_count()); |
| 3923 | EXPECT_EQ(0U, post_release_count()); |
| 3924 | |
| 3925 | /* |
| 3926 | All other lock types can be used with tables and don't cause |
| 3927 | notification except X requests. |
| 3928 | */ |
| 3929 | for (uint i = static_cast<uint>(MDL_INTENTION_EXCLUSIVE) + 1; |
| 3930 | i < static_cast<uint>(MDL_EXCLUSIVE); i++) { |
| 3931 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 3932 | static_cast<enum_mdl_type>(i), MDL_TRANSACTION); |
| 3933 | |
| 3934 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 3935 | m_mdl_context.release_transactional_locks(); |
| 3936 | |
| 3937 | EXPECT_EQ(0U, pre_acquire_count()); |
| 3938 | EXPECT_EQ(0U, post_release_count()); |
| 3939 | } |
| 3940 | |
| 3941 | // X lock causes notifications. |
| 3942 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 3943 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 3944 | |
| 3945 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 3946 | m_mdl_context.release_transactional_locks(); |
| 3947 | |
| 3948 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3949 | EXPECT_EQ(1U, post_release_count()); |
| 3950 | |
| 3951 | // There are no other lock types! |
| 3952 | assert(static_cast<uint>(MDL_EXCLUSIVE) + 1 == |
| 3953 | static_cast<uint>(MDL_TYPE_END)); |
| 3954 | } |
| 3955 | |
| 3956 | /** |
| 3957 | Test for SE notification in some acquire/release scenarios |
| 3958 | including case when SE refuses lock acquisition. |
| 3959 | */ |
| 3960 | |
| 3961 | TEST_F(MDLHtonNotifyTest, NotifyAcquireRelease) { |
| 3962 | MDL_request request1, request2; |
| 3963 | |
| 3964 | // Straightforward acquire/release cycle. |
| 3965 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 3966 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 3967 | |
| 3968 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 3969 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3970 | EXPECT_EQ(0U, post_release_count()); |
| 3971 | EXPECT_TRUE(pre_acquire_key().is_equal(&request1.key)); |
| 3972 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 3973 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 3974 | |
| 3975 | m_mdl_context.release_transactional_locks(); |
| 3976 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3977 | EXPECT_EQ(1U, post_release_count()); |
| 3978 | EXPECT_TRUE(post_release_key().is_equal(&request1.key)); |
| 3979 | |
| 3980 | reset_counts_and_keys(); |
| 3981 | |
| 3982 | // Case when we try to acquire lock several times. |
| 3983 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 3984 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 3985 | MDL_REQUEST_INIT(&request2, MDL_key::TABLE, db_name, table_name1, |
| 3986 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 3987 | |
| 3988 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 3989 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3990 | EXPECT_EQ(0U, post_release_count()); |
| 3991 | EXPECT_TRUE(pre_acquire_key().is_equal(&request1.key)); |
| 3992 | |
| 3993 | // The second acquisition should not cause notification. |
| 3994 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request2, long_timeout)); |
| 3995 | EXPECT_EQ(1U, pre_acquire_count()); |
| 3996 | EXPECT_EQ(0U, post_release_count()); |
| 3997 | |
| 3998 | // On release there is only one notification call. |
| 3999 | m_mdl_context.release_transactional_locks(); |
| 4000 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4001 | EXPECT_EQ(1U, post_release_count()); |
| 4002 | EXPECT_TRUE(post_release_key().is_equal(&request1.key)); |
| 4003 | |
| 4004 | reset_counts_and_keys(); |
| 4005 | |
| 4006 | // Case when lock acquisition is refused by SE. |
| 4007 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 4008 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 4009 | |
| 4010 | expected_error = ER_LOCK_REFUSED_BY_ENGINE; |
| 4011 | set_refuse_acquire(); |
| 4012 | |
| 4013 | EXPECT_TRUE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 4014 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4015 | EXPECT_EQ(0U, post_release_count()); |
| 4016 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 4017 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 4018 | |
| 4019 | // Nothing to release and nothing to notify about. |
| 4020 | m_mdl_context.release_transactional_locks(); |
| 4021 | EXPECT_EQ(0U, post_release_count()); |
| 4022 | } |
| 4023 | |
| 4024 | /** |
| 4025 | Test for SE notification in some scenarios when we successfully perform |
| 4026 | SE notification but then fail to acquire lock for some reason. |
| 4027 | */ |
| 4028 | |
| 4029 | TEST_F(MDLHtonNotifyTest, NotifyAcquireFail) { |
| 4030 | Notification lock_grabbed, release_lock; |
| 4031 | MDL_request request; |
| 4032 | |
| 4033 | // Acquire S lock on the table in another thread. |
| 4034 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_lock, |
| 4035 | nullptr, nullptr); |
| 4036 | mdl_thread.start(); |
| 4037 | lock_grabbed.wait_for_notification(); |
| 4038 | |
| 4039 | /* |
| 4040 | Try to acquire X lock on the table using try_acquire_lock(). |
| 4041 | */ |
| 4042 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 4043 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 4044 | EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request)); |
| 4045 | EXPECT_EQ(m_null_ticket, request.ticket); |
| 4046 | /* |
| 4047 | We treat failure to acquire X lock after successful pre-acquire |
| 4048 | notification in the same way as lock release. |
| 4049 | */ |
| 4050 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4051 | EXPECT_EQ(1U, post_release_count()); |
| 4052 | EXPECT_TRUE(pre_acquire_key().is_equal(&request.key)); |
| 4053 | EXPECT_TRUE(post_release_key().is_equal(&request.key)); |
| 4054 | |
| 4055 | reset_counts_and_keys(); |
| 4056 | |
| 4057 | /* |
| 4058 | Now do the same thing using acquire_lock() and zero timeout, |
| 4059 | */ |
| 4060 | expected_error = ER_LOCK_WAIT_TIMEOUT; |
| 4061 | |
| 4062 | EXPECT_TRUE(m_mdl_context.acquire_lock(&request, zero_timeout)); |
| 4063 | /* |
| 4064 | Again we treat failure to acquire X lock after successful pre-acquire |
| 4065 | notification in the same way as lock release. |
| 4066 | */ |
| 4067 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4068 | EXPECT_EQ(1U, post_release_count()); |
| 4069 | EXPECT_TRUE(pre_acquire_key().is_equal(&request.key)); |
| 4070 | EXPECT_TRUE(post_release_key().is_equal(&request.key)); |
| 4071 | |
| 4072 | release_lock.notify(); |
| 4073 | mdl_thread.join(); |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | Test for SE notification in lock upgrade scenarios. |
| 4078 | */ |
| 4079 | |
| 4080 | TEST_F(MDLHtonNotifyTest, NotifyUpgrade) { |
| 4081 | MDL_request request; |
| 4082 | |
| 4083 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 4084 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 4085 | |
| 4086 | // Check that we notify SE about upgrade. |
| 4087 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4088 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(request.ticket, MDL_EXCLUSIVE, |
| 4089 | long_timeout)); |
| 4090 | |
| 4091 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4092 | EXPECT_EQ(0U, post_release_count()); |
| 4093 | EXPECT_TRUE(pre_acquire_key().is_equal(&request.key)); |
| 4094 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 4095 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 4096 | |
| 4097 | // Second attempt to upgrade should not cause additional notification. |
| 4098 | EXPECT_FALSE(m_mdl_context.upgrade_shared_lock(request.ticket, MDL_EXCLUSIVE, |
| 4099 | long_timeout)); |
| 4100 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4101 | EXPECT_EQ(0U, post_release_count()); |
| 4102 | |
| 4103 | m_mdl_context.release_transactional_locks(); |
| 4104 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4105 | EXPECT_EQ(1U, post_release_count()); |
| 4106 | EXPECT_TRUE(post_release_key().is_equal(&request.key)); |
| 4107 | |
| 4108 | reset_counts_and_keys(); |
| 4109 | |
| 4110 | // Case when lock upgrade is refused by SE. |
| 4111 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 4112 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 4113 | |
| 4114 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4115 | |
| 4116 | expected_error = ER_LOCK_REFUSED_BY_ENGINE; |
| 4117 | set_refuse_acquire(); |
| 4118 | |
| 4119 | EXPECT_TRUE(m_mdl_context.upgrade_shared_lock(request.ticket, MDL_EXCLUSIVE, |
| 4120 | long_timeout)); |
| 4121 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4122 | EXPECT_EQ(0U, post_release_count()); |
| 4123 | EXPECT_FALSE(m_mdl_context.owns_equal_or_stronger_lock( |
| 4124 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 4125 | |
| 4126 | // Nothing to notify about during release. |
| 4127 | m_mdl_context.release_transactional_locks(); |
| 4128 | EXPECT_EQ(0U, post_release_count()); |
| 4129 | |
| 4130 | reset_counts_and_keys(); |
| 4131 | |
| 4132 | /* |
| 4133 | Now case when notification is successful but we fail to upgrade for some |
| 4134 | other reason. |
| 4135 | */ |
| 4136 | |
| 4137 | // Acquire S lock on the table in another thread. |
| 4138 | Notification lock_grabbed, release_lock; |
| 4139 | MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_lock, |
| 4140 | nullptr, nullptr); |
| 4141 | mdl_thread.start(); |
| 4142 | lock_grabbed.wait_for_notification(); |
| 4143 | |
| 4144 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 4145 | MDL_SHARED_UPGRADABLE, MDL_TRANSACTION); |
| 4146 | |
| 4147 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4148 | |
| 4149 | expected_error = ER_LOCK_WAIT_TIMEOUT; |
| 4150 | |
| 4151 | EXPECT_TRUE(m_mdl_context.upgrade_shared_lock(request.ticket, MDL_EXCLUSIVE, |
| 4152 | zero_timeout)); |
| 4153 | |
| 4154 | /* |
| 4155 | Failure to upgrade lock after successful notification is treated as release. |
| 4156 | */ |
| 4157 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4158 | EXPECT_EQ(1U, post_release_count()); |
| 4159 | EXPECT_TRUE(pre_acquire_key().is_equal(&request.key)); |
| 4160 | EXPECT_TRUE(post_release_key().is_equal(&request.key)); |
| 4161 | |
| 4162 | m_mdl_context.release_transactional_locks(); |
| 4163 | |
| 4164 | // No additional notifications! |
| 4165 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4166 | EXPECT_EQ(1U, post_release_count()); |
| 4167 | |
| 4168 | release_lock.notify(); |
| 4169 | mdl_thread.join(); |
| 4170 | } |
| 4171 | |
| 4172 | /** |
| 4173 | Test for SE notification in lock downgrade scenarios. |
| 4174 | */ |
| 4175 | |
| 4176 | TEST_F(MDLHtonNotifyTest, NotifyDowngrade) { |
| 4177 | MDL_request request; |
| 4178 | |
| 4179 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, db_name, table_name1, |
| 4180 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 4181 | |
| 4182 | // Acquire X lock. |
| 4183 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4184 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4185 | EXPECT_EQ(0U, post_release_count()); |
| 4186 | EXPECT_TRUE(pre_acquire_key().is_equal(&request.key)); |
| 4187 | |
| 4188 | // First try no-op downgrade. No notification should be done. |
| 4189 | request.ticket->downgrade_lock(MDL_EXCLUSIVE); |
| 4190 | |
| 4191 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4192 | EXPECT_EQ(0U, post_release_count()); |
| 4193 | |
| 4194 | // Now downgrade to SNRW lock. |
| 4195 | request.ticket->downgrade_lock(MDL_SHARED_NO_READ_WRITE); |
| 4196 | |
| 4197 | // We should notify SE as if doing lock release. |
| 4198 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4199 | EXPECT_EQ(1U, post_release_count()); |
| 4200 | EXPECT_TRUE(post_release_key().is_equal(&request.key)); |
| 4201 | |
| 4202 | m_mdl_context.release_transactional_locks(); |
| 4203 | |
| 4204 | // No additional notifications! |
| 4205 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4206 | EXPECT_EQ(1U, post_release_count()); |
| 4207 | } |
| 4208 | |
| 4209 | /** |
| 4210 | Test for SE notification in scenarios involving clone operation. |
| 4211 | */ |
| 4212 | |
| 4213 | TEST_F(MDLHtonNotifyTest, NotifyClone) { |
| 4214 | MDL_request request1, request2, request3, request4; |
| 4215 | |
| 4216 | // Acquire X lock. |
| 4217 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 4218 | MDL_EXCLUSIVE, MDL_TRANSACTION); |
| 4219 | |
| 4220 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 4221 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4222 | EXPECT_EQ(0U, post_release_count()); |
| 4223 | EXPECT_TRUE(pre_acquire_key().is_equal(&request1.key)); |
| 4224 | EXPECT_TRUE(m_mdl_context.owns_equal_or_stronger_lock( |
| 4225 | MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE)); |
| 4226 | |
| 4227 | // Now try to clone it to S lock. |
| 4228 | MDL_REQUEST_INIT(&request2, MDL_key::TABLE, db_name, table_name1, MDL_SHARED, |
| 4229 | MDL_EXPLICIT); |
| 4230 | request2.ticket = request1.ticket; |
| 4231 | EXPECT_FALSE(m_mdl_context.clone_ticket(&request2)); |
| 4232 | |
| 4233 | // There should be no additional notification in this case |
| 4234 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4235 | EXPECT_EQ(0U, post_release_count()); |
| 4236 | |
| 4237 | reset_counts_and_keys(); |
| 4238 | |
| 4239 | // Now try to clone it to another X lock instance. |
| 4240 | MDL_REQUEST_INIT(&request3, MDL_key::TABLE, db_name, table_name1, |
| 4241 | MDL_EXCLUSIVE, MDL_EXPLICIT); |
| 4242 | request3.ticket = request1.ticket; |
| 4243 | EXPECT_FALSE(m_mdl_context.clone_ticket(&request3)); |
| 4244 | |
| 4245 | // This should cause additional SE notification |
| 4246 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4247 | EXPECT_EQ(0U, post_release_count()); |
| 4248 | EXPECT_TRUE(pre_acquire_key().is_equal(&request1.key)); |
| 4249 | |
| 4250 | reset_counts_and_keys(); |
| 4251 | |
| 4252 | // Finally try to clone it X lock and see what happens when SE refuses. |
| 4253 | MDL_REQUEST_INIT(&request4, MDL_key::TABLE, db_name, table_name1, |
| 4254 | MDL_EXCLUSIVE, MDL_EXPLICIT); |
| 4255 | |
| 4256 | request4.ticket = request1.ticket; |
| 4257 | expected_error = ER_LOCK_REFUSED_BY_ENGINE; |
| 4258 | set_refuse_acquire(); |
| 4259 | |
| 4260 | EXPECT_TRUE(m_mdl_context.clone_ticket(&request4)); |
| 4261 | |
| 4262 | // This should cause additional SE notification (which fails). |
| 4263 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4264 | EXPECT_EQ(0U, post_release_count()); |
| 4265 | EXPECT_TRUE(pre_acquire_key().is_equal(&request1.key)); |
| 4266 | |
| 4267 | // Release all locks and see how much SE notifications it will cause. |
| 4268 | m_mdl_context.release_transactional_locks(); |
| 4269 | m_mdl_context.release_lock(request2.ticket); |
| 4270 | m_mdl_context.release_lock(request3.ticket); |
| 4271 | |
| 4272 | // All locks are released. |
| 4273 | EXPECT_FALSE(m_mdl_context.has_locks()); |
| 4274 | |
| 4275 | // Two instances of X locks were released so 2 post-release calls. |
| 4276 | EXPECT_EQ(1U, pre_acquire_count()); |
| 4277 | EXPECT_EQ(2U, post_release_count()); |
| 4278 | EXPECT_TRUE(post_release_key().is_equal(&request1.key)); |
| 4279 | } |
| 4280 | |
| 4281 | /** |
| 4282 | Thread class for testing optimizations which skip or delay |
| 4283 | MDL_context::find_deadlock() in some cases. |
| 4284 | */ |
| 4285 | |
| 4286 | class MDL_skip_find_deadlock_thread : public Thread, |
| 4287 | public Test_MDL_context_owner { |
| 4288 | public: |
| 4289 | enum enum_simulation_type { |
| 4290 | AUTOCOMMIT_STMT, |
| 4291 | TRANSACTION_STMT, |
| 4292 | TRANSACTION_STMT_DEADLOCK, |
| 4293 | REPLICATION_APPLIER |
| 4294 | }; |
| 4295 | |
| 4296 | MDL_skip_find_deadlock_thread(enum_simulation_type simulation_type, |
| 4297 | Notification *lock_blocked, |
| 4298 | Notification *wait_resume) |
| 4299 | : m_simulation_type(simulation_type), |
| 4300 | m_lock_blocked(lock_blocked), |
| 4301 | m_wait_resume(wait_resume) { |
| 4302 | m_mdl_context.init(this); |
| 4303 | } |
| 4304 | |
| 4305 | ~MDL_skip_find_deadlock_thread() override { m_mdl_context.destroy(); } |
| 4306 | |
| 4307 | void run() override; |
| 4308 | |
| 4309 | void notify_shared_lock(MDL_context_owner *, bool) override {} |
| 4310 | |
| 4311 | void enter_cond(mysql_cond_t *cond, mysql_mutex_t *mutex, |
| 4312 | const PSI_stage_info *stage, PSI_stage_info *old_stage, |
| 4313 | const char *src_function, const char *src_file, |
| 4314 | int src_line) override { |
| 4315 | Test_MDL_context_owner::enter_cond(cond, mutex, stage, old_stage, |
| 4316 | src_function, src_file, src_line); |
| 4317 | |
| 4318 | m_lock_blocked->notify(); |
| 4319 | return; |
| 4320 | } |
| 4321 | |
| 4322 | void exit_cond(const PSI_stage_info *stage, const char *src_function, |
| 4323 | const char *src_file, int src_line) override { |
| 4324 | Test_MDL_context_owner::exit_cond(stage, src_function, src_file, src_line); |
| 4325 | if (m_wait_resume) m_wait_resume->wait_for_notification(); |
| 4326 | return; |
| 4327 | } |
| 4328 | |
| 4329 | bool might_have_commit_order_waiters() const override { |
| 4330 | return m_simulation_type == REPLICATION_APPLIER; |
| 4331 | } |
| 4332 | |
| 4333 | private: |
| 4334 | enum_simulation_type m_simulation_type; |
| 4335 | Notification *m_lock_blocked; |
| 4336 | Notification *m_wait_resume; |
| 4337 | MDL_context m_mdl_context; |
| 4338 | }; |
| 4339 | |
| 4340 | void MDL_skip_find_deadlock_thread::run() { |
| 4341 | if (m_simulation_type == TRANSACTION_STMT || |
| 4342 | m_simulation_type == TRANSACTION_STMT_DEADLOCK) { |
| 4343 | MDL_request request1; |
| 4344 | MDL_REQUEST_INIT(&request1, MDL_key::TABLE, db_name, table_name1, |
| 4345 | MDL_SHARED_WRITE, MDL_TRANSACTION); |
| 4346 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 4347 | } |
| 4348 | |
| 4349 | MDL_request request; |
| 4350 | MDL_REQUEST_INIT(&request, MDL_key::ACL_CACHE, "", "", MDL_SHARED, |
| 4351 | MDL_EXPLICIT); |
| 4352 | |
| 4353 | if (m_simulation_type == TRANSACTION_STMT_DEADLOCK) { |
| 4354 | // We expect that deadlock will be detected and reported. |
| 4355 | expected_error = ER_LOCK_DEADLOCK; |
| 4356 | EXPECT_TRUE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4357 | } else { |
| 4358 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout)); |
| 4359 | |
| 4360 | m_mdl_context.release_lock(request.ticket); |
| 4361 | } |
| 4362 | |
| 4363 | m_mdl_context.release_transactional_locks(); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | Additional thread class for testing optimization which delays |
| 4368 | MDL_context::find_deadlock() in some cases. |
| 4369 | */ |
| 4370 | |
| 4371 | class MDL_skip_find_deadlock_helper_thread : public Thread, |
| 4372 | public Test_MDL_context_owner { |
| 4373 | public: |
| 4374 | MDL_skip_find_deadlock_helper_thread(Notification *acl_lock_acquired, |
| 4375 | Notification *non_acl_acquire) |
| 4376 | : m_acl_lock_acquired(acl_lock_acquired), |
| 4377 | m_non_acl_acquire(non_acl_acquire) { |
| 4378 | m_mdl_context.init(this); |
| 4379 | } |
| 4380 | |
| 4381 | ~MDL_skip_find_deadlock_helper_thread() override { m_mdl_context.destroy(); } |
| 4382 | |
| 4383 | void run() override; |
| 4384 | |
| 4385 | void notify_shared_lock(MDL_context_owner *, bool) override {} |
| 4386 | |
| 4387 | private: |
| 4388 | Notification *m_acl_lock_acquired; |
| 4389 | Notification *m_non_acl_acquire; |
| 4390 | MDL_context m_mdl_context; |
| 4391 | }; |
| 4392 | |
| 4393 | void MDL_skip_find_deadlock_helper_thread::run() { |
| 4394 | // Acquire X lock on ACL_CACHE singleton and notify that we have done so. |
| 4395 | MDL_request request1; |
| 4396 | MDL_REQUEST_INIT(&request1, MDL_key::ACL_CACHE, "", "", MDL_EXCLUSIVE, |
| 4397 | MDL_EXPLICIT); |
| 4398 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request1, long_timeout)); |
| 4399 | m_acl_lock_acquired->notify(); |
| 4400 | |
| 4401 | // Wait until we are commanded to acquire X lock on the table. |
| 4402 | m_non_acl_acquire->wait_for_notification(); |
| 4403 | |
| 4404 | // Acquire X lock on db_name.table_name1. |
| 4405 | MDL_request request2; |
| 4406 | MDL_REQUEST_INIT(&request2, MDL_key::TABLE, db_name, table_name1, |
| 4407 | MDL_EXCLUSIVE, MDL_EXPLICIT); |
| 4408 | EXPECT_FALSE(m_mdl_context.acquire_lock(&request2, long_timeout)); |
| 4409 | |
| 4410 | // Clean-up. |
| 4411 | m_mdl_context.release_lock(request1.ticket); |
| 4412 | m_mdl_context.release_lock(request2.ticket); |
| 4413 | } |
| 4414 | |
| 4415 | /** |
| 4416 | Mock class which allows to simulate wait on some non-MDL resource |
| 4417 | and catch whether this wait was inspected by deadlock detector. |
| 4418 | */ |
| 4419 | |
| 4420 | class Mock_MDL_wait_for_subgraph : public MDL_wait_for_subgraph { |
| 4421 | public: |
| 4422 | bool accept_visitor(MDL_wait_for_graph_visitor *) override { |
| 4423 | m_was_visited = true; |
| 4424 | return false; |
| 4425 | } |
| 4426 | |
| 4427 | uint get_deadlock_weight() const override { return DEADLOCK_WEIGHT_DML; } |
| 4428 | |
| 4429 | bool was_visited() const { return m_was_visited; } |
| 4430 | |
| 4431 | private: |
| 4432 | bool m_was_visited{false}; |
| 4433 | }; |
| 4434 | |
| 4435 | /** |
| 4436 | Test coverage for optimization which skips MDL_context::find_deadlock() in |
| 4437 | some cases. |
| 4438 | */ |
| 4439 | |
| 4440 | TEST_F(MDLTest, SkipFindDeadlock) { |
| 4441 | Notification lock_blocked1; |
| 4442 | Mock_MDL_wait_for_subgraph mock_subgraph1; |
| 4443 | MDL_skip_find_deadlock_thread thread1( |
| 4444 | MDL_skip_find_deadlock_thread::AUTOCOMMIT_STMT, &lock_blocked1, nullptr); |
| 4445 | |
| 4446 | /* |
| 4447 | Acquire X lock on ACL_CACHE singleton and pretend that we are blocked |
| 4448 | waiting for some other lock/resource. |
| 4449 | */ |
| 4450 | MDL_REQUEST_INIT(&m_request, MDL_key::ACL_CACHE, "", "", MDL_EXCLUSIVE, |
| 4451 | MDL_EXPLICIT); |
| 4452 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 4453 | m_mdl_context.will_wait_for(&mock_subgraph1); |
| 4454 | |
| 4455 | /* |
| 4456 | Start the concurrent thread that will try to acquire S lock on ACL_CACHE, |
| 4457 | simulating privilege check in statement in @@autocommit=1 mode, and |
| 4458 | wait until it gets blocked. |
| 4459 | */ |
| 4460 | thread1.start(); |
| 4461 | lock_blocked1.wait_for_notification(); |
| 4462 | |
| 4463 | /* |
| 4464 | This concurrent thread should not have done deadlock detection thanks |
| 4465 | to optimization. |
| 4466 | */ |
| 4467 | EXPECT_FALSE(mock_subgraph1.was_visited()); |
| 4468 | |
| 4469 | /* Clean-up. Release lock and wait for concurrent thread to complete. */ |
| 4470 | m_mdl_context.done_waiting_for(); |
| 4471 | m_mdl_context.release_lock(m_request.ticket); |
| 4472 | thread1.join(); |
| 4473 | |
| 4474 | Notification lock_blocked2, wait_resume2; |
| 4475 | Mock_MDL_wait_for_subgraph mock_subgraph2; |
| 4476 | MDL_skip_find_deadlock_thread thread2( |
| 4477 | MDL_skip_find_deadlock_thread::TRANSACTION_STMT, &lock_blocked2, |
| 4478 | &wait_resume2); |
| 4479 | /* |
| 4480 | Acquire X lock on ACL_CACHE singleton and pretend that we are blocked |
| 4481 | waiting for some other lock/resource again. |
| 4482 | */ |
| 4483 | m_request.ticket = nullptr; |
| 4484 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 4485 | m_mdl_context.will_wait_for(&mock_subgraph2); |
| 4486 | |
| 4487 | /* |
| 4488 | Start the concurrent thread that will try to acquire S lock on ACL_CACHE |
| 4489 | after acquiring another lock, simulating privilege check in the middle |
| 4490 | of transaction. Wait until it gets blocked and is pauses during initial |
| 4491 | 1 second wait for the lock. |
| 4492 | */ |
| 4493 | thread2.start(); |
| 4494 | lock_blocked2.wait_for_notification(); |
| 4495 | |
| 4496 | /* |
| 4497 | The concurrent thread should not do immediate deadlock detection, |
| 4498 | before 1 second wait for the lock completes, as delayed deadlock |
| 4499 | detection optimization applies. |
| 4500 | */ |
| 4501 | EXPECT_FALSE(mock_subgraph2.was_visited()); |
| 4502 | |
| 4503 | // Unpause initial 1 second wait. |
| 4504 | wait_resume2.notify(); |
| 4505 | |
| 4506 | /* Clean-up. Release lock and wait for concurrent thread to complete. */ |
| 4507 | m_mdl_context.done_waiting_for(); |
| 4508 | m_mdl_context.release_lock(m_request.ticket); |
| 4509 | thread2.join(); |
| 4510 | |
| 4511 | Notification acl_acquired3, non_acl_acquire3, lock_blocked3; |
| 4512 | MDL_skip_find_deadlock_helper_thread helper_thread(&acl_acquired3, |
| 4513 | &non_acl_acquire3); |
| 4514 | MDL_skip_find_deadlock_thread thread3( |
| 4515 | MDL_skip_find_deadlock_thread::TRANSACTION_STMT_DEADLOCK, &lock_blocked3, |
| 4516 | nullptr); |
| 4517 | /* |
| 4518 | Start thread that will acquire X lock on ACL_CACHE singleton and |
| 4519 | pause before trying to acquire X lock on the table. |
| 4520 | */ |
| 4521 | helper_thread.start(); |
| 4522 | acl_acquired3.wait_for_notification(); |
| 4523 | |
| 4524 | /* |
| 4525 | Start another thread that will acquire shared lock on the same table and |
| 4526 | gets blocked while trying to acquire S lock on ACL_CACHE singleton. |
| 4527 | This simulates privilege check in the middle of transaction. |
| 4528 | */ |
| 4529 | thread3.start(); |
| 4530 | lock_blocked3.wait_for_notification(); |
| 4531 | |
| 4532 | /* |
| 4533 | Ask the first thread to acquire X lock on the table. This will created |
| 4534 | deadlock which will be detected and resolved by aborting the second's |
| 4535 | thread wait. |
| 4536 | */ |
| 4537 | non_acl_acquire3.notify(); |
| 4538 | |
| 4539 | /* |
| 4540 | The first thread should be able to proceed and complete. |
| 4541 | Do clean-up. |
| 4542 | */ |
| 4543 | helper_thread.join(); |
| 4544 | thread3.join(); |
| 4545 | |
| 4546 | Notification lock_blocked4; |
| 4547 | Mock_MDL_wait_for_subgraph mock_subgraph4; |
| 4548 | MDL_skip_find_deadlock_thread thread4( |
| 4549 | MDL_skip_find_deadlock_thread::REPLICATION_APPLIER, &lock_blocked4, |
| 4550 | nullptr); |
| 4551 | |
| 4552 | /* |
| 4553 | Acquire X lock on ACL_CACHE singleton and pretend that we are blocked |
| 4554 | on other resource once again. |
| 4555 | */ |
| 4556 | m_request.ticket = nullptr; |
| 4557 | EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout)); |
| 4558 | m_mdl_context.will_wait_for(&mock_subgraph4); |
| 4559 | |
| 4560 | /* |
| 4561 | Start the concurrent thread that will try to acquire S lock on ACL_CACHE |
| 4562 | while pretending to be replication applier thread. Wait until it gets |
| 4563 | blocked. |
| 4564 | */ |
| 4565 | thread4.start(); |
| 4566 | lock_blocked4.wait_for_notification(); |
| 4567 | |
| 4568 | /* |
| 4569 | The concurrent thread should do deadlock detection as optimization |
| 4570 | should not apply. |
| 4571 | */ |
| 4572 | EXPECT_TRUE(mock_subgraph4.was_visited()); |
| 4573 | |
| 4574 | /* Clean-up. Release lock and wait for concurrent thread to complete. */ |
| 4575 | m_mdl_context.done_waiting_for(); |
| 4576 | m_mdl_context.release_lock(m_request.ticket); |
| 4577 | thread4.join(); |
| 4578 | } |
| 4579 | |
| 4580 | /** Test class for MDL_key class testing. Doesn't require MDL initialization. */ |
| 4581 | |
| 4582 | class MDLKeyTest : public ::testing::Test { |
| 4583 | protected: |
| 4584 | MDLKeyTest() = default; |
| 4585 | |
| 4586 | private: |
| 4587 | MDLKeyTest(MDLKeyTest const &) = delete; |
| 4588 | MDLKeyTest &operator=(MDLKeyTest const &) = delete; |
| 4589 | }; |
| 4590 | |
| 4591 | // Google Test recommends DeathTest suffix for classes use in death tests. |
| 4592 | typedef MDLKeyTest MDLKeyDeathTest; |
| 4593 | |
| 4594 | /* |
| 4595 | Verifies that debug build dies with a assert if we try to construct |
| 4596 | MDL_key with too long database or object names. |
| 4597 | */ |
| 4598 | |
| 4599 | #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 4600 | TEST_F(MDLKeyDeathTest, DieWhenNamesAreTooLong) { |
| 4601 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 4602 | |
| 4603 | /* We need a name which is longer than NAME_LEN = 64*3 = 192.*/ |
| 4604 | const char *too_long_name = |
| 4605 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4606 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4607 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4608 | "0123456789"; |
| 4609 | |
| 4610 | EXPECT_DEATH(MDL_key key0(MDL_key::TABLE, too_long_name, ""), |
| 4611 | ".*Assertion.*strlen.*"); |
| 4612 | EXPECT_DEATH(MDL_key key1(MDL_key::TABLE, "", too_long_name), |
| 4613 | ".*Assertion.*strlen.*"); |
| 4614 | |
| 4615 | MDL_key key2; |
| 4616 | |
| 4617 | EXPECT_DEATH(key2.mdl_key_init(MDL_key::TABLE, too_long_name, ""), |
| 4618 | ".*Assertion.*strlen.*"); |
| 4619 | EXPECT_DEATH(key2.mdl_key_init(MDL_key::TABLE, "", too_long_name), |
| 4620 | ".*Assertion.*strlen.*"); |
| 4621 | } |
| 4622 | #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 4623 | |
| 4624 | /* |
| 4625 | Verifies that for production build we allow construction of |
| 4626 | MDL_key with too long database or object names, but they are |
| 4627 | truncated. |
| 4628 | */ |
| 4629 | |
| 4630 | #if defined(NDEBUG) |
| 4631 | TEST_F(MDLKeyTest, TruncateTooLongNames) { |
| 4632 | /* We need a name which is longer than NAME_LEN = 64*3 = 192.*/ |
| 4633 | const char *too_long_name = |
| 4634 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4635 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4636 | "0123456789012345678901234567890123456789012345678901234567890123" |
| 4637 | "0123456789"; |
| 4638 | |
| 4639 | MDL_key key(MDL_key::TABLE, too_long_name, too_long_name); |
| 4640 | |
| 4641 | const char *db_name = key.db_name(); |
| 4642 | const char *name = key.name(); |
| 4643 | |
| 4644 | EXPECT_LE(strlen(db_name), (uint)NAME_LEN); |
| 4645 | EXPECT_TRUE(strncmp(db_name, too_long_name, NAME_LEN) == 0); |
| 4646 | EXPECT_LE(strlen(name), (uint)NAME_LEN); |
| 4647 | EXPECT_TRUE(strncmp(name, too_long_name, NAME_LEN) == 0); |
| 4648 | } |
| 4649 | |
| 4650 | struct Mock_MDL_context_owner : public Test_MDL_context_owner { |
| 4651 | void notify_shared_lock(MDL_context_owner *in_use, |
| 4652 | bool needs_thr_lock_abort) final { |
| 4653 | in_use->notify_shared_lock(nullptr, needs_thr_lock_abort); |
| 4654 | } |
| 4655 | }; |
| 4656 | |
| 4657 | using Name_vec = std::vector<std::string>; |
| 4658 | |
| 4659 | /* |
| 4660 | Helper function for benchmark. |
| 4661 | */ |
| 4662 | inline size_t read_from_env(const char *var, size_t def) { |
| 4663 | const char *val = getenv(var); |
| 4664 | return val ? static_cast<size_t>(atoi(val)) : def; |
| 4665 | } |
| 4666 | |
| 4667 | /* |
| 4668 | Helper function for benchmark. |
| 4669 | */ |
| 4670 | static Name_vec make_name_vec(size_t t) { |
| 4671 | std::vector<std::string> names; |
| 4672 | names.reserve(t); |
| 4673 | static std::stringstream str; |
| 4674 | for (size_t i = 0; i < t; ++i) { |
| 4675 | str << "t_" << i; |
| 4676 | names.push_back(str.str()); |
| 4677 | str.str(""); |
| 4678 | } |
| 4679 | return names; |
| 4680 | } |
| 4681 | |
| 4682 | /* |
| 4683 | Helper function for benchmark. |
| 4684 | */ |
| 4685 | static void lock_bench(MDL_context &ctx, const Name_vec &names) { |
| 4686 | for (auto &name : names) { |
| 4687 | MDL_request request; |
| 4688 | MDL_REQUEST_INIT(&request, MDL_key::TABLE, "S", name.c_str(), |
| 4689 | MDL_INTENTION_EXCLUSIVE, MDL_TRANSACTION); |
| 4690 | ctx.acquire_lock(&request, 2); |
| 4691 | } |
| 4692 | ctx.release_transactional_locks(); |
| 4693 | } |
| 4694 | |
| 4695 | /** |
| 4696 | Microbenchmark which tests the performance of acquire_lock (find_ticket) |
| 4697 | */ |
| 4698 | static void BM_FindTicket(size_t num_iterations) { |
| 4699 | StopBenchmarkTiming(); |
| 4700 | system_charset_info = &my_charset_utf8mb3_bin; |
| 4701 | mdl_init(); |
| 4702 | MDL_context ctx; |
| 4703 | Mock_MDL_context_owner owner; |
| 4704 | ctx.init(&owner); |
| 4705 | size_t ntickets = read_from_env("NTICKETS", 512); |
| 4706 | |
| 4707 | std::cout << "Tickets: " << ntickets << "\n"; |
| 4708 | |
| 4709 | Name_vec names = make_name_vec(ntickets); |
| 4710 | |
| 4711 | StartBenchmarkTiming(); |
| 4712 | |
| 4713 | for (size_t i = 0; i < num_iterations; ++i) { |
| 4714 | lock_bench(ctx, names); |
| 4715 | } |
| 4716 | |
| 4717 | StopBenchmarkTiming(); |
| 4718 | mdl_destroy(); |
| 4719 | } |
| 4720 | |
| 4721 | BENCHMARK(BM_FindTicket) |
| 4722 | |
| 4723 | #endif // defined(NDEBUG) |
| 4724 | |
| 4725 | } // namespace mdl_unittest |