MySQL  8.0.27
Source Code Documentation
binlog_reader.h
Go to the documentation of this file.
1 /* Copyright (c) 2018, 2021, 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::SUCCEED Succeed
39  @retval Other than Binlog_read_error::SUCCEED Error
40 */
42  const unsigned char *event_data, unsigned int event_data_len,
43  const Format_description_event *fde, bool verify_checksum,
44  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 Succeed
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) {
84  DBUG_TRACE;
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 Succeed
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 Succeed
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 Succeed
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) {
130  DBUG_TRACE;
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 Succeed
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 */
176 template <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 ojbect 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 succeed.
195  @retval nullptr Error
196  */
197  template <class ALLOCATOR>
199  bool verify_checksum, ALLOCATOR *allocator) {
200  DBUG_TRACE;
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 /**
231  It owns an allocator, a byte stream, an event_data stream and an event object
232  stream. The stream pipeline is setup in the constructor. All the objects
233  required for reading a binlog file is initialized in reader class.
234 
235  It maintains the Format_description_event which is needed for reading the
236  following binlog events. A default format_description_event is initialized
237  at the begining. Then it will be replaced by the one read from the binlog
238  file.
239 
240  Some convenient functions is added to encapsulate the access of IFILE,
241  EVENT_DATA_ISTREAM, EVENT_OBJECT_ISTREAM. It makes the code
242  simpler for reading a binlog file.
243 */
244 template <class IFILE, class EVENT_DATA_ISTREAM,
245  template <class> class EVENT_OBJECT_ISTREAM, class ALLOCATOR>
247  public:
248  typedef EVENT_DATA_ISTREAM Event_data_istream;
249  typedef EVENT_OBJECT_ISTREAM<Event_data_istream> Event_object_istream;
250 
251  Basic_binlog_file_reader(bool verify_checksum,
252  unsigned int max_event_size = UINT_MAX)
253  : m_ifile(&m_error),
254  m_data_istream(&m_error, &m_ifile, max_event_size),
257  m_verify_checksum(verify_checksum) {}
258 
261  delete;
263 
264  /**
265  Open a binlog file and set read position to offset. It will read and store
266  Format_description_event automatically if offset is bigger than current
267  position and fde is nullptr. Otherwise fde is use instead of finding fde
268  from the file if fde is not null.
269 
270  @param[in] file_name name of the binlog file which will be opened.
271  @param[in] offset The position where it starts to read.
272  @param[out] fdle For returning an Format_description_log_event object.
273  @retval false Succeed
274  @retval true Error
275  */
276  bool open(const char *file_name, my_off_t offset = 0,
277  Format_description_log_event **fdle = nullptr) {
278  DBUG_TRACE;
279  if (m_ifile.open(file_name)) return true;
280 
282  if (!fd) return has_fatal_error();
283 
284  if (position() < offset && seek(offset)) {
285  delete fd;
286  return true;
287  }
288  if (fdle)
289  *fdle = fd;
290  else
291  delete fd;
292  return false;
293  }
294  /**
295  Close the binlog file.
296  */
297  void close() {
298  m_ifile.close();
300  }
301 
302  bool is_open() { return m_ifile.is_open(); }
303  my_off_t position() { return m_ifile.position(); }
304  bool seek(my_off_t pos) { return m_ifile.seek(pos); }
305 
306  /**
307  Wrapper of EVENT_DATA_ISTREAM::read_event_data.
308  */
309  bool read_event_data(unsigned char **data, unsigned int *length) {
311  return m_data_istream.read_event_data(data, length, &m_allocator,
314  }
315  /**
316  wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
317  */
320  Log_event *ev = m_object_istream.read_event_object(m_fde, m_verify_checksum,
321  &m_allocator);
323  m_fde = dynamic_cast<Format_description_event &>(*ev);
324  return ev;
325  }
326 
328  /**
329  Return the error happened in the stream pipeline.
330  */
332  /**
333  Return the error message of the error happened in the stream pipeline.
334  */
335  const char *get_error_str() { return m_error.get_str(); }
336 
337  IFILE *ifile() { return &m_ifile; }
340  ALLOCATOR *allocator() { return &m_allocator; }
341 
343  m_fde = fde;
344  }
347 
348  private:
350 
351  IFILE m_ifile;
354  ALLOCATOR m_allocator;
355 
357  bool m_verify_checksum = false;
359 
360  /**
361  Read the Format_description_log_events before 'offset'.
362 
363  @param[in] offset The position where the read should stop.
364  @return A valid Format_description_log_event pointer or nullptr.
365  */
367  DBUG_TRACE;
369  Format_description_log_event *fdle = nullptr;
370  /*
371  Format_description_event is skipped, so we initialize m_fde here. For
372  relay log, it need to find master's Format_description_event. master's
373  Format_description_event is the 3rd or 4th event of a relay log file.
374  Relay log's format looks like:
375  Format_description_event : relay log's Format_description_event
376  Previous_gtid_event
377  [Rotate_event] : In the case rotating relaylog, no Rotate here
378  Format_description_event : master's Format_description_event
379  */
380  while (position() < offset) {
382 
383  Log_event *ev = m_object_istream.read_event_object(
385 
386  if (ev == nullptr) break;
388  delete fdle;
389  fdle = dynamic_cast<Format_description_log_event *>(ev);
390  m_fde = *fdle;
391  } else {
393  delete ev;
396  break;
397  }
398  }
399  if (has_fatal_error()) {
400  delete fdle;
401  return nullptr;
402  }
403  return fdle;
404  }
405 };
406 
407 #ifdef MYSQL_SERVER
416 #endif // MYSQL_SERVER
417 #endif // BINLOG_READER_INCLUDED
#define LOG_EVENT_MINIMAL_HEADER_LEN
Fixed header length, where 4.x and 5.0 agree.
Definition: binlog_event.h:406
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:411
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:109
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:415
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:246
Event_data_istream m_data_istream
Definition: binlog_reader.h:352
Event_data_istream * event_data_istream()
Definition: binlog_reader.h:338
IFILE * ifile()
Definition: binlog_reader.h:337
EVENT_DATA_ISTREAM Event_data_istream
Definition: binlog_reader.h:248
Format_description_event m_fde
Definition: binlog_reader.h:356
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:276
Binlog_read_error::Error_type get_error_type()
Return the error happened in the stream pipeline.
Definition: binlog_reader.h:331
bool m_verify_checksum
Definition: binlog_reader.h:357
ALLOCATOR m_allocator
Definition: binlog_reader.h:354
my_off_t event_start_pos()
Definition: binlog_reader.h:346
Binlog_read_error m_error
Definition: binlog_reader.h:349
ALLOCATOR * allocator()
Definition: binlog_reader.h:340
EVENT_OBJECT_ISTREAM< Event_data_istream > Event_object_istream
Definition: binlog_reader.h:249
Event_object_istream m_object_istream
Definition: binlog_reader.h:353
void close()
Close the binlog file.
Definition: binlog_reader.h:297
IFILE m_ifile
Definition: binlog_reader.h:351
Event_object_istream * event_object_istream()
Definition: binlog_reader.h:339
const Format_description_event * format_description_event()
Definition: binlog_reader.h:345
bool read_event_data(unsigned char **data, unsigned int *length)
Wrapper of EVENT_DATA_ISTREAM::read_event_data.
Definition: binlog_reader.h:309
my_off_t m_event_start_pos
Definition: binlog_reader.h:358
Basic_binlog_file_reader(bool verify_checksum, unsigned int max_event_size=UINT_MAX)
Definition: binlog_reader.h:251
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &)=delete
Basic_binlog_file_reader(const Basic_binlog_file_reader &)=delete
const char * get_error_str()
Return the error message of the error happened in the stream pipeline.
Definition: binlog_reader.h:335
my_off_t position()
Definition: binlog_reader.h:303
Log_event * read_event_object()
wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
Definition: binlog_reader.h:318
bool seek(my_off_t pos)
Definition: binlog_reader.h:304
void set_format_description_event(const Format_description_event &fde)
Definition: binlog_reader.h:342
Format_description_log_event * read_fdle(my_off_t offset)
Read the Format_description_log_events before 'offset'.
Definition: binlog_reader.h:366
~Basic_binlog_file_reader()
Definition: binlog_reader.h:262
bool has_fatal_error()
Definition: binlog_reader.h:327
bool is_open()
Definition: binlog_reader.h:302
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:32
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:71
bool check_event_header()
Check if it is a valid event header.
Definition: binlog_reader.cc:98
virtual bool read_event_header()
Read the event header from the Basic_istream.
Definition: binlog_reader.cc:66
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
Log_event * read_event_object(const Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Read an event ojbect from the stream.
Definition: binlog_reader.h:198
Binlog_event_object_istream(Binlog_read_error *error, EVENT_DATA_ISTREAM *istream)
Definition: binlog_reader.h:179
Binlog_event_object_istream & operator=(const Binlog_event_object_istream &)=delete
Binlog input file.
Definition: binlog_istream.h:236
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 encounted 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
@ 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()
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:1613
This is the abstract base class for binary log events.
Definition: log_event.h:659
Log_event_type get_type_code() const
Definition: log_event.h:916
void register_temp_buf(char *buf, bool free_in_destructor=true)
Definition: log_event.h:971
Relaylog input file.
Definition: binlog_istream.h:248
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:905
For binlog version 4.
Definition: control_events.h:230
DBUG_TRACE
Definition: do_ctype.cc:46
char * pos
Definition: do_ctype.cc:76
#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.
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:108
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:411
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:266
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:292
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:334
@ ROTATE_EVENT
Definition: binlog_event.h:282
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
required string type
Definition: replication_group_member_actions.proto:33
required string event
Definition: replication_group_member_actions.proto:31