MySQL 26.7.0
Source Code Documentation
log_prefetcher.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_LOG_PREFETCHER_H
25#define MYSQL_CSA_LOG_PREFETCHER_H
26
27#include <fstream>
28#include <functional>
29#include <memory>
30#include <optional>
31
41#include "sql/binlog.h"
44
45namespace mysql::csa {
46
47class Log_prefetcher;
48using Log_prefetcher_sptr = std::shared_ptr<Log_prefetcher>;
49
50/// @brief This class starts an asynchronous thread that prefetches consecutive
51/// logs recorded in the index file. It contains the following main functions:
52/// - dequeue - function to get the next prefetched batch (see Data_source)
53/// - open - open a given file, check if file is hot and notify prefetcher
54/// to work on it
55/// - stop - Gracefully stops the prefetcher and blocks until
56/// thread is joined
57/// - is_waiting_for_next_file - checks whether prefetcher finished processing
58/// a given file and if prefetcher works on the next file or blocked
59/// - is_error - Checks whether prefetcher reported an error
61 public:
67 /// @param log_ptr The MYSQL_BIN_LOG object we use to know from where we need
68 /// to read data
69 /// @param mt_key_wait Instrumentation key for internal mutex (wait for work)
70 /// @param cv_key_wait Instrumentation key for internal cv (wait for work)
71 /// @param mt_key_file_move Instrumentation key for internal mutex (wait for
72 /// file rotation)
73 /// @param cv_key_file_move Instrumentation key for internal cv (wait for file
74 /// rotation)
75 /// @param key_th_prefetcher Instrumentation key for prefetcher thread
76 /// @param key_memory Instrumentation key for allocated memory
77 Log_prefetcher(MYSQL_BIN_LOG *log_ptr, Mt_key mt_key_wait = 0,
78 Cv_key cv_key_wait = 0, Mt_key mt_key_file_move = 0,
79 Cv_key cv_key_file_move = 0, Th_key key_th_prefetcher = 0,
80 Mem_key key_memory = 0);
81 virtual ~Log_prefetcher();
82
83 Log_prefetcher(const Log_prefetcher &) = delete;
87
90
91 /// @brief Starts asynchronous thread prefetching data
92 void start_prefetcher();
93
94 /// @brief Tries to open a new, gifen file. Prefetcher must stop on cv
95 /// before calling this function (see is_waiting_for_next_file).
96 /// @return True in case file is inactive and prefetcher will work on it,
97 /// false otherwise
98 /// @param file_name Starts reading from this file path
99 bool open(const char *file_name);
100
101 /// @brief Consumes the next batch of data
102 /// @return The next batch of data or empty object in case stopped in the
103 /// process
104 template <typename P>
105 std::optional<Elem_type> dequeue(P &&wait_predicate);
106
107 /// Checks whether prefetcher is stopped
108 /// @brief True if stop was requested, false otherwise
109 bool is_stopped() const;
110
111 /// @brief Gracefully stops the prefetcher. Blocks waiting for notification
112 /// that prefetcher thread is done
113 void stop();
114
115 /// @brief Returns true if error occurred
116 /// @return True in case error occurred, false otherwise
117 bool is_error() const;
118
119 /// @brief Checks whether we reached inactive file after the prev_file
120 /// This function takes previous file to check if prefetcher finished rotation
121 /// on this file and consecutive file to check if prefetcher opened it and
122 /// stopped working until file becomes inactive.
123 /// @param prev_file File we know we read all data from (got eof in the
124 /// last consumed batch) to check if prefetcher finished rotation on this file
125 /// @param next_file Consecutive file we want to open the stream on.
126 /// @retval true Prefetcher waits for file_name to become inactive
127 /// @retval false Prefetcher does not wait for 'file_name'
128 bool is_waiting_for_next_file(const std::string &prev_file,
129 const std::string &next_file);
130
131 private:
132 /// Thread type
134
135 /// @brief Opens new or next file for reading
136 /// @param next_file True if reading file after the current one
137 /// @return True in case file is inactive and prefetcher will work on it,
138 /// false otherwise
139 bool open_file(bool next_file);
140
141 /// @brief If currently rotated file is file_name, waits until rotate is done.
142 /// If prefetcher moves a different file, function returns immediately.
143 /// @param file_name File expected to be moved from.
144 void ensure_file_done(const std::string &file_name);
145
146 /// @brief Internal stop flag set to true when stop of the thread is
147 /// requested
148 std::atomic<bool> m_stopped{false};
149
150 /// Sets internal error (under lock) and internal error message. Later on,
151 /// stops the prefetcher.
152 /// This function will report any error to error log
153 /// @param msg Error message
154 void set_error(const char *msg);
155
156 /// Obtains the current file under the lock
157 /// @return File currently being prefetched
158 std::string get_current_file() const;
159
160 /// Updates the current file and its activity under the lock
161 /// @param file_name Update the current file name to this file
162 /// @param active True if file_name is an active log
163 void update_current_file(const char *file_name, bool active);
164
172
173 /// The MYSQL_BIN_LOG object we use to know from where we need to read data
175
176 /// Queue into which prefetcher puts data batches read from a raw binary
177 /// file
179
180 /// Thread that runs prefetching
182
183 /// True in case reading from the active relay log file (used currently by
184 /// the receiver thread), protected with m_mt_prefetcher.
185 /// Prefetcher can read only from inactive files
186 /// Protected by m_mt_prefetcher
187 bool m_log_active{true};
188
189 /// @brief Runs a thread that prefetch data from the relay log
190 void run_prefetch_thread();
191
192 /// Mutex protecting access to m_tasks
194 /// Cv used by the scheduler main thread to wait on, when no task is
195 /// available or tasks in m_task queue are not read to execute
197
198 /// Notification atomic for end of execution
199 std::atomic<bool> m_end{false};
200
201 /// Mutex protecting access to m_move_file
203 /// CV to notify the watcher that prefetcher finished moving
204 /// from m_move_file to the next file (see ensure_file_done)
206
207 /// Is set to a value if prefetcher initiated file move procedure
208 /// Watcher may wait until prefetcher moves to the next file by
209 /// using the m_cv_move_file notification cv and associated
210 /// m_mt_move_file
211 std::string m_move_file{""};
212
213 /// This way we track the actual number of bytes that are cached in the
214 /// m_cache
215 std::atomic<std::size_t> m_bytes_fetched{0};
216 /// Default batch size ~16MB
218 /// The maximum number of bytes we can prefetch
219 static constexpr std::size_t max_bytes_fetched{1073741824};
220
221 /// Stream to read from
222 std::ifstream m_istream;
223
224 /// Error message if any. Protected by m_mt_prefetcher
225 std::string m_error_message{""};
226
227 /// Becomes true in case error has been encountered
228 std::atomic<bool> m_is_error{false};
229
230 /// Currently processed log file
231 /// Protected by m_mt_prefetcher
232 std::string m_current_log_name{""};
233
234 /// Length of the currently opened file
235 std::size_t m_current_file_length{0};
236
237 /// Current file offset
238 std::size_t m_current_offset{0};
239
240 /// Key for prefetcher thread
242
243 /// Memory_resource to handle all allocations.
245};
246
247} // namespace mysql::csa
248
250
251#endif // MYSQL_CSA_LOG_PREFETCHER_H
Definition: binlog.h:108
MySQL wrapper for a condition variable, using mysql_cond_t as implementation of a condition variable ...
Definition: condition_variable_wrapper.h:37
MySQL wrapper for a mutex, template which may be specialized with a specific implementation of a mute...
Definition: mutex_wrapper.h:38
Bounded concurrent queue supporting multiple producers and consumers.
Definition: sync_bounded_queue.h:84
Wrapper to mysql thread, which matches interface of std::thread.
Definition: thread_srv.h:46
Represents a cached chunk of data that comes from the specific relay log file (events / headers may c...
Definition: data_source.h:45
This class starts an asynchronous thread that prefetches consecutive logs recorded in the index file.
Definition: log_prefetcher.h:60
void set_error(const char *msg)
Sets internal error (under lock) and internal error message.
Definition: log_prefetcher.cpp:204
Memory_allocator m_allocator
Memory_resource to handle all allocations.
Definition: log_prefetcher.h:244
std::string m_error_message
Error message if any. Protected by m_mt_prefetcher.
Definition: log_prefetcher.h:225
std::atomic< bool > m_end
Notification atomic for end of execution.
Definition: log_prefetcher.h:199
Log_prefetcher & operator=(const Log_prefetcher &)=delete
Log_prefetcher(MYSQL_BIN_LOG *log_ptr, Mt_key mt_key_wait=0, Cv_key cv_key_wait=0, Mt_key mt_key_file_move=0, Cv_key cv_key_file_move=0, Th_key key_th_prefetcher=0, Mem_key key_memory=0)
Definition: log_prefetcher.cpp:37
bool is_error() const
Returns true if error occurred.
Definition: log_prefetcher.cpp:180
std::string m_move_file
Is set to a value if prefetcher initiated file move procedure Watcher may wait until prefetcher moves...
Definition: log_prefetcher.h:211
mysql::concurrency::Cv_key Cv_key
Definition: log_prefetcher.h:63
Log_prefetcher & operator=(Log_prefetcher &&)=delete
bool open(const char *file_name)
Tries to open a new, gifen file.
Definition: log_prefetcher.cpp:98
bool is_stopped() const
Checks whether prefetcher is stopped.
Definition: log_prefetcher.cpp:212
std::conditional< tune::csa_prefetcher_simple_queue, Locking_queue_type, Sync_bounded_queue >::type Queue_type
Definition: log_prefetcher.h:171
bool open_file(bool next_file)
Opens new or next file for reading.
Definition: log_prefetcher.cpp:57
std::atomic< bool > m_stopped
Internal stop flag set to true when stop of the thread is requested.
Definition: log_prefetcher.h:148
std::atomic< std::size_t > m_bytes_fetched
This way we track the actual number of bytes that are cached in the m_cache.
Definition: log_prefetcher.h:215
std::size_t m_batch_size
Default batch size ~16MB.
Definition: log_prefetcher.h:217
std::ifstream m_istream
Stream to read from.
Definition: log_prefetcher.h:222
virtual ~Log_prefetcher()
Definition: log_prefetcher.cpp:50
mysql::concurrency::Condition_variable m_cv_move_file
CV to notify the watcher that prefetcher finished moving from m_move_file to the next file (see ensur...
Definition: log_prefetcher.h:205
bool is_waiting_for_next_file(const std::string &prev_file, const std::string &next_file)
Checks whether we reached inactive file after the prev_file This function takes previous file to chec...
Definition: log_prefetcher.cpp:193
static constexpr std::size_t max_bytes_fetched
The maximum number of bytes we can prefetch.
Definition: log_prefetcher.h:219
std::string get_current_file() const
Obtains the current file under the lock.
Definition: log_prefetcher.cpp:182
Th_key m_key_th_prefetcher
Key for prefetcher thread.
Definition: log_prefetcher.h:241
std::atomic< bool > m_is_error
Becomes true in case error has been encountered.
Definition: log_prefetcher.h:228
std::optional< Elem_type > dequeue(P &&wait_predicate)
Consumes the next batch of data.
Definition: log_prefetcher_impl.hpp:34
Data_source_sptr Elem_type
Definition: log_prefetcher.h:89
mysql::concurrency::Thread_key Th_key
Definition: log_prefetcher.h:64
mysql::concurrency::Locking_queue< Elem_type > Locking_queue_type
Definition: log_prefetcher.h:165
void ensure_file_done(const std::string &file_name)
If currently rotated file is file_name, waits until rotate is done.
Definition: log_prefetcher.cpp:172
Thread_type m_prefetcher
Thread that runs prefetching.
Definition: log_prefetcher.h:181
mysql::concurrency::Mutex_key Mt_key
Definition: log_prefetcher.h:62
void update_current_file(const char *file_name, bool active)
Updates the current file and its activity under the lock.
Definition: log_prefetcher.cpp:187
MYSQL_BIN_LOG * m_log
The MYSQL_BIN_LOG object we use to know from where we need to read data.
Definition: log_prefetcher.h:174
Log_prefetcher(Log_prefetcher &&)=delete
std::string m_current_log_name
Currently processed log file Protected by m_mt_prefetcher.
Definition: log_prefetcher.h:232
PSI_memory_key Mem_key
Definition: log_prefetcher.h:65
std::size_t m_current_file_length
Length of the currently opened file.
Definition: log_prefetcher.h:235
Log_prefetcher(const Log_prefetcher &)=delete
std::size_t m_current_offset
Current file offset.
Definition: log_prefetcher.h:238
mysql::concurrency::Mutex m_mt_prefetcher
Mutex protecting access to m_tasks.
Definition: log_prefetcher.h:193
void stop()
Gracefully stops the prefetcher.
Definition: log_prefetcher.cpp:164
bool m_log_active
True in case reading from the active relay log file (used currently by the receiver thread),...
Definition: log_prefetcher.h:187
mysql::concurrency::Condition_variable m_cv_prefetcher
Cv used by the scheduler main thread to wait on, when no task is available or tasks in m_task queue a...
Definition: log_prefetcher.h:196
Queue_type m_cache
Queue into which prefetcher puts data batches read from a raw binary file.
Definition: log_prefetcher.h:178
void start_prefetcher()
Starts asynchronous thread prefetching data.
Definition: log_prefetcher.cpp:52
void run_prefetch_thread()
Runs a thread that prefetch data from the relay log.
Definition: log_prefetcher.cpp:107
mysql::concurrency::Mutex m_mt_move_file
Mutex protecting access to m_move_file.
Definition: log_prefetcher.h:202
#define P
Definition: dtoa.cc:620
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
Allocator class that uses a polymorphic Memory_resource to allocate memory.
Class that wraps resources in a polymorphic manner.
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
PSI_cond_key Cv_key
Definition: condition_variable_srv.h:39
PSI_mutex_key Mutex_key
Definition: mutex_srv.h:41
std::thread Thread
Definition: thread_stl.h:42
PSI_thread_key Thread_key
Definition: thread_srv.h:43
constexpr std::size_t prefetcher_batch_size
Definition: tune.h:44
constexpr bool csa_prefetcher_simple_queue
Definition: tune.h:42
constexpr std::size_t prefetcher_queue_max_size
Definition: tune.h:43
Definition: channel.cpp:28
std::shared_ptr< Log_prefetcher > Log_prefetcher_sptr
Definition: log_prefetcher.h:48
std::shared_ptr< Data_source > Data_source_sptr
Definition: data_source.h:39
required string type
Definition: replication_group_member_actions.proto:34
Experimental API header.