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
|
/* Copyright (c) 2022, 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 "sql/binlog/recovery.h"
#include "sql/binlog/decompressing_event_object_istream.h" // binlog::Decompressing_event_object_istream
#include "sql/raii/sentry.h" // raii::Sentry<>
#include "sql/xa/xid_extract.h" // xa::XID_extractor
binlog::Binlog_recovery::Binlog_recovery(Binlog_file_reader &binlog_file_reader)
: m_reader{binlog_file_reader},
m_mem_root{key_memory_binlog_recover_exec,
static_cast<size_t>(my_getpagesize())},
m_set_alloc{&m_mem_root},
m_map_alloc{&m_mem_root},
m_internal_xids{m_set_alloc},
m_external_xids{m_map_alloc} {}
my_off_t binlog::Binlog_recovery::get_valid_pos() const {
return this->m_valid_pos;
}
bool binlog::Binlog_recovery::has_failures() const {
return this->m_is_malformed || this->m_no_engine_recovery;
}
bool binlog::Binlog_recovery::is_binlog_malformed() const {
return this->m_is_malformed;
}
bool binlog::Binlog_recovery::has_engine_recovery_failed() const {
return this->m_no_engine_recovery;
}
std::string const &binlog::Binlog_recovery::get_failure_message() const {
return this->m_failure_message;
}
binlog::Binlog_recovery &binlog::Binlog_recovery::recover() {
binlog::Decompressing_event_object_istream istream{this->m_reader};
std::shared_ptr<Log_event> ev;
this->m_valid_pos = this->m_reader.position();
while (istream >> ev) {
switch (ev->get_type_code()) {
case binary_log::QUERY_EVENT: {
this->process_query_event(dynamic_cast<Query_log_event &>(*ev));
break;
}
case binary_log::XID_EVENT: {
this->process_xid_event(dynamic_cast<Xid_log_event &>(*ev));
break;
}
case binary_log::XA_PREPARE_LOG_EVENT: {
this->process_xa_prepare_event(
dynamic_cast<XA_prepare_log_event &>(*ev));
break;
}
default: {
break;
}
}
// Whenever the current position is at a transaction boundary, save it
// to m_valid_pos
if (!this->m_is_malformed && !this->m_in_transaction &&
!is_gtid_event(ev.get()) && !is_session_control_event(ev.get()))
this->m_valid_pos = this->m_reader.position();
if (this->m_is_malformed) break;
}
if (istream.has_error()) {
using Status_t = binlog::Decompressing_event_object_istream::Status_t;
switch (istream.get_status()) {
case Status_t::corrupted:
case Status_t::out_of_memory:
case Status_t::exceeds_max_size:
// @todo Uncomment this to fix BUG#34828252
// this->m_is_malformed = true;
// this->m_failure_message.assign(istream.get_error_str());
// break;
case Status_t::success:
case Status_t::end:
case Status_t::truncated:
// not malformed, just truncated
break;
}
}
if (!this->m_is_malformed && total_ha_2pc > 1) {
Xa_state_list xa_list{this->m_external_xids};
this->m_no_engine_recovery = ha_recover(&this->m_internal_xids, &xa_list);
if (this->m_no_engine_recovery) {
this->m_failure_message.assign("Recovery failed in storage engines");
}
}
return (*this);
}
void binlog::Binlog_recovery::process_query_event(Query_log_event const &ev) {
std::string query{ev.query};
if (query == "BEGIN" || query.find("XA START") == 0)
this->process_start();
else if (query == "COMMIT")
this->process_commit();
else if (query == "ROLLBACK")
this->process_rollback();
else if (is_atomic_ddl_event(&ev))
this->process_atomic_ddl(ev);
else if (query.find("XA COMMIT") == 0)
this->process_xa_commit(query);
else if (query.find("XA ROLLBACK") == 0)
this->process_xa_rollback(query);
}
void binlog::Binlog_recovery::process_xid_event(Xid_log_event const &ev) {
this->m_is_malformed = !this->m_in_transaction;
if (this->m_is_malformed) {
this->m_failure_message.assign(
"Xid_log_event outside the boundary of a sequence of events "
"representing an active transaction");
return;
}
this->m_in_transaction = false;
if (!this->m_internal_xids.insert(ev.xid).second) {
this->m_is_malformed = true;
this->m_failure_message.assign("Xid_log_event holds an invalid XID");
}
}
void binlog::Binlog_recovery::process_xa_prepare_event(
XA_prepare_log_event const &ev) {
this->m_is_malformed = !this->m_in_transaction;
if (this->m_is_malformed) {
this->m_failure_message.assign(
"XA_prepare_log_event outside the boundary of a sequence of events "
"representing an active transaction");
return;
}
this->m_in_transaction = false;
XID xid;
xid = ev.get_xid();
auto found = this->m_external_xids.find(xid);
if (found != this->m_external_xids.end()) {
assert(found->second != enum_ha_recover_xa_state::PREPARED_IN_SE);
if (found->second == enum_ha_recover_xa_state::PREPARED_IN_TC) {
// If it was found already, must have been committed or rolled back, it
// can't be in prepared state
this->m_is_malformed = true;
this->m_failure_message.assign(
"XA_prepare_log_event holds an invalid XID");
return;
}
}
this->m_external_xids[xid] =
ev.is_one_phase() ? enum_ha_recover_xa_state::COMMITTED_WITH_ONEPHASE
: enum_ha_recover_xa_state::PREPARED_IN_TC;
}
void binlog::Binlog_recovery::process_start() {
this->m_is_malformed = this->m_in_transaction;
if (this->m_is_malformed)
this->m_failure_message.assign(
"Query_log_event containing `BEGIN/XA START` inside the boundary of a "
"sequence of events representing an active transaction");
this->m_in_transaction = true;
}
void binlog::Binlog_recovery::process_commit() {
this->m_is_malformed = !this->m_in_transaction;
if (this->m_is_malformed)
this->m_failure_message.assign(
"Query_log_event containing `COMMIT` outside the boundary of a "
"sequence of events representing an active transaction");
this->m_in_transaction = false;
}
void binlog::Binlog_recovery::process_rollback() {
this->m_is_malformed = !this->m_in_transaction;
if (this->m_is_malformed)
this->m_failure_message.assign(
"Query_log_event containing `ROLLBACK` outside the boundary of a "
"sequence of events representing an active transaction");
this->m_in_transaction = false;
}
void binlog::Binlog_recovery::process_atomic_ddl(Query_log_event const &ev) {
this->m_is_malformed = this->m_in_transaction;
if (this->m_is_malformed) {
this->m_failure_message.assign(
"Query_log event containing a DDL inside the boundary of a sequence of "
"events representing an active transaction");
return;
}
if (!this->m_internal_xids.insert(ev.ddl_xid).second) {
this->m_is_malformed = true;
this->m_failure_message.assign(
"Query_log_event containing a DDL holds an invalid XID");
}
}
void binlog::Binlog_recovery::process_xa_commit(std::string const &query) {
this->m_is_malformed = this->m_in_transaction;
this->m_in_transaction = false;
if (this->m_is_malformed) {
this->m_failure_message.assign(
"Query_log_event containing `XA COMMIT` inside the boundary of a "
"sequence of events representing a transaction not yet in prepared "
"state");
return;
}
this->add_external_xid(query, enum_ha_recover_xa_state::COMMITTED);
if (this->m_is_malformed)
this->m_failure_message.assign(
"Query_log_event containing `XA COMMIT` holds an invalid XID");
}
void binlog::Binlog_recovery::process_xa_rollback(std::string const &query) {
this->m_is_malformed = this->m_in_transaction;
this->m_in_transaction = false;
if (this->m_is_malformed) {
this->m_failure_message.assign(
"Query_log_event containing `XA ROLLBACK` inside the boundary of a "
"sequence of events representing a transaction not yet in prepared "
"state");
return;
}
this->add_external_xid(query, enum_ha_recover_xa_state::ROLLEDBACK);
if (this->m_is_malformed)
this->m_failure_message.assign(
"Query_log_event containing `XA ROLLBACK` holds an invalid XID");
}
void binlog::Binlog_recovery::add_external_xid(std::string const &query,
enum_ha_recover_xa_state state) {
xa::XID_extractor tokenizer{query, 1};
if (tokenizer.size() == 0) {
this->m_is_malformed = true;
return;
}
auto found = this->m_external_xids.find(tokenizer[0]);
if (found != this->m_external_xids.end()) {
assert(found->second != enum_ha_recover_xa_state::PREPARED_IN_SE);
if (found->second != enum_ha_recover_xa_state::PREPARED_IN_TC) {
// If it was found already, it needs to be in prepared in TC state
this->m_is_malformed = true;
return;
}
}
this->m_external_xids[tokenizer[0]] = state;
}
|