Debian Codegraph @

unittest/gunit/bgc_ticket_manager-t.cc

raw symbols SCIP
1 /* Copyright (c) 2022, 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 "sql/binlog/group_commit/bgc_ticket_manager.h"
26
27 #include <chrono>
28 #include <condition_variable>
29 #include <mutex>
30 #include <random>
31
32 #include <gmock/gmock.h>
33 #include <gtest/gtest.h>
34
35 namespace binlog {
36 namespace unittests {
37
38 using ticket_t = binlog::BgcTicket::ValueType;
39
40 /**
41 Test for Bgc_ticket_manager API.
42
43 The purpose of the test is for each created thread to be assigned to a
44 given ticket, wait for such ticket to be the front ticket and have it's
45 processing window active, add itself to the ticket's processed sessions
46 and either end the thread task or finish the current active processing
47 window. If a thread does one or the other, depends on a sequential number
48 that is passed on to each thread. Logic is as follows:
49
50 1. Creates set of threads and assigns to each a sequential number,
51 `n_thread`.
52
53 2. If the remainder of the integer division of `n_thread` by 10 is not zero,
54 the thread will:
55 1. Assign it self to the current back ticket.
56 2. Wait for it's own ticket to be active as front ticket.
57 3. Add itself to the front ticket processed sessions.
58
59 3. If the remainder of the integer division of `n_thread` by 10 is zero, the
60 thread will:
61 1. Wait for a random number (between 25 and 200) of micro-seconds, in
62 order to allow for different sizes in the active front ticket
63 processing window.
64 2. Atomically, assign it self to the current back ticket, close the
65 current back ticket to assignments and create a new back ticket.
66 3. Wait for it's own ticket to be active as front ticket.
67 4. Add itself to the front ticket processed sessions.
68 5. Finish the front ticket processing window.
69 6. Notify all waiting threads that a window has been closed.
70
71 */
72 class Bgc_ticket_manager_test : public ::testing::Test {
73 protected:
74 Bgc_ticket_manager_test() = default;
75 virtual void SetUp() {}
76 virtual void TearDown() {}
77 };
78
79 TEST_F(Bgc_ticket_manager_test, Several_tickets_test) {
80 size_t total_threads{300};
81 std::vector<std::thread> threads;
82 std::mutex mtx;
83 std::condition_variable condition;
84 std::random_device r_device;
85 std::default_random_engine r_engine{r_device()};
86 std::uniform_int_distribution<int> uniform_dist{25, 200};
87 size_t thread_test_loops = 100;
88 size_t max_ticket = (total_threads / 10) * thread_test_loops;
89 std::vector<std::uint64_t> tickets;
90 std::mutex mutex_tickets;
91
92 auto &ticket_manager = binlog::Bgc_ticket_manager::instance();
93
94 for (size_t idx = 0; idx != total_threads; ++idx) {
95 threads.emplace_back(
96 [&](size_t n_thread) -> void {
97 size_t thread_cnt = 0;
98 while (thread_cnt++ < thread_test_loops) {
99 if (n_thread % 10 != 0) {
100 auto this_thread_ticket =
101 ticket_manager.assign_session_to_ticket();
102 {
103 std::unique_lock lock{mtx};
104 while (this_thread_ticket !=
105 ticket_manager.get_front_ticket()) {
106 condition.wait(lock);
107 }
108 }
109 mutex_tickets.lock();
110 tickets.push_back(this_thread_ticket.get());
111 mutex_tickets.unlock();
112 ticket_manager.add_processed_sessions_to_front_ticket(
113 1, this_thread_ticket);
114 } else {
115 std::this_thread::sleep_for(
116 std::chrono::duration<int, std::micro>{
117 uniform_dist(r_engine)});
118 auto [this_thread_ticket, _] = ticket_manager.push_new_ticket(
119 binlog::BgcTmOptions::inc_session_count);
120
121 while (this_thread_ticket != ticket_manager.get_front_ticket()) {
122 std::this_thread::yield();
123 }
124 mutex_tickets.lock();
125 tickets.push_back(this_thread_ticket.get());
126 mutex_tickets.unlock();
127
128 ticket_manager.add_processed_sessions_to_front_ticket(
129 1, this_thread_ticket);
130
131 while (std::get<1>(ticket_manager.pop_front_ticket()) ==
132 this_thread_ticket) {
133 std::this_thread::yield();
134 }
135 {
136 std::unique_lock lock{mtx};
137 condition.notify_all();
138 }
139 }
140 }
141 },
142 idx + 1);
143 }
144
145 for (size_t idx = 0; idx != total_threads; ++idx) {
146 threads[idx].join();
147 }
148
149 // additional coalescing
150 // to ensure that m_back_ticket_sessions_count is always 0 (testing purposes)
151 ticket_manager.coalesce();
152
153 std::ostringstream expected;
154 expected << "Bgc_ticket_manager (" << std::hex << &ticket_manager << std::dec
155 << "):" << std::endl
156 << " · m_back_ticket: " << (max_ticket + 2) << "/0" << std::endl
157 << " · m_front_ticket: " << (max_ticket + 2) << "/0" << std::endl
158 << " · m_coalesced_ticket: " << (max_ticket + 1) << "/0" << std::endl
159 << " · m_back_ticket_sessions_count: 0" << std::endl
160 << " · m_front_ticket_processed_sessions_count: 0" << std::endl
161 << " · m_sessions_per_ticket: EOF" << std::flush;
162 std::ostringstream observed;
163 observed << ticket_manager << std::flush;
164 EXPECT_EQ(observed.str(), expected.str());
165 EXPECT_EQ(ticket_manager.to_string(), expected.str());
166 EXPECT_EQ(tickets.size(), total_threads * thread_test_loops);
167 EXPECT_EQ(std::is_sorted(tickets.begin(), tickets.end()), true);
168 }
169
170 } // namespace unittests
171 } // namespace binlog