MySQL 26.7.0
Source Code Documentation
istream_prefetched.h
Go to the documentation of this file.
1// Copyright (c) 2026, 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 MYSQL_CSA_STORAGE_RELAY_LOG_ISTREAM_PREFETCHED_H
25#define MYSQL_CSA_STORAGE_RELAY_LOG_ISTREAM_PREFETCHED_H
26
27#include <atomic>
28#include <cstring>
29#include <memory>
30#include <vector>
31
32#include "sql/basic_istream.h" // Basic_istream
34#include "sql/log_event.h" // Log_event
35
36namespace mysql::csa {
37
38/// @brief Implementation of the Basic_istream, which reads data from the
39/// prefetched relay log. Reads cannot go outside a single relay log file
40/// boundary (legacy design). Note that prefetcher is able to move across relay
41/// log files. Therefore, to read from the next file, it is sufficient to
42/// create a new Istream_prefetched object with the same shared prefetcher
43/// object.
44/// Istream_prefetched allows for:
45/// - reading requested number of bytes into the preallocated buffer (read)
46/// - skipping requested number of bytes
47/// - seeking to a given file position (forward only)
48/// - length Function that will return the size of the file; length function
49/// is implemented as required by
50/// the `Basic_seekable_istream`, however, it is probably not required
51/// (note Stdin_binlog_istream has the 'length' function disabled)
52/// Istream_prefetched does not add new errors to error model, however, it needs
53/// to propagate prefetcher error. In case of prefetcher error, read, skip and
54/// seek functions will return an error value.
56 public:
57 /// Construct from prefetcher
58 /// @param prefetcher Prefetcher object used to obtain data
59 /// @param file_name The file we are allowed to read from
61 const std::string &file_name);
62 /// @brief Read requested number of bytes from the input stream.
63 /// It will block when reaching the end of the open stream that is not yet
64 /// prefetched. This function will read beyond the one file boundary if called
65 /// many times, unless the requested read would be split into several files.
66 /// In that case, it will return the number of bytes read till reaching the
67 /// end of the file.
68 /// @param buffer Preallocated buffer to >= length or nullptr
69 /// @param length The requested number of bytes to read.
70 /// @retval length Bytes read
71 /// @retval >=0 Reached the end of the file or was stopped in the
72 /// process. EOF : Since one operation cannot go beyond file boundary,
73 /// operation has been stopped
74 /// @retval -1 Error, i.e. prefetcher errored out and cannot read more data
75 ssize_t read(unsigned char *buffer, std::size_t length) override;
76
77 /// If possible, seeks to a given file offset
78 /// @retval False Success
79 /// @retval True Cannot seek to requested postion or prefetcher has errored
80 /// out
81 bool seek(my_off_t offset) override;
82
83 /// Returns currently opened file length...
84 /// @return file length
85 my_off_t length() override;
86
87 virtual ~Istream_prefetched() override = default;
88
89 private:
90 /// @brief Helper function that copies the requested number of bytes from
91 /// the current batch into buffer, using the current buffer offset. Buffer
92 /// offset is updated after copy
93 /// @param buffer Data will be copied into the following address:
94 /// buffer + buffer_offset
95 /// @param buffer_offset Current buffer offset, which will be advanced after
96 /// copy
97 /// @param bytes Copies this amount of bytes
98 void copy_to(unsigned char *buffer, std::size_t &buffer_offset,
99 std::size_t bytes);
100
101 /// Skips requested number of bytes. Applies the same rules as for read
102 /// fuction
103 /// @retval length The requested number of bytes have been skipped
104 /// @retval >=0 Reached the end of the file or was stopped in the
105 /// process. EOF : Since one operation cannot go beyond file boundary,
106 /// operation has been stopped
107 /// @retval -1 Error, i.e. prefetcher errored out and cannot read more data
108 ssize_t skip(std::size_t length);
109
110 /// @brief Reads the next batch
111 /// @retval true Stopped in the process or source filename changed
112 /// @retval false Data read successfully without file change
113 [[nodiscard]] bool read_next_batch();
114
116
117 /// Prefetcher that asynchronously fetches chunks of data from the relay log
119 /// Prefetcher works asynchronously on the whole relay log. To comply with
120 /// implemented structure of Binlog readers, we need to restrict fetching
121 /// the data to the file that was requested to open. After reaching end of
122 /// this file, instead of fetching next batches we report EOF.
123 std::string m_allowed_file_name{""};
124 /// Batch currently in use
126 /// Current batch offset
127 std::size_t m_current_offset{0};
128 /// Variable used to stop waiting for data (read may block)
129 std::atomic<bool> m_is_stopped{false};
130 /// True in case there has been prefetcher error which needs to be
131 /// propagated
133 /// Offset from start of the file
134 std::size_t m_file_offset{0};
135};
136
137} // namespace mysql::csa
138#endif // MYSQL_CSA_STORAGE_RELAY_LOG_ISTREAM_PREFETCHED_H
The abstract class for seekable input streams which have fixed length and provide seek operation.
Definition: basic_istream.h:60
Implementation of the Basic_istream, which reads data from the prefetched relay log.
Definition: istream_prefetched.h:55
Log_prefetcher_sptr m_prefetcher
Prefetcher that asynchronously fetches chunks of data from the relay log.
Definition: istream_prefetched.h:118
Batch_type m_current_batch
Batch currently in use.
Definition: istream_prefetched.h:125
virtual ~Istream_prefetched() override=default
bool m_prefetcher_error
True in case there has been prefetcher error which needs to be propagated.
Definition: istream_prefetched.h:132
Log_prefetcher::Elem_type Batch_type
Definition: istream_prefetched.h:115
void copy_to(unsigned char *buffer, std::size_t &buffer_offset, std::size_t bytes)
Helper function that copies the requested number of bytes from the current batch into buffer,...
Definition: istream_prefetched.cpp:36
bool read_next_batch()
Reads the next batch.
Definition: istream_prefetched.cpp:74
std::size_t m_file_offset
Offset from start of the file.
Definition: istream_prefetched.h:134
std::string m_allowed_file_name
Prefetcher works asynchronously on the whole relay log.
Definition: istream_prefetched.h:123
std::size_t m_current_offset
Current batch offset.
Definition: istream_prefetched.h:127
bool seek(my_off_t offset) override
If possible, seeks to a given file offset.
Definition: istream_prefetched.cpp:92
std::atomic< bool > m_is_stopped
Variable used to stop waiting for data (read may block)
Definition: istream_prefetched.h:129
ssize_t skip(std::size_t length)
Skips requested number of bytes.
Definition: istream_prefetched.cpp:32
ssize_t read(unsigned char *buffer, std::size_t length) override
Read requested number of bytes from the input stream.
Definition: istream_prefetched.cpp:46
Istream_prefetched(Log_prefetcher_sptr prefetcher, const std::string &file_name)
Construct from prefetcher.
Definition: istream_prefetched.cpp:28
my_off_t length() override
Returns currently opened file length...
Definition: istream_prefetched.cpp:101
Data_source_sptr Elem_type
Definition: log_prefetcher.h:89
Binary log event definitions.
ulonglong my_off_t
Definition: my_inttypes.h:72
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:45
Definition: channel.cpp:28
std::shared_ptr< Log_prefetcher > Log_prefetcher_sptr
Definition: log_prefetcher.h:48
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418