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
|
/* Copyright (c) 2011, 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 <string.h>
#include <memory>
#include <unordered_map>
#include <utility>
#include "libbinlogevents/include/control_events.h"
#include "map_helpers.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "mysqld_error.h" // IWYU pragma: keep
#include "prealloced_array.h"
#include "sql/rpl_gtid.h"
#include "sql/thr_malloc.h"
#ifndef MYSQL_SERVER
#include "client/mysqlbinlog.h" // IWYU pragma: keep
#endif
PSI_memory_key key_memory_Sid_map_Node;
Sid_map::Sid_map(Checkable_rwlock *_sid_lock)
: sid_lock(_sid_lock),
_sidno_to_sid(key_memory_Sid_map_Node),
_sorted(key_memory_Sid_map_Node) {
DBUG_TRACE;
}
Sid_map::~Sid_map() { DBUG_TRACE; }
enum_return_status Sid_map::clear() {
DBUG_TRACE;
_sid_to_sidno.clear();
_sidno_to_sid.clear();
_sorted.clear();
RETURN_OK;
}
rpl_sidno Sid_map::add_sid(const rpl_sid &sid) {
DBUG_TRACE;
#ifndef NDEBUG
char buf[binary_log::Uuid::TEXT_LENGTH + 1];
sid.to_string(buf);
DBUG_PRINT("info", ("SID=%s", buf));
#endif
if (sid_lock) sid_lock->assert_some_lock();
auto it = _sid_to_sidno.find(sid);
if (it != _sid_to_sidno.end()) {
DBUG_PRINT("info", ("existed as sidno=%d", it->second->sidno));
return it->second->sidno;
}
bool is_wrlock = false;
if (sid_lock) {
is_wrlock = sid_lock->is_wrlock();
if (!is_wrlock) {
sid_lock->unlock();
sid_lock->wrlock();
}
}
DBUG_PRINT("info", ("is_wrlock=%d sid_lock=%p", is_wrlock, sid_lock));
rpl_sidno sidno;
it = _sid_to_sidno.find(sid);
if (it != _sid_to_sidno.end())
sidno = it->second->sidno;
else {
sidno = get_max_sidno() + 1;
if (add_node(sidno, sid) != RETURN_STATUS_OK) sidno = -1;
}
if (sid_lock) {
if (!is_wrlock) {
sid_lock->unlock();
sid_lock->rdlock();
}
}
return sidno;
}
enum_return_status Sid_map::add_node(rpl_sidno sidno, const rpl_sid &sid) {
DBUG_TRACE;
if (sid_lock) sid_lock->assert_some_wrlock();
unique_ptr_my_free<Node> node(
(Node *)my_malloc(key_memory_Sid_map_Node, sizeof(Node), MYF(MY_WME)));
if (node == nullptr) RETURN_REPORTED_ERROR;
node->sidno = sidno;
node->sid = sid;
if (!_sidno_to_sid.push_back(node.get())) {
if (!_sorted.push_back(sidno)) {
if (_sid_to_sidno.emplace(node->sid, std::move(node)).second) {
#ifdef MYSQL_SERVER
/*
If this is the global_sid_map, we take the opportunity to
resize all arrays in gtid_state while holding the wrlock.
*/
if (this != global_sid_map ||
gtid_state->ensure_sidno() == RETURN_STATUS_OK)
#endif
{
// We have added one element to the end of _sorted. Now we
// bubble it down to the sorted position.
int sorted_i = sidno - 1;
rpl_sidno *prev_sorted_p = &_sorted[sorted_i];
sorted_i--;
while (sorted_i >= 0) {
rpl_sidno *sorted_p = &_sorted[sorted_i];
const rpl_sid &other_sid = sidno_to_sid(*sorted_p);
if (memcmp(sid.bytes, other_sid.bytes,
binary_log::Uuid::BYTE_LENGTH) >= 0)
break;
memcpy(prev_sorted_p, sorted_p, sizeof(rpl_sidno));
sorted_i--;
prev_sorted_p = sorted_p;
}
memcpy(prev_sorted_p, &sidno, sizeof(rpl_sidno));
RETURN_OK;
}
}
_sorted.pop_back();
}
_sidno_to_sid.pop_back();
}
BINLOG_ERROR(("Out of memory."), (ER_OUT_OF_RESOURCES, MYF(0)));
RETURN_REPORTED_ERROR;
}
enum_return_status Sid_map::copy(Sid_map *dest) {
DBUG_TRACE;
enum_return_status return_status = RETURN_STATUS_OK;
rpl_sidno max_sidno = get_max_sidno();
for (rpl_sidno sidno = 1;
sidno <= max_sidno && return_status == RETURN_STATUS_OK; sidno++) {
rpl_sid sid;
sid.copy_from(sidno_to_sid(sidno));
return_status = dest->add_node(sidno, sid);
}
return return_status;
}
|