| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stddef.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "my_inttypes.h" |
| 29 | #include "my_sys.h" // ErrorHandlerFunctionPointer |
| 30 | #include "mysqld_error.h" |
| 31 | #include "sql/locking_service.h" |
| 32 | #include "sql/mdl.h" |
| 33 | #include "sql/sql_base.h" |
| 34 | #include "sql/sql_class.h" |
| 35 | #include "unittest/gunit/test_utils.h" |
| 36 | #include "unittest/gunit/thread_utils.h" |
| 37 | |
| 38 | /* |
| 39 | Putting everything in a namespace prevents any (unintentional) |
| 40 | name clashes with the code under test. |
| 41 | */ |
| 42 | namespace locking_service { |
| 43 | |
| 44 | using my_testing::Mock_error_handler; |
| 45 | using my_testing::Server_initializer; |
| 46 | using thread::Notification; |
| 47 | using thread::Thread; |
| 48 | |
| 49 | const char namespace1[] = "namespace1"; |
| 50 | const char namespace2[] = "namespace2"; |
| 51 | const char lock_name1[] = "lock1"; |
| 52 | const char lock_name2[] = "lock2"; |
| 53 | const char lock_name3[] = "lock3"; |
| 54 | const char lock_name4[] = "lock4"; |
| 55 | |
| 56 | class LockingServiceTest : public ::testing::Test { |
| 57 | protected: |
| 58 | LockingServiceTest() = default; |
| 59 | |
| 60 | static void SetUpTestCase() { |
| 61 | m_old_error_handler_hook = error_handler_hook; |
| 62 | // Make sure my_error() ends up calling my_message_sql so that |
| 63 | // Mock_error_handler is actually triggered. |
| 64 | error_handler_hook = my_message_sql; |
| 65 | table_def_init(); |
| 66 | } |
| 67 | |
| 68 | static void TearDownTestCase() { |
| 69 | error_handler_hook = m_old_error_handler_hook; |
| 70 | table_def_free(); |
| 71 | } |
| 72 | |
| 73 | void SetUp() override { |
| 74 | mdl_init(); |
| 75 | m_initializer.SetUp(); |
| 76 | m_thd = m_initializer.thd(); |
| 77 | // Slight hack: Makes THD::is_connected() return true. |
| 78 | // This prevents MDL_context::acquire_lock() from thinking |
| 79 | // the connection has died and the wait should be aborted. |
| 80 | m_thd->system_thread = SYSTEM_THREAD_SLAVE_IO; |
| 81 | } |
| 82 | |
| 83 | void TearDown() override { |
| 84 | m_initializer.TearDown(); |
| 85 | mdl_destroy(); |
| 86 | } |
| 87 | |
| 88 | Server_initializer m_initializer; |
| 89 | THD *m_thd; |
| 90 | |
| 91 | static ErrorHandlerFunctionPointer m_old_error_handler_hook; |
| 92 | }; |
| 93 | |
| 94 | ErrorHandlerFunctionPointer LockingServiceTest::m_old_error_handler_hook; |
| 95 | |
| 96 | /** |
| 97 | Test acquire and release of several read or write locks. |
| 98 | */ |
| 99 | TEST_F(LockingServiceTest, AcquireAndRelease) { |
| 100 | // Take two read locks |
| 101 | const char *names1[] = {lock_name1, lock_name2}; |
| 102 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 2, |
| 103 | LOCKING_SERVICE_READ, 3600)); |
| 104 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 105 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 106 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 107 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 108 | |
| 109 | // Release the locks |
| 110 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 111 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 112 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 113 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 114 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 115 | |
| 116 | // Take one write lock |
| 117 | const char *names2[] = {lock_name3}; |
| 118 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names2, 1, |
| 119 | LOCKING_SERVICE_WRITE, 3600)); |
| 120 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 121 | MDL_key::LOCKING_SERVICE, namespace1, lock_name3, MDL_EXCLUSIVE)); |
| 122 | |
| 123 | // Take another write lock |
| 124 | const char *names3[] = {lock_name4}; |
| 125 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names3, 1, |
| 126 | LOCKING_SERVICE_WRITE, 3600)); |
| 127 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 128 | MDL_key::LOCKING_SERVICE, namespace1, lock_name3, MDL_EXCLUSIVE)); |
| 129 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 130 | MDL_key::LOCKING_SERVICE, namespace1, lock_name4, MDL_EXCLUSIVE)); |
| 131 | |
| 132 | // Take the read locks again |
| 133 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 2, |
| 134 | LOCKING_SERVICE_READ, 3600)); |
| 135 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 136 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 137 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 138 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 139 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 140 | MDL_key::LOCKING_SERVICE, namespace1, lock_name3, MDL_EXCLUSIVE)); |
| 141 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 142 | MDL_key::LOCKING_SERVICE, namespace1, lock_name4, MDL_EXCLUSIVE)); |
| 143 | |
| 144 | // Release all locks |
| 145 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 146 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 147 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 148 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 149 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 150 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 151 | MDL_key::LOCKING_SERVICE, namespace1, lock_name3, MDL_SHARED)); |
| 152 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 153 | MDL_key::LOCKING_SERVICE, namespace1, lock_name4, MDL_SHARED)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | Test that names are case sensitive |
| 158 | */ |
| 159 | TEST_F(LockingServiceTest, CaseSensitive) { |
| 160 | const char *lower[] = {"test"}; |
| 161 | const char *upper[] = {"TEST"}; |
| 162 | |
| 163 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, lower, 1, |
| 164 | LOCKING_SERVICE_WRITE, 3600)); |
| 165 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 166 | MDL_key::LOCKING_SERVICE, namespace1, "test", MDL_EXCLUSIVE)); |
| 167 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 168 | MDL_key::LOCKING_SERVICE, namespace1, "TEST", MDL_EXCLUSIVE)); |
| 169 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 170 | |
| 171 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, "test", upper, 1, |
| 172 | LOCKING_SERVICE_WRITE, 3600)); |
| 173 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 174 | MDL_key::LOCKING_SERVICE, "test", "TEST", MDL_EXCLUSIVE)); |
| 175 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 176 | MDL_key::LOCKING_SERVICE, "TEST", "TEST", MDL_EXCLUSIVE)); |
| 177 | EXPECT_FALSE(release_locking_service_locks(m_thd, "test")); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | Test verfication of name lengths. |
| 182 | */ |
| 183 | TEST_F(LockingServiceTest, ValidNames) { |
| 184 | const char *ok_name[] = {"test"}; |
| 185 | { |
| 186 | const char *null = nullptr; |
| 187 | const char *names1[] = {null}; |
| 188 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_WRONG_NAME); |
| 189 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 190 | LOCKING_SERVICE_READ, 3600)); |
| 191 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, null, ok_name, 1, |
| 192 | LOCKING_SERVICE_READ, 3600)); |
| 193 | EXPECT_TRUE(release_locking_service_locks(m_thd, null)); |
| 194 | EXPECT_EQ(3, error_handler.handle_called()); |
| 195 | } |
| 196 | |
| 197 | { |
| 198 | const char *empty = ""; |
| 199 | const char *names2[] = {empty}; |
| 200 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_WRONG_NAME); |
| 201 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names2, 1, |
| 202 | LOCKING_SERVICE_READ, 3600)); |
| 203 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, empty, ok_name, 1, |
| 204 | LOCKING_SERVICE_READ, 3600)); |
| 205 | EXPECT_TRUE(release_locking_service_locks(m_thd, empty)); |
| 206 | EXPECT_EQ(3, error_handler.handle_called()); |
| 207 | } |
| 208 | |
| 209 | { |
| 210 | const char *long65 = |
| 211 | "12345678901234567890123456789012345678901234567890123456789012345"; |
| 212 | const char *names3[] = {long65}; |
| 213 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_WRONG_NAME); |
| 214 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names3, 1, |
| 215 | LOCKING_SERVICE_READ, 3600)); |
| 216 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, long65, ok_name, 1, |
| 217 | LOCKING_SERVICE_READ, 3600)); |
| 218 | EXPECT_TRUE(release_locking_service_locks(m_thd, long65)); |
| 219 | EXPECT_EQ(3, error_handler.handle_called()); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | Test interaction (or lack of it) with transactional locks. |
| 225 | */ |
| 226 | TEST_F(LockingServiceTest, TransactionInteraction) { |
| 227 | // Releasing transactional locks should not affect lock service locks. |
| 228 | const char *names1[] = {lock_name1}; |
| 229 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 230 | LOCKING_SERVICE_READ, 3600)); |
| 231 | |
| 232 | m_thd->mdl_context.release_transactional_locks(); |
| 233 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 234 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 235 | |
| 236 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 237 | LOCKING_SERVICE_WRITE, 3600)); |
| 238 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 239 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_EXCLUSIVE)); |
| 240 | m_thd->mdl_context.release_transactional_locks(); |
| 241 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 242 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_EXCLUSIVE)); |
| 243 | |
| 244 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 245 | |
| 246 | // Releasing lock service locks should not affect transactional locks. |
| 247 | MDL_request fake_request; |
| 248 | MDL_REQUEST_INIT(&fake_request, MDL_key::SCHEMA, "db", "table", MDL_EXCLUSIVE, |
| 249 | MDL_TRANSACTION); |
| 250 | EXPECT_FALSE(m_thd->mdl_context.acquire_lock(&fake_request, 3600)); |
| 251 | |
| 252 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 253 | LOCKING_SERVICE_READ, 3600)); |
| 254 | |
| 255 | m_thd->mdl_context.release_transactional_locks(); |
| 256 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 257 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 258 | |
| 259 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | Utility thread for acquiring lock service locks with notification of |
| 264 | acquire and release. |
| 265 | */ |
| 266 | class LockServiceThread : public Thread { |
| 267 | public: |
| 268 | LockServiceThread(const char **names, size_t num, |
| 269 | enum_locking_service_lock_type lock_type, |
| 270 | Notification *grabbed_arg, Notification *release_arg) |
| 271 | : m_names(names), |
| 272 | m_num(num), |
| 273 | m_lock_type(lock_type), |
| 274 | m_lock_grabbed(grabbed_arg), |
| 275 | m_lock_release(release_arg) {} |
| 276 | |
| 277 | void run() override { |
| 278 | Server_initializer m_initializer; |
| 279 | m_initializer.SetUp(); |
| 280 | THD *m_thd = m_initializer.thd(); |
| 281 | |
| 282 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, m_names, |
| 283 | m_num, m_lock_type, 3600)); |
| 284 | if (m_lock_grabbed) m_lock_grabbed->notify(); |
| 285 | if (m_lock_release) m_lock_release->wait_for_notification(); |
| 286 | |
| 287 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 288 | |
| 289 | m_initializer.TearDown(); |
| 290 | } |
| 291 | |
| 292 | private: |
| 293 | const char **m_names; |
| 294 | size_t m_num; |
| 295 | enum_locking_service_lock_type m_lock_type; |
| 296 | Notification *m_lock_grabbed; |
| 297 | Notification *m_lock_release; |
| 298 | }; |
| 299 | |
| 300 | /** |
| 301 | Test that read locks are compatible with read locks but |
| 302 | incompatible with write locks. |
| 303 | */ |
| 304 | TEST_F(LockingServiceTest, ReadCompatibility) { |
| 305 | const char *names1[] = {lock_name1}; |
| 306 | Notification lock_grabbed, lock_release; |
| 307 | LockServiceThread thread(names1, 1, LOCKING_SERVICE_READ, &lock_grabbed, |
| 308 | &lock_release); |
| 309 | thread.start(); |
| 310 | lock_grabbed.wait_for_notification(); |
| 311 | |
| 312 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 313 | LOCKING_SERVICE_READ, 3600)); |
| 314 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 315 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 316 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 317 | |
| 318 | { |
| 319 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_TIMEOUT); |
| 320 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 321 | LOCKING_SERVICE_WRITE, 2)); |
| 322 | // Wait 2 seconds here so that we hit the "abs_timeout is far away" |
| 323 | // code path in MDL_context::acquire_lock() on all platforms. |
| 324 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 325 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 326 | EXPECT_EQ(1, error_handler.handle_called()); |
| 327 | } |
| 328 | |
| 329 | lock_release.notify(); |
| 330 | thread.join(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | Test that write locks are incompatible with write locks. |
| 335 | */ |
| 336 | TEST_F(LockingServiceTest, WriteCompatibility) { |
| 337 | const char *names1[] = {lock_name1}; |
| 338 | Notification lock_grabbed, lock_release; |
| 339 | LockServiceThread thread(names1, 1, LOCKING_SERVICE_WRITE, &lock_grabbed, |
| 340 | &lock_release); |
| 341 | thread.start(); |
| 342 | lock_grabbed.wait_for_notification(); |
| 343 | |
| 344 | { |
| 345 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_TIMEOUT); |
| 346 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 347 | LOCKING_SERVICE_WRITE, 1)); |
| 348 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 349 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 350 | EXPECT_EQ(1, error_handler.handle_called()); |
| 351 | } |
| 352 | |
| 353 | lock_release.notify(); |
| 354 | thread.join(); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | Test that if acquisition of multiple locks fails because of one |
| 359 | lock conflicts, no locks are acquired. |
| 360 | */ |
| 361 | TEST_F(LockingServiceTest, AtomicAcquire) { |
| 362 | const char *names1[] = {lock_name1}; |
| 363 | Notification lock_grabbed, lock_release; |
| 364 | LockServiceThread thread(names1, 1, LOCKING_SERVICE_READ, &lock_grabbed, |
| 365 | &lock_release); |
| 366 | thread.start(); |
| 367 | lock_grabbed.wait_for_notification(); |
| 368 | |
| 369 | { |
| 370 | // Conflict on lock_name1, lock_name2 should not be acquired. |
| 371 | const char *names2[] = {lock_name1, lock_name2}; |
| 372 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_TIMEOUT); |
| 373 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names2, 2, |
| 374 | LOCKING_SERVICE_WRITE, 1)); |
| 375 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 376 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 377 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 378 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 379 | EXPECT_EQ(1, error_handler.handle_called()); |
| 380 | } |
| 381 | |
| 382 | { |
| 383 | // Reverse order of lock names - should give same result. |
| 384 | const char *names2[] = {lock_name2, lock_name1}; |
| 385 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_TIMEOUT); |
| 386 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names2, 2, |
| 387 | LOCKING_SERVICE_WRITE, 1)); |
| 388 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 389 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 390 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 391 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_SHARED)); |
| 392 | EXPECT_EQ(1, error_handler.handle_called()); |
| 393 | } |
| 394 | |
| 395 | lock_release.notify(); |
| 396 | thread.join(); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | Test that namespaces are independent. |
| 401 | */ |
| 402 | TEST_F(LockingServiceTest, Namespaces) { |
| 403 | const char *names1[] = {lock_name1}; |
| 404 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 405 | LOCKING_SERVICE_READ, 3600)); |
| 406 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 407 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 408 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 409 | MDL_key::LOCKING_SERVICE, namespace2, lock_name1, MDL_SHARED)); |
| 410 | |
| 411 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace2)); |
| 412 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 413 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_SHARED)); |
| 414 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 415 | |
| 416 | // Take write lock with namespace1 in a separate thread. |
| 417 | Notification lock_grabbed, lock_release; |
| 418 | LockServiceThread thread(names1, 1, LOCKING_SERVICE_WRITE, &lock_grabbed, |
| 419 | &lock_release); |
| 420 | thread.start(); |
| 421 | lock_grabbed.wait_for_notification(); |
| 422 | |
| 423 | // We should be able to take a write lock in namespace2. |
| 424 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace2, names1, 1, |
| 425 | LOCKING_SERVICE_WRITE, 3600)); |
| 426 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 427 | MDL_key::LOCKING_SERVICE, namespace2, lock_name1, MDL_EXCLUSIVE)); |
| 428 | EXPECT_FALSE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 429 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_EXCLUSIVE)); |
| 430 | |
| 431 | lock_release.notify(); |
| 432 | thread.join(); |
| 433 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace2)); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | Thread which acquires a write lock on lock_name1 and then disconnects. |
| 438 | */ |
| 439 | class LockServiceDisconnectThread : public Thread { |
| 440 | public: |
| 441 | LockServiceDisconnectThread() = default; |
| 442 | |
| 443 | void run() override { |
| 444 | Server_initializer m_initializer; |
| 445 | m_initializer.SetUp(); |
| 446 | THD *m_thd = m_initializer.thd(); |
| 447 | |
| 448 | const char *names1[] = {lock_name1}; |
| 449 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 450 | LOCKING_SERVICE_WRITE, 3600)); |
| 451 | m_initializer.TearDown(); |
| 452 | } |
| 453 | }; |
| 454 | |
| 455 | /** |
| 456 | Test that locks are released automatically on disconnect. |
| 457 | */ |
| 458 | TEST_F(LockingServiceTest, Disconnect) { |
| 459 | LockServiceDisconnectThread thread; |
| 460 | thread.start(); |
| 461 | thread.join(); |
| 462 | |
| 463 | // Check that we now can acquire a write lock on name1. |
| 464 | const char *names1[] = {lock_name1}; |
| 465 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 466 | LOCKING_SERVICE_WRITE, 3600)); |
| 467 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | Utility thread for deadlock tests. Acquires two locks in order. |
| 472 | */ |
| 473 | class LockServiceDeadlockThread : public Thread { |
| 474 | public: |
| 475 | LockServiceDeadlockThread(Notification *grabbed1_arg, Notification *wait_arg) |
| 476 | : m_lock_grabbed1(grabbed1_arg), m_wait(wait_arg) {} |
| 477 | |
| 478 | void run() override { |
| 479 | Server_initializer m_initializer; |
| 480 | m_initializer.SetUp(); |
| 481 | THD *m_thd = m_initializer.thd(); |
| 482 | |
| 483 | const char *names1[] = {lock_name1}; |
| 484 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 485 | LOCKING_SERVICE_WRITE, 3600)); |
| 486 | m_lock_grabbed1->notify(); |
| 487 | |
| 488 | m_wait->wait_for_notification(); |
| 489 | |
| 490 | { |
| 491 | // The deadlock should be resolved by aborting the wait for the read lock |
| 492 | // We should therefore fail. |
| 493 | const char *names2[] = {lock_name2}; |
| 494 | Mock_error_handler error_handler(m_thd, ER_LOCKING_SERVICE_DEADLOCK); |
| 495 | EXPECT_TRUE(acquire_locking_service_locks(m_thd, namespace1, names2, 1, |
| 496 | LOCKING_SERVICE_READ, 3600)); |
| 497 | EXPECT_EQ(1, error_handler.handle_called()); |
| 498 | } |
| 499 | |
| 500 | // We still have the first lock |
| 501 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 502 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_EXCLUSIVE)); |
| 503 | |
| 504 | // Release locks so that the other thread can continue. |
| 505 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 506 | |
| 507 | m_initializer.TearDown(); |
| 508 | } |
| 509 | |
| 510 | private: |
| 511 | Notification *m_lock_grabbed1; |
| 512 | Notification *m_wait; |
| 513 | }; |
| 514 | |
| 515 | /** |
| 516 | Test that deadlock is detected and lock acquisition fails. |
| 517 | The wait for a read lock should be aborted in preference for |
| 518 | aborting the wait for a write lock. |
| 519 | */ |
| 520 | TEST_F(LockingServiceTest, DeadlockRead) { |
| 521 | // Start a thread which acquires a write lock on name1 |
| 522 | Notification lock_grabbed1, wait; |
| 523 | LockServiceDeadlockThread thread(&lock_grabbed1, &wait); |
| 524 | thread.start(); |
| 525 | lock_grabbed1.wait_for_notification(); |
| 526 | |
| 527 | // Acquire write lock on name 2 |
| 528 | const char *names2[] = {lock_name2}; |
| 529 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names2, 1, |
| 530 | LOCKING_SERVICE_WRITE, 3600)); |
| 531 | |
| 532 | // Signal the other thread to continue, taking write lock on name1 |
| 533 | wait.notify(); |
| 534 | |
| 535 | // The other thread will be aborted so that we will acquire the lock. |
| 536 | const char *names1[] = {lock_name1}; |
| 537 | EXPECT_FALSE(acquire_locking_service_locks(m_thd, namespace1, names1, 1, |
| 538 | LOCKING_SERVICE_WRITE, 3600)); |
| 539 | |
| 540 | // Both locks should now be held |
| 541 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 542 | MDL_key::LOCKING_SERVICE, namespace1, lock_name1, MDL_EXCLUSIVE)); |
| 543 | EXPECT_TRUE(m_thd->mdl_context.owns_equal_or_stronger_lock( |
| 544 | MDL_key::LOCKING_SERVICE, namespace1, lock_name2, MDL_EXCLUSIVE)); |
| 545 | |
| 546 | thread.join(); |
| 547 | EXPECT_FALSE(release_locking_service_locks(m_thd, namespace1)); |
| 548 | } |
| 549 | } // namespace locking_service |