MySQL 8.1.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,
43 const Format_description_event *fde, bool verify_checksum,
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>
81 bool read_event_data(unsigned char **data, unsigned int *length,
82 ALLOCATOR *allocator, bool verify_checksum,
83 enum_binlog_checksum_alg checksum_alg) {
85 if (read_event_header() || check_event_header()) return true;
86
87 unsigned char *event_data = allocator->allocate(m_event_length);
88 if (event_data == nullptr)
90
91 if (fill_event_data(event_data, verify_checksum, checksum_alg)) {
92 allocator->deallocate(event_data);
93 return true;
94 }
95 *data = event_data;
97 return false;
98 }
99
100 protected:
102 /**
103 Read the event header from the Basic_istream
104
105 @retval false Success
106 @retval true Error
107 */
108 virtual bool read_event_header();
109 /**
110 Check if it is a valid event header
111
112 @retval false Success
113 @retval true Error
114 */
115 bool check_event_header();
116 /**
117 Read fixed length of data from Basic_istream. It sets error to
118 - Binlog_read_error::SYTEM_IO if Basic_istream returns error.
119 - Binlog_read_error::TRUNC_EVENT if less than length is read.
120 - ERROR_TYPE if zero byte is read. ERROR_TYPE is either TRUNC_EVENT or
121 READ_EOF.
122
123 @param[in] data The buffer where the data stored.
124 @param[in] length Bytes of the data to be read.
125 @retval false Success
126 @retval true Error
127 */
128 template <Binlog_read_error::Error_type ERROR_TYPE>
129 bool read_fixed_length(unsigned char *data, unsigned int length) {
131 if (length == 0) return false;
132
133 longlong ret = m_istream->read(data, length);
134 if (ret == length) return false;
135 switch (ret) {
136 case -1:
138 case 0:
139 return m_error->set_type(ERROR_TYPE);
140 default:
142 }
143 }
144
145 /**
146 It is convenient for caller to share a Binlog_read_error object between
147 streams. So Binlog_read_error pointer is defined here. It should be
148 initialized in constructor by caller.
149 */
151
152 private:
154 unsigned int m_max_event_size;
155 unsigned int m_event_length = 0;
156
157 /**
158 Fill the event data into the given buffer and verify checksum if
159 'verify_checksum' is true.
160
161 @param[in] event_data The buffer where the event data will be stored.
162 @param[in] verify_checksum Verify the event data's checksum if it is true.
163 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
164 is used only when verify_checksum is true.
165 @retval false Success
166 @retval true Error
167 */
168 bool fill_event_data(unsigned char *event_data, bool verify_checksum,
169 enum_binlog_checksum_alg checksum_alg);
170};
171
172/**
173 It reads event_data from an event_data stream and deserialize them to event
174 object.
175*/
176template <class EVENT_DATA_ISTREAM>
178 public:
180 EVENT_DATA_ISTREAM *istream)
181 : m_error(error), m_data_istream(istream) {}
182
186 delete;
187
188 /**
189 Read an event object from the stream
190
191 @param[in] fde The Format_description_event for deserialization.
192 @param[in] verify_checksum Verify the checksum of the event_data before
193 @param[in] allocator It is used to allocate memory for the event data.
194 @return An valid event object if success.
195 @retval nullptr Error
196 */
197 template <class ALLOCATOR>
199 bool verify_checksum, ALLOCATOR *allocator) {
201 unsigned char *data = nullptr;
202 unsigned int length = 0;
203
204 if (m_data_istream->read_event_data(&data, &length, allocator, false,
205 fde.footer()->checksum_alg))
206 return nullptr;
207
208 Log_event *event = nullptr;
210 verify_checksum, &event))) {
211 allocator->deallocate(data);
212 return nullptr;
213 }
214
215 event->register_temp_buf(reinterpret_cast<char *>(data),
216 ALLOCATOR::DELEGATE_MEMORY_TO_EVENT_OBJECT);
217 return event;
218 }
219
220 private:
221 /**
222 It is convenient for caller to share a Binlog_read_error object between
223 streams. So Binlog_read_error pointer is defined here. It should be
224 initialized in constructor by caller.
225 */
227 EVENT_DATA_ISTREAM *m_data_istream = nullptr;
228};
229
230/// Interface class that all specializations of
231/// template <...> Basic_binlog_file_reader inherit from.
233 public:
239 virtual ~IBasic_binlog_file_reader() = default;
240
241 /// Return true if there was a previous error.
242 virtual bool has_fatal_error() const = 0;
243
244 /// Return the type of error.
246
247 /// Return a string representing the error.
248 ///
249 /// The return value is static memory that is never deallocated.
250 virtual const char *get_error_str() const = 0;
251
252 /// Return the current position in bytes, relative to the beginning
253 /// of the file.
254 virtual my_off_t position() const = 0;
255
256 /// Read the next event from the stream.
257 ///
258 /// This function creates the object with "new". It is the caller's
259 /// responsibility to delete it.
260 ///
261 /// @return Log_event on success, nullptr on error. More
262 /// information about the error can be found using `get_error_type`
263 /// and `get_error_str`.
265
266 /// Return a pointer the currently used
267 /// Format_description_log_event.
268 ///
269 /// The event is a member by this reader, so the caller must not use
270 /// the returned reference after this reader has been deleted.
272};
273
274/**
275 It owns an allocator, a byte stream, an event_data stream and an event object
276 stream. The stream pipeline is setup in the constructor. All the objects
277 required for reading a binlog file is initialized in reader class.
278
279 It maintains the Format_description_event which is needed for reading the
280 following binlog events. A default format_description_event is initialized
281 at the beginning. Then it will be replaced by the one read from the binlog
282 file.
283
284 Some convenient functions is added to encapsulate the access of IFILE,
285 EVENT_DATA_ISTREAM, EVENT_OBJECT_ISTREAM. It makes the code
286 simpler for reading a binlog file.
287*/
288template <class IFILE, class EVENT_DATA_ISTREAM,
289 template <class> class EVENT_OBJECT_ISTREAM, class ALLOCATOR>
291 public:
292 typedef EVENT_DATA_ISTREAM Event_data_istream;
293 typedef EVENT_OBJECT_ISTREAM<Event_data_istream> Event_object_istream;
294
295 Basic_binlog_file_reader(bool verify_checksum,
296 unsigned int max_event_size = UINT_MAX)
297 : m_ifile(&m_error),
298 m_data_istream(&m_error, &m_ifile, max_event_size),
301 m_verify_checksum(verify_checksum) {}
302
306 delete;
308 delete;
310
311 /**
312 Open a binlog file and set read position to offset. It will read and store
313 Format_description_event automatically if offset is bigger than current
314 position and fde is nullptr. Otherwise fde is use instead of finding fde
315 from the file if fde is not null.
316
317 @param[in] file_name name of the binlog file which will be opened.
318 @param[in] offset The position where it starts to read.
319 @param[out] fdle For returning an Format_description_log_event object.
320 @retval false Success
321 @retval true Error
322 */
323 bool open(const char *file_name, my_off_t offset = 0,
324 Format_description_log_event **fdle = nullptr) {
326 if (m_ifile.open(file_name)) return true;
327
329 if (!fd) return has_fatal_error();
330
331 if (position() < offset && seek(offset)) {
332 delete fd;
333 return true;
334 }
335 if (fdle)
336 *fdle = fd;
337 else
338 delete fd;
339 return false;
340 }
341 /**
342 Close the binlog file.
343 */
344 void close() {
345 m_ifile.close();
347 }
348
349 bool is_open() const { return m_ifile.is_open(); }
350 my_off_t position() const override { return m_ifile.position(); }
351 bool seek(my_off_t pos) { return m_ifile.seek(pos); }
352
353 /**
354 Wrapper of EVENT_DATA_ISTREAM::read_event_data.
355 */
356 bool read_event_data(unsigned char **data, unsigned int *length) {
358 return m_data_istream.read_event_data(data, length, &m_allocator,
361 }
362 /**
363 wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
364 */
367 Log_event *ev = m_object_istream.read_event_object(m_fde, m_verify_checksum,
368 &m_allocator);
370 m_fde = dynamic_cast<Format_description_event &>(*ev);
371 return ev;
372 }
373
374 bool has_fatal_error() const override { return m_error.has_fatal_error(); }
375 /**
376 Return the error happened in the stream pipeline.
377 */
379 return m_error.get_type();
380 }
381 /**
382 Return the error message of the error happened in the stream pipeline.
383 */
384 const char *get_error_str() const override { return m_error.get_str(); }
385
386 IFILE *ifile() { return &m_ifile; }
389 ALLOCATOR *allocator() { return &m_allocator; }
390
392 m_fde = fde;
393 }
395 return m_fde;
396 }
398
399 /**
400 @brief Resets the error. Sets it to Binlog_read_error::SUCCESS.
401 */
403
404 private:
406
407 IFILE m_ifile;
410 ALLOCATOR m_allocator;
411
413 bool m_verify_checksum = false;
415
416 /**
417 Read the Format_description_log_events before 'offset'.
418
419 @param[in] offset The position where the read should stop.
420 @return A valid Format_description_log_event pointer or nullptr.
421 */
425 Format_description_log_event *fdle = nullptr;
426 /*
427 Format_description_event is skipped, so we initialize m_fde here. For
428 relay log, it need to find master's Format_description_event. master's
429 Format_description_event is the 3rd or 4th event of a relay log file.
430 Relay log's format looks like:
431 Format_description_event : relay log's Format_description_event
432 Previous_gtid_event
433 [Rotate_event] : In the case rotating relaylog, no Rotate here
434 Format_description_event : master's Format_description_event
435 */
436 while (position() < offset) {
438
439 Log_event *ev = m_object_istream.read_event_object(
441
442 if (ev == nullptr) break;
444 delete fdle;
445 fdle = dynamic_cast<Format_description_log_event *>(ev);
446 m_fde = *fdle;
447 } else {
449 delete ev;
452 break;
453 }
454 }
455 if (has_fatal_error()) {
456 delete fdle;
457 return nullptr;
458 }
459 return fdle;
460 }
461};
462
463#ifdef MYSQL_SERVER
472#endif // MYSQL_SERVER
473#endif // BINLOG_READER_INCLUDED
#define LOG_EVENT_MINIMAL_HEADER_LEN
Fixed header length, where 4.x and 5.0 agree.
Definition: binlog_event.h:428
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:467
Binlog_read_error::Error_type binlog_event_deserialize(const unsigned char *event_data, unsigned int event_data_len, const Format_description_event *fde, bool verify_checksum, Log_event **event)
Deserialize a binlog event from event_data.
Definition: binlog_reader.cc:117
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:471
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:290
Event_data_istream m_data_istream
Definition: binlog_reader.h:408
const Format_description_event & format_description_event() const override
Return a pointer the currently used Format_description_log_event.
Definition: binlog_reader.h:394
Event_data_istream * event_data_istream()
Definition: binlog_reader.h:387
EVENT_DATA_ISTREAM Event_data_istream
Definition: binlog_reader.h:292
Format_description_log_event * read_fdle(my_off_t offset)
Read the Format_description_log_events before 'offset'.
Definition: binlog_reader.h:422
Format_description_event m_fde
Definition: binlog_reader.h:412
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:323
~Basic_binlog_file_reader() override
Definition: binlog_reader.h:309
const char * get_error_str() const override
Return the error message of the error happened in the stream pipeline.
Definition: binlog_reader.h:384
bool m_verify_checksum
Definition: binlog_reader.h:413
Binlog_read_error::Error_type get_error_type() const override
Return the error happened in the stream pipeline.
Definition: binlog_reader.h:378
ALLOCATOR m_allocator
Definition: binlog_reader.h:410
Event_object_istream * event_object_istream()
Definition: binlog_reader.h:388
bool is_open() const
Definition: binlog_reader.h:349
bool has_fatal_error() const override
Return true if there was a previous error.
Definition: binlog_reader.h:374
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &&)=delete
my_off_t event_start_pos()
Definition: binlog_reader.h:397
Binlog_read_error m_error
Definition: binlog_reader.h:405
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &)=delete
EVENT_OBJECT_ISTREAM< Event_data_istream > Event_object_istream
Definition: binlog_reader.h:293
Event_object_istream m_object_istream
Definition: binlog_reader.h:409
ALLOCATOR * allocator()
Definition: binlog_reader.h:389
void close()
Close the binlog file.
Definition: binlog_reader.h:344
IFILE m_ifile
Definition: binlog_reader.h:407
bool read_event_data(unsigned char **data, unsigned int *length)
Wrapper of EVENT_DATA_ISTREAM::read_event_data.
Definition: binlog_reader.h:356
my_off_t m_event_start_pos
Definition: binlog_reader.h:414
Basic_binlog_file_reader(bool verify_checksum, unsigned int max_event_size=UINT_MAX)
Definition: binlog_reader.h:295
my_off_t position() const override
Return the current position in bytes, relative to the beginning of the file.
Definition: binlog_reader.h:350
Basic_binlog_file_reader(const Basic_binlog_file_reader &&)=delete
IFILE * ifile()
Definition: binlog_reader.h:386
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:365
void reset_error()
Resets the error.
Definition: binlog_reader.h:402
bool seek(my_off_t pos)
Definition: binlog_reader.h:351
void set_format_description_event(const Format_description_event &fde)
Definition: binlog_reader.h:391
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:154
Basic_istream * m_istream
Definition: binlog_reader.h:153
bool read_fixed_length(unsigned char *data, unsigned int length)
Read fixed length of data from Basic_istream.
Definition: binlog_reader.h:129
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:150
unsigned int m_event_length
Definition: binlog_reader.h:155
Binlog_event_data_istream & operator=(const Binlog_event_data_istream &)=delete
bool fill_event_data(unsigned char *event_data, bool verify_checksum, 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:72
bool check_event_header()
Check if it is a valid event header.
Definition: binlog_reader.cc:100
virtual bool read_event_header()
Read the event header from the Basic_istream.
Definition: binlog_reader.cc:67
virtual ~Binlog_event_data_istream()=default
Binlog_event_data_istream(const Binlog_event_data_istream &)=delete
unsigned char m_header[LOG_EVENT_MINIMAL_HEADER_LEN]
Definition: binlog_reader.h:101
bool read_event_data(unsigned char **data, unsigned int *length, ALLOCATOR *allocator, bool verify_checksum, 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
It reads event_data from an event_data stream and deserialize them to event object.
Definition: binlog_reader.h:177
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:226
EVENT_DATA_ISTREAM * m_data_istream
Definition: binlog_reader.h:227
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:179
Log_event * read_event_object(const Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Read an event object from the stream.
Definition: binlog_reader.h:198
Binlog input file.
Definition: binlog_istream.h:238
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:85
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:103
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:80
Definition: binlog_reader.h:46
@ DELEGATE_MEMORY_TO_EVENT_OBJECT
Definition: binlog_reader.h:48
unsigned char * allocate(size_t)
Definition: binlog_reader.cc:27
void deallocate(unsigned char *ptr)
Definition: binlog_reader.cc:33
For binlog version 4.
Definition: log_event.h:1503
Interface class that all specializations of template <...> Basic_binlog_file_reader inherit from.
Definition: binlog_reader.h:232
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 const Format_description_event & format_description_event() const =0
Return a pointer the currently used Format_description_log_event.
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 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:542
virtual Log_event_type get_type_code() const
Definition: log_event.h:799
void register_temp_buf(char *buf, bool free_in_destructor=true)
Definition: log_event.h:856
Relaylog input file.
Definition: binlog_istream.h:250
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:927
For binlog version 4.
Definition: control_events.h:230
#define BINLOG_VERSION
binlog_version 3 is MySQL 4.x; 4 is MySQL 5.0.0.
Definition: binlog_event.h:94
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:118
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:433
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:274
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:300
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:342
@ ROTATE_EVENT
Definition: binlog_event.h:290
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
required string type
Definition: replication_group_member_actions.proto:33
required string event
Definition: replication_group_member_actions.proto:31