| 1 |
/* Copyright (c) 2015, 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 |
@addtogroup Replication |
| 26 |
@{ |
| 27 |
|
| 28 |
@file |
| 29 |
|
| 30 |
@brief Transaction boundary parser definitions. This includes code for |
| 31 |
parsing a stream of events identifying the transaction boundaries (like |
| 32 |
if the event is starting a transaction, is in the middle of a transaction |
| 33 |
or if the event is ending a transaction). |
| 34 |
*/ |
| 35 |
|
| 36 |
#ifndef TRX_BOUNDARY_PARSER_H |
| 37 |
#define TRX_BOUNDARY_PARSER_H |
| 38 |
|
| 39 |
#include <stddef.h> |
| 40 |
|
| 41 |
#include "libbinlogevents/include/control_events.h" |
| 42 |
|
| 43 |
/** |
| 44 |
@class Transaction_boundary_parser |
| 45 |
|
| 46 |
This is the base class for verifying transaction boundaries. |
| 47 |
*/ |
| 48 |
class Transaction_boundary_parser { |
| 49 |
public: |
| 50 |
/** |
| 51 |
The context where the parser is used |
| 52 |
*/ |
| 53 |
enum enum_trx_boundary_parser_context { |
| 54 |
/* Parser used on a receiver, like an IO thread */ |
| 55 |
TRX_BOUNDARY_PARSER_RECEIVER, |
| 56 |
/* Parser used in an applier parsing Relay Log files */ |
| 57 |
TRX_BOUNDARY_PARSER_APPLIER |
| 58 |
}; |
| 59 |
|
| 60 |
/** |
| 61 |
Constructor. |
| 62 |
@param context If this parser is used on a receiver or applier context |
| 63 |
*/ |
| 64 |
Transaction_boundary_parser(enum_trx_boundary_parser_context context) |
| 65 |
: current_parser_state(EVENT_PARSER_NONE), |
| 66 |
last_parser_state(EVENT_PARSER_NONE), |
| 67 |
m_current_boundary_state(EVENT_BOUNDARY_TYPE_ERROR), |
| 68 |
m_trx_boundary_parser_context(context) {} |
| 69 |
|
| 70 |
/** |
| 71 |
Destructor |
| 72 |
*/ |
| 73 |
virtual ~Transaction_boundary_parser(); |
| 74 |
|
| 75 |
/** |
| 76 |
Reset the transaction boundary parser state. |
| 77 |
*/ |
| 78 |
void reset(); |
| 79 |
|
| 80 |
/* |
| 81 |
In an event stream, an event is considered safe to be separated from the |
| 82 |
next if it is not inside a transaction. |
| 83 |
We need to know this in order to evaluate if we will let the relay log |
| 84 |
to be rotated or not. |
| 85 |
*/ |
| 86 |
|
| 87 |
/** |
| 88 |
State if the transaction boundary parser is inside a transaction. |
| 89 |
This "inside a transaction" means that the parser was fed with at least |
| 90 |
one event of a transaction, but the transaction wasn't completely fed yet. |
| 91 |
This also means that the last event fed depends on following event(s) to |
| 92 |
be correctly applied. |
| 93 |
|
| 94 |
@return false if the boundary parser is not inside a transaction. |
| 95 |
true if the boundary parser is inside a transaction. |
| 96 |
*/ |
| 97 |
inline bool is_inside_transaction() { |
| 98 |
return (current_parser_state != EVENT_PARSER_ERROR && |
| 99 |
current_parser_state != EVENT_PARSER_NONE); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
State if the transaction boundary parser is not inside a transaction. |
| 104 |
This "not inside a transaction" means that the parser was fed with an |
| 105 |
event that doesn't depend on following events. |
| 106 |
|
| 107 |
@return false if the boundary parser is inside a transaction. |
| 108 |
true if the boundary parser is not inside a transaction. |
| 109 |
*/ |
| 110 |
inline bool is_not_inside_transaction() { |
| 111 |
return (current_parser_state == EVENT_PARSER_NONE); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
State if the transaction boundary parser was fed with a sequence of events |
| 116 |
that the parser wasn't able to parse correctly. |
| 117 |
|
| 118 |
@return false if the boundary parser is not in the error state. |
| 119 |
true if the boundary parser is in the error state. |
| 120 |
*/ |
| 121 |
inline bool is_error() { |
| 122 |
return (current_parser_state == EVENT_PARSER_ERROR); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
Feed the transaction boundary parser with a Log_event of any type |
| 127 |
in object type. |
| 128 |
|
| 129 |
@param log_event_info the event object |
| 130 |
@param throw_warnings If the function should throw warning messages while |
| 131 |
updating the boundary parser state. |
| 132 |
While initializing the Relay_log_info the |
| 133 |
relay log is scanned backwards and this could |
| 134 |
generate false errors. So, in this case, we |
| 135 |
don't want to throw warnings. |
| 136 |
|
| 137 |
@return false if the transaction boundary parser accepted the event. |
| 138 |
true if the transaction boundary parser didn't accepted the event. |
| 139 |
*/ |
| 140 |
bool feed_event(binary_log::Log_event_basic_info log_event_info, |
| 141 |
bool throw_warnings); |
| 142 |
|
| 143 |
/** |
| 144 |
Evaluate given the current info about boundary type, event type and |
| 145 |
parser state if the given event violates any restriction associated |
| 146 |
to row based only modes. |
| 147 |
|
| 148 |
@param event_info the event information: type, query, is it ignorable |
| 149 |
|
| 150 |
@return true if it violates any restrictions |
| 151 |
false if it passes all tests |
| 152 |
*/ |
| 153 |
bool check_row_logging_constraints( |
| 154 |
binary_log::Log_event_basic_info event_info); |
| 155 |
|
| 156 |
/** |
| 157 |
Rolls back to the last parser state. |
| 158 |
|
| 159 |
This should be called in the case of a failed queued event. |
| 160 |
*/ |
| 161 |
void rollback() { current_parser_state = last_parser_state; } |
| 162 |
|
| 163 |
/** |
| 164 |
Internal error indentifiers for parser issues |
| 165 |
*/ |
| 166 |
enum enum_event_parser_error { |
| 167 |
/* Unexpected event that the parser can't ignore */ |
| 168 |
ER_TRX_BOUND_UNSUPPORTED_UNIGNORABLE_EVENT_IN_STREAM, |
| 169 |
/* Unexpected GTID event in the stream */ |
| 170 |
ER_TRX_BOUND_GTID_LOG_EVENT_IN_STREAM, |
| 171 |
/* Unexpected BEGIN event in the stream */ |
| 172 |
ER_TRX_BOUND_UNEXPECTED_BEGIN_IN_STREAM, |
| 173 |
/* Unexpected Commit event in the stream */ |
| 174 |
ER_TRX_BOUND_UNEXPECTED_COMMIT_ROLLBACK_OR_XID_LOG_EVENT_IN_STREAM, |
| 175 |
/* Unexpected XA Rollback event in the stream */ |
| 176 |
ER_TRX_BOUND_UNEXPECTED_XA_ROLLBACK_IN_STREAM |
| 177 |
}; |
| 178 |
|
| 179 |
private: |
| 180 |
enum enum_event_boundary_type { |
| 181 |
EVENT_BOUNDARY_TYPE_ERROR = -1, |
| 182 |
/* Gtid_log_event */ |
| 183 |
EVENT_BOUNDARY_TYPE_GTID = 0, |
| 184 |
/* Query_log_event(BEGIN), Query_log_event(XA START) */ |
| 185 |
EVENT_BOUNDARY_TYPE_BEGIN_TRX = 1, |
| 186 |
/* Xid, Query_log_event(COMMIT), Query_log_event(ROLLBACK), |
| 187 |
XA_Prepare_log_event */ |
| 188 |
EVENT_BOUNDARY_TYPE_END_TRX = 2, |
| 189 |
/* Query_log_event(XA ROLLBACK) */ |
| 190 |
EVENT_BOUNDARY_TYPE_END_XA_TRX = 3, |
| 191 |
/* User_var, Intvar and Rand */ |
| 192 |
EVENT_BOUNDARY_TYPE_PRE_STATEMENT = 4, |
| 193 |
/* |
| 194 |
All other Query_log_events and all other DML events |
| 195 |
(Rows, Load_data, etc.) |
| 196 |
*/ |
| 197 |
EVENT_BOUNDARY_TYPE_STATEMENT = 5, |
| 198 |
/* Incident */ |
| 199 |
EVENT_BOUNDARY_TYPE_INCIDENT = 6, |
| 200 |
/* |
| 201 |
All non DDL/DML events: Format_desc, Rotate, |
| 202 |
Previous_gtids, Stop, etc. |
| 203 |
*/ |
| 204 |
EVENT_BOUNDARY_TYPE_IGNORE = 7, |
| 205 |
/* |
| 206 |
Transaction payload boundary. |
| 207 |
*/ |
| 208 |
EVENT_BOUNDARY_TYPE_TRANSACTION_PAYLOAD = 8 |
| 209 |
}; |
| 210 |
|
| 211 |
/* |
| 212 |
Internal states for parsing a stream of events. |
| 213 |
|
| 214 |
DDL has the format: |
| 215 |
DDL-1: [GTID] |
| 216 |
DDL-2: [User] [Intvar] [Rand] |
| 217 |
DDL-3: Query |
| 218 |
|
| 219 |
DML has the format: |
| 220 |
DML-1: [GTID] |
| 221 |
DML-2: Query(BEGIN) |
| 222 |
DML-3: Statements |
| 223 |
DML-4: (Query(COMMIT) | Query([XA] ROLLBACK) | Xid | Xa_prepare) |
| 224 |
|
| 225 |
Compressed DML/DDL has the format: |
| 226 |
DDL-1: GTID |
| 227 |
RAW-1: Transaction_Payload |
| 228 |
*/ |
| 229 |
enum enum_event_parser_state { |
| 230 |
/* NONE is set after DDL-3 or DML-4 or RAW-1*/ |
| 231 |
EVENT_PARSER_NONE, |
| 232 |
/* GTID is set after DDL-1 or DML-1 */ |
| 233 |
EVENT_PARSER_GTID, |
| 234 |
/* DDL is set after DDL-2 */ |
| 235 |
EVENT_PARSER_DDL, |
| 236 |
/* DML is set after DML-2 */ |
| 237 |
EVENT_PARSER_DML, |
| 238 |
/* ERROR is set whenever the above pattern is not followed */ |
| 239 |
EVENT_PARSER_ERROR |
| 240 |
}; |
| 241 |
|
| 242 |
/** |
| 243 |
Current internal state of the event parser. |
| 244 |
*/ |
| 245 |
enum_event_parser_state current_parser_state; |
| 246 |
|
| 247 |
/** |
| 248 |
Last internal state of the event parser. |
| 249 |
|
| 250 |
This should be used if we had to roll back the last parsed event. |
| 251 |
*/ |
| 252 |
enum_event_parser_state last_parser_state; |
| 253 |
|
| 254 |
/** |
| 255 |
The last processed boundary event type |
| 256 |
*/ |
| 257 |
enum_event_boundary_type m_current_boundary_state; |
| 258 |
|
| 259 |
/** |
| 260 |
In which context of the boundary parser is used |
| 261 |
*/ |
| 262 |
enum_trx_boundary_parser_context m_trx_boundary_parser_context; |
| 263 |
|
| 264 |
/** |
| 265 |
Parses an raw event based on the event parser logic. |
| 266 |
|
| 267 |
@param event_info Info about an event: type, query, is it ignorable |
| 268 |
@param throw_warnings If the function should throw warning messages |
| 269 |
while updating the boundary parser state. |
| 270 |
@return What is the boundary type associated to this event |
| 271 |
*/ |
| 272 |
enum_event_boundary_type get_event_boundary_type( |
| 273 |
binary_log::Log_event_basic_info event_info, bool throw_warnings); |
| 274 |
|
| 275 |
/** |
| 276 |
Set the boundary parser state based on the event parser logic. |
| 277 |
|
| 278 |
@param event_boundary_type the current event boundary type |
| 279 |
@param throw_warnings If the function should throw warning messages |
| 280 |
while updating the boundary parser state. |
| 281 |
|
| 282 |
@return true if there is an error while updating the state, like unexpected |
| 283 |
event order |
| 284 |
*/ |
| 285 |
bool update_state(enum_event_boundary_type event_boundary_type, |
| 286 |
bool throw_warnings); |
| 287 |
|
| 288 |
/** |
| 289 |
Log warnings using some defined logging interface. |
| 290 |
|
| 291 |
@note: this method is empty by default. Extend it to add a logging routine. |
| 292 |
|
| 293 |
@param error the error number |
| 294 |
@param message the error message |
| 295 |
*/ |
| 296 |
virtual void log_server_warning(int error, const char *message); |
| 297 |
}; |
| 298 |
|
| 299 |
/** |
| 300 |
@} (End of group Replication) |
| 301 |
*/ |
| 302 |
|
| 303 |
#endif /* TRX_BOUNDARY_PARSER_H */ |