| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify it under |
| 6 | the terms of the GNU General Public License, version 2.0, as published by the |
| 7 | Free Software Foundation. |
| 8 | |
| 9 | This program is designed to work with certain software (including |
| 10 | but not limited to OpenSSL) that is licensed under separate terms, |
| 11 | as designated in a particular file or component or in included license |
| 12 | documentation. The authors of MySQL hereby grant you an additional |
| 13 | permission to link the program and your derivative works with the |
| 14 | separately licensed software that they have either included with |
| 15 | the program or referenced in the documentation. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 | for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License along with |
| 23 | this program; if not, write to the Free Software Foundation, Inc., |
| 24 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | |
| 26 | *****************************************************************************/ |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | #include <random> |
| 30 | #include <thread> |
| 31 | #include "row0mysql.h" |
| 32 | #include "sql_class.h" |
| 33 | #include "srv0conc.h" |
| 34 | |
| 35 | /** RAII wrapper for the mock prebuilt object, avoids memory management stuff in |
| 36 | * actual tests. */ |
| 37 | class prebuilt_guard { |
| 38 | public: |
| 39 | prebuilt_guard() : thd(false) { |
| 40 | ptr = static_cast<row_prebuilt_t *>(malloc(sizeof(*ptr))); |
| 41 | ptr->trx = &trx; |
| 42 | ptr->trx->mysql_thd = &thd; |
| 43 | } |
| 44 | ~prebuilt_guard() { free(ptr); } |
| 45 | |
| 46 | public: |
| 47 | row_prebuilt_t *ptr; |
| 48 | |
| 49 | private: |
| 50 | THD thd; |
| 51 | trx_t trx{}; |
| 52 | }; |
| 53 | |
| 54 | void run_threads(std::function<void()> thread, int count) { |
| 55 | std::vector<std::thread> threads; |
| 56 | |
| 57 | for (int i = 0; i < count; ++i) { |
| 58 | threads.push_back(std::thread(thread)); |
| 59 | } |
| 60 | |
| 61 | for (auto &t : threads) { |
| 62 | t.join(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** This is a user thread simulation which performs an enter/sleep/exit multiple |
| 67 | * times. The thread will sleep for a random duration in [1-MaxSleep] |
| 68 | * microseconds. It will repeat this multiple times, controlled by Iterations |
| 69 | */ |
| 70 | template <int Iterations, int MaxSleep> |
| 71 | void user_thread_simulation() { |
| 72 | prebuilt_guard prebuilt_guard; |
| 73 | auto prebuilt = prebuilt_guard.ptr; |
| 74 | |
| 75 | std::random_device rd; |
| 76 | std::mt19937 gen(rd()); |
| 77 | std::uniform_int_distribution<> distr(1, MaxSleep); |
| 78 | |
| 79 | for (int i = 0; i < Iterations; ++i) { |
| 80 | srv_conc_enter_innodb(prebuilt); |
| 81 | std::this_thread::sleep_for(std::chrono::microseconds(distr(gen))); |
| 82 | srv_conc_force_exit_innodb(prebuilt->trx); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | class srv0conc : public ::testing::Test { |
| 87 | protected: |
| 88 | static void TearDownTestCase() { srv_thread_concurrency = 0; } |
| 89 | }; |
| 90 | |
| 91 | TEST_F(srv0conc, no_concurrency_limit) { |
| 92 | const int THREADS = std::thread::hardware_concurrency(); |
| 93 | run_threads(user_thread_simulation<100, 100>, THREADS); |
| 94 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 95 | EXPECT_EQ(srv_conc_get_active_threads(), 0); |
| 96 | } |
| 97 | |
| 98 | TEST_F(srv0conc, concurrency_limit_equals_hw_threads) { |
| 99 | const int THREADS = std::thread::hardware_concurrency(); |
| 100 | srv_thread_concurrency = THREADS; |
| 101 | run_threads(user_thread_simulation<100, 100>, THREADS); |
| 102 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 103 | EXPECT_EQ(srv_conc_get_active_threads(), 0); |
| 104 | } |
| 105 | |
| 106 | TEST_F(srv0conc, concurrency_limit_half_hw_threads) { |
| 107 | const int THREADS = std::thread::hardware_concurrency(); |
| 108 | srv_thread_concurrency = THREADS / 2; |
| 109 | run_threads(user_thread_simulation<100, 100>, THREADS); |
| 110 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 111 | EXPECT_EQ(srv_conc_get_active_threads(), 0); |
| 112 | } |
| 113 | |
| 114 | TEST_F(srv0conc, concurrency_limit_2) { |
| 115 | const int THREADS = std::thread::hardware_concurrency(); |
| 116 | srv_thread_concurrency = 2; |
| 117 | run_threads(user_thread_simulation<100, 100>, THREADS); |
| 118 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 119 | EXPECT_EQ(srv_conc_get_active_threads(), 0); |
| 120 | } |
| 121 | |
| 122 | TEST_F(srv0conc, concurrency_limit_1) { |
| 123 | const int THREADS = std::thread::hardware_concurrency(); |
| 124 | srv_thread_concurrency = 1; |
| 125 | run_threads(user_thread_simulation<100, 100>, THREADS); |
| 126 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 127 | EXPECT_EQ(srv_conc_get_active_threads(), 0); |
| 128 | } |
| 129 | |
| 130 | /* This test case simulates the situation where the transaction is interrupted |
| 131 | while waiting for n_active threads to drop below the concurrency limit. */ |
| 132 | TEST_F(srv0conc, trx_interrupted) { |
| 133 | srv_thread_concurrency = 1; |
| 134 | prebuilt_guard active_prebuilt_guard; |
| 135 | prebuilt_guard interrupted_prebuilt_guard; |
| 136 | auto active_prebuilt = active_prebuilt_guard.ptr; |
| 137 | auto interrupted_prebuilt = interrupted_prebuilt_guard.ptr; |
| 138 | |
| 139 | /* Active trx enters InnoDB without issues. */ |
| 140 | srv_conc_enter_innodb(active_prebuilt); |
| 141 | EXPECT_TRUE(active_prebuilt->trx->declared_to_be_inside_innodb); |
| 142 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 143 | EXPECT_EQ(srv_conc_get_active_threads(), 1); |
| 144 | |
| 145 | /* Set up the mock trx in such way it simulates "interrupted" state */ |
| 146 | interrupted_prebuilt->trx->mysql_thd->killed = |
| 147 | THD::killed_state::KILL_CONNECTION; |
| 148 | |
| 149 | /* Thread fails to enter InnoDB, number of waiters must remain 0, and number |
| 150 | of active is 1 */ |
| 151 | srv_conc_enter_innodb(interrupted_prebuilt); |
| 152 | EXPECT_TRUE(active_prebuilt->trx->declared_to_be_inside_innodb); |
| 153 | EXPECT_FALSE(interrupted_prebuilt->trx->declared_to_be_inside_innodb); |
| 154 | EXPECT_EQ(srv_conc_get_waiting_threads(), 0); |
| 155 | EXPECT_EQ(srv_conc_get_active_threads(), 1); |
| 156 | } |