MySQL 8.3.0
Source Code Documentation
binlog_istream.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_ISTREAM_INCLUDED
24#define BINLOG_ISTREAM_INCLUDED
25#include "my_sys.h"
26#include "sql/basic_istream.h"
28
29/**
30 It defines the error types which could happen when reading binlog files or
31 deserializing binlog events. String error message of the error types are
32 defined as well. It has a member variable to store an error type and
33 provides a few functions to check the error type stored in the member
34 variable.
35 */
37 public:
38 /**
39 Possible errors which happens when reading an event.
40 */
42 // No error happens
44 /*
45 Arrive at the end of the stream. Nothing was read. It is smaller than any
46 other errors. Because READ_EOF is often not an error, and others are
47 usually errors.
48 */
50 // malformed event
52 // IO error while reading
54 // Failed to allocate memory
56 // Only a partial event could be read
58 // Only a partial Format_description_log_event could be read
62 // Event's is_valid returned false
64 // Cannot open the binlog file
66 // System IO error happened while reading the binlog magic
68 // The binlog magic is incorrect
74 // Event comes from new server and cannot neglect unknown event fields
76 };
77
78 Binlog_read_error() = default;
80
81 bool has_error() const { return m_type != SUCCESS; }
82 bool has_fatal_error() const { return m_type > READ_EOF; }
83
84 /**
85 Return the error encountered when reading events.
86 */
87 Error_type get_type() const { return m_type; }
88
89 /**
90 Return error message of the error type.
91
92 @return It will return nullptr if m_type is SUCCESS. In practice, it should
93 never be called if m_type is SUCCESS. So an assertion is added in
94 debug mode which predicts m_type is not SUCCESS.
95 */
96 const char *get_str() const;
97
98 /**
99 Set m_error to error.
100
101 @param[in] type The error type will be set
102 @retval false If error is SUCCESS
103 @retval true If error is not SUCCESS.
104 */
106 m_type = type;
107 return has_error();
108 }
109
110 private:
112};
113
114/**
115 Seekable_istream with decryption feature. It can be setup into an stream
116 pipeline. In the pipeline, it decrypts the data from down stream and then
117 feeds the decrypted data into up stream.
118*/
120 public:
122
123 /**
124 Initialize the context used in the decryption stream.
125
126 @param[in] down_istream The down stream where the encrypted data is stored.
127 @param[in] binlog_read_error Binlog_encryption_istream doesn't own a
128 Binlog_read_error. So the caller should provide
129 one to it. When error happens, the error type
130 will be set into 'binlog_read_error'.
131
132 @retval false Succeed.
133 @retval true Error.
134 */
135 bool open(std::unique_ptr<Basic_seekable_istream> down_istream,
136 Binlog_read_error *binlog_read_error);
137 /**
138 Closes the stream. It also closes the down stream and the decryptor.
139 */
140 void close();
141
142 ssize_t read(unsigned char *buffer, size_t length) override;
143 bool seek(my_off_t offset) override;
144 my_off_t length() override;
145
146 private:
147 /* The decryptor cypher to decrypt the content read from down stream */
148 std::unique_ptr<Stream_cipher> m_decryptor;
149 /* The down stream containing the encrypted content */
150 std::unique_ptr<Basic_seekable_istream> m_down_istream;
151};
152
153/**
154 Base class of binlog input files. It is a logical binlog file which wraps
155 and hides the detail of lower layer storage implementation. Binlog reader and
156 other binlog code just uses this class to control real storage.
157*/
159 public:
160 /**
161 @param[in] binlog_read_error Basic_binlog_ifile doesn't own an
162 Binlog_read_error. So the caller should
163 provide one to it. When error happens,
164 the error type will be set into 'error'.
165 */
166 Basic_binlog_ifile(Binlog_read_error *binlog_read_error);
169 ~Basic_binlog_ifile() override;
170 /**
171 Open a binlog file.
172
173 @param[in] file_name name of the binlog file which will be opened.
174 */
175 bool open(const char *file_name);
176 /**
177 Close the binlog file it is reading.
178 */
179 void close();
180
181 ssize_t read(unsigned char *buffer, size_t length) override;
182 bool seek(my_off_t position) override;
183
184 my_off_t position() const { return m_position; }
185 bool is_open() const { return m_istream != nullptr; }
186
187 const std::string &file_name() const { return m_file_name; }
188 /**
189 Get length of the binlog file. It is not os file length. The content maybe
190 encrypted or compressed. It is the total length of BINLOG_MAGIC and all
191 raw binlog events.
192 */
193 my_off_t length() override;
194
195 protected:
196 /**
197 Open the system layer file. It is the entry of the stream pipeline.
198 Implementation is delegated to sub-classes. Sub-classes opens system layer
199 files in different way.
200
201 @param[in] file_name name of the binlog file which will be opened.
202 */
203 virtual std::unique_ptr<Basic_seekable_istream> open_file(
204 const char *file_name) = 0;
205
206 /**
207 It is convenient for caller to share a Binlog_read_error object between
208 streams. So Binlog_read_error pointer is defined here. It should be
209 initialized in constructor by caller.
210 */
212
213 private:
214 /**
215 The binlog's position where it is reading. It is the position in logical
216 binlog file, but not the position of system file.
217 */
219 /** It is the entry of the low level stream pipeline. */
220 std::unique_ptr<Basic_seekable_istream> m_istream;
221 /** Name of the file opened */
222 std::string m_file_name;
223 /**
224 Read binlog magic from binlog file and check if it is valid binlog magic.
225
226 This function also takes care of setting up any other stream layer (i.e.
227 encryption) when needed.
228
229 @retval false The high level stream layer was recognized as a binary log.
230 @retval true Failure identifying the high level stream layer.
231 */
232 bool read_binlog_magic();
233};
234
235#ifdef MYSQL_SERVER
236/**
237 Binlog input file. It is responsible for opening binlog files generated by
238 the server itself, but not relaylog files.
239*/
241 public:
243
244 protected:
245 std::unique_ptr<Basic_seekable_istream> open_file(
246 const char *file_name) override;
247};
248
249/**
250 Relaylog input file. It is responsible for opening relay log files.
251*/
253 public:
255
256 protected:
257 std::unique_ptr<Basic_seekable_istream> open_file(
258 const char *file_name) override;
259};
260#endif
261
262#endif // BINLOG_ISTREAM_INCLUDED
Base class of binlog input files.
Definition: binlog_istream.h:158
bool open(const char *file_name)
Open a binlog file.
Definition: binlog_istream.cc:185
bool seek(my_off_t position) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:206
virtual std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name)=0
Open the system layer file.
bool read_binlog_magic()
Read binlog magic from binlog file and check if it is valid binlog magic.
Definition: binlog_istream.cc:137
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:200
Basic_binlog_ifile & operator=(const Basic_binlog_ifile &)=delete
my_off_t length() override
Get length of the binlog file.
Definition: binlog_istream.cc:215
const std::string & file_name() const
Definition: binlog_istream.h:187
my_off_t position() const
Definition: binlog_istream.h:184
my_off_t m_position
The binlog's position where it is reading.
Definition: binlog_istream.h:218
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_istream.h:211
~Basic_binlog_ifile() override
Definition: binlog_istream.cc:180
bool is_open() const
Definition: binlog_istream.h:185
std::string m_file_name
Name of the file opened.
Definition: binlog_istream.h:222
Basic_binlog_ifile(Binlog_read_error *binlog_read_error)
Definition: binlog_istream.cc:175
std::unique_ptr< Basic_seekable_istream > m_istream
It is the entry of the low level stream pipeline.
Definition: binlog_istream.h:220
void close()
Close the binlog file it is reading.
Definition: binlog_istream.cc:193
Basic_binlog_ifile(const Basic_binlog_ifile &)=delete
The abstract class for seekable input streams which have fixed length and provide seek operation.
Definition: basic_istream.h:59
Seekable_istream with decryption feature.
Definition: binlog_istream.h:119
bool seek(my_off_t offset) override
Puts the read position to a given offset.
Definition: binlog_istream.cc:123
~Binlog_encryption_istream() override
Definition: binlog_istream.cc:135
bool open(std::unique_ptr< Basic_seekable_istream > down_istream, Binlog_read_error *binlog_read_error)
Initialize the context used in the decryption stream.
Definition: binlog_istream.cc:80
ssize_t read(unsigned char *buffer, size_t length) override
Read some bytes from the input stream.
Definition: binlog_istream.cc:116
void close()
Closes the stream.
Definition: binlog_istream.cc:110
std::unique_ptr< Basic_seekable_istream > m_down_istream
Definition: binlog_istream.h:150
my_off_t length() override
The total length of the stream.
Definition: binlog_istream.cc:131
std::unique_ptr< Stream_cipher > m_decryptor
Definition: binlog_istream.h:148
Binlog input file.
Definition: binlog_istream.h:240
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:219
It defines the error types which could happen when reading binlog files or deserializing binlog event...
Definition: binlog_istream.h:36
bool has_error() const
Definition: binlog_istream.h:81
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:87
Binlog_read_error(Error_type type)
Definition: binlog_istream.h:79
Error_type
Possible errors which happens when reading an event.
Definition: binlog_istream.h:41
@ EVENT_TOO_LARGE
Definition: binlog_istream.h:60
@ MEM_ALLOCATE
Definition: binlog_istream.h:55
@ BOGUS
Definition: binlog_istream.h:51
@ SUCCESS
Definition: binlog_istream.h:43
@ SYSTEM_IO
Definition: binlog_istream.h:53
@ READ_ENCRYPTED_LOG_FILE_IS_NOT_SUPPORTED
Definition: binlog_istream.h:72
@ CANNOT_GET_FILE_PASSWORD
Definition: binlog_istream.h:71
@ EVENT_UNSUPPORTED_NEW_VERSION
Definition: binlog_istream.h:75
@ ERROR_DECRYPTING_FILE
Definition: binlog_istream.h:73
@ READ_EOF
Definition: binlog_istream.h:49
@ HEADER_IO_FAILURE
Definition: binlog_istream.h:67
@ TRUNC_EVENT
Definition: binlog_istream.h:57
@ CANNOT_OPEN
Definition: binlog_istream.h:65
@ BAD_BINLOG_MAGIC
Definition: binlog_istream.h:69
@ INVALID_EVENT
Definition: binlog_istream.h:63
@ TRUNC_FD_EVENT
Definition: binlog_istream.h:59
@ CHECKSUM_FAILURE
Definition: binlog_istream.h:61
@ INVALID_ENCRYPTION_HEADER
Definition: binlog_istream.h:70
Binlog_read_error()=default
bool set_type(Error_type type)
Set m_error to error.
Definition: binlog_istream.h:105
Error_type m_type
Definition: binlog_istream.h:111
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
Relaylog input file.
Definition: binlog_istream.h:252
std::unique_ptr< Basic_seekable_istream > open_file(const char *file_name) override
Open the system layer file.
Definition: binlog_istream.cc:230
ulonglong my_off_t
Definition: my_inttypes.h:71
Common header for many mysys elements.
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
required string type
Definition: replication_group_member_actions.proto:33
This file includes the major components for encrypting/decrypting binary log files.