| 1 | /* Copyright (c) 2012, 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 <sys/types.h> |
| 26 | |
| 27 | #include "lex_string.h" |
| 28 | #include "my_inttypes.h" |
| 29 | #include "sql/mysqld_thd_manager.h" |
| 30 | #include "sql/table_cache.h" |
| 31 | #include "storage/example/ha_example.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | /* |
| 35 | We need example_hton to be able short-cut creation of example |
| 36 | handler instances for mock TABLE objects. |
| 37 | */ |
| 38 | extern handlerton *example_hton; |
| 39 | |
| 40 | namespace table_cache_unittest { |
| 41 | |
| 42 | using my_testing::Server_initializer; |
| 43 | |
| 44 | #ifdef SAFE_MUTEX |
| 45 | static const char *assert_string = ".*Assertion.*count > 0.*my_thread_equal.*"; |
| 46 | #endif |
| 47 | |
| 48 | /** |
| 49 | Test fixture for basic tests involving Table_cache |
| 50 | and Table_cache_manager classes. |
| 51 | |
| 52 | Unlike more advanced fixture it doesn't initialize |
| 53 | table cache manager, but only prepares THD objects |
| 54 | necessary for testing. |
| 55 | */ |
| 56 | |
| 57 | class TableCacheBasicTest : public ::testing::Test { |
| 58 | protected: |
| 59 | static const uint MAX_THREADS = 3; |
| 60 | |
| 61 | void SetUp() override { |
| 62 | Global_THD_manager *thd_manager = Global_THD_manager::get_instance(); |
| 63 | thd_manager->set_unit_test(); |
| 64 | // Reset thread ID counter for each test. |
| 65 | thd_manager->set_thread_id_counter(1); |
| 66 | for (uint i = 0; i < MAX_THREADS; ++i) { |
| 67 | initializer[i].SetUp(); |
| 68 | } |
| 69 | |
| 70 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 71 | } |
| 72 | void TearDown() override { |
| 73 | for (uint i = 0; i < MAX_THREADS; ++i) initializer[i].TearDown(); |
| 74 | } |
| 75 | |
| 76 | THD *get_thd(uint index) { return initializer[index].thd(); } |
| 77 | |
| 78 | Server_initializer initializer[MAX_THREADS]; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | A more advanced fixture that also initializes table_cache_manager |
| 83 | with one Table_cache instance and TDC. |
| 84 | */ |
| 85 | |
| 86 | class TableCacheSingleCacheTest : public TableCacheBasicTest { |
| 87 | protected: |
| 88 | virtual uint CachesNumber() { return 1; } |
| 89 | void SetUp() override { |
| 90 | TableCacheBasicTest::SetUp(); |
| 91 | |
| 92 | /* |
| 93 | In addition to table_cache_manager we want to have initialized |
| 94 | TDC so we can use its HASH object for calculating hash values |
| 95 | and be able to free TABLE objects correctly (we need LOCK_open |
| 96 | initialized for this). |
| 97 | */ |
| 98 | table_cache_instances = CachesNumber(); |
| 99 | table_cache_size_per_instance = 100; |
| 100 | ASSERT_FALSE(table_def_init()); |
| 101 | } |
| 102 | void TearDown() override { |
| 103 | table_def_free(); |
| 104 | TableCacheBasicTest::TearDown(); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | Another advanced fixture that also initializes table_cache_manager |
| 110 | with two Table_cache instances and TDC. |
| 111 | */ |
| 112 | |
| 113 | class TableCacheDoubleCacheTest : public TableCacheSingleCacheTest { |
| 114 | protected: |
| 115 | uint CachesNumber() override { return 2; } |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | Class for mock TABLE_SHARE object which also allows to create |
| 120 | associated TABLE objects which are usable with Table_cache. |
| 121 | */ |
| 122 | |
| 123 | class Mock_share : public TABLE_SHARE { |
| 124 | MEM_ROOT m_mem_root; |
| 125 | Table_cache_element *cache_element_arr[Table_cache_manager::MAX_TABLE_CACHES]; |
| 126 | |
| 127 | public: |
| 128 | Mock_share(const char *key) |
| 129 | : // Assertion in some of Table_cache methods check that the |
| 130 | // version of the share is up-to-date, so make sure it's set. |
| 131 | TABLE_SHARE(refresh_version, false), |
| 132 | // MEM_ROOT is used for constructing ha_example() instances. |
| 133 | m_mem_root(PSI_NOT_INSTRUMENTED, 1024) { |
| 134 | /* |
| 135 | Both table_cache_key and cache_element array are used by |
| 136 | Table_cache code. |
| 137 | */ |
| 138 | table_cache_key.str = key; |
| 139 | table_cache_key.length = strlen(key); |
| 140 | memset(cache_element_arr, 0, sizeof(cache_element_arr)); |
| 141 | cache_element = cache_element_arr; |
| 142 | // Ensure that share is never destroyed. |
| 143 | increment_ref_count(); |
| 144 | } |
| 145 | |
| 146 | ~Mock_share() { m_mem_root.Clear(); } |
| 147 | |
| 148 | TABLE *create_table(THD *thd) { |
| 149 | TABLE *result = |
| 150 | (TABLE *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(TABLE), MYF(0)); |
| 151 | new (result) TABLE; |
| 152 | |
| 153 | result->s = this; |
| 154 | // We create TABLE which is already marked as used |
| 155 | result->in_use = thd; |
| 156 | /* |
| 157 | Assertions in some of Table_cache methods need non-NULL |
| 158 | TABLE::file and TABLE::db_stat. Code that frees unused |
| 159 | TABLE objects needs proper "handler" instance. |
| 160 | */ |
| 161 | result->file = new (&m_mem_root) ha_example(example_hton, this); |
| 162 | result->db_stat = HA_READ_ONLY; |
| 163 | |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | void destroy_table(TABLE *table) { my_free(table); } |
| 168 | }; |
| 169 | |
| 170 | // Google Test recommends DeathTest suffix for classes used in death tests. |
| 171 | typedef TableCacheBasicTest TableCacheBasicDeathTest; |
| 172 | typedef TableCacheDoubleCacheTest TableCacheDoubleCacheDeathTest; |
| 173 | |
| 174 | /* |
| 175 | Test initilization/destruction of Table_cache. |
| 176 | */ |
| 177 | |
| 178 | TEST_F(TableCacheBasicDeathTest, CacheCreateAndDestroy) { |
| 179 | Table_cache table_cache; |
| 180 | |
| 181 | ASSERT_FALSE(table_cache.init()); |
| 182 | |
| 183 | // Cache should be empty after creation |
| 184 | EXPECT_EQ(0U, table_cache.cached_tables()); |
| 185 | |
| 186 | // Cache should be not locked after creation |
| 187 | #ifdef SAFE_MUTEX |
| 188 | EXPECT_DEATH_IF_SUPPORTED(table_cache.assert_owner(), assert_string); |
| 189 | #endif |
| 190 | table_cache.destroy(); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | Test locking for Table_cache object. |
| 195 | */ |
| 196 | |
| 197 | TEST_F(TableCacheBasicDeathTest, CacheLockAndUnlock) { |
| 198 | Table_cache table_cache; |
| 199 | |
| 200 | ASSERT_FALSE(table_cache.init()); |
| 201 | |
| 202 | #ifdef SAFE_MUTEX |
| 203 | // Cache should not be locked after creation |
| 204 | EXPECT_DEATH_IF_SUPPORTED(table_cache.assert_owner(), assert_string); |
| 205 | #endif |
| 206 | |
| 207 | // And get locked after we call its lock() method |
| 208 | table_cache.lock(); |
| 209 | table_cache.assert_owner(); |
| 210 | |
| 211 | // And get unlocked after we call its unlock() method |
| 212 | table_cache.unlock(); |
| 213 | #ifdef SAFE_MUTEX |
| 214 | EXPECT_DEATH_IF_SUPPORTED(table_cache.assert_owner(), assert_string); |
| 215 | #endif |
| 216 | |
| 217 | table_cache.destroy(); |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | Tests for the rest of methods of Table_cache need to use an |
| 222 | object controlled by the global instance of Table_cache_manager. |
| 223 | Let us start testing of Table_cache_manager with test for |
| 224 | its initialization/destruction. This test also covers well |
| 225 | Table_cache_manager::get_cache() method. |
| 226 | */ |
| 227 | |
| 228 | TEST_F(TableCacheBasicDeathTest, ManagerCreateAndDestroy) { |
| 229 | // Request two instances of Table_cache |
| 230 | table_cache_instances = 2; |
| 231 | |
| 232 | ASSERT_FALSE(table_cache_manager.init()); |
| 233 | |
| 234 | // All caches are empty after creation |
| 235 | EXPECT_EQ(0U, table_cache_manager.cached_tables()); |
| 236 | |
| 237 | // There should be two different caches in the manager |
| 238 | Table_cache *cache_1, *cache_2, *cache_3; |
| 239 | cache_1 = table_cache_manager.get_cache(get_thd(0)); |
| 240 | cache_2 = table_cache_manager.get_cache(get_thd(1)); |
| 241 | cache_3 = table_cache_manager.get_cache(get_thd(2)); |
| 242 | EXPECT_TRUE(cache_1 != cache_2); |
| 243 | // And not three ! |
| 244 | EXPECT_TRUE(cache_3 == cache_1); |
| 245 | |
| 246 | // Both caches should be empty |
| 247 | EXPECT_EQ(0U, cache_1->cached_tables()); |
| 248 | EXPECT_EQ(0U, cache_2->cached_tables()); |
| 249 | |
| 250 | // And not locked |
| 251 | #ifdef SAFE_MUTEX |
| 252 | EXPECT_DEATH_IF_SUPPORTED(cache_1->assert_owner(), assert_string); |
| 253 | EXPECT_DEATH_IF_SUPPORTED(cache_2->assert_owner(), assert_string); |
| 254 | #endif |
| 255 | |
| 256 | table_cache_manager.destroy(); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | Add a TABLE to a table cache and increment the reference count of |
| 261 | its TABLE_SHARE. |
| 262 | */ |
| 263 | void add_used_table(Table_cache *table_cache, THD *thd, TABLE *table) { |
| 264 | table->s->increment_ref_count(); |
| 265 | EXPECT_FALSE(table_cache->add_used_table(thd, table)); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | Test addition and removal of TABLE objects to/from the table cache. |
| 270 | */ |
| 271 | |
| 272 | TEST_F(TableCacheSingleCacheTest, CacheAddAndRemove) { |
| 273 | THD *thd = get_thd(0); |
| 274 | |
| 275 | Mock_share share_1("share_1"); |
| 276 | TABLE *table_1 = share_1.create_table(thd); |
| 277 | |
| 278 | Table_cache *table_cache = table_cache_manager.get_cache(thd); |
| 279 | table_cache->lock(); |
| 280 | add_used_table(table_cache, thd, table_1); |
| 281 | |
| 282 | // There should be one TABLE in the cache after we have added table_1. |
| 283 | EXPECT_EQ(1U, table_cache->cached_tables()); |
| 284 | |
| 285 | // There should be no unused TABLE objects for the same table in the |
| 286 | // cache. OTOH it should contain info about table share of table_1. |
| 287 | TABLE *table_2; |
| 288 | TABLE_SHARE *share_2; |
| 289 | table_2 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 290 | share_1.table_cache_key.length, &share_2); |
| 291 | EXPECT_TRUE(table_2 == nullptr); |
| 292 | EXPECT_TRUE(share_2 == &share_1); |
| 293 | |
| 294 | // Table_cache_iterator should be able to find only one TABLE instance |
| 295 | // in all caches. And this instance should be table_1. |
| 296 | Table_cache_iterator it(&share_1); |
| 297 | EXPECT_TRUE(it++ == table_1); |
| 298 | EXPECT_TRUE(it++ == nullptr); |
| 299 | |
| 300 | // We must be able to release TABLE into table cache and reuse it after |
| 301 | // this. |
| 302 | table_cache->release_table(thd, table_1); |
| 303 | table_2 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 304 | share_1.table_cache_key.length, &share_2); |
| 305 | EXPECT_TRUE(table_2 == table_1); |
| 306 | EXPECT_TRUE(share_2 == &share_1); |
| 307 | |
| 308 | table_cache->remove_table(table_1); |
| 309 | |
| 310 | // Once TABLE is removed from the cache the latter should become empty. |
| 311 | EXPECT_EQ(0U, table_cache->cached_tables()); |
| 312 | |
| 313 | table_2 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 314 | share_1.table_cache_key.length, &share_2); |
| 315 | EXPECT_TRUE(table_2 == nullptr); |
| 316 | EXPECT_TRUE(share_2 == nullptr); |
| 317 | |
| 318 | it.rewind(); |
| 319 | EXPECT_TRUE(it++ == nullptr); |
| 320 | |
| 321 | // Also it should be possible to remove unused TABLE from the cache |
| 322 | // Add TABLE instance and mark it as unused |
| 323 | add_used_table(table_cache, thd, table_1); |
| 324 | table_cache->release_table(thd, table_1); |
| 325 | |
| 326 | table_cache->remove_table(table_1); |
| 327 | |
| 328 | // Once TABLE is removed from cache the latter should become empty. |
| 329 | EXPECT_EQ(0U, table_cache->cached_tables()); |
| 330 | |
| 331 | table_2 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 332 | share_1.table_cache_key.length, &share_2); |
| 333 | EXPECT_TRUE(table_2 == nullptr); |
| 334 | EXPECT_TRUE(share_2 == nullptr); |
| 335 | |
| 336 | table_cache->unlock(); |
| 337 | |
| 338 | share_1.destroy_table(table_1); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | Now let us test how Table_cache handles overflows. |
| 343 | */ |
| 344 | |
| 345 | TEST_F(TableCacheSingleCacheTest, CacheOverflow) { |
| 346 | THD *thd = get_thd(0); |
| 347 | |
| 348 | // Set cache size low so it will overflow quickly. |
| 349 | table_cache_size_per_instance = 2; |
| 350 | |
| 351 | Mock_share share_1("share_1"); |
| 352 | Mock_share share_2("share_2"); |
| 353 | TABLE *table_1 = share_1.create_table(thd); |
| 354 | TABLE *table_2 = share_1.create_table(thd); |
| 355 | TABLE *table_3 = share_2.create_table(thd); |
| 356 | |
| 357 | Table_cache *table_cache = table_cache_manager.get_cache(thd); |
| 358 | |
| 359 | table_cache->lock(); |
| 360 | add_used_table(table_cache, thd, table_1); |
| 361 | add_used_table(table_cache, thd, table_2); |
| 362 | |
| 363 | // There should be two TABLE instances in the cache. |
| 364 | EXPECT_EQ(2U, table_cache->cached_tables()); |
| 365 | |
| 366 | table_cache->release_table(thd, table_1); |
| 367 | table_cache->release_table(thd, table_2); |
| 368 | |
| 369 | // Still there should be two TABLE instances in the cache. |
| 370 | EXPECT_EQ(2U, table_cache->cached_tables()); |
| 371 | |
| 372 | add_used_table(table_cache, thd, table_3); |
| 373 | |
| 374 | // One TABLE was added and one expelled (table_1), so still two TABLE objects. |
| 375 | EXPECT_EQ(2U, table_cache->cached_tables()); |
| 376 | |
| 377 | // Old value of table_1 points to garbage thanks to expelling |
| 378 | table_1 = share_1.create_table(thd); |
| 379 | add_used_table(table_cache, thd, table_1); |
| 380 | |
| 381 | // Still two TABLE instances (table_2 was expelled). |
| 382 | EXPECT_EQ(2U, table_cache->cached_tables()); |
| 383 | |
| 384 | // Old value of table_2 points to garbage thanks to expelling |
| 385 | table_2 = share_1.create_table(thd); |
| 386 | add_used_table(table_cache, thd, table_2); |
| 387 | |
| 388 | /* |
| 389 | Now we should have three TABLE instances in cache since all |
| 390 | of them are used. |
| 391 | */ |
| 392 | EXPECT_EQ(3U, table_cache->cached_tables()); |
| 393 | |
| 394 | table_cache->release_table(thd, table_2); |
| 395 | |
| 396 | // The first table that gets released is expelled. |
| 397 | EXPECT_EQ(2U, table_cache->cached_tables()); |
| 398 | |
| 399 | table_cache->remove_table(table_1); |
| 400 | table_cache->remove_table(table_3); |
| 401 | |
| 402 | // Cache should be empty after that |
| 403 | EXPECT_EQ(0U, table_cache->cached_tables()); |
| 404 | |
| 405 | table_cache->unlock(); |
| 406 | |
| 407 | share_1.destroy_table(table_1); |
| 408 | share_1.destroy_table(table_3); |
| 409 | } |
| 410 | |
| 411 | TEST_F(TableCacheSingleCacheTest, CacheGetAndRelease) { |
| 412 | THD *thd = get_thd(0); |
| 413 | |
| 414 | Table_cache *table_cache = table_cache_manager.get_cache(thd); |
| 415 | |
| 416 | table_cache->lock(); |
| 417 | |
| 418 | TABLE *table_1, *table_2, *table_3; |
| 419 | Mock_share share_1("share_1"), share_0("share_0"); |
| 420 | TABLE_SHARE *share_2; |
| 421 | |
| 422 | // There should be no TABLE in cache, nor information about share. |
| 423 | table_1 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 424 | share_1.table_cache_key.length, &share_2); |
| 425 | EXPECT_TRUE(table_1 == nullptr); |
| 426 | EXPECT_TRUE(share_2 == nullptr); |
| 427 | |
| 428 | table_1 = share_1.create_table(thd); |
| 429 | add_used_table(table_cache, thd, table_1); |
| 430 | |
| 431 | // There should be no unused TABLE in cache, but there should be |
| 432 | // information about the share. |
| 433 | table_2 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 434 | share_1.table_cache_key.length, &share_2); |
| 435 | EXPECT_TRUE(table_2 == nullptr); |
| 436 | EXPECT_TRUE(share_2 == &share_1); |
| 437 | |
| 438 | // There should be even no information about the share for which |
| 439 | // TABLE was not added to cache. |
| 440 | table_2 = table_cache->get_table(thd, share_0.table_cache_key.str, |
| 441 | share_0.table_cache_key.length, &share_2); |
| 442 | EXPECT_TRUE(table_2 == nullptr); |
| 443 | EXPECT_TRUE(share_2 == nullptr); |
| 444 | |
| 445 | table_2 = share_1.create_table(thd); |
| 446 | add_used_table(table_cache, thd, table_2); |
| 447 | |
| 448 | // Still there should be no unused TABLE in cache, but there should |
| 449 | // be information about the share. |
| 450 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 451 | share_1.table_cache_key.length, &share_2); |
| 452 | EXPECT_TRUE(table_3 == nullptr); |
| 453 | EXPECT_TRUE(share_2 == &share_1); |
| 454 | |
| 455 | table_cache->release_table(thd, table_1); |
| 456 | |
| 457 | // After releasing one of TABLE objects it should be possible to get |
| 458 | // unused TABLE from cache. |
| 459 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 460 | share_1.table_cache_key.length, &share_2); |
| 461 | EXPECT_TRUE(table_3 == table_1); |
| 462 | EXPECT_TRUE(share_2 == &share_1); |
| 463 | |
| 464 | // But only once! |
| 465 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 466 | share_1.table_cache_key.length, &share_2); |
| 467 | EXPECT_TRUE(table_3 == nullptr); |
| 468 | EXPECT_TRUE(share_2 == &share_1); |
| 469 | |
| 470 | // After releasing of both TABLE objects it should be possible to |
| 471 | // get two unused TABLE objects from cache (for 'share_1'). |
| 472 | // There should be nothing for 'share_0'. |
| 473 | table_cache->release_table(thd, table_1); |
| 474 | table_cache->release_table(thd, table_2); |
| 475 | |
| 476 | table_3 = table_cache->get_table(thd, share_0.table_cache_key.str, |
| 477 | share_0.table_cache_key.length, &share_2); |
| 478 | EXPECT_TRUE(table_3 == nullptr); |
| 479 | EXPECT_TRUE(share_2 == nullptr); |
| 480 | |
| 481 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 482 | share_1.table_cache_key.length, &share_2); |
| 483 | EXPECT_TRUE(table_3 != nullptr); |
| 484 | EXPECT_TRUE(share_2 == &share_1); |
| 485 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 486 | share_1.table_cache_key.length, &share_2); |
| 487 | EXPECT_TRUE(table_3 != nullptr); |
| 488 | EXPECT_TRUE(share_2 == &share_1); |
| 489 | table_3 = table_cache->get_table(thd, share_1.table_cache_key.str, |
| 490 | share_1.table_cache_key.length, &share_2); |
| 491 | EXPECT_TRUE(table_3 == nullptr); |
| 492 | EXPECT_TRUE(share_2 == &share_1); |
| 493 | |
| 494 | // Clean-up |
| 495 | table_cache->remove_table(table_1); |
| 496 | table_cache->remove_table(table_2); |
| 497 | |
| 498 | share_1.destroy_table(table_1); |
| 499 | share_1.destroy_table(table_2); |
| 500 | |
| 501 | table_cache->unlock(); |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | Test for Table_cache_manager/Table_cache::free_all_unused_tables(). |
| 506 | */ |
| 507 | |
| 508 | TEST_F(TableCacheDoubleCacheTest, ManagerFreeAllUnused) { |
| 509 | THD *thd_1 = get_thd(0); |
| 510 | THD *thd_2 = get_thd(1); |
| 511 | |
| 512 | Table_cache *table_cache_1 = table_cache_manager.get_cache(thd_1); |
| 513 | Table_cache *table_cache_2 = table_cache_manager.get_cache(thd_2); |
| 514 | |
| 515 | // There should be no TABLE instances in all cachea. |
| 516 | EXPECT_EQ(0U, table_cache_manager.cached_tables()); |
| 517 | |
| 518 | Mock_share share_1("share_1"); |
| 519 | Mock_share share_2("share_2"); |
| 520 | Mock_share share_3("share_2"); |
| 521 | TABLE *table_1 = share_1.create_table(thd_1); |
| 522 | TABLE *table_2 = share_1.create_table(thd_1); |
| 523 | TABLE *table_3 = share_2.create_table(thd_1); |
| 524 | TABLE *table_4 = share_2.create_table(thd_1); |
| 525 | TABLE *table_5 = share_1.create_table(thd_2); |
| 526 | TABLE *table_6 = share_3.create_table(thd_2); |
| 527 | |
| 528 | table_cache_manager.lock_all_and_tdc(); |
| 529 | |
| 530 | add_used_table(table_cache_1, thd_1, table_1); |
| 531 | add_used_table(table_cache_1, thd_1, table_2); |
| 532 | add_used_table(table_cache_1, thd_1, table_3); |
| 533 | add_used_table(table_cache_1, thd_1, table_4); |
| 534 | add_used_table(table_cache_2, thd_2, table_5); |
| 535 | add_used_table(table_cache_2, thd_2, table_6); |
| 536 | |
| 537 | EXPECT_EQ(4U, table_cache_1->cached_tables()); |
| 538 | EXPECT_EQ(2U, table_cache_2->cached_tables()); |
| 539 | EXPECT_EQ(6U, table_cache_manager.cached_tables()); |
| 540 | |
| 541 | table_cache_manager.free_all_unused_tables(); |
| 542 | |
| 543 | // All TABLE instances should stay around in caches as |
| 544 | // all of them are used. |
| 545 | EXPECT_EQ(4U, table_cache_1->cached_tables()); |
| 546 | EXPECT_EQ(2U, table_cache_2->cached_tables()); |
| 547 | EXPECT_EQ(6U, table_cache_manager.cached_tables()); |
| 548 | |
| 549 | table_cache_1->release_table(thd_1, table_1); |
| 550 | |
| 551 | table_cache_manager.free_all_unused_tables(); |
| 552 | |
| 553 | // One table should be freed. So there should be 3 + 2 TABLE instances. |
| 554 | EXPECT_EQ(3U, table_cache_1->cached_tables()); |
| 555 | EXPECT_EQ(2U, table_cache_2->cached_tables()); |
| 556 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 557 | |
| 558 | table_cache_1->release_table(thd_1, table_2); |
| 559 | table_cache_1->release_table(thd_1, table_3); |
| 560 | table_cache_2->release_table(thd_2, table_5); |
| 561 | |
| 562 | table_cache_manager.free_all_unused_tables(); |
| 563 | |
| 564 | // Now there should be 1 + 1 used TABLE instances left. |
| 565 | EXPECT_EQ(1U, table_cache_1->cached_tables()); |
| 566 | EXPECT_EQ(1U, table_cache_2->cached_tables()); |
| 567 | EXPECT_EQ(2U, table_cache_manager.cached_tables()); |
| 568 | |
| 569 | table_cache_1->release_table(thd_1, table_4); |
| 570 | |
| 571 | table_cache_manager.free_all_unused_tables(); |
| 572 | |
| 573 | // There should be 0 + 1 TABLE instances around. |
| 574 | EXPECT_EQ(0U, table_cache_1->cached_tables()); |
| 575 | EXPECT_EQ(1U, table_cache_2->cached_tables()); |
| 576 | EXPECT_EQ(1U, table_cache_manager.cached_tables()); |
| 577 | |
| 578 | table_cache_2->release_table(thd_2, table_6); |
| 579 | |
| 580 | table_cache_manager.free_all_unused_tables(); |
| 581 | |
| 582 | // All caches should become empty. |
| 583 | EXPECT_EQ(0U, table_cache_1->cached_tables()); |
| 584 | EXPECT_EQ(0U, table_cache_2->cached_tables()); |
| 585 | EXPECT_EQ(0U, table_cache_manager.cached_tables()); |
| 586 | |
| 587 | table_cache_manager.unlock_all_and_tdc(); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | Test for Table_cache_manager/Table_cache::cached_tables(). |
| 592 | */ |
| 593 | |
| 594 | TEST_F(TableCacheDoubleCacheTest, ManagerCachedTables) { |
| 595 | THD *thd_1 = get_thd(0); |
| 596 | THD *thd_2 = get_thd(1); |
| 597 | |
| 598 | Table_cache *table_cache_1 = table_cache_manager.get_cache(thd_1); |
| 599 | Table_cache *table_cache_2 = table_cache_manager.get_cache(thd_2); |
| 600 | |
| 601 | // There should be no TABLE instances in all cachea. |
| 602 | EXPECT_EQ(0U, table_cache_1->cached_tables()); |
| 603 | EXPECT_EQ(0U, table_cache_2->cached_tables()); |
| 604 | EXPECT_EQ(0U, table_cache_manager.cached_tables()); |
| 605 | |
| 606 | Mock_share share_1("share_1"); |
| 607 | Mock_share share_2("share_2"); |
| 608 | TABLE *table_1 = share_1.create_table(thd_1); |
| 609 | TABLE *table_2 = share_1.create_table(thd_1); |
| 610 | TABLE *table_3 = share_2.create_table(thd_1); |
| 611 | TABLE *table_4 = share_1.create_table(thd_2); |
| 612 | TABLE *table_5 = share_2.create_table(thd_2); |
| 613 | |
| 614 | table_cache_manager.lock_all_and_tdc(); |
| 615 | |
| 616 | add_used_table(table_cache_1, thd_1, table_1); |
| 617 | add_used_table(table_cache_1, thd_1, table_2); |
| 618 | add_used_table(table_cache_1, thd_1, table_3); |
| 619 | |
| 620 | // There should be 3 + 0 TABLE objects in cache |
| 621 | EXPECT_EQ(3U, table_cache_1->cached_tables()); |
| 622 | EXPECT_EQ(0U, table_cache_2->cached_tables()); |
| 623 | EXPECT_EQ(3U, table_cache_manager.cached_tables()); |
| 624 | |
| 625 | add_used_table(table_cache_2, thd_2, table_4); |
| 626 | add_used_table(table_cache_2, thd_2, table_5); |
| 627 | |
| 628 | // There should be 3 + 2 TABLE objects in cache |
| 629 | EXPECT_EQ(3U, table_cache_1->cached_tables()); |
| 630 | EXPECT_EQ(2U, table_cache_2->cached_tables()); |
| 631 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 632 | |
| 633 | table_cache_1->release_table(thd_1, table_1); |
| 634 | table_cache_2->release_table(thd_2, table_4); |
| 635 | |
| 636 | // There should be the same number of TABLE objects - 3 + 2 |
| 637 | EXPECT_EQ(3U, table_cache_1->cached_tables()); |
| 638 | EXPECT_EQ(2U, table_cache_2->cached_tables()); |
| 639 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 640 | |
| 641 | table_cache_2->remove_table(table_5); |
| 642 | |
| 643 | // There should be 3 + 1 TABLE objects in cache |
| 644 | EXPECT_EQ(3U, table_cache_1->cached_tables()); |
| 645 | EXPECT_EQ(1U, table_cache_2->cached_tables()); |
| 646 | EXPECT_EQ(4U, table_cache_manager.cached_tables()); |
| 647 | |
| 648 | table_cache_1->remove_table(table_1); |
| 649 | table_cache_2->remove_table(table_4); |
| 650 | |
| 651 | // There should be 2 + 0 TABLE objects in cache |
| 652 | EXPECT_EQ(2U, table_cache_1->cached_tables()); |
| 653 | EXPECT_EQ(0U, table_cache_2->cached_tables()); |
| 654 | EXPECT_EQ(2U, table_cache_manager.cached_tables()); |
| 655 | |
| 656 | table_cache_1->remove_table(table_2); |
| 657 | table_cache_1->remove_table(table_3); |
| 658 | |
| 659 | // Caches should be empty |
| 660 | EXPECT_EQ(0U, table_cache_1->cached_tables()); |
| 661 | EXPECT_EQ(0U, table_cache_2->cached_tables()); |
| 662 | EXPECT_EQ(0U, table_cache_manager.cached_tables()); |
| 663 | |
| 664 | table_cache_manager.unlock_all_and_tdc(); |
| 665 | |
| 666 | share_1.destroy_table(table_1); |
| 667 | share_1.destroy_table(table_2); |
| 668 | share_2.destroy_table(table_3); |
| 669 | share_1.destroy_table(table_4); |
| 670 | share_2.destroy_table(table_5); |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | Coverage for lock and unlock methods of Table_cache_manager class. |
| 675 | */ |
| 676 | |
| 677 | TEST_F(TableCacheDoubleCacheDeathTest, ManagerLockAndUnlock) { |
| 678 | // Nor caches nor LOCK_open should not be locked after initialization |
| 679 | #ifdef SAFE_MUTEX |
| 680 | EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.assert_owner_all(), |
| 681 | assert_string); |
| 682 | EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.assert_owner_all_and_tdc(), |
| 683 | assert_string); |
| 684 | #endif |
| 685 | |
| 686 | // And get locked after we call its lock_all_and_tdc() method. |
| 687 | table_cache_manager.lock_all_and_tdc(); |
| 688 | table_cache_manager.assert_owner_all(); |
| 689 | table_cache_manager.assert_owner_all_and_tdc(); |
| 690 | |
| 691 | // In addition to Table_cache_manager method we check this by |
| 692 | // calling Table_cache methods and asserting state of LOCK_open. |
| 693 | Table_cache *cache_1 = table_cache_manager.get_cache(get_thd(0)); |
| 694 | Table_cache *cache_2 = table_cache_manager.get_cache(get_thd(1)); |
| 695 | |
| 696 | cache_1->assert_owner(); |
| 697 | cache_2->assert_owner(); |
| 698 | mysql_mutex_assert_owner(&LOCK_open); |
| 699 | |
| 700 | // Locks should be unlocked after we call unlock method |
| 701 | table_cache_manager.unlock_all_and_tdc(); |
| 702 | |
| 703 | #ifdef SAFE_MUTEX |
| 704 | EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.assert_owner_all(), |
| 705 | assert_string); |
| 706 | EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.assert_owner_all_and_tdc(), |
| 707 | assert_string); |
| 708 | #endif |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | Coverage for Table_cache_manager::free_table(); |
| 713 | */ |
| 714 | |
| 715 | TEST_F(TableCacheDoubleCacheDeathTest, ManagerFreeTable) { |
| 716 | THD *thd_1 = get_thd(0); |
| 717 | THD *thd_2 = get_thd(1); |
| 718 | |
| 719 | Table_cache *table_cache_1 = table_cache_manager.get_cache(thd_1); |
| 720 | Table_cache *table_cache_2 = table_cache_manager.get_cache(thd_2); |
| 721 | |
| 722 | Mock_share share_1("share_1"); |
| 723 | Mock_share share_2("share_2"); |
| 724 | TABLE *table_1 = share_1.create_table(thd_1); |
| 725 | TABLE *table_2 = share_1.create_table(thd_1); |
| 726 | TABLE *table_3 = share_2.create_table(thd_1); |
| 727 | TABLE *table_4 = share_1.create_table(thd_2); |
| 728 | TABLE *table_5 = share_2.create_table(thd_2); |
| 729 | |
| 730 | table_cache_manager.lock_all_and_tdc(); |
| 731 | |
| 732 | /* |
| 733 | Coverage for TDC_RT_REMOVE_ALL case. |
| 734 | */ |
| 735 | add_used_table(table_cache_1, thd_1, table_1); |
| 736 | add_used_table(table_cache_1, thd_1, table_2); |
| 737 | table_cache_1->release_table(thd_1, table_2); |
| 738 | add_used_table(table_cache_1, thd_1, table_3); |
| 739 | add_used_table(table_cache_2, thd_2, table_4); |
| 740 | add_used_table(table_cache_2, thd_2, table_5); |
| 741 | |
| 742 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 743 | |
| 744 | // Added three tables for share_1 and two tables for share_2. The |
| 745 | // reference count should be one higher due to Mock_share's |
| 746 | // constructor setting it to 1. |
| 747 | EXPECT_EQ(4U, share_1.ref_count()); |
| 748 | EXPECT_EQ(3U, share_2.ref_count()); |
| 749 | |
| 750 | // There should be assert failure since we are trying |
| 751 | // to free all tables for share_1, while some tables |
| 752 | // are in use. |
| 753 | #ifndef NDEBUG |
| 754 | EXPECT_DEATH_IF_SUPPORTED( |
| 755 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_ALL, &share_1), |
| 756 | ".*Assertion.*is_empty.*"); |
| 757 | #endif |
| 758 | |
| 759 | table_cache_1->release_table(thd_1, table_1); |
| 760 | table_cache_2->release_table(thd_2, table_4); |
| 761 | |
| 762 | // After all tables for share_1 marked as unused freeing |
| 763 | // all tables should succeed. |
| 764 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_ALL, &share_1); |
| 765 | |
| 766 | // After all the tables for share_1 are freed, the reference count |
| 767 | // should go down to 1. Not to 0, since Mock_share sets it to 1 in |
| 768 | // its constructor. |
| 769 | EXPECT_EQ(1U, share_1.ref_count()); |
| 770 | |
| 771 | // We still should have 2 TABLE objects for share_2. |
| 772 | EXPECT_EQ(2U, table_cache_manager.cached_tables()); |
| 773 | |
| 774 | /* |
| 775 | Coverage for TDC_RT_REMOVE_NOT_OWN case. |
| 776 | */ |
| 777 | table_1 = share_1.create_table(thd_1); |
| 778 | table_2 = share_1.create_table(thd_1); |
| 779 | table_4 = share_1.create_table(thd_2); |
| 780 | |
| 781 | add_used_table(table_cache_1, thd_1, table_1); |
| 782 | add_used_table(table_cache_1, thd_1, table_2); |
| 783 | table_cache_1->release_table(thd_1, table_2); |
| 784 | add_used_table(table_cache_2, thd_2, table_4); |
| 785 | |
| 786 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 787 | |
| 788 | // There should be assert failure since we are trying |
| 789 | // to free all not own TABLEs for share_1, while thd_2 |
| 790 | // has a TABLE object for it in used |
| 791 | #ifndef NDEBUG |
| 792 | EXPECT_DEATH_IF_SUPPORTED( |
| 793 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_NOT_OWN, &share_1), |
| 794 | ".*Assertion.*0.*"); |
| 795 | #endif |
| 796 | |
| 797 | table_cache_2->release_table(thd_2, table_4); |
| 798 | |
| 799 | // After TABLE owned by thd_2 is marked as unused, the below |
| 800 | // call should succeed. |
| 801 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_NOT_OWN, &share_1); |
| 802 | |
| 803 | // We still have 1 TABLE object for share_1 in thd_1 and |
| 804 | // 2 TABLE objects for share_2. |
| 805 | EXPECT_EQ(3U, table_cache_manager.cached_tables()); |
| 806 | |
| 807 | /* |
| 808 | Coverage for TDC_RT_REMOVE_UNUSED case. |
| 809 | */ |
| 810 | table_2 = share_1.create_table(thd_1); |
| 811 | table_4 = share_1.create_table(thd_2); |
| 812 | |
| 813 | add_used_table(table_cache_1, thd_1, table_2); |
| 814 | table_cache_1->release_table(thd_1, table_2); |
| 815 | add_used_table(table_cache_2, thd_2, table_4); |
| 816 | |
| 817 | EXPECT_EQ(5U, table_cache_manager.cached_tables()); |
| 818 | |
| 819 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_UNUSED, &share_1); |
| 820 | |
| 821 | // The above call should have been freed only 1 table. |
| 822 | EXPECT_EQ(4U, table_cache_manager.cached_tables()); |
| 823 | |
| 824 | // Mark all remaining TABLE objects for share_1 as unused |
| 825 | table_cache_1->release_table(thd_1, table_1); |
| 826 | table_cache_2->release_table(thd_2, table_4); |
| 827 | |
| 828 | table_cache_manager.free_table(thd_1, TDC_RT_REMOVE_UNUSED, &share_1); |
| 829 | |
| 830 | // The above call should free all unused TABLE objects for share_1. |
| 831 | // Therefore only 2 objects for share_2 should be remaining |
| 832 | EXPECT_EQ(2U, table_cache_manager.cached_tables()); |
| 833 | |
| 834 | // Clean-up. |
| 835 | table_cache_1->remove_table(table_3); |
| 836 | table_cache_2->remove_table(table_5); |
| 837 | |
| 838 | share_2.destroy_table(table_3); |
| 839 | share_2.destroy_table(table_5); |
| 840 | |
| 841 | table_cache_manager.unlock_all_and_tdc(); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | Coverage for Table_cache_iterator |
| 846 | */ |
| 847 | |
| 848 | TEST_F(TableCacheDoubleCacheTest, Iterator) { |
| 849 | THD *thd_1 = get_thd(0); |
| 850 | THD *thd_2 = get_thd(1); |
| 851 | |
| 852 | table_cache_manager.lock_all_and_tdc(); |
| 853 | |
| 854 | Mock_share share_1("share_1"); |
| 855 | Mock_share share_2("share_2"); |
| 856 | |
| 857 | // There is no TABLE objects for share_1 so the below iterator |
| 858 | // should not find anything. |
| 859 | Table_cache_iterator it(&share_1); |
| 860 | EXPECT_TRUE(it++ == nullptr); |
| 861 | // Attempt to iterate behind the end should not give anything. |
| 862 | EXPECT_TRUE(it++ == nullptr); |
| 863 | |
| 864 | Table_cache *table_cache_1 = table_cache_manager.get_cache(thd_1); |
| 865 | Table_cache *table_cache_2 = table_cache_manager.get_cache(thd_2); |
| 866 | TABLE *table_1 = share_1.create_table(thd_1); |
| 867 | TABLE *table_2 = share_1.create_table(thd_1); |
| 868 | TABLE *table_3 = share_2.create_table(thd_1); |
| 869 | TABLE *table_4 = share_1.create_table(thd_2); |
| 870 | TABLE *table_5 = share_2.create_table(thd_2); |
| 871 | |
| 872 | add_used_table(table_cache_2, thd_2, table_4); |
| 873 | |
| 874 | // Now the iterato should see table_4. |
| 875 | it.rewind(); |
| 876 | TABLE *table_r1 = it++; |
| 877 | EXPECT_TRUE(table_r1 == table_4); |
| 878 | // But only it. |
| 879 | EXPECT_TRUE(it++ == nullptr); |
| 880 | EXPECT_TRUE(it++ == nullptr); |
| 881 | |
| 882 | add_used_table(table_cache_1, thd_1, table_1); |
| 883 | |
| 884 | // Now we should see two tables: |
| 885 | it.rewind(); |
| 886 | table_r1 = it++; |
| 887 | EXPECT_TRUE(table_r1 != nullptr); |
| 888 | TABLE *table_r2 = it++; |
| 889 | EXPECT_TRUE(table_r2 != nullptr); |
| 890 | EXPECT_TRUE(table_r1 != table_r2); |
| 891 | EXPECT_TRUE(it++ == nullptr); |
| 892 | EXPECT_TRUE(it++ == nullptr); |
| 893 | |
| 894 | add_used_table(table_cache_1, thd_1, table_2); |
| 895 | |
| 896 | // And now three ! |
| 897 | it.rewind(); |
| 898 | table_r1 = it++; |
| 899 | EXPECT_TRUE(table_r1 != nullptr); |
| 900 | table_r2 = it++; |
| 901 | EXPECT_TRUE(table_r2 != nullptr); |
| 902 | TABLE *table_r3 = it++; |
| 903 | EXPECT_TRUE(table_r3 != nullptr); |
| 904 | EXPECT_TRUE(table_r1 != table_r2 && table_r1 != table_r3 && |
| 905 | table_r2 != table_r3); |
| 906 | EXPECT_TRUE(it++ == nullptr); |
| 907 | EXPECT_TRUE(it++ == nullptr); |
| 908 | |
| 909 | table_cache_1->release_table(thd_1, table_1); |
| 910 | |
| 911 | // We should be seeing only used TABLE objects, so two tables now |
| 912 | it.rewind(); |
| 913 | table_r1 = it++; |
| 914 | EXPECT_TRUE(table_r1 != nullptr); |
| 915 | table_r2 = it++; |
| 916 | EXPECT_TRUE(table_r2 != nullptr); |
| 917 | EXPECT_TRUE(table_r1 != table_r2); |
| 918 | EXPECT_TRUE(it++ == nullptr); |
| 919 | EXPECT_TRUE(it++ == nullptr); |
| 920 | |
| 921 | add_used_table(table_cache_1, thd_1, table_3); |
| 922 | add_used_table(table_cache_2, thd_2, table_5); |
| 923 | |
| 924 | // We also should not be seeing TABLE objects for share_2 |
| 925 | it.rewind(); |
| 926 | table_r1 = it++; |
| 927 | EXPECT_TRUE(table_r1 != nullptr); |
| 928 | table_r2 = it++; |
| 929 | EXPECT_TRUE(table_r2 != nullptr); |
| 930 | EXPECT_TRUE(table_r1 != table_r2); |
| 931 | EXPECT_TRUE(it++ == nullptr); |
| 932 | EXPECT_TRUE(it++ == nullptr); |
| 933 | |
| 934 | table_cache_1->remove_table(table_2); |
| 935 | |
| 936 | // Now we should se only one used TABLE |
| 937 | it.rewind(); |
| 938 | table_r1 = it++; |
| 939 | EXPECT_TRUE(table_r1 == table_4); |
| 940 | EXPECT_TRUE(it++ == nullptr); |
| 941 | EXPECT_TRUE(it++ == nullptr); |
| 942 | |
| 943 | table_cache_1->remove_table(table_4); |
| 944 | |
| 945 | // And now no used TABLE objects for share_1 at all |
| 946 | it.rewind(); |
| 947 | EXPECT_TRUE(it++ == nullptr); |
| 948 | EXPECT_TRUE(it++ == nullptr); |
| 949 | |
| 950 | table_cache_1->remove_table(table_1); |
| 951 | |
| 952 | // Still the same |
| 953 | it.rewind(); |
| 954 | EXPECT_TRUE(it++ == nullptr); |
| 955 | EXPECT_TRUE(it++ == nullptr); |
| 956 | |
| 957 | table_cache_1->remove_table(table_3); |
| 958 | table_cache_2->remove_table(table_5); |
| 959 | |
| 960 | // Cache is empty so iterator should not show any TABLE objects. |
| 961 | it.rewind(); |
| 962 | EXPECT_TRUE(it++ == nullptr); |
| 963 | EXPECT_TRUE(it++ == nullptr); |
| 964 | |
| 965 | table_cache_manager.unlock_all_and_tdc(); |
| 966 | |
| 967 | share_1.destroy_table(table_1); |
| 968 | share_1.destroy_table(table_2); |
| 969 | share_2.destroy_table(table_3); |
| 970 | share_1.destroy_table(table_4); |
| 971 | share_2.destroy_table(table_5); |
| 972 | } |
| 973 | |
| 974 | } // namespace table_cache_unittest |