| 1 | /* Copyright (c) 2013, 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <stddef.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include "my_inttypes.h" |
| 31 | #include "my_io.h" |
| 32 | #include "sql/handler.h" |
| 33 | #include "sql/mysqld.h" |
| 34 | #include "sql/sql_class.h" |
| 35 | #include "sql/tc_log.h" |
| 36 | #include "sql/transaction_info.h" |
| 37 | #include "unittest/gunit/test_utils.h" |
| 38 | #include "unittest/gunit/thread_utils.h" |
| 39 | |
| 40 | #ifdef _WIN32 |
| 41 | #include <process.h> // getpid |
| 42 | #endif |
| 43 | |
| 44 | using my_testing::Server_initializer; |
| 45 | |
| 46 | /** |
| 47 | Override msync/fsync, saves a *lot* of time during unit testing. |
| 48 | */ |
| 49 | |
| 50 | class TC_LOG_MMAP_no_msync : public TC_LOG_MMAP { |
| 51 | protected: |
| 52 | int do_msync_and_fsync(int, void *, size_t, int) override { return 0; } |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | This class is a friend of TC_LOG_MMAP, so it needs to be outside the unittest |
| 57 | namespace. |
| 58 | */ |
| 59 | |
| 60 | class TCLogMMapTest : public ::testing::Test { |
| 61 | public: |
| 62 | TCLogMMapTest() : tc_log_mmap(nullptr) {} |
| 63 | |
| 64 | void SetUp() override { |
| 65 | initializer.SetUp(); |
| 66 | total_ha_2pc = 2; |
| 67 | tc_heuristic_recover = TC_HEURISTIC_NOT_USED; |
| 68 | /* |
| 69 | Assign a transaction coordinator object to am |
| 70 | instance of TCLogMMapTest. This transaction coordinator |
| 71 | is shared among all threads are run. |
| 72 | */ |
| 73 | |
| 74 | tc_log_mmap = new TC_LOG_MMAP_no_msync(); |
| 75 | // Make a slightly randomized name for the file, |
| 76 | // to avoid recovery from other runs. |
| 77 | char namebuff[FN_REFLEN]; |
| 78 | snprintf(namebuff, FN_REFLEN, "tc_log_mmap_test_%d", |
| 79 | static_cast<int>(getpid())); |
| 80 | ASSERT_EQ(0, tc_log_mmap->open(namebuff)); |
| 81 | } |
| 82 | |
| 83 | void TearDown() override { |
| 84 | tc_log_mmap->close(); |
| 85 | delete tc_log_mmap; |
| 86 | initializer.TearDown(); |
| 87 | } |
| 88 | |
| 89 | THD *thd() { return initializer.thd(); } |
| 90 | |
| 91 | /** |
| 92 | Run test case in single threaded environment. |
| 93 | This method uses THD value hold by the initializer data member. |
| 94 | */ |
| 95 | |
| 96 | void testCommit(ulonglong xid) { testCommit(xid, thd()); } |
| 97 | |
| 98 | /** |
| 99 | Run a test case with the THD supplied outside. This method is used |
| 100 | when there are several threads running the same test case. In this case |
| 101 | we need a dedicated THD object for every thread in order not to hit |
| 102 | upon the wrong condition when two threads free a memroot (indirectly by |
| 103 | calling thd->get_transaction()->cleanup()) for the same THD object. |
| 104 | */ |
| 105 | |
| 106 | void testCommit(ulonglong xid, THD *thd_val) { |
| 107 | XID_STATE *xid_state = thd_val->get_transaction()->xid_state(); |
| 108 | xid_state->set_query_id(xid); |
| 109 | EXPECT_EQ(TC_LOG_MMAP::RESULT_SUCCESS, tc_log_mmap->commit(thd_val, true)); |
| 110 | thd_val->get_transaction()->cleanup(); |
| 111 | } |
| 112 | |
| 113 | ulong testLog(ulonglong xid) { return tc_log_mmap->log_xid(xid); } |
| 114 | |
| 115 | void testUnlog(ulong cookie, ulonglong xid) { |
| 116 | tc_log_mmap->unlog(cookie, xid); |
| 117 | } |
| 118 | |
| 119 | protected: |
| 120 | TC_LOG_MMAP_no_msync *tc_log_mmap; |
| 121 | Server_initializer initializer; |
| 122 | }; |
| 123 | |
| 124 | namespace tc_log_mmap_unittest { |
| 125 | |
| 126 | TEST_F(TCLogMMapTest, TClogCommit) { |
| 127 | // test calling of log/unlog for xid=1 |
| 128 | testCommit(1); |
| 129 | } |
| 130 | |
| 131 | class TC_Log_MMap_thread : public thread::Thread { |
| 132 | public: |
| 133 | TC_Log_MMap_thread() |
| 134 | : m_start_xid(0), |
| 135 | m_end_xid(0), |
| 136 | m_tc_log_mmap(nullptr), |
| 137 | initializer(nullptr) {} |
| 138 | |
| 139 | ~TC_Log_MMap_thread() override { |
| 140 | initializer->TearDown(); |
| 141 | delete initializer; |
| 142 | } |
| 143 | |
| 144 | void init(ulonglong start_value, ulonglong end_value, |
| 145 | TCLogMMapTest *tc_log_mmap, Server_initializer *initializer_value) { |
| 146 | m_start_xid = start_value; |
| 147 | m_end_xid = end_value; |
| 148 | m_tc_log_mmap = tc_log_mmap; |
| 149 | initializer = initializer_value; |
| 150 | initializer->SetUp(); |
| 151 | } |
| 152 | |
| 153 | void run() override { |
| 154 | ulonglong xid = m_start_xid; |
| 155 | while (xid < m_end_xid) { |
| 156 | m_tc_log_mmap->testCommit(xid++, initializer->thd()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | protected: |
| 161 | ulonglong m_start_xid, m_end_xid; |
| 162 | TCLogMMapTest *m_tc_log_mmap; |
| 163 | Server_initializer *initializer; |
| 164 | }; |
| 165 | |
| 166 | TEST_F(TCLogMMapTest, ConcurrentAccess) { |
| 167 | static const unsigned MAX_WORKER_THREADS = 10; |
| 168 | static const unsigned VALUE_INTERVAL = 100; |
| 169 | |
| 170 | TC_Log_MMap_thread tclog_threads[MAX_WORKER_THREADS]; |
| 171 | |
| 172 | ulonglong start_value = 0; |
| 173 | for (unsigned i = 0; i < MAX_WORKER_THREADS; ++i) { |
| 174 | /* |
| 175 | Each thread gets a dedicated instance of class Server_initializer and |
| 176 | hence it also gets a separate THD object. |
| 177 | */ |
| 178 | tclog_threads[i].init(start_value, start_value + VALUE_INTERVAL, this, |
| 179 | new Server_initializer()); |
| 180 | tclog_threads[i].start(); |
| 181 | start_value += VALUE_INTERVAL; |
| 182 | } |
| 183 | |
| 184 | for (unsigned i = 0; i < MAX_WORKER_THREADS; ++i) tclog_threads[i].join(); |
| 185 | } |
| 186 | |
| 187 | TEST_F(TCLogMMapTest, FillAllPagesAndReuse) { |
| 188 | /* Get maximum number of XIDs which can be stored in TC log. */ |
| 189 | const uint MAX_XIDS = tc_log_mmap->size(); |
| 190 | ulong cookie; |
| 191 | /* Fill TC log. */ |
| 192 | for (my_xid xid = 1; xid < MAX_XIDS; ++xid) (void)testLog(xid); |
| 193 | cookie = testLog(MAX_XIDS); |
| 194 | /* |
| 195 | Now free one slot and try to reuse it. |
| 196 | This should work and not crash on assert. |
| 197 | */ |
| 198 | testUnlog(cookie, MAX_XIDS); |
| 199 | testLog(MAX_XIDS + 1); |
| 200 | } |
| 201 | |
| 202 | TEST_F(TCLogMMapTest, ConcurrentOverflow) { |
| 203 | const uint WORKER_THREADS = 10; |
| 204 | const uint XIDS_TO_REUSE = 100; |
| 205 | |
| 206 | /* |
| 207 | Get maximum number of XIDs which can be stored in TC log. |
| 208 | */ |
| 209 | const uint MAX_XIDS = tc_log_mmap->size(); |
| 210 | ulong cookies[XIDS_TO_REUSE]; |
| 211 | |
| 212 | /* |
| 213 | Fill TC log. Remember cookies for last XIDS_TO_REUSE xids. |
| 214 | */ |
| 215 | for (my_xid xid = 1; xid <= MAX_XIDS - XIDS_TO_REUSE; ++xid) testLog(xid); |
| 216 | for (uint i = 0; i < XIDS_TO_REUSE; ++i) |
| 217 | cookies[i] = testLog(MAX_XIDS - XIDS_TO_REUSE + 1 + i); |
| 218 | |
| 219 | /* |
| 220 | Now create several threads which will try to do commit. |
| 221 | Since log is full they will have to wait until we free some slots. |
| 222 | */ |
| 223 | TC_Log_MMap_thread threads[WORKER_THREADS]; |
| 224 | for (uint i = 0; i < WORKER_THREADS; ++i) { |
| 225 | /* |
| 226 | Each thread gets a dedicated instance of class Server_initializer and |
| 227 | hence it also gets a separate THD object. |
| 228 | */ |
| 229 | threads[i].init(MAX_XIDS + i * (XIDS_TO_REUSE / WORKER_THREADS), |
| 230 | MAX_XIDS + (i + 1) * (XIDS_TO_REUSE / WORKER_THREADS), this, |
| 231 | new Server_initializer()); |
| 232 | threads[i].start(); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | Once started all threads should block since we are out of free slots |
| 237 | in the log, Resume threads by freeing necessary slots. Resumed thread |
| 238 | should not hang or assert. |
| 239 | */ |
| 240 | for (uint i = 0; i < XIDS_TO_REUSE; ++i) |
| 241 | testUnlog(cookies[i], MAX_XIDS - XIDS_TO_REUSE + 1 + i); |
| 242 | |
| 243 | /* Wait till all threads are done. */ |
| 244 | for (uint i = 0; i < WORKER_THREADS; ++i) threads[i].join(); |
| 245 | } |
| 246 | |
| 247 | } // namespace tc_log_mmap_unittest |