MySQL 8.0.39
Source Code Documentation
binlog_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 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_READER_INCLUDED
25#define BINLOG_READER_INCLUDED
26#include "sql/binlog_istream.h"
27#include "sql/log_event.h"
28
29/**
30 Deserialize a binlog event from event_data. event_data is serialized
31 event object. It is a chunk of data in buffer.
32
33 @param[in] event_data The event data used for deserialization.
34 @param[in] event_data_len Length of event_data
35 @param[in] fde The format_description_event of the event
36 @param[in] verify_checksum Verify event_data's checksum if it is true.
37 @param[out] event the event object generated.
38
39 @retval Binlog_read_error::SUCCESS Success
40 @retval Other than Binlog_read_error::SUCCESS Error
41*/
43 const unsigned char *event_data, unsigned int event_data_len,
44 const Format_description_event *fde, bool verify_checksum,
46
48 public:
50 unsigned char *allocate(size_t);
51 void deallocate(unsigned char *ptr);
52};
53/**
54 Binlog_event_data_istream fetches byte data from Basic_istream and
55 divides them into event_data chunk according to the format. Event_data is a
56 serialized event object. It is a chunk of data in buffer.
57*/
59 public:
61 unsigned int max_event_size);
65 delete;
66 virtual ~Binlog_event_data_istream() = default;
67
68 /**
69 Read an event data from the stream and verify its checksum if
70 verify_checksum is true.
71
72 @param[out] data The pointer of the event data
73 @param[out] length The length of the event data
74 @param[in] allocator It is used to allocate memory for the event data.
75 @param[in] verify_checksum Verify the event data's checksum if it is true.
76 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
77 is used only when verify_checksum is true.
78 @retval false Success
79 @retval true Error
80 */
81 template <class ALLOCATOR>
82 bool read_event_data(unsigned char **data, unsigned int *length,
83 ALLOCATOR *allocator, bool verify_checksum,
84 enum_binlog_checksum_alg checksum_alg) {
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(unsigned char *event_data, bool verify_checksum,
170 enum_binlog_checksum_alg checksum_alg);
171};
172
173/**
174 It reads event_data from an event_data stream and deserialize them to event
175 object.
176*/
177template <class EVENT_DATA_ISTREAM>
179 public:
181 EVENT_DATA_ISTREAM *istream)
182 : m_error(error), m_data_istream(istream) {}
183
187 delete;
188
189 /**
190 Read an event object from the stream
191
192 @param[in] fde The Format_description_event for deserialization.
193 @param[in] verify_checksum Verify the checksum of the event_data before
194 @param[in] allocator It is used to allocate memory for the event data.
195 @return An valid event object if success.
196 @retval nullptr Error
197 */
198 template <class ALLOCATOR>
200 bool verify_checksum, ALLOCATOR *allocator) {
202 unsigned char *data = nullptr;
203 unsigned int length = 0;
204
205 if (m_data_istream->read_event_data(&data, &length, allocator, false,
206 fde.footer()->checksum_alg))
207 return nullptr;
208
209 Log_event *event = nullptr;
211 verify_checksum, &event))) {
212 allocator->deallocate(data);
213 return nullptr;
214 }
215
216 event->register_temp_buf(reinterpret_cast<char *>(data),
217 ALLOCATOR::DELEGATE_MEMORY_TO_EVENT_OBJECT);
218 return event;
219 }
220
221 private:
222 /**
223 It is convenient for caller to share a Binlog_read_error object between
224 streams. So Binlog_read_error pointer is defined here. It should be
225 initialized in constructor by caller.
226 */
228 EVENT_DATA_ISTREAM *m_data_istream = nullptr;
229};
230
231/// Interface class that all specializations of
232/// template <...> Basic_binlog_file_reader inherit from.
234 public:
240 virtual ~IBasic_binlog_file_reader() = default;
241
242 /// Return true if there was a previous error.
243 virtual bool has_fatal_error() const = 0;
244
245 /// Return the type of error.
247
248 /// Return a string representing the error.
249 ///
250 /// The return value is static memory that is never deallocated.
251 virtual const char *get_error_str() const = 0;
252
253 /// Return the current position in bytes, relative to the beginning
254 /// of the file.
255 virtual my_off_t position() const = 0;
256
257 /// Read the next event from the stream.
258 ///
259 /// This function creates the object with "new". It is the caller's
260 /// responsibility to delete it.
261 ///
262 /// @return Log_event on success, nullptr on error. More
263 /// information about the error can be found using `get_error_type`
264 /// and `get_error_str`.
266
267 /// Return a pointer the currently used
268 /// Format_description_log_event.
269 ///
270 /// The event is a member by this reader, so the caller must not use
271 /// the returned reference after this reader has been deleted.
273};
274
275/**
276 It owns an allocator, a byte stream, an event_data stream and an event object
277 stream. The stream pipeline is setup in the constructor. All the objects
278 required for reading a binlog file is initialized in reader class.
279
280 It maintains the Format_description_event which is needed for reading the
281 following binlog events. A default format_description_event is initialized
282 at the beginning. Then it will be replaced by the one read from the binlog
283 file.
284
285 Some convenient functions is added to encapsulate the access of IFILE,
286 EVENT_DATA_ISTREAM, EVENT_OBJECT_ISTREAM. It makes the code
287 simpler for reading a binlog file.
288*/
289template <class IFILE, class EVENT_DATA_ISTREAM,
290 template <class> class EVENT_OBJECT_ISTREAM, class ALLOCATOR>
292 public:
293 typedef EVENT_DATA_ISTREAM Event_data_istream;
294 typedef EVENT_OBJECT_ISTREAM<Event_data_istream> Event_object_istream;
295
296 Basic_binlog_file_reader(bool verify_checksum,
297 unsigned int max_event_size = UINT_MAX)
298 : m_ifile(&m_error),
299 m_data_istream(&m_error, &m_ifile, max_event_size),
302 m_verify_checksum(verify_checksum) {}
303
307 delete;
309 delete;
311
312 /**
313 Open a binlog file and set read position to offset. It will read and store
314 Format_description_event automatically if offset is bigger than current
315 position and fde is nullptr. Otherwise fde is use instead of finding fde
316 from the file if fde is not null.
317
318 @param[in] file_name name of the binlog file which will be opened.
319 @param[in] offset The position where it starts to read.
320 @param[out] fdle For returning an Format_description_log_event object.
321 @retval false Success
322 @retval true Error
323 */
324 bool open(const char *file_name, my_off_t offset = 0,
325 Format_description_log_event **fdle = nullptr) {
327 if (m_ifile.open(file_name)) return true;
328
330 if (!fd) return has_fatal_error();
331
332 if (position() < offset && seek(offset)) {
333 delete fd;
334 return true;
335 }
336 if (fdle)
337 *fdle = fd;
338 else
339 delete fd;
340 return false;
341 }
342 /**
343 Close the binlog file.
344 */
345 void close() {
346 m_ifile.close();
348 }
349
350 bool is_open() const { return m_ifile.is_open(); }
351 my_off_t position() const override { return m_ifile.position(); }
352 bool seek(my_off_t pos) { return m_ifile.seek(pos); }
353
354 /**
355 Wrapper of EVENT_DATA_ISTREAM::read_event_data.
356 */
357 bool read_event_data(unsigned char **data, unsigned int *length) {
359 return m_data_istream.read_event_data(data, length, &m_allocator,
362 }
363 /**
364 wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
365 */
368 Log_event *ev = m_object_istream.read_event_object(m_fde, m_verify_checksum,
369 &m_allocator);
371 m_fde = dynamic_cast<Format_description_event &>(*ev);
372 return ev;
373 }
374
375 bool has_fatal_error() const override { return m_error.has_fatal_error(); }
376 /**
377 Return the error happened in the stream pipeline.
378 */
380 return m_error.get_type();
381 }
382 /**
383 Return the error message of the error happened in the stream pipeline.
384 */
385 const char *get_error_str() const override { return m_error.get_str(); }
386
387 IFILE *ifile() { return &m_ifile; }
390 ALLOCATOR *allocator() { return &m_allocator; }
391
393 m_fde = fde;
394 }
396 return m_fde;
397 }
399
400 private:
402
403 IFILE m_ifile;
406 ALLOCATOR m_allocator;
407
409 bool m_verify_checksum = false;
411
412 /**
413 Read the Format_description_log_events before 'offset'.
414
415 @param[in] offset The position where the read should stop.
416 @return A valid Format_description_log_event pointer or nullptr.
417 */
421 Format_description_log_event *fdle = nullptr;
422 /*
423 Format_description_event is skipped, so we initialize m_fde here. For
424 relay log, it need to find master's Format_description_event. master's
425 Format_description_event is the 3rd or 4th event of a relay log file.
426 Relay log's format looks like:
427 Format_description_event : relay log's Format_description_event
428 Previous_gtid_event
429 [Rotate_event] : In the case rotating relaylog, no Rotate here
430 Format_description_event : master's Format_description_event
431 */
432 while (position() < offset) {
434
435 Log_event *ev = m_object_istream.read_event_object(
437
438 if (ev == nullptr) break;
440 delete fdle;
441 fdle = dynamic_cast<Format_description_log_event *>(ev);
442 m_fde = *fdle;
443 } else {
445 delete ev;
448 break;
449 }
450 }
451 if (has_fatal_error()) {
452 delete fdle;
453 return nullptr;
454 }
455 return fdle;
456 }
457};
458
459#ifdef MYSQL_SERVER
468#endif // MYSQL_SERVER
469#endif // BINLOG_READER_INCLUDED
#define LOG_EVENT_MINIMAL_HEADER_LEN
Fixed header length, where 4.x and 5.0 agree.
Definition: binlog_event.h:435
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:463
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:118
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:467
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:291
Event_data_istream m_data_istream
Definition: binlog_reader.h:404
const Format_description_event & format_description_event() const override
Return a pointer the currently used Format_description_log_event.
Definition: binlog_reader.h:395
Event_data_istream * event_data_istream()
Definition: binlog_reader.h:388
EVENT_DATA_ISTREAM Event_data_istream
Definition: binlog_reader.h:293
Format_description_log_event * read_fdle(my_off_t offset)
Read the Format_description_log_events before 'offset'.
Definition: binlog_reader.h:418
Format_description_event m_fde
Definition: binlog_reader.h:408
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:324
~Basic_binlog_file_reader() override
Definition: binlog_reader.h:310
const char * get_error_str() const override
Return the error message of the error happened in the stream pipeline.
Definition: binlog_reader.h:385
bool m_verify_checksum
Definition: binlog_reader.h:409
Binlog_read_error::Error_type get_error_type() const override
Return the error happened in the stream pipeline.
Definition: binlog_reader.h:379
ALLOCATOR m_allocator
Definition: binlog_reader.h:406
Event_object_istream * event_object_istream()
Definition: binlog_reader.h:389
bool is_open() const
Definition: binlog_reader.h:350
bool has_fatal_error() const override
Return true if there was a previous error.
Definition: binlog_reader.h:375
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &&)=delete
my_off_t event_start_pos()
Definition: binlog_reader.h:398
Binlog_read_error m_error
Definition: binlog_reader.h:401
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &)=delete
EVENT_OBJECT_ISTREAM< Event_data_istream > Event_object_istream
Definition: binlog_reader.h:294
Event_object_istream m_object_istream
Definition: binlog_reader.h:405
ALLOCATOR * allocator()
Definition: binlog_reader.h:390
void close()
Close the binlog file.
Definition: binlog_reader.h:345
IFILE m_ifile
Definition: binlog_reader.h:403
bool read_event_data(unsigned char **data, unsigned int *length)
Wrapper of EVENT_DATA_ISTREAM::read_event_data.
Definition: binlog_reader.h:357
my_off_t m_event_start_pos
Definition: binlog_reader.h:410
Basic_binlog_file_reader(bool verify_checksum, unsigned int max_event_size=UINT_MAX)
Definition: binlog_reader.h:296
my_off_t position() const override
Return the current position in bytes, relative to the beginning of the file.
Definition: binlog_reader.h:351
Basic_binlog_file_reader(const Basic_binlog_file_reader &&)=delete
IFILE * ifile()
Definition: binlog_reader.h:387
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:366
bool seek(my_off_t pos)
Definition: binlog_reader.h:352
void set_format_description_event(const Format_description_event &fde)
Definition: binlog_reader.h:392
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:33
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:58
unsigned int m_max_event_size
Definition: binlog_reader.h:155
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 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:73
bool check_event_header()
Check if it is a valid event header.
Definition: binlog_reader.cc:101
virtual bool read_event_header()
Read the event header from the Basic_istream.
Definition: binlog_reader.cc:68
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:102
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:82
It reads event_data from an event_data stream and deserialize them to event object.
Definition: binlog_reader.h:178
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:227
EVENT_DATA_ISTREAM * m_data_istream
Definition: binlog_reader.h:228
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:180
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:199
Binlog input file.
Definition: binlog_istream.h:239
It defines the error types which could happen when reading binlog files or deserializing binlog event...
Definition: binlog_istream.h:37
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:86
Error_type
Possible errors which happens when reading an event.
Definition: binlog_istream.h:42
@ MEM_ALLOCATE
Definition: binlog_istream.h:56
@ SYSTEM_IO
Definition: binlog_istream.h:54
@ TRUNC_EVENT
Definition: binlog_istream.h:58
bool set_type(Error_type type)
Set m_error to error.
Definition: binlog_istream.h:104
const char * get_str() const
Return error message of the error type.
Definition: binlog_istream.cc:34
bool has_fatal_error() const
Definition: binlog_istream.h:81
Definition: binlog_reader.h:47
unsigned char * allocate(size_t)
Definition: binlog_reader.cc:28
void deallocate(unsigned char *ptr)
Definition: binlog_reader.cc:34
@ DELEGATE_MEMORY_TO_EVENT_OBJECT
Definition: binlog_reader.h:49
For binlog version 4.
Definition: log_event.h:1515
Interface class that all specializations of template <...> Basic_binlog_file_reader inherit from.
Definition: binlog_reader.h:233
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:541
virtual Log_event_type get_type_code() const
Definition: log_event.h:798
void register_temp_buf(char *buf, bool free_in_destructor=true)
Definition: log_event.h:865
Relaylog input file.
Definition: binlog_istream.h:251
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:934
For binlog version 4.
Definition: control_events.h:231
#define BINLOG_VERSION
binlog_version 3 is MySQL 4.x; 4 is MySQL 5.0.0.
Definition: binlog_event.h:95
Binary log event definitions.
#define DBUG_TRACE
Definition: my_dbug.h:146
ulonglong my_off_t
Definition: my_inttypes.h:72
long long int longlong
Definition: my_inttypes.h:55
static char * server_version
Definition: mysql.cc:109
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:440
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:275
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:301
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:343
@ ROTATE_EVENT
Definition: binlog_event.h:291
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:76
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:94
required string type
Definition: replication_group_member_actions.proto:34
required string event
Definition: replication_group_member_actions.proto:32