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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/* Copyright (c) 2020, 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 "mysql/components/services/log_builtins.h"
#include "sql/changestreams/apply/replication_thread_status.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/rpl_async_conn_failover.h"
#include "sql/rpl_async_conn_failover_table_operations.h"
#include "sql/rpl_io_monitor.h"
#include "sql/rpl_msr.h" // channel_map
#include "sql/rpl_replica.h"
#include <algorithm>
/* replication_asynchronous_connection_failover table column position */
enum class enum_sender_tuple : std::size_t {
CHANNEL = 0,
HOST,
PORT,
NETNS,
WEIGHT,
MANAGED_NAME
};
/* Cast enum_sender_tuple to uint */
constexpr uint enum_convert(enum_sender_tuple eval) {
return static_cast<uint>(eval);
}
Async_conn_failover_manager::DoAutoConnFailoverError
Async_conn_failover_manager::do_auto_conn_failover(Master_info *mi,
bool force_highest_weight) {
DBUG_TRACE;
channel_map.assert_some_lock();
/* The list of different source connection details. */
RPL_FAILOVER_SOURCE_LIST source_conn_detail_list{};
/*
On the first connection to a group through a source that is in RECOVERING
state, the replication_asynchronous_connection_failover table may not be
yet populated with the group membership. Instead of immediately bailing out
we do retry read the sources for this channel.
*/
int retries = 0;
do {
if (retries > 0) {
my_sleep(500000);
}
/* Get network configuration details of all sources from this channel. */
Rpl_async_conn_failover_table_operations table_op(TL_READ);
auto tmp_details = table_op.read_source_rows_for_channel(mi->get_channel());
bool table_error = std::get<0>(tmp_details);
if (!table_error) {
source_conn_detail_list = std::get<1>(tmp_details);
std::sort(
source_conn_detail_list.begin(), source_conn_detail_list.end(),
[](auto const &t1, auto const &t2) {
auto tmp_t1 = std::make_tuple(
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1),
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1),
std::get<enum_convert(enum_sender_tuple::HOST)>(t1),
std::get<enum_convert(enum_sender_tuple::PORT)>(t1),
std::get<enum_convert(enum_sender_tuple::NETNS)>(t1));
auto tmp_t2 = std::make_tuple(
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2),
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2),
std::get<enum_convert(enum_sender_tuple::HOST)>(t2),
std::get<enum_convert(enum_sender_tuple::PORT)>(t2),
std::get<enum_convert(enum_sender_tuple::NETNS)>(t2));
return (
(std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) >
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) ||
((std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1) >
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2))) ||
((std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1) ==
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::HOST)>(t1) >
std::get<enum_convert(enum_sender_tuple::HOST)>(t2))) ||
((std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1) ==
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::HOST)>(t1) ==
std::get<enum_convert(enum_sender_tuple::HOST)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::PORT)>(t1) >
std::get<enum_convert(enum_sender_tuple::PORT)>(t2))) ||
((std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1) ==
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::HOST)>(t1) ==
std::get<enum_convert(enum_sender_tuple::HOST)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::PORT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::PORT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::NETNS)>(t1) >
std::get<enum_convert(enum_sender_tuple::NETNS)>(t2))) ||
((std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::WEIGHT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t1) ==
std::get<enum_convert(enum_sender_tuple::CHANNEL)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::HOST)>(t1) ==
std::get<enum_convert(enum_sender_tuple::HOST)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::PORT)>(t1) ==
std::get<enum_convert(enum_sender_tuple::PORT)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::NETNS)>(t1) ==
std::get<enum_convert(enum_sender_tuple::NETNS)>(t2)) &&
(std::get<enum_convert(enum_sender_tuple::MANAGED_NAME)>(t1) >
std::get<enum_convert(enum_sender_tuple::MANAGED_NAME)>(
t2))));
});
}
retries++;
} while (source_conn_detail_list.size() == 0 && retries < 10);
/* if there are no source to connect */
if (source_conn_detail_list.size() == 0) {
LogErr(SYSTEM_LEVEL, ER_RPL_ASYNC_RECONNECT_FAIL_NO_SOURCE,
mi->get_channel(),
"no alternative source is"
" specified",
"add new source details for the channel");
return DoAutoConnFailoverError::no_sources_error;
}
/* When sender list is exhausted reset position. */
if (force_highest_weight ||
mi->get_failover_list_position() >= source_conn_detail_list.size()) {
mi->reset_failover_list_position();
}
#ifndef NDEBUG
if (mi->get_failover_list_position() == 0) {
DBUG_EXECUTE_IF("async_conn_failover_wait_new_sender", {
const char act[] =
"now SIGNAL wait_for_new_sender_selection "
"WAIT_FOR continue_connect_new_sender";
assert(source_conn_detail_list.size() == 3UL);
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
});
DBUG_EXECUTE_IF("async_conn_failover_wait_new_4sender", {
const char act[] =
"now SIGNAL wait_for_new_4sender_selection "
"WAIT_FOR continue_connect_new_4sender";
assert(source_conn_detail_list.size() == 4UL);
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
});
}
#endif
/*
reset current network configuration details with new network
configuration details of chosen source.
*/
if (!set_channel_conn_details(
mi,
std::get<enum_convert(enum_sender_tuple::HOST)>(
source_conn_detail_list[mi->get_failover_list_position()]),
std::get<enum_convert(enum_sender_tuple::PORT)>(
source_conn_detail_list[mi->get_failover_list_position()]),
std::get<enum_convert(enum_sender_tuple::NETNS)>(
source_conn_detail_list[mi->get_failover_list_position()]))) {
/* Increment to next position in source_conn_detail_list list. */
mi->increment_failover_list_position();
return DoAutoConnFailoverError::no_error;
}
return DoAutoConnFailoverError::retriable_error;
}
bool Async_conn_failover_manager::set_channel_conn_details(
Master_info *mi, const std::string host, const uint port,
const std::string network_namespace) {
DBUG_TRACE;
/* used as a bit mask to indicate running replica threads. */
int thread_mask{0};
bool error{false};
/*
CHANGE MASTER command should ignore 'read-only' and 'super_read_only'
options so that it can update 'mysql.slave_master_info' replication
repository tables.
*/
if (mi->channel_trywrlock()) {
return true;
}
/*
When we change master, we first decide which thread is running and
which is not. We dont want this assumption to break while we change master.
Suppose we decide that receiver thread is running and thus it is
safe to change receive related options in mi. By this time if
the receive thread is started, we may have a race condition between
the client thread and receiver thread.
*/
lock_slave_threads(mi);
assert(!host.empty());
strmake(mi->host, host.c_str(), sizeof(mi->host) - 1);
assert(port);
mi->port = port;
if (!network_namespace.empty())
strmake(mi->network_namespace, network_namespace.c_str(),
sizeof(mi->network_namespace) - 1);
/*
Sometimes mi->rli->master_log_pos == 0 (it happens when the SQL thread
is not initialized), so we use a max(). What happens to
mi->rli->master_log_pos during the initialization stages of replication
is not 100% clear, so we guard against problems using max().
*/
mi->set_master_log_pos(std::max<ulonglong>(
BIN_LOG_HEADER_SIZE, mi->rli->get_group_master_log_pos()));
mi->set_master_log_name("");
/*
Get a bit mask for the replica threads that are running.
Since the third argument is false, thread_mask after the function
returns stands for running threads.
*/
init_thread_mask(&thread_mask, mi, false);
/* If the receiver is stopped, flush master_info to disk. */
if ((thread_mask & SLAVE_IO) == 0 && flush_master_info(mi, true)) {
error = true;
my_error(ER_RELAY_LOG_INIT, MYF(0),
"Failed to flush connection metadata repository");
}
unlock_slave_threads(mi);
mi->channel_unlock();
return error;
}
Async_conn_failover_manager::SourceQuorumStatus
Async_conn_failover_manager::get_source_quorum_status(MYSQL *mysql,
Master_info *mi) {
SourceQuorumStatus quorum_status{SourceQuorumStatus::no_error};
MYSQL_RES *source_res = nullptr;
MYSQL_ROW source_row = nullptr;
std::vector<SENDER_CONN_MERGE_TUPLE> source_conn_merged_list{};
bool error{false}, connected_source_in_sender_list{false};
mi->reset_network_error();
/*
Get stored primary details for channel from
replication_asynchronous_connection_failover table.
*/
std::tie(error, source_conn_merged_list) =
Source_IO_monitor::get_instance()->get_senders_details(mi->get_channel());
if (error) {
return SourceQuorumStatus::transient_network_error;
}
for (auto source_conn_detail : source_conn_merged_list) {
std::string host{}, managed_name{};
uint port{0};
std::tie(std::ignore, host, port, std::ignore, std::ignore, managed_name,
std::ignore, std::ignore) = source_conn_detail;
if (host.compare(mi->host) == 0 && port == mi->port &&
!managed_name.empty()) {
connected_source_in_sender_list = true;
break;
}
}
if (!connected_source_in_sender_list) return SourceQuorumStatus::no_error;
std::string query = Source_IO_monitor::get_instance()->get_query(
enum_sql_query_tag::CONFIG_MODE_QUORUM_IO);
if (!mysql_real_query(mysql, query.c_str(), query.length()) &&
(source_res = mysql_store_result(mysql)) &&
(source_row = mysql_fetch_row(source_res))) {
auto curr_quorum_status{
static_cast<enum_conf_mode_quorum_status>(std::stoi(source_row[0]))};
if (curr_quorum_status ==
enum_conf_mode_quorum_status::MANAGED_GR_HAS_QUORUM) {
quorum_status = SourceQuorumStatus::no_error;
} else if (curr_quorum_status ==
enum_conf_mode_quorum_status::MANAGED_GR_HAS_ERROR) {
LogErr(ERROR_LEVEL, ER_RPL_ASYNC_CHANNEL_CANT_CONNECT_NO_QUORUM, mi->host,
mi->port, "", mi->get_channel());
quorum_status = SourceQuorumStatus::no_quorum_error;
}
} else if (mysql_errno(mysql) != ER_UNKNOWN_SYSTEM_VARIABLE) {
if (is_network_error(mysql_errno(mysql))) {
mi->set_network_error();
quorum_status = SourceQuorumStatus::transient_network_error;
} else {
LogErr(WARNING_LEVEL, ER_RPL_ASYNC_EXECUTING_QUERY,
"The IO thread failed to detect if the source belongs to the "
"group majority",
mi->host, mi->port, "", mi->get_channel());
quorum_status = SourceQuorumStatus::fatal_error;
}
}
if (source_res) mysql_free_result(source_res);
return quorum_status;
}
|