MySQL 8.3.0
Source Code Documentation
binlog_ostream.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_OSTREAM_INCLUDED
24#define BINLOG_OSTREAM_INCLUDED
25
26#include <openssl/evp.h>
27#include "sql/basic_ostream.h"
29
30// True if binlog cache is reset.
31#ifndef NDEBUG
32extern bool binlog_cache_is_reset;
33#endif
34
35/**
36 Copy data from an input stream to an output stream.
37
38 @param[in] istream the input stream where data will be copied from
39 @param[out] ostream the output stream where data will be copied into
40 @param[out] ostream_error It will be set to true if an error happens on
41 ostream and the pointer is not null. It is valid
42 only when the function returns true.
43
44 @retval false Success
45 @retval true Error happens in either the istream or ostream.
46*/
47template <class ISTREAM, class OSTREAM>
48bool stream_copy(ISTREAM *istream, OSTREAM *ostream,
49 bool *ostream_error = nullptr) {
51 unsigned char *buffer = nullptr;
52 my_off_t length = 0;
53
54 bool ret = istream->begin(&buffer, &length);
55 while (!ret && length > 0) {
56 if (ostream->write(buffer, length)) {
57 if (ostream_error != nullptr) *ostream_error = true;
58 return true;
59 }
60
61 ret = istream->next(&buffer, &length);
62 }
63 return ret;
64}
65
66/**
67 A binlog cache implementation based on IO_CACHE.
68*/
70 public:
73 const IO_CACHE_binlog_cache_storage &) = delete;
76
77 /**
78 Opens the binlog cache. It creates a memory buffer as long as cache_size.
79 The buffer will be extended up to max_cache_size when writing data. The
80 data exceeding max_cache_size will be written into a temporary file.
81
82 @param[in] dir Where the temporary file will be created
83 @param[in] prefix Prefix of the temporary file name
84 @param[in] cache_size Size of the memory buffer.
85 @param[in] max_cache_size Maximum size of the memory buffer
86 @retval false Success
87 @retval true Error
88 */
89 bool open(const char *dir, const char *prefix, my_off_t cache_size,
90 my_off_t max_cache_size);
91 void close();
92
93 bool write(const unsigned char *buffer, my_off_t length) override;
94 bool truncate(my_off_t offset) override;
95 /* purecov: inspected */
96 /* binlog cache doesn't need seek operation. Setting true to return error */
97 bool seek(my_off_t offset [[maybe_unused]]) override { return true; }
98 /**
99 Reset status and drop all data. It looks like a cache never was used after
100 reset.
101 */
102 bool reset();
103 /**
104 Returns the file name if a temporary file is opened, otherwise nullptr is
105 returned.
106 */
107 const char *tmp_file_name() const;
108 /**
109 Returns the count of calling temporary file's write()
110 */
111 size_t disk_writes() const;
112
113 /**
114 Initializes binlog cache for reading and returns the data at the begin.
115 buffer is controlled by binlog cache implementation, so caller should
116 not release it. If the function sets *length to 0 and no error happens,
117 it has reached the end of the cache.
118
119 @param[out] buffer It points to buffer where data is read.
120 @param[out] length Length of the data in the buffer.
121 @retval false Success
122 @retval true Error
123 */
124 bool begin(unsigned char **buffer, my_off_t *length);
125 /**
126 Returns next piece of data. buffer is controlled by binlog cache
127 implementation, so caller should not release it. If the function sets
128 *length to 0 and no error happens, it has reached the end of the cache.
129
130 @param[out] buffer It points to buffer where data is read.
131 @param[out] length Length of the data in the buffer.
132 @retval false Success
133 @retval true Error
134 */
135 bool next(unsigned char **buffer, my_off_t *length);
136 my_off_t length() const;
137 bool flush() override { return false; }
138 bool sync() override { return false; }
139
140 private:
143 /**
144 Enable IO Cache temporary file encryption.
145
146 @retval false Success.
147 @retval true Error.
148 */
149 bool enable_encryption();
150 /**
151 Disable IO Cache temporary file encryption.
152 */
153 void disable_encryption();
154 /**
155 Generate a new password for the temporary file encryption.
156
157 This function is called by reset() that is called every time a transaction
158 commits to cleanup the binary log cache. The file password shall vary not
159 only per temporary file, but also per transaction being committed within a
160 single client connection.
161
162 @retval false Success.
163 @retval true Error.
164 */
166};
167
168/**
169 Byte container that provides a storage for serializing session
170 binlog events. This way of arranging the classes separates storage layer
171 and binlog layer, hides the implementation detail of low level storage.
172*/
174 public:
175 ~Binlog_cache_storage() override;
176
177 bool open(my_off_t cache_size, my_off_t max_cache_size);
178 void close();
179
180 bool write(const unsigned char *buffer, my_off_t length) override {
181 assert(m_pipeline_head != nullptr);
183 }
184 /**
185 Truncates some data at the end of the binlog cache.
186
187 @param[in] offset Where the binlog cache will be truncated to.
188 @retval false Success
189 @retval true Error
190 */
191 bool truncate(my_off_t offset) { return m_pipeline_head->truncate(offset); }
192
193 /**
194 Reset status and drop all data. It looks like a cache was never used
195 after reset.
196 */
197 bool reset() { return m_file.reset(); }
198 /**
199 Returns the count of disk writes
200 */
201 size_t disk_writes() const { return m_file.disk_writes(); }
202 /**
203 Returns the name of the temporary file.
204 */
205 const char *tmp_file_name() const { return m_file.tmp_file_name(); }
206
207 /**
208 Copy all data to a output stream. This function hides the internal
209 implementation of storage detail. So it will not disturb the callers
210 if the implementation of Binlog_cache_storage is changed. If we add
211 a pipeline stream in this class, then we need to change the implementation
212 of this function. But callers are not affected.
213
214 @param[out] ostream Where the data will be copied into
215 @param[out] ostream_error It will be set to true if an error happens on
216 ostream and the pointer is not null. It is valid
217 only when the function returns true.
218 @retval false Success
219 @retval true Error happens in either the istream or ostream.
220 */
221 bool copy_to(Basic_ostream *ostream, bool *ostream_error = nullptr) {
223 return stream_copy(&m_file, ostream, ostream_error);
224 }
225
226 /**
227 Returns data length.
228 */
229 my_off_t length() const { return m_file.length(); }
230 /**
231 Returns true if binlog cache is empty.
232 */
233 bool is_empty() const { return length() == 0; }
234
235 private:
238};
239
240/**
241 It is an Truncatable_ostream which provides encryption feature. It can be
242 setup into an stream pipeline. In the pipeline, it encrypts the data
243 from up stream and then feeds the encrypted data into down stream.
244*/
246 public:
248
249 /**
250 Initialize the context used in the encryption stream and write encryption
251 header into down stream.
252
253 @param[in] down_ostream The stream for storing encrypted data.
254
255 @retval false Success
256 @retval true Error.
257 */
258 bool open(std::unique_ptr<Truncatable_ostream> down_ostream);
259
260 /**
261 Initialize the context used in the encryption stream based on the
262 header passed as parameter. It shall be used when opening an ostream for
263 a stream that was already encrypted (the cypher password already exists).
264
265 @param[in] down_ostream the stream for storing encrypted data.
266 @param[in] header the encryption header to setup the cypher.
267
268 @retval false Success.
269 @retval true Error.
270 */
271 bool open(std::unique_ptr<Truncatable_ostream> down_ostream,
272 std::unique_ptr<Rpl_encryption_header> header);
273
274 /**
275 Re-encrypt the encrypted binary/relay log file header by replacing its
276 binlog encryption key id with the current one and its encrypted file
277 password with the new one, which is got by encrypting its file password
278 with the current binlog encryption key.
279
280 @retval false Success with an empty error message.
281 @retval true Error with an error message.
282 */
283 std::pair<bool, std::string> reencrypt();
284
285 void close();
286 bool write(const unsigned char *buffer, my_off_t length) override;
287 bool truncate(my_off_t offset) override;
288 bool seek(my_off_t offset) override;
289 bool flush() override;
290 bool sync() override;
291 /**
292 Return the encrypted file header size.
293
294 @return the encrypted file header size.
295 */
296 int get_header_size();
297
298 private:
299 std::unique_ptr<Truncatable_ostream> m_down_ostream;
300 std::unique_ptr<Rpl_encryption_header> m_header;
301 std::unique_ptr<Stream_cipher> m_encryptor;
302};
303#endif // BINLOG_OSTREAM_INCLUDED
bool stream_copy(ISTREAM *istream, OSTREAM *ostream, bool *ostream_error=nullptr)
Copy data from an input stream to an output stream.
Definition: binlog_ostream.h:48
bool binlog_cache_is_reset
Definition: binlog_ostream.cc:37
The abstract class for basic output streams which provides write operation.
Definition: basic_ostream.h:36
virtual bool write(const unsigned char *buffer, my_off_t length)=0
Write some bytes into the output stream.
Byte container that provides a storage for serializing session binlog events.
Definition: binlog_ostream.h:173
bool truncate(my_off_t offset)
Truncates some data at the end of the binlog cache.
Definition: binlog_ostream.h:191
~Binlog_cache_storage() override
Definition: binlog_ostream.cc:266
my_off_t length() const
Returns data length.
Definition: binlog_ostream.h:229
IO_CACHE_binlog_cache_storage m_file
Definition: binlog_ostream.h:237
bool is_empty() const
Returns true if binlog cache is empty.
Definition: binlog_ostream.h:233
Truncatable_ostream * m_pipeline_head
Definition: binlog_ostream.h:236
bool open(my_off_t cache_size, my_off_t max_cache_size)
Definition: binlog_ostream.cc:252
bool reset()
Reset status and drop all data.
Definition: binlog_ostream.h:197
size_t disk_writes() const
Returns the count of disk writes.
Definition: binlog_ostream.h:201
const char * tmp_file_name() const
Returns the name of the temporary file.
Definition: binlog_ostream.h:205
bool copy_to(Basic_ostream *ostream, bool *ostream_error=nullptr)
Copy all data to a output stream.
Definition: binlog_ostream.h:221
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: binlog_ostream.h:180
void close()
Definition: binlog_ostream.cc:261
It is an Truncatable_ostream which provides encryption feature.
Definition: binlog_ostream.h:245
int get_header_size()
Return the encrypted file header size.
Definition: binlog_ostream.cc:410
bool open(std::unique_ptr< Truncatable_ostream > down_ostream)
Initialize the context used in the encryption stream and write encryption header into down stream.
Definition: binlog_ostream.cc:279
std::unique_ptr< Rpl_encryption_header > m_header
Definition: binlog_ostream.h:300
std::unique_ptr< Truncatable_ostream > m_down_ostream
Definition: binlog_ostream.h:299
std::unique_ptr< Stream_cipher > m_encryptor
Definition: binlog_ostream.h:301
void close()
Definition: binlog_ostream.cc:362
std::pair< bool, std::string > reencrypt()
Re-encrypt the encrypted binary/relay log file header by replacing its binlog encryption key id with ...
Definition: binlog_ostream.cc:315
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: binlog_ostream.cc:368
bool sync() override
Sync.
Definition: binlog_ostream.cc:408
~Binlog_encryption_ostream() override
Definition: binlog_ostream.cc:268
bool flush() override
Flush data.
Definition: binlog_ostream.cc:406
bool truncate(my_off_t offset) override
Truncate some data at the end of the output stream.
Definition: binlog_ostream.cc:400
bool seek(my_off_t offset) override
Put the write position to a given offset.
Definition: binlog_ostream.cc:395
A binlog cache implementation based on IO_CACHE.
Definition: binlog_ostream.h:69
void disable_encryption()
Disable IO Cache temporary file encryption.
Definition: binlog_ostream.cc:222
bool seek(my_off_t offset) override
Put the write position to a given offset.
Definition: binlog_ostream.h:97
const char * tmp_file_name() const
Returns the file name if a temporary file is opened, otherwise nullptr is returned.
Definition: binlog_ostream.cc:146
my_off_t m_max_cache_size
Definition: binlog_ostream.h:142
IO_CACHE_binlog_cache_storage(const IO_CACHE_binlog_cache_storage &)=delete
bool sync() override
Sync.
Definition: binlog_ostream.h:138
bool begin(unsigned char **buffer, my_off_t *length)
Initializes binlog cache for reading and returns the data at the begin.
Definition: binlog_ostream.cc:150
IO_CACHE_binlog_cache_storage & operator=(const IO_CACHE_binlog_cache_storage &)=delete
bool flush() override
Flush data.
Definition: binlog_ostream.h:137
bool reset()
Reset status and drop all data.
Definition: binlog_ostream.cc:107
bool truncate(my_off_t offset) override
Truncate some data at the end of the output stream.
Definition: binlog_ostream.cc:92
bool setup_ciphers_password()
Generate a new password for the temporary file encryption.
Definition: binlog_ostream.cc:233
size_t disk_writes() const
Returns the count of calling temporary file's write()
Definition: binlog_ostream.cc:142
bool open(const char *dir, const char *prefix, my_off_t cache_size, my_off_t max_cache_size)
Opens the binlog cache.
Definition: binlog_ostream.cc:43
my_off_t length() const
Definition: binlog_ostream.cc:195
bool enable_encryption()
Enable IO Cache temporary file encryption.
Definition: binlog_ostream.cc:200
void close()
Definition: binlog_ostream.cc:58
IO_CACHE m_io_cache
Definition: binlog_ostream.h:141
~IO_CACHE_binlog_cache_storage() override
Definition: binlog_ostream.cc:41
bool write(const unsigned char *buffer, my_off_t length) override
Write some bytes into the output stream.
Definition: binlog_ostream.cc:60
bool next(unsigned char **buffer, my_off_t *length)
Returns next piece of data.
Definition: binlog_ostream.cc:183
Truncatable_ostream abstract class provides seek() and truncate() interfaces to all truncatable outpu...
Definition: basic_ostream.h:57
virtual bool truncate(my_off_t offset)=0
Truncate some data at the end of the output stream.
#define DBUG_TRACE
Definition: my_dbug.h:145
ulonglong my_off_t
Definition: my_inttypes.h:71
std::string dir
Double write files location.
Definition: buf0dblwr.cc:76
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
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
This file includes the major components for encrypting/decrypting binary log files.
Definition: my_sys.h:345
static uint64_t cache_size
Definition: xcom_cache.cc:361