1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* Copyright (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <iostream>
#include <sstream>
#include <thread>
#include "sql/changestreams/apply/commit_order_queue.h"
bool cs::apply::Commit_order_queue::Node::freeze_commit_sequence_nr(
Commit_order_queue::sequence_type expected) {
return this->m_commit_sequence_nr->compare_exchange_strong(
expected, SEQUENCE_NR_FROZEN, std::memory_order_seq_cst);
}
bool cs::apply::Commit_order_queue::Node::unfreeze_commit_sequence_nr(
Commit_order_queue::sequence_type reset_to) {
auto to_replace{SEQUENCE_NR_FROZEN};
return this->m_commit_sequence_nr->compare_exchange_strong(
to_replace, reset_to, std::memory_order_seq_cst);
}
cs::apply::Commit_order_queue::sequence_type
cs::apply::Commit_order_queue::Node::reset_commit_sequence_nr() {
for (; true;) {
auto ticket_nr =
this->m_commit_sequence_nr->load(std::memory_order_acquire);
auto next_ticket_nr =
this->m_next_commit_sequence_nr->load(std::memory_order_acquire);
if (ticket_nr != SEQUENCE_NR_FROZEN &&
this->m_commit_sequence_nr->compare_exchange_strong(
ticket_nr, NO_SEQUENCE_NR, std::memory_order_release)) {
this->m_next_commit_sequence_nr->store(Node::NO_SEQUENCE_NR);
return next_ticket_nr;
}
std::this_thread::yield();
}
assert(false);
return NO_SEQUENCE_NR;
}
cs::apply::Commit_order_queue::Iterator::Iterator(Commit_order_queue &queue,
index_type current)
: m_target{&queue}, m_current{queue.m_commit_queue, current} {}
cs::apply::Commit_order_queue::Iterator::Iterator(const Iterator &) = default;
cs::apply::Commit_order_queue::Iterator::Iterator(Iterator &&rhs)
: m_target{rhs.m_target}, m_current{rhs.m_current} {
rhs.m_current = rhs.m_target->m_commit_queue.end();
rhs.m_target = nullptr;
}
cs::apply::Commit_order_queue::Iterator &
cs::apply::Commit_order_queue::Iterator::operator=(const Iterator &) = default;
cs::apply::Commit_order_queue::Iterator &
cs::apply::Commit_order_queue::Iterator::operator=(Iterator &&rhs) {
this->m_target = rhs.m_target;
this->m_current = rhs.m_current;
rhs.m_current = rhs.m_target->m_commit_queue.end();
rhs.m_target = nullptr;
return (*this);
}
cs::apply::Commit_order_queue::Iterator &
cs::apply::Commit_order_queue::Iterator::operator++() {
++this->m_current;
return (*this);
}
cs::apply::Commit_order_queue::Node *
cs::apply::Commit_order_queue::Iterator::operator*() {
auto index = *this->m_current;
if (index == cs::apply::Commit_order_queue::NO_WORKER) return nullptr;
return &(this->m_target->m_workers[index]);
}
cs::apply::Commit_order_queue::Iterator
cs::apply::Commit_order_queue::Iterator::operator++(int) {
Iterator to_return{*this};
++(*this);
return to_return;
}
cs::apply::Commit_order_queue::Node *
cs::apply::Commit_order_queue::Iterator::operator->() {
auto index = *this->m_current;
if (index == cs::apply::Commit_order_queue::NO_WORKER) return nullptr;
return &(this->m_target->m_workers[index]);
}
bool cs::apply::Commit_order_queue::Iterator::operator==(
Iterator const &rhs) const {
return this->m_current == rhs.m_current;
}
bool cs::apply::Commit_order_queue::Iterator::operator!=(
Iterator const &rhs) const {
return !((*this) == rhs);
}
cs::apply::Commit_order_queue::Commit_order_queue(size_t n_workers)
: m_workers{n_workers}, m_commit_queue{static_cast<size_t>(n_workers)} {
for (size_t w = 0; w != this->m_workers.size(); ++w) {
this->m_workers[w].m_worker_id = w;
}
DBUG_EXECUTE_IF("commit_order_queue_seq_wrap_around", {
this->m_commit_sequence_generator->store(
std::numeric_limits<unsigned long long>::max() - 2);
});
}
cs::apply::Commit_order_queue::Node &cs::apply::Commit_order_queue::operator[](
value_type id) {
auto idx = static_cast<size_t>(id);
assert(idx < this->m_workers.size());
return this->m_workers[idx];
}
cs::apply::Commit_order_queue::queue_type::enum_queue_state
cs::apply::Commit_order_queue::get_state() {
return this->m_commit_queue.get_state();
}
bool cs::apply::Commit_order_queue::is_empty() {
return this->m_commit_queue.is_empty();
}
std::tuple<cs::apply::Commit_order_queue::value_type,
cs::apply::Commit_order_queue::sequence_type>
cs::apply::Commit_order_queue::pop() {
lock::Shared_spin_lock::Guard pop_sentry{
this->m_push_pop_lock,
lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED};
value_type value_to_return{NO_WORKER};
sequence_type next_seq_nr{Node::NO_SEQUENCE_NR};
this->m_commit_queue >> value_to_return;
this->m_commit_queue.clear_state();
if (value_to_return != NO_WORKER) {
next_seq_nr = this->m_workers[value_to_return].reset_commit_sequence_nr();
}
return std::make_tuple(value_to_return, next_seq_nr);
}
void cs::apply::Commit_order_queue::push(value_type index) {
lock::Shared_spin_lock::Guard push_sentry{
this->m_push_pop_lock,
lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED};
assert(this->m_workers[index].m_commit_sequence_nr == Node::NO_SEQUENCE_NR);
sequence_type next{Node::NO_SEQUENCE_NR};
do {
next = this->m_commit_sequence_generator->fetch_add(1);
} while (next <= Node::SEQUENCE_NR_FROZEN);
this->m_workers[index].m_commit_sequence_nr->store(next);
this->m_workers[index].m_next_commit_sequence_nr->store(
get_next_sequence_nr(next));
this->m_commit_queue << index;
assert(this->m_commit_queue.get_state() !=
Commit_order_queue::queue_type::enum_queue_state::NO_SPACE_AVAILABLE);
this->m_commit_queue.clear_state();
}
cs::apply::Commit_order_queue::value_type
cs::apply::Commit_order_queue::front() {
lock::Shared_spin_lock::Guard front_sentry{
this->m_push_pop_lock,
lock::Shared_spin_lock::enum_lock_acquisition::SL_SHARED};
return this->m_commit_queue.front();
}
void cs::apply::Commit_order_queue::clear() { this->m_commit_queue.clear(); }
void cs::apply::Commit_order_queue::freeze() {
this->m_push_pop_lock.acquire_exclusive();
}
void cs::apply::Commit_order_queue::unfreeze() {
this->m_push_pop_lock.release_exclusive();
}
cs::apply::Commit_order_queue::Iterator cs::apply::Commit_order_queue::begin() {
return cs::apply::Commit_order_queue::Iterator{*this,
this->m_commit_queue.head()};
}
cs::apply::Commit_order_queue::Iterator cs::apply::Commit_order_queue::end() {
return cs::apply::Commit_order_queue::Iterator{*this,
this->m_commit_queue.tail()};
}
std::string cs::apply::Commit_order_queue::to_string() {
return this->m_commit_queue.to_string();
}
cs::apply::Commit_order_queue::sequence_type
cs::apply::Commit_order_queue::get_next_sequence_nr(
sequence_type current_seq_nr) {
sequence_type next{current_seq_nr};
do {
++next;
} while (next <= Node::SEQUENCE_NR_FROZEN);
return next;
}
std::tuple<cs::apply::Commit_order_queue::value_type,
cs::apply::Commit_order_queue::sequence_type>
cs::apply::Commit_order_queue::remove(value_type index) {
lock::Shared_spin_lock::Guard remove_sentry{
this->m_push_pop_lock,
lock::Shared_spin_lock::enum_lock_acquisition::SL_EXCLUSIVE};
value_type value_to_return{NO_WORKER};
value_type previous_worker{NO_WORKER};
sequence_type next_seq_nr{Node::NO_SEQUENCE_NR};
std::tie(value_to_return, previous_worker) = remove_from_commit_queue(index);
this->m_commit_queue.clear_state();
if (value_to_return != NO_WORKER) {
next_seq_nr = this->m_workers[value_to_return].reset_commit_sequence_nr();
if (previous_worker != NO_WORKER) {
/*
The previous worker will be responsible to unblock the next worker.
Example:
+----------------------+----+----+----+
| worker | 1 | 2 | 3 |
| sequence number | 11 | 12 | 13 |
| next sequence number | 12 | 13 | 14 |
+----------------------+----+----+----+
Worker 2 is removed:
+----------------------+----+----+
| worker | 1 | 3 |
| sequence number | 11 | 13 |
| next sequence number | 13 | 14 |
+----------------------+----+----+
Worker 1 will be responsible to unblock worker 3, thence the next
sequence number 13.
*/
this->m_workers[previous_worker].m_next_commit_sequence_nr->store(
next_seq_nr);
next_seq_nr = Node::NO_SEQUENCE_NR;
}
}
return std::make_tuple(value_to_return, next_seq_nr);
}
std::tuple<cs::apply::Commit_order_queue::value_type,
cs::apply::Commit_order_queue::value_type>
cs::apply::Commit_order_queue::remove_from_commit_queue(value_type to_remove) {
assert(to_remove != NO_WORKER);
// Iterator to the first match, if any.
auto it = std::find(this->m_commit_queue.begin(), this->m_commit_queue.end(),
to_remove);
// If to_remove is not in the set, return.
if (it == this->m_commit_queue.end()) {
return std::make_tuple(NO_WORKER, NO_WORKER);
}
// If to_remove is the first, just pop it and return.
if (it == this->m_commit_queue.begin()) {
value_type value_removed = this->m_commit_queue.pop();
return std::make_tuple(value_removed, NO_WORKER);
}
// If to_remove is in the queue but not the first,
// rebuild the queue omitting to_remove.
value_type value_removed{NO_WORKER};
value_type previous_value{NO_WORKER};
const Commit_order_queue::queue_type::index_type original_size =
this->m_commit_queue.tail() - this->m_commit_queue.head();
// Pop the first value so that we have the
// previous value.
value_type value = this->m_commit_queue.pop();
this->m_commit_queue.push(value);
for (Commit_order_queue::queue_type::index_type i = 1; i < original_size;
++i) {
value_type current_previous_value = value;
value = this->m_commit_queue.pop();
if (value_removed == NO_WORKER && value == to_remove) {
value_removed = value;
previous_value = current_previous_value;
} else {
this->m_commit_queue.push(value);
}
}
assert(this->m_commit_queue.get_state() ==
Commit_order_queue::queue_type::enum_queue_state::SUCCESS);
return std::make_tuple(value_removed, previous_value);
}
|