| 1 | /* Copyright (c) 2020, 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 | #include <array> |
| 26 | #include <chrono> |
| 27 | #include <thread> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "sql/changestreams/apply/commit_order_queue.h" |
| 31 | #include "sql/memory/aligned_atomic.h" |
| 32 | |
| 33 | #include <gmock/gmock.h> |
| 34 | #include <gtest/gtest.h> |
| 35 | |
| 36 | namespace rpl { |
| 37 | namespace unittests { |
| 38 | |
| 39 | class Rpl_commit_order_queue_test : public ::testing::Test { |
| 40 | protected: |
| 41 | Rpl_commit_order_queue_test() = default; |
| 42 | virtual void SetUp() {} |
| 43 | virtual void TearDown() {} |
| 44 | |
| 45 | std::atomic<bool> m_go{false}; |
| 46 | std::atomic<size_t> m_count{0}; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(Rpl_commit_order_queue_test, Simulate_mts) { |
| 50 | constexpr cs::apply::Commit_order_queue::value_type total_workers{32}; |
| 51 | constexpr size_t total_transactions{25000}; |
| 52 | cs::apply::Commit_order_queue scheduled{total_workers}; |
| 53 | cs::apply::Commit_order_queue free{total_workers}; |
| 54 | std::array<std::atomic<bool>, total_workers> context; |
| 55 | std::atomic<cs::apply::Commit_order_queue::value_type> transactions{ |
| 56 | total_transactions}; |
| 57 | |
| 58 | for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) { |
| 59 | context[n_wrk].store(true); |
| 60 | free.push(n_wrk); |
| 61 | } |
| 62 | |
| 63 | std::vector<std::thread> threads; |
| 64 | for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) { |
| 65 | threads.emplace_back( |
| 66 | [&](cs::apply::Commit_order_queue::value_type worker_id) -> void { |
| 67 | assert(worker_id != cs::apply::Commit_order_queue::NO_WORKER); |
| 68 | |
| 69 | for (; transactions > 0;) { |
| 70 | if (scheduled[worker_id].m_stage == |
| 71 | cs::apply::Commit_order_queue::enum_worker_stage::FINISHED) { |
| 72 | scheduled[worker_id].m_stage = |
| 73 | cs::apply::Commit_order_queue::enum_worker_stage::REGISTERED; |
| 74 | |
| 75 | for (; context[worker_id].exchange( |
| 76 | true);) { // Wait for coordinator to |
| 77 | // schedule the worker in |
| 78 | std::this_thread::yield(); |
| 79 | } |
| 80 | if (transactions <= 0) break; |
| 81 | } |
| 82 | |
| 83 | // Worker would apply the transaction here |
| 84 | |
| 85 | // Worker enters the wait on the commit order |
| 86 | scheduled[worker_id].m_stage = cs::apply::Commit_order_queue:: |
| 87 | enum_worker_stage::FINISHED_APPLYING; |
| 88 | |
| 89 | if (worker_id == scheduled.front()) { // Is the head of the queue |
| 90 | scheduled[worker_id].m_stage = |
| 91 | cs::apply::Commit_order_queue::enum_worker_stage::WAITED; |
| 92 | |
| 93 | auto this_worker{cs::apply::Commit_order_queue::NO_WORKER}; |
| 94 | auto next_seq_nr{0}; |
| 95 | std::tie(this_worker, next_seq_nr) = |
| 96 | scheduled.pop(); // Pops the head of the |
| 97 | // queue and gets the |
| 98 | // the next sequence |
| 99 | // number |
| 100 | EXPECT_EQ(worker_id, this_worker); |
| 101 | EXPECT_NE(next_seq_nr, 0); // NO_SEQUENCE_NR |
| 102 | EXPECT_NE(next_seq_nr, 1); // SEQUENCE_NR_FROZEN |
| 103 | |
| 104 | auto next_worker = |
| 105 | scheduled.front(); // Gets the next worker and checks if it |
| 106 | // needs to release it |
| 107 | if (next_worker != cs::apply::Commit_order_queue::NO_WORKER && |
| 108 | (scheduled[next_worker].m_stage == |
| 109 | cs::apply::Commit_order_queue::enum_worker_stage:: |
| 110 | FINISHED_APPLYING || |
| 111 | scheduled[next_worker].m_stage == |
| 112 | cs::apply::Commit_order_queue::enum_worker_stage:: |
| 113 | REQUESTED_GRANT) && |
| 114 | scheduled[next_worker].freeze_commit_sequence_nr( |
| 115 | next_seq_nr)) { |
| 116 | context[next_worker].store(false); // Releases the next worker |
| 117 | scheduled[next_worker].unfreeze_commit_sequence_nr(next_seq_nr); |
| 118 | } |
| 119 | |
| 120 | --transactions; // One less transaction in the workload |
| 121 | |
| 122 | scheduled[worker_id].m_stage = |
| 123 | cs::apply::Commit_order_queue::enum_worker_stage::FINISHED; |
| 124 | context[worker_id].store(true); |
| 125 | free.push(worker_id); // Finishes the work and pushes itself |
| 126 | // into the free worker queue |
| 127 | |
| 128 | } else { |
| 129 | scheduled[worker_id].m_stage = |
| 130 | cs::apply::Commit_order_queue::enum_worker_stage:: |
| 131 | REQUESTED_GRANT; // Starts the commit order wait |
| 132 | |
| 133 | for (; context[worker_id].exchange(true);) { // Wait for previous |
| 134 | // worker to release |
| 135 | std::this_thread::yield(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | for (cs::apply::Commit_order_queue::value_type sib = 0; |
| 140 | sib != total_workers; ++sib) { // Work is finished |
| 141 | context[sib].store(false); // Release all waiting workers |
| 142 | } |
| 143 | }, |
| 144 | total_workers - n_wrk - 1); |
| 145 | } |
| 146 | |
| 147 | std::thread coordinator{[&]() -> void { |
| 148 | for (size_t n_trx = 0; n_trx != total_transactions; ++n_trx) { |
| 149 | auto w{cs::apply::Commit_order_queue::NO_WORKER}; |
| 150 | for (std::tie(w, std::ignore) = free.pop(); |
| 151 | w == cs::apply::Commit_order_queue::NO_WORKER; |
| 152 | std::tie(w, std::ignore) = |
| 153 | free.pop()) // Get a free worker to schedule |
| 154 | std::this_thread::yield(); |
| 155 | scheduled.push(w); // Schedule the worker |
| 156 | context[w].store(false); // Signal the worker |
| 157 | } |
| 158 | }}; |
| 159 | |
| 160 | coordinator.join(); |
| 161 | for (auto n_wrk = 0; n_wrk != total_workers; ++n_wrk) { |
| 162 | threads[n_wrk].join(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | TEST_F(Rpl_commit_order_queue_test, Pushing_while_poping_test) { |
| 167 | constexpr cs::apply::Commit_order_queue::value_type total_workers{32}; |
| 168 | cs::apply::Commit_order_queue q{total_workers}; |
| 169 | cs::apply::Commit_order_queue f{total_workers}; |
| 170 | |
| 171 | std::vector<std::thread> threads; |
| 172 | for (auto idx = 0; idx != total_workers; ++idx) { |
| 173 | threads.emplace_back( |
| 174 | [&](size_t) -> void { |
| 175 | for (; true;) { |
| 176 | auto v{cs::apply::Commit_order_queue::NO_WORKER}; |
| 177 | std::tie(v, std::ignore) = q.pop(); |
| 178 | if (v != cs::apply::Commit_order_queue::NO_WORKER) { |
| 179 | ++m_count; |
| 180 | f.push(v); |
| 181 | break; |
| 182 | } |
| 183 | std::this_thread::yield(); |
| 184 | } |
| 185 | }, |
| 186 | idx); |
| 187 | } |
| 188 | |
| 189 | std::thread producer{[&]() -> void { |
| 190 | for (cs::apply::Commit_order_queue::value_type idx = 0; |
| 191 | idx != total_workers; ++idx) { |
| 192 | q.push(total_workers - idx - 1); |
| 193 | } |
| 194 | }}; |
| 195 | |
| 196 | producer.join(); |
| 197 | for (auto idx = 0; idx != total_workers; ++idx) { |
| 198 | threads[idx].join(); |
| 199 | } |
| 200 | |
| 201 | EXPECT_EQ(q.is_empty(), true); |
| 202 | EXPECT_EQ(m_count.load(), total_workers); |
| 203 | |
| 204 | EXPECT_EQ(q.to_string(), "EOF"); |
| 205 | std::ostringstream oss; |
| 206 | for (int k = total_workers - 1; k != -1; --k) { |
| 207 | oss << k << ", " << std::flush; |
| 208 | } |
| 209 | oss << "EOF" << std::flush; |
| 210 | EXPECT_EQ(f.to_string().length(), oss.str().length()); |
| 211 | |
| 212 | std::map<cs::apply::Commit_order_queue::value_type, |
| 213 | cs::apply::Commit_order_queue::value_type> |
| 214 | dup; |
| 215 | for (auto v : f) { |
| 216 | if (v == nullptr) continue; |
| 217 | bool inserted{false}; |
| 218 | std::tie(std::ignore, inserted) = |
| 219 | dup.insert(std::make_pair(v->m_worker_id, v->m_worker_id)); |
| 220 | EXPECT_EQ(inserted, true); |
| 221 | } |
| 222 | EXPECT_EQ(dup.size(), total_workers); |
| 223 | |
| 224 | EXPECT_EQ(f.is_empty(), false); |
| 225 | f.clear(); |
| 226 | EXPECT_EQ(f.is_empty(), true); |
| 227 | } |
| 228 | |
| 229 | TEST_F(Rpl_commit_order_queue_test, Pushing_then_poping_test) { |
| 230 | constexpr cs::apply::Commit_order_queue::value_type total_workers{32}; |
| 231 | cs::apply::Commit_order_queue q{total_workers}; |
| 232 | cs::apply::Commit_order_queue f{total_workers}; |
| 233 | m_go.store(false); |
| 234 | |
| 235 | std::vector<std::thread> threads; |
| 236 | for (auto idx = 0; idx != total_workers; ++idx) { |
| 237 | threads.emplace_back( |
| 238 | [&](size_t) -> void { |
| 239 | for (; true;) { |
| 240 | if (m_go.load()) break; |
| 241 | std::this_thread::yield(); |
| 242 | } |
| 243 | |
| 244 | for (; true;) { |
| 245 | auto v{cs::apply::Commit_order_queue::NO_WORKER}; |
| 246 | std::tie(v, std::ignore) = q.pop(); |
| 247 | if (v != cs::apply::Commit_order_queue::NO_WORKER) { |
| 248 | ++m_count; |
| 249 | f.push(v); |
| 250 | break; |
| 251 | } |
| 252 | std::this_thread::yield(); |
| 253 | } |
| 254 | }, |
| 255 | idx); |
| 256 | } |
| 257 | |
| 258 | std::thread producer{[&]() -> void { |
| 259 | for (cs::apply::Commit_order_queue::value_type idx = 0; |
| 260 | idx != total_workers; ++idx) { |
| 261 | q.push(total_workers - idx - 1); |
| 262 | } |
| 263 | m_go.store(true); |
| 264 | }}; |
| 265 | |
| 266 | producer.join(); |
| 267 | for (auto idx = 0; idx != total_workers; ++idx) { |
| 268 | threads[idx].join(); |
| 269 | } |
| 270 | |
| 271 | EXPECT_EQ(q.is_empty(), true); |
| 272 | EXPECT_EQ( |
| 273 | q.get_state(), |
| 274 | cs::apply::Commit_order_queue::queue_type::enum_queue_state::SUCCESS); |
| 275 | EXPECT_EQ(m_count.load(), total_workers); |
| 276 | |
| 277 | EXPECT_EQ(q.to_string(), "EOF"); |
| 278 | std::ostringstream oss; |
| 279 | for (int k = total_workers - 1; k != -1; --k) { |
| 280 | oss << k << ", " << std::flush; |
| 281 | } |
| 282 | oss << "EOF" << std::flush; |
| 283 | EXPECT_EQ(f.to_string().length(), oss.str().length()); |
| 284 | |
| 285 | auto it = f.begin(); |
| 286 | auto it1{it}; |
| 287 | auto it2 = it1++; |
| 288 | auto it3 = std::move(it2); |
| 289 | auto it4{std::move(it3)}; |
| 290 | it2 = it4; |
| 291 | it3 = std::move(it1); |
| 292 | EXPECT_NE(*it4, nullptr); |
| 293 | EXPECT_EQ(it4->m_worker_id, it2->m_worker_id); |
| 294 | |
| 295 | std::map<cs::apply::Commit_order_queue::value_type, |
| 296 | cs::apply::Commit_order_queue::value_type> |
| 297 | dup; |
| 298 | for (auto v : f) { |
| 299 | if (v == nullptr) continue; |
| 300 | bool inserted{false}; |
| 301 | std::tie(std::ignore, inserted) = |
| 302 | dup.insert(std::make_pair(v->m_worker_id, v->m_worker_id)); |
| 303 | EXPECT_EQ(inserted, true); |
| 304 | } |
| 305 | EXPECT_EQ(dup.size(), total_workers); |
| 306 | |
| 307 | EXPECT_EQ(f.is_empty(), false); |
| 308 | f.clear(); |
| 309 | EXPECT_EQ(f.is_empty(), true); |
| 310 | } |
| 311 | |
| 312 | TEST_F(Rpl_commit_order_queue_test, Remove) { |
| 313 | constexpr cs::apply::Commit_order_queue::value_type total_workers{4}; |
| 314 | cs::apply::Commit_order_queue queue{total_workers}; |
| 315 | auto this_worker{cs::apply::Commit_order_queue::NO_WORKER}; |
| 316 | auto next_seq_nr{0}; |
| 317 | |
| 318 | queue.push(0); |
| 319 | queue.push(1); |
| 320 | queue.push(2); |
| 321 | queue.push(3); |
| 322 | /* |
| 323 | +----------------------+----+----+----+----+ |
| 324 | | worker | 0 | 1 | 2 | 3 | |
| 325 | | sequence number | 2 | 3 | 4 | 5 | |
| 326 | | next sequence number | 3 | 4 | 5 | 6 | |
| 327 | +----------------------+----+----+----+----+ |
| 328 | */ |
| 329 | EXPECT_EQ(queue.to_string(), "0, 1, 2, 3, EOF"); |
| 330 | |
| 331 | std::tie(this_worker, next_seq_nr) = queue.remove(0); |
| 332 | /* |
| 333 | +----------------------+----+----+----+ |
| 334 | | worker | 1 | 2 | 3 | |
| 335 | | sequence number | 3 | 4 | 5 | |
| 336 | | next sequence number | 4 | 5 | 6 | |
| 337 | +----------------------+----+----+----+ |
| 338 | */ |
| 339 | EXPECT_EQ(this_worker, 0); |
| 340 | EXPECT_EQ(next_seq_nr, 3); |
| 341 | EXPECT_EQ(queue.to_string(), "1, 2, 3, EOF"); |
| 342 | |
| 343 | std::tie(this_worker, next_seq_nr) = queue.remove(3); |
| 344 | /* |
| 345 | +----------------------+----+----+ |
| 346 | | worker | 1 | 2 | |
| 347 | | sequence number | 3 | 4 | |
| 348 | | next sequence number | 4 | 6 | |
| 349 | +----------------------+----+----+ |
| 350 | */ |
| 351 | EXPECT_EQ(this_worker, 3); |
| 352 | EXPECT_EQ(next_seq_nr, 0); // NO_SEQUENCE_NR |
| 353 | EXPECT_EQ(queue.to_string(), "1, 2, EOF"); |
| 354 | |
| 355 | queue.push(0); |
| 356 | /* |
| 357 | +----------------------+----+----+----+ |
| 358 | | worker | 1 | 2 | 0 | |
| 359 | | sequence number | 3 | 4 | 6 | |
| 360 | | next sequence number | 4 | 6 | 7 | |
| 361 | +----------------------+----+----+----+ |
| 362 | */ |
| 363 | EXPECT_EQ(queue.to_string(), "1, 2, 0, EOF"); |
| 364 | |
| 365 | std::tie(this_worker, next_seq_nr) = queue.remove(2); |
| 366 | /* |
| 367 | +----------------------+----+----+ |
| 368 | | worker | 1 | 0 | |
| 369 | | sequence number | 3 | 6 | |
| 370 | | next sequence number | 6 | 7 | |
| 371 | +----------------------+----+----+ |
| 372 | */ |
| 373 | EXPECT_EQ(this_worker, 2); |
| 374 | EXPECT_EQ(next_seq_nr, 0); // NO_SEQUENCE_NR |
| 375 | EXPECT_EQ(queue.to_string(), "1, 0, EOF"); |
| 376 | |
| 377 | std::tie(this_worker, next_seq_nr) = queue.pop(); |
| 378 | /* |
| 379 | +----------------------+----+ |
| 380 | | worker | 0 | |
| 381 | | sequence number | 6 | |
| 382 | | next sequence number | 7 | |
| 383 | +----------------------+----+ |
| 384 | */ |
| 385 | EXPECT_EQ(this_worker, 1); |
| 386 | EXPECT_EQ(next_seq_nr, 6); |
| 387 | EXPECT_EQ(queue.to_string(), "0, EOF"); |
| 388 | |
| 389 | std::tie(this_worker, next_seq_nr) = queue.pop(); |
| 390 | /* |
| 391 | +----------------------+ |
| 392 | | worker | |
| 393 | | sequence number | |
| 394 | | next sequence number | |
| 395 | +----------------------+ |
| 396 | */ |
| 397 | EXPECT_EQ(this_worker, 0); |
| 398 | EXPECT_EQ(next_seq_nr, 7); |
| 399 | EXPECT_EQ(queue.to_string(), "EOF"); |
| 400 | |
| 401 | std::tie(this_worker, next_seq_nr) = queue.remove(50); |
| 402 | EXPECT_EQ(this_worker, -1); // NO_WORKER |
| 403 | EXPECT_EQ(next_seq_nr, 0); // NO_SEQUENCE_NR |
| 404 | EXPECT_EQ(queue.to_string(), "EOF"); |
| 405 | } |
| 406 | |
| 407 | } // namespace unittests |
| 408 | } // namespace rpl |