MySQL 8.3.0
Source Code Documentation
binlog_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef BINLOG_READER_INCLUDED
24#define BINLOG_READER_INCLUDED
25#include "sql/binlog_istream.h"
26#include "sql/log_event.h"
27
28/**
29 Deserialize a binlog event from event_data. event_data is serialized
30 event object. It is a chunk of data in buffer.
31
32 @param[in] event_data The event data used for deserialization.
33 @param[in] event_data_len Length of event_data
34 @param[in] fde The format_description_event of the event
35 @param[in] verify_checksum Verify event_data's checksum if it is true.
36 @param[out] event the event object generated.
37
38 @retval Binlog_read_error::SUCCESS Success
39 @retval Other than Binlog_read_error::SUCCESS Error
40*/
42 const unsigned char *event_data, unsigned int event_data_len,
44 bool verify_checksum, Log_event **event);
45
47 public:
49 unsigned char *allocate(size_t);
50 void deallocate(unsigned char *ptr);
51};
52/**
53 Binlog_event_data_istream fetches byte data from Basic_istream and
54 divides them into event_data chunk according to the format. Event_data is a
55 serialized event object. It is a chunk of data in buffer.
56*/
58 public:
60 unsigned int max_event_size);
64 delete;
65 virtual ~Binlog_event_data_istream() = default;
66
67 /**
68 Read an event data from the stream and verify its checksum if
69 verify_checksum is true.
70
71 @param[out] data The pointer of the event data
72 @param[out] length The length of the event data
73 @param[in] allocator It is used to allocate memory for the event data.
74 @param[in] verify_checksum Verify the event data's checksum if it is true.
75 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
76 is used only when verify_checksum is true.
77 @retval false Success
78 @retval true Error
79 */
80 template <class ALLOCATOR>
82 unsigned char **data, unsigned int *length, ALLOCATOR *allocator,
83 bool verify_checksum,
86 if (read_event_header() || check_event_header()) return true;
87
88 unsigned char *event_data = allocator->allocate(m_event_length);
89 if (event_data == nullptr)
91
92 if (fill_event_data(event_data, verify_checksum, checksum_alg)) {
93 allocator->deallocate(event_data);
94 return true;
95 }
96 *data = event_data;
98 return false;
99 }
100
101 protected:
103 /**
104 Read the event header from the Basic_istream
105
106 @retval false Success
107 @retval true Error
108 */
109 virtual bool read_event_header();
110 /**
111 Check if it is a valid event header
112
113 @retval false Success
114 @retval true Error
115 */
116 bool check_event_header();
117 /**
118 Read fixed length of data from Basic_istream. It sets error to
119 - Binlog_read_error::SYTEM_IO if Basic_istream returns error.
120 - Binlog_read_error::TRUNC_EVENT if less than length is read.
121 - ERROR_TYPE if zero byte is read. ERROR_TYPE is either TRUNC_EVENT or
122 READ_EOF.
123
124 @param[in] data The buffer where the data stored.
125 @param[in] length Bytes of the data to be read.
126 @retval false Success
127 @retval true Error
128 */
129 template <Binlog_read_error::Error_type ERROR_TYPE>
130 bool read_fixed_length(unsigned char *data, unsigned int length) {
132 if (length == 0) return false;
133
134 longlong ret = m_istream->read(data, length);
135 if (ret == length) return false;
136 switch (ret) {
137 case -1:
139 case 0:
140 return m_error->set_type(ERROR_TYPE);
141 default:
143 }
144 }
145
146 /**
147 It is convenient for caller to share a Binlog_read_error object between
148 streams. So Binlog_read_error pointer is defined here. It should be
149 initialized in constructor by caller.
150 */
152
153 private:
155 unsigned int m_max_event_size;
156 unsigned int m_event_length = 0;
157
158 /**
159 Fill the event data into the given buffer and verify checksum if
160 'verify_checksum' is true.
161
162 @param[in] event_data The buffer where the event data will be stored.
163 @param[in] verify_checksum Verify the event data's checksum if it is true.
164 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
165 is used only when verify_checksum is true.
166 @retval false Success
167 @retval true Error
168 */
169 bool fill_event_data(
170 unsigned char *event_data, bool verify_checksum,
172};
173
174/**
175 It reads event_data from an event_data stream and deserialize them to event
176 object.
177*/
178template <class EVENT_DATA_ISTREAM>
180 public:
182 EVENT_DATA_ISTREAM *istream)
183 : m_error(error), m_data_istream(istream) {}
184
188 delete;
189
190 /**
191 Read an event object from the stream
192
193 @param[in] fde The Format_description_event for deserialization.
194 @param[in] verify_checksum Verify the checksum of the event_data before
195 @param[in] allocator It is used to allocate memory for the event data.
196 @return An valid event object if success.
197 @retval nullptr Error
198 */
199 template <class ALLOCATOR>
202 bool verify_checksum, ALLOCATOR *allocator) {
204 unsigned char *data = nullptr;
205 unsigned int length = 0;
206
207 if (m_data_istream->read_event_data(&data, &length, allocator, false,
208 fde.footer()->checksum_alg))
209 return nullptr;
210
211 Log_event *event = nullptr;
213 verify_checksum, &event))) {
214 allocator->deallocate(data);
215 return nullptr;
216 }
217
218 event->register_temp_buf(reinterpret_cast<char *>(data),
219 ALLOCATOR::DELEGATE_MEMORY_TO_EVENT_OBJECT);
220 return event;
221 }
222
223 private:
224 /**
225 It is convenient for caller to share a Binlog_read_error object between
226 streams. So Binlog_read_error pointer is defined here. It should be
227 initialized in constructor by caller.
228 */
230 EVENT_DATA_ISTREAM *m_data_istream = nullptr;
231};
232
233/// Interface class that all specializations of
234/// template <...> Basic_binlog_file_reader inherit from.
236 public:
242 virtual ~IBasic_binlog_file_reader() = default;
243
244 /// Return true if there was a previous error.
245 virtual bool has_fatal_error() const = 0;
246
247 /// Return the type of error.
249
250 /// Return a string representing the error.
251 ///
252 /// The return value is static memory that is never deallocated.
253 virtual const char *get_error_str() const = 0;
254
255 /// Return the current position in bytes, relative to the beginning
256 /// of the file.
257 virtual my_off_t position() const = 0;
258
259 /// Read the next event from the stream.
260 ///
261 /// This function creates the object with "new". It is the caller's
262 /// responsibility to delete it.
263 ///
264 /// @return Log_event on success, nullptr on error. More
265 /// information about the error can be found using `get_error_type`
266 /// and `get_error_str`.
268
269 /// Return a pointer the currently used
270 /// Format_description_log_event.
271 ///
272 /// The event is a member by this reader, so the caller must not use
273 /// the returned reference after this reader has been deleted.
276};
277
278/**
279 It owns an allocator, a byte stream, an event_data stream and an event object
280 stream. The stream pipeline is setup in the constructor. All the objects
281 required for reading a binlog file is initialized in reader class.
282
283 It maintains the Format_description_event which is needed for reading the
284 following binlog events. A default format_description_event is initialized
285 at the beginning. Then it will be replaced by the one read from the binlog
286 file.
287
288 Some convenient functions is added to encapsulate the access of IFILE,
289 EVENT_DATA_ISTREAM, EVENT_OBJECT_ISTREAM. It makes the code
290 simpler for reading a binlog file.
291*/
292template <class IFILE, class EVENT_DATA_ISTREAM,
293 template <class> class EVENT_OBJECT_ISTREAM, class ALLOCATOR>
295 public:
296 typedef EVENT_DATA_ISTREAM Event_data_istream;
297 typedef EVENT_OBJECT_ISTREAM<Event_data_istream> Event_object_istream;
298
299 Basic_binlog_file_reader(bool verify_checksum,
300 unsigned int max_event_size = UINT_MAX)
301 : m_ifile(&m_error),
302 m_data_istream(&m_error, &m_ifile, max_event_size),
305 m_verify_checksum(verify_checksum) {}
306
310 delete;
312 delete;
314
315 /**
316 Open a binlog file and set read position to offset. It will read and store
317 Format_description_event automatically if offset is bigger than current
318 position and fde is nullptr. Otherwise fde is use instead of finding fde
319 from the file if fde is not null.
320
321 @param[in] file_name name of the binlog file which will be opened.
322 @param[in] offset The position where it starts to read.
323 @param[out] fdle For returning an Format_description_log_event object.
324 @retval false Success
325 @retval true Error
326 */
327 bool open(const char *file_name, my_off_t offset = 0,
328 Format_description_log_event **fdle = nullptr) {
330 if (m_ifile.open(file_name)) return true;
331
333 if (!fd) return has_fatal_error();
334
335 if (position() < offset && seek(offset)) {
336 delete fd;
337 return true;
338 }
339 if (fdle)
340 *fdle = fd;
341 else
342 delete fd;
343 return false;
344 }
345 /**
346 Close the binlog file.
347 */
348 void close() {
349 m_ifile.close();
352 }
353
354 bool is_open() const { return m_ifile.is_open(); }
355 my_off_t position() const override { return m_ifile.position(); }
356 bool seek(my_off_t pos) { return m_ifile.seek(pos); }
357
358 /**
359 Wrapper of EVENT_DATA_ISTREAM::read_event_data.
360 */
361 bool read_event_data(unsigned char **data, unsigned int *length) {
363 return m_data_istream.read_event_data(data, length, &m_allocator,
366 }
367 /**
368 wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
369 */
372 Log_event *ev = m_object_istream.read_event_object(m_fde, m_verify_checksum,
373 &m_allocator);
374 if (ev != nullptr &&
376 m_fde =
378 return ev;
379 }
380
381 bool has_fatal_error() const override { return m_error.has_fatal_error(); }
382 /**
383 Return the error happened in the stream pipeline.
384 */
386 return m_error.get_type();
387 }
388 /**
389 Return the error message of the error happened in the stream pipeline.
390 */
391 const char *get_error_str() const override { return m_error.get_str(); }
392
393 IFILE *ifile() { return &m_ifile; }
396 ALLOCATOR *allocator() { return &m_allocator; }
397
400 m_fde = fde;
401 }
403 format_description_event() const override {
404 return m_fde;
405 }
407
408 /**
409 @brief Resets the error. Sets it to Binlog_read_error::SUCCESS.
410 */
412
413 private:
415
416 IFILE m_ifile;
419 ALLOCATOR m_allocator;
420
422 bool m_verify_checksum = false;
424
425 /**
426 Read the Format_description_log_events before 'offset'.
427
428 @param[in] offset The position where the read should stop.
429 @return A valid Format_description_log_event pointer or nullptr.
430 */
434 Format_description_log_event *fdle = nullptr;
435 /*
436 Format_description_event is skipped, so we initialize m_fde here. For
437 relay log, it need to find master's Format_description_event. master's
438 Format_description_event is the 3rd or 4th event of a relay log file.
439 Relay log's format looks like:
440 Format_description_event : relay log's Format_description_event
441 Previous_gtid_event
442 [Rotate_event] : In the case rotating relaylog, no Rotate here
443 Format_description_event : master's Format_description_event
444 */
445 while (position() < offset) {
447
448 Log_event *ev = m_object_istream.read_event_object(
450
451 if (ev == nullptr) break;
452 if (ev->get_type_code() ==
454 delete fdle;
455 fdle = dynamic_cast<Format_description_log_event *>(ev);
456 m_fde = *fdle;
457 } else {
459 delete ev;
462 break;
463 }
464 }
465 if (has_fatal_error()) {
466 delete fdle;
467 return nullptr;
468 }
469 return fdle;
470 }
471};
472
473#ifdef MYSQL_SERVER
482#endif // MYSQL_SERVER
483#endif // BINLOG_READER_INCLUDED
#define LOG_EVENT_MINIMAL_HEADER_LEN
Fixed header length, where 4.x and 5.0 agree.
Definition: binlog_event.h:455
Binlog_read_error::Error_type binlog_event_deserialize(const unsigned char *event_data, unsigned int event_data_len, const mysql::binlog::event::Format_description_event *fde, bool verify_checksum, Log_event **event)
Deserialize a binlog event from event_data.
Definition: binlog_reader.cc:124
Basic_binlog_file_reader< Binlog_ifile, Binlog_event_data_istream, Binlog_event_object_istream, Default_binlog_event_allocator > Binlog_file_reader
Definition: binlog_reader.h:477
Basic_binlog_file_reader< Relaylog_ifile, Binlog_event_data_istream, Binlog_event_object_istream, Default_binlog_event_allocator > Relaylog_file_reader
Definition: binlog_reader.h:481
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:294
Event_data_istream m_data_istream
Definition: binlog_reader.h:417
Event_data_istream * event_data_istream()
Definition: binlog_reader.h:394
EVENT_DATA_ISTREAM Event_data_istream
Definition: binlog_reader.h:296
Format_description_log_event * read_fdle(my_off_t offset)
Read the Format_description_log_events before 'offset'.
Definition: binlog_reader.h:431
void set_format_description_event(const mysql::binlog::event::Format_description_event &fde)
Definition: binlog_reader.h:398
bool open(const char *file_name, my_off_t offset=0, Format_description_log_event **fdle=nullptr)
Open a binlog file and set read position to offset.
Definition: binlog_reader.h:327
~Basic_binlog_file_reader() override
Definition: binlog_reader.h:313
const char * get_error_str() const override
Return the error message of the error happened in the stream pipeline.
Definition: binlog_reader.h:391
bool m_verify_checksum
Definition: binlog_reader.h:422
Binlog_read_error::Error_type get_error_type() const override
Return the error happened in the stream pipeline.
Definition: binlog_reader.h:385
ALLOCATOR m_allocator
Definition: binlog_reader.h:419
Event_object_istream * event_object_istream()
Definition: binlog_reader.h:395
bool is_open() const
Definition: binlog_reader.h:354
bool has_fatal_error() const override
Return true if there was a previous error.
Definition: binlog_reader.h:381
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &&)=delete
my_off_t event_start_pos()
Definition: binlog_reader.h:406
Binlog_read_error m_error
Definition: binlog_reader.h:414
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &)=delete
EVENT_OBJECT_ISTREAM< Event_data_istream > Event_object_istream
Definition: binlog_reader.h:297
Event_object_istream m_object_istream
Definition: binlog_reader.h:418
ALLOCATOR * allocator()
Definition: binlog_reader.h:396
void close()
Close the binlog file.
Definition: binlog_reader.h:348
IFILE m_ifile
Definition: binlog_reader.h:416
mysql::binlog::event::Format_description_event m_fde
Definition: binlog_reader.h:421
bool read_event_data(unsigned char **data, unsigned int *length)
Wrapper of EVENT_DATA_ISTREAM::read_event_data.
Definition: binlog_reader.h:361
my_off_t m_event_start_pos
Definition: binlog_reader.h:423
Basic_binlog_file_reader(bool verify_checksum, unsigned int max_event_size=UINT_MAX)
Definition: binlog_reader.h:299
my_off_t position() const override
Return the current position in bytes, relative to the beginning of the file.
Definition: binlog_reader.h:355
Basic_binlog_file_reader(const Basic_binlog_file_reader &&)=delete
const mysql::binlog::event::Format_description_event & format_description_event() const override
Return a pointer the currently used Format_description_log_event.
Definition: binlog_reader.h:403
IFILE * ifile()
Definition: binlog_reader.h:393
Basic_binlog_file_reader(const Basic_binlog_file_reader &)=delete
Log_event * read_event_object() override
wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
Definition: binlog_reader.h:370
void reset_error()
Resets the error.
Definition: binlog_reader.h:411
bool seek(my_off_t pos)
Definition: binlog_reader.h:356
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:34
virtual ssize_t read(unsigned char *buffer, size_t length)=0
Read some bytes from the input stream.
Binlog_event_data_istream fetches byte data from Basic_istream and divides them into event_data chunk...
Definition: binlog_reader.h:57
unsigned int m_max_event_size
Definition: binlog_reader.h:155
bool read_event_data(unsigned char **data, unsigned int *length, ALLOCATOR *allocator, bool verify_checksum, mysql::binlog::event::enum_binlog_checksum_alg checksum_alg)
Read an event data from the stream and verify its checksum if verify_checksum is true.
Definition: binlog_reader.h:81
Basic_istream * m_istream
Definition: binlog_reader.h:154
bool read_fixed_length(unsigned char *data, unsigned int length)
Read fixed length of data from Basic_istream.
Definition: binlog_reader.h:130
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:151
unsigned int m_event_length
Definition: binlog_reader.h:156
Binlog_event_data_istream & operator=(const Binlog_event_data_istream &)=delete
bool check_event_header()
Check if it is a valid event header.
Definition: binlog_reader.cc:107
virtual bool read_event_header()
Read the event header from the Basic_istream.
Definition: binlog_reader.cc:73
virtual ~Binlog_event_data_istream()=default
Binlog_event_data_istream(const Binlog_event_data_istream &)=delete
bool fill_event_data(unsigned char *event_data, bool verify_checksum, mysql::binlog::event::enum_binlog_checksum_alg checksum_alg)
Fill the event data into the given buffer and verify checksum if 'verify_checksum' is true.
Definition: binlog_reader.cc:78
unsigned char m_header[LOG_EVENT_MINIMAL_HEADER_LEN]
Definition: binlog_reader.h:102
It reads event_data from an event_data stream and deserialize them to event object.
Definition: binlog_reader.h:179
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:229
EVENT_DATA_ISTREAM * m_data_istream
Definition: binlog_reader.h:230
Binlog_event_object_istream(const Binlog_event_object_istream &)=delete
Binlog_event_object_istream & operator=(const Binlog_event_object_istream &)=delete
Binlog_event_object_istream(Binlog_read_error *error, EVENT_DATA_ISTREAM *istream)
Definition: binlog_reader.h:181
Log_event * read_event_object(const mysql::binlog::event::Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Read an event object from the stream.
Definition: binlog_reader.h:200
Binlog input file.
Definition: binlog_istream.h:240
It defines the error types which could happen when reading binlog files or deserializing binlog event...
Definition: binlog_istream.h:36
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:87
Error_type
Possible errors which happens when reading an event.
Definition: binlog_istream.h:41
@ MEM_ALLOCATE
Definition: binlog_istream.h:55
@ SUCCESS
Definition: binlog_istream.h:43
@ SYSTEM_IO
Definition: binlog_istream.h:53
@ TRUNC_EVENT
Definition: binlog_istream.h:57
bool set_type(Error_type type)
Set m_error to error.
Definition: binlog_istream.h:105
const char * get_str() const
Return error message of the error type.
Definition: binlog_istream.cc:33
bool has_fatal_error() const
Definition: binlog_istream.h:82
Definition: binlog_reader.h:46
unsigned char * allocate(size_t)
Definition: binlog_reader.cc:32
@ DELEGATE_MEMORY_TO_EVENT_OBJECT
Definition: binlog_reader.h:48
void deallocate(unsigned char *ptr)
Definition: binlog_reader.cc:38
For binlog version 4.
Definition: log_event.h:1524
Interface class that all specializations of template <...> Basic_binlog_file_reader inherit from.
Definition: binlog_reader.h:235
IBasic_binlog_file_reader(IBasic_binlog_file_reader &)=delete
IBasic_binlog_file_reader & operator=(IBasic_binlog_file_reader &&)=delete
virtual ~IBasic_binlog_file_reader()=default
IBasic_binlog_file_reader & operator=(IBasic_binlog_file_reader &)=delete
virtual const char * get_error_str() const =0
Return a string representing the error.
virtual my_off_t position() const =0
Return the current position in bytes, relative to the beginning of the file.
IBasic_binlog_file_reader(IBasic_binlog_file_reader &&)=delete
IBasic_binlog_file_reader()=default
virtual Binlog_read_error::Error_type get_error_type() const =0
Return the type of error.
virtual const mysql::binlog::event::Format_description_event & format_description_event() const =0
Return a pointer the currently used Format_description_log_event.
virtual Log_event * read_event_object()=0
Read the next event from the stream.
virtual bool has_fatal_error() const =0
Return true if there was a previous error.
This is the abstract base class for binary log events.
Definition: log_event.h:537
virtual mysql::binlog::event::Log_event_type get_type_code() const
Definition: log_event.h:796
void register_temp_buf(char *buf, bool free_in_destructor=true)
Definition: log_event.h:864
Relaylog input file.
Definition: binlog_istream.h:252
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:965
For binlog version 4.
Definition: control_events.h:238
mysql::binlog::event::Format_description_event Format_description_event
Definition: file_storage.cc:39
#define BINLOG_VERSION
binlog_version 3 is MySQL 4.x; 4 is MySQL 5.0.0.
Definition: binlog_event.h:105
Binary log event definitions.
#define DBUG_TRACE
Definition: my_dbug.h:145
ulonglong my_off_t
Definition: my_inttypes.h:71
long long int longlong
Definition: my_inttypes.h:54
static char * server_version
Definition: mysql.cc:119
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:93
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:285
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:311
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:353
@ ROTATE_EVENT
Definition: binlog_event.h:301
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:460
required string type
Definition: replication_group_member_actions.proto:33
required string event
Definition: replication_group_member_actions.proto:31