MySQL 8.0.37
Source Code Documentation
recovery.h
Go to the documentation of this file.
1/* Copyright (c) 2022, 2024, 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#ifndef BINLOG_RECOVERY_H_INCLUDED
25#define BINLOG_RECOVERY_H_INCLUDED
26
27#include "sql/binlog/global.h"
28#include "sql/binlog_ostream.h" // binlog::tools::Iterator
29#include "sql/binlog_reader.h" // Binlog_file_reader
30#include "sql/log_event.h" // Log_event
31#include "sql/xa.h" // XID
32
33namespace binlog {
34/**
35 Recovers from last crashed binlog at server start.
36
37 After a crash, storage engines may contain transactions that are prepared
38 but not committed (in theory any engine, in practice InnoDB). This
39 classe's methods use the binary log as the source of truth to determine
40 which of these transactions should be committed and which should be
41 rolled back.
42
43 The `Binlog::recovery()` method collects the following from the last
44 available binary log:
45 - the list of internally coordinated transactions (normal) that are
46 completely written to the binary log.
47 - the list of externally coordinated transactions (XA) that appear in the
48 binary log, along the state those transactions are in.
49
50 The list of XIDs of all internally coordinated transactions that are
51 completely written to the binary log is passed to the storage engines
52 through the ha_recover function in the handler interface. This tells the
53 storage engines to commit all prepared transactions that are in the set,
54 and to roll back all prepared transactions that are not in the set.
55
56 The list of XIDs of all externally coordinated transactions that appear
57 in the binary log, along with the state they are in, is passed to the
58 storage engines through the ha_recover function in the handler
59 interface. The storage engine will determine if the transaction is to be
60 kept at PREPARE, is to be COMMITTED or ROLLED BACK, in accordance with:
61 the state that is provided in the list; the internal storage engine state
62 for the transaction.
63*/
65 public:
66 /**
67 Class constructor.
68
69 @param binlog_file_reader The already instantiated and initialized file
70 reader for the last available binary log
71 file.
72 */
73 Binlog_recovery(Binlog_file_reader &binlog_file_reader);
74 virtual ~Binlog_recovery() = default;
75
76 /**
77 Retrieves the position of the last binlog event that ended a
78 transaction.
79
80 @return The position of the last binlog event that ended a
81 transaction.
82 */
83 my_off_t get_valid_pos() const;
84 /**
85 Retrieves whether or not the recovery process ended successfully.
86
87 @see Binlog_recovery::is_binlog_malformed()
88 @see Binlog_recovery::has_engine_recovery_failed()
89
90 @return true if the recovery process ended with errors, false
91 otherwise.
92 */
93 bool has_failures() const;
94 /**
95 Retrieves whether or not the binary log was correctly processed in
96 full.
97
98 @return true if the binary log processing ended with errors, false
99 otherwise.
100 */
101 bool is_binlog_malformed() const;
102 /**
103 Retrieves whether or not the storage engines XA recovery process
104 completed successfully.
105
106 @return false if the storge engines completed the XA recovery process
107 successfully, true otherwise.
108 */
109 bool has_engine_recovery_failed() const;
110 /**
111 Retrieves the textual representation of the encontered failure, if any.
112
113 @return the string containing the textual representation of the failure,
114 an empty string otherwise.
115 */
116 std::string const &get_failure_message() const;
117 /**
118 Uses the provided binary log file reader to inspect the binary log and
119 extract transaction information.
120
121 The following is collected from the provided binlog file reader:
122 - the list of internally coordinated transactions (normal) that are
123 completely written to the binary log.
124 - the list of externally coordinated transactions (XA) that appear in
125 the binary log, along the state those transactions are in.
126
127 The list of XIDs of all internally coordinated transactions that are
128 completely written to the binary log is passed to the storage engines
129 through the ha_recover function in the handler interface. This tells the
130 storage engines to commit all prepared transactions that are in the set,
131 and to roll back all prepared transactions that are not in the set.
132
133 The list of XIDs of all externally coordinated transactions that appear
134 in the binary log, along with the state they are in, is passed to the
135 storage engines through the ha_recover function in the handler
136 interface. The storage engine will determine if the transaction is to be
137 kept at PREPARE, is to be COMMITTED or ROLLED BACK, in accordance with:
138 the state that is provided in the list; the internal storage engine state
139 for the transaction.
140
141 After `recover()` returns, `has_failures()` should be invoked to
142 determine if the recover process ended successfully. Additionally,
143 `is_binlog_malformed()` and `has_engine_recovery_failed()` can be
144 invoked to determine the type of error that occurred.
145
146 @return This instance's reference, for chaining purposes.
147 */
149
150 private:
151 /** File reader for the last available binary log file */
153 /** Position of the last binlog event that ended a transaction */
155 /** Whether or not the event being processed is within a transaction */
156 bool m_in_transaction{false};
157 /** Whether or not the binary log is malformed/corrupted */
158 bool m_is_malformed{false};
159 /** Whether or not the recovery in the storage engines failed */
161 /** Textual representation of the encountered failure */
162 std::string m_failure_message{""};
163 /** Memory pool to use for the XID lists */
165 /** Memory pool allocator to use with the normal transaction list */
167 /** Memory pool allocator to use with the XA transaction list */
169 /** List of normal transactions fully written to the binary log */
171 /** List of XA transactions and states that appear in the binary log */
173
174 /**
175 Invoked when a `Query_log_event` is read from the binary log file
176 reader. The underlying query string is inspected to determine if the
177 SQL command starts or ends a transaction. The following commands are
178 searched for:
179 - BEGIN
180 - COMMIT
181 - ROLLBACK
182 - DDL
183 - XA START
184 - XA COMMIT
185 - XA ROLLBACK
186
187 Check below for the description of the action that is taken for each.
188
189 @param ev The `Query_log_event` to process
190 */
191 void process_query_event(Query_log_event const &ev);
192 /**
193 Invoked when a `Xid_log_event` is read from the binary log file
194 reader.
195
196 Actions taken to process the event:
197 - If `m_in_transaction` flag is set to false, `m_is_malformed` is set
198 to true, indicating that the binary log is malformed.
199 - The `m_in_transaction` flag is set to false, indicating that the
200 event ends a transaction.
201 - The XID of the transaction is extracted and added to the list of
202 internally coordinated transactions `m_internal_xids`.
203 - If the XID already exists in the list, `m_is_malformed` is set to
204 true, indicating that the binary log is malformed.
205
206 @param ev The `Xid_log_event` to process
207 */
208 void process_xid_event(Xid_log_event const &ev);
209 /**
210 Invoked when a `XA_prepare_log_event` is read from the binary log file
211 reader.
212
213 Actions taken to process the event:
214 - If `m_in_transaction` flag is set to false, `m_is_malformed` is set
215 to true, indicating that the binary log is malformed.
216 - The `m_in_transaction` flag is set to false, indicating that the
217 event ends a transaction.
218 - The XID of the transaction is extracted and added to the list of
219 externally coordinated transactions `m_external_xids`, along side the
220 state COMMITTED if the event represents an `XA COMMIT ONE_PHASE` or
221 PREPARED if not.
222 - If the XID already exists in the list associated with a state other
223 than `COMMITTED` or `ROLLEDBACK`, `m_is_malformed` is set to true,
224 indicating that the binary log is malformed.
225
226 @param ev The `XA_prepare_log_event` to process
227 */
229 /**
230 Invoked when a `BEGIN` or an `XA START' is found in a
231 `Query_log_event`.
232
233 Actions taken to process the statement:
234 - If `m_in_transaction` flag is set to true, `m_is_malformed` is set
235 to true, indicating that the binary log is malformed.
236 - The `m_in_transaction` flag is set to true, indicating that the
237 event starts a transaction.
238 */
239 void process_start();
240 /**
241 Invoked when a `COMMIT` is found in a `Query_log_event`.
242
243 Actions taken to process the statement:
244 - If `m_in_transaction` flag is set to false, `m_is_malformed` is set
245 to true, indicating that the binary log is malformed.
246 - The `m_in_transaction` flag is set to false, indicating that the
247 event starts a transaction.
248 */
249 void process_commit();
250 /**
251 Invoked when a `ROLLBACK` is found in a `Query_log_event`.
252
253 Actions taken to process the statement:
254 - If `m_in_transaction` flag is set to false, `m_is_malformed` is set
255 to true, indicating that the binary log is malformed.
256 - The `m_in_transaction` flag is set to false, indicating that the
257 event starts a transaction.
258 */
259 void process_rollback();
260 /**
261 Invoked when a DDL is found in a `Query_log_event`.
262
263 Actions taken to process the statement:
264 - If `m_in_transaction` flag is set to true, `m_is_malformed` is set
265 to true, indicating that the binary log is malformed.
266 - The XID of the transaction is extracted and added to the list of
267 internally coordinated transactions `m_internal_xids`.
268 - If the XID already exists in the list, `m_is_malformed` is set to
269 true, indicating that the binary log is malformed.
270
271 @param ev The `Query_log_event` to process
272 */
273 void process_atomic_ddl(Query_log_event const &ev);
274 /**
275 Invoked when an `XA COMMIT` is found in a `Query_log_event`.
276
277 Actions taken to process the statement:
278 - If `m_in_transaction` flag is set to true, `m_is_malformed` is set
279 to true, indicating that the binary log is malformed.
280 - The `m_in_transaction` flag is set to false, indicating that the
281 event ends a transaction.
282 - The XID of the transaction is extracted and added to the list of
283 externally coordinated transactions `m_external_xids`, alongside the
284 state COMMITTED.
285 - If the XID already exists in the list associated with a state other
286 than `PREPARED`, `m_is_malformed` is set to true, indicating that the
287 binary log is malformed.
288
289 @param query The query string to process
290 */
291 void process_xa_commit(std::string const &query);
292 /**
293 Invoked when an `XA ROLLBACK` is found in a `Query_log_event`.
294
295 Actions taken to process the statement:
296 - If `m_in_transaction` flag is set to true, `m_is_malformed` is set
297 to true, indicating that the binary log is malformed.
298 - The `m_in_transaction` flag is set to false, indicating that the
299 event ends a transaction.
300 - The XID of the transaction is extracted and added to the list of
301 externally coordinated transactions `m_external_xids`, along side the
302 state ROLLEDBACK.
303 - If the XID already exists in the list associated with a state other
304 than `PREPARED`, `m_is_malformed` is set to true, indicating that the
305 binary log is malformed.
306
307 @param query The query string to process
308 */
309 void process_xa_rollback(std::string const &query);
310 /**
311 Parses the provided string for an XID and adds it to the externally
312 coordinated transactions map, along side the provided state.
313
314 @param query The query to search and retrieve the XID from
315 @param state The state to add to the map, along side the XID
316 */
317 void add_external_xid(std::string const &query,
319};
320} // namespace binlog
321
322#endif // BINLOG_RECOVERY_H_INCLUDED
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:291
Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
Definition: mem_root_allocator.h:68
A Query event is written to the binary log whenever the database is modified on the master,...
Definition: log_event.h:1284
Similar to Xid_log_event except that.
Definition: log_event.h:1784
std::map< XID, enum_ha_recover_xa_state, std::less< XID >, Xa_state_list::allocator > list
Definition: handler.h:1256
This is the subclass of Xid_event defined in libbinlogevent, An XID event is generated for a commit o...
Definition: log_event.h:1740
Recovers from last crashed binlog at server start.
Definition: recovery.h:64
void process_xa_commit(std::string const &query)
Invoked when an XA COMMIT is found in a Query_log_event.
Definition: recovery.cc:232
bool m_is_malformed
Whether or not the binary log is malformed/corrupted.
Definition: recovery.h:158
bool m_no_engine_recovery
Whether or not the recovery in the storage engines failed.
Definition: recovery.h:160
Binlog_file_reader & m_reader
File reader for the last available binary log file.
Definition: recovery.h:152
Mem_root_allocator< my_xid > m_set_alloc
Memory pool allocator to use with the normal transaction list.
Definition: recovery.h:166
Binlog_recovery(Binlog_file_reader &binlog_file_reader)
Class constructor.
Definition: recovery.cc:30
bool has_failures() const
Retrieves whether or not the recovery process ended successfully.
Definition: recovery.cc:43
void process_start()
Invoked when a BEGIN or an ‘XA START’ is found in a Query_log_event.
Definition: recovery.cc:190
Mem_root_allocator< std::pair< const XID, XID_STATE::xa_states > > m_map_alloc
Memory pool allocator to use with the XA transaction list.
Definition: recovery.h:168
bool m_in_transaction
Whether or not the event being processed is within a transaction.
Definition: recovery.h:156
void process_rollback()
Invoked when a ROLLBACK is found in a Query_log_event.
Definition: recovery.cc:208
virtual ~Binlog_recovery()=default
Xa_state_list::list m_external_xids
List of XA transactions and states that appear in the binary log.
Definition: recovery.h:172
Binlog_recovery & recover()
Uses the provided binary log file reader to inspect the binary log and extract transaction informatio...
Definition: recovery.cc:59
void process_atomic_ddl(Query_log_event const &ev)
Invoked when a DDL is found in a Query_log_event.
Definition: recovery.cc:217
Xid_commit_list m_internal_xids
List of normal transactions fully written to the binary log.
Definition: recovery.h:170
void process_xa_prepare_event(XA_prepare_log_event const &ev)
Invoked when a XA_prepare_log_event is read from the binary log file reader.
Definition: recovery.cc:158
void process_xa_rollback(std::string const &query)
Invoked when an XA ROLLBACK is found in a Query_log_event.
Definition: recovery.cc:248
void process_query_event(Query_log_event const &ev)
Invoked when a Query_log_event is read from the binary log file reader.
Definition: recovery.cc:121
void process_xid_event(Xid_log_event const &ev)
Invoked when a Xid_log_event is read from the binary log file reader.
Definition: recovery.cc:143
my_off_t get_valid_pos() const
Retrieves the position of the last binlog event that ended a transaction.
Definition: recovery.cc:39
void process_commit()
Invoked when a COMMIT is found in a Query_log_event.
Definition: recovery.cc:199
std::string const & get_failure_message() const
Retrieves the textual representation of the encontered failure, if any.
Definition: recovery.cc:55
void add_external_xid(std::string const &query, enum_ha_recover_xa_state state)
Parses the provided string for an XID and adds it to the externally coordinated transactions map,...
Definition: recovery.cc:264
std::string m_failure_message
Textual representation of the encountered failure.
Definition: recovery.h:162
my_off_t m_valid_pos
Position of the last binlog event that ended a transaction.
Definition: recovery.h:154
bool is_binlog_malformed() const
Retrieves whether or not the binary log was correctly processed in full.
Definition: recovery.cc:47
MEM_ROOT m_mem_root
Memory pool to use for the XID lists.
Definition: recovery.h:164
bool has_engine_recovery_failed() const
Retrieves whether or not the storage engines XA recovery process completed successfully.
Definition: recovery.cc:51
Binary log event definitions.
ulonglong my_off_t
Definition: my_inttypes.h:72
static char * query
Definition: myisam_ftdump.cc:45
Definition: decompressing_event_object_istream.cc:31
enum_ha_recover_xa_state
Enumeration of possible states for externally coordinated transactions (XA).
Definition: handler.h:1231
std::unordered_set< my_xid, std::hash< my_xid >, std::equal_to< my_xid >, Mem_root_allocator< my_xid > > Xid_commit_list
Single occurrence set of XIDs of internally coordinated transactions found as been committed in the t...
Definition: handler.h:1245
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83