MySQL 26.7.0
Source Code Documentation
event_set_fetchable_relay_log.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_EVENT_SET_FETCHABLE_RELAY_LOG_H
25#define MYSQL_CSA_EVENT_SET_FETCHABLE_RELAY_LOG_H
26
27#include <condition_variable>
28#include <memory>
29#include <mutex>
30#include <optional>
31#include <string>
32
34#include "sql/binlog/decompressing_event_object_istream.h" // Decompressing_event_object_istream
35#include "sql/binlog_reader.h" // Relaylog_file_reader
38
39namespace mysql::csa {
40
41/// @brief Implementation of Event_set_fetchable that fetches events from the
42/// relay log.
43///
44/// If the stream is compressed, the fetching function will perform
45/// decompression. Objects of this class are created by the relay log reader and
46/// used to fetch consecutive parts of a transaction (relay log events).
47/// Typically, a transaction will contain as many event sets as the number of
48/// files it spans.
49/// @note Each object initializes its own reader and sets it to the position of
50/// the first event metadata it contains.
52 public:
53 /// @brief Shared pointer to a Log_event.
54 using Log_event_ptr = std::shared_ptr<Log_event>;
55 /// @brief Type alias for return status.
57 /// @brief Type alias for the decompressing event stream.
59 /// @brief Unique pointer to the decompressing stream.
60 using Stream_ptr = std::unique_ptr<Stream_type>;
61
62 /// @brief Constructs an Event_set_fetchable_relay_log with relay log
63 /// coordinates.
64 ///
65 /// The coordinates must remain valid until the transaction is applied or
66 /// deemed non-appliable. Uses a relay log deleter handler to ensure the file
67 /// is not deleted while in use.
68 ///
69 /// @param filename The name of the relay log file.
70 /// @param start_file_pos The starting position in the file.
71 /// @param end_file_pos The "end" position for this event set.
72 /// @param deleter Handle to the relay log deleter.
73 /// @param checksum_validation Flag to enable checksum validation.
74 /// @param is_trx Flag indicating if this set represents a transaction.
75 /// @param fde Shared pointer to FDE for this event set.
76 /// @param streaming_open If true, this set is stream-open and accepts
77 /// incremental end position updates.
79 std::size_t start_file_pos,
80 std::size_t end_file_pos,
82 bool checksum_validation, bool is_trx,
83 Log_event_ptr fde, bool streaming_open = false);
84
85 /// @brief Fetches the next event from the initialized internal stream.
86 ///
87 /// @return Managed_event or empty optional in case of stream end or error.
88 /// Error can be checked with 'is_error'.
89 bool wait_next() override;
90 std::optional<Managed_event> fetch_next() override;
91
92 /// @brief Retrieves the error message if any error occurred.
93 ///
94 /// @return Const reference to the error message string.
95 const std::string &get_error_str() const override;
96
97 /// @brief Checks if the fetchable stream has finished without error.
98 ///
99 /// @return true if finished without error, false otherwise.
100 bool is_done() const override;
101
102 /// @brief Checks if an error occurred in the fetchable stream.
103 ///
104 /// @return true if an error occurred, false otherwise.
105 bool is_error() const override;
106
107 /// @brief Checks if this event set represents a transaction.
108 ///
109 /// @return true if it contains a transaction, false otherwise.
110 bool is_trx() const override;
111
112 /// @brief Resets the state to allow fetching again, clearing error state.
113 ///
114 /// Leaves the reader closed to conserve file descriptors.
115 /// @param reset_events When true events states need to be reset
116 void reset(bool reset_events) override;
117
118 /// @brief Callback notifying that task was executed successfully.
119 void set_success() override;
120
121 /// @brief Destructor.
122 virtual ~Event_set_fetchable_relay_log() override;
123
124 /// @brief Returns a string representation of this batch's information.
125 ///
126 /// @return String containing basic information about the batch.
127 std::string to_string() const;
128
129 /// @brief Obtains non-owning pointer to current transaction FDE
130 /// @return Non-owning pointer to FDE
131 Fde_ptr get_fde() override;
132
133 /// @brief Appends one published event boundary for stream-open batch.
134 /// @param end_file_pos End position (exclusive) for the next available event.
135 /// @param seal_after When true, the batch is sealed together with publish.
136 void append_event_end(std::size_t end_file_pos, bool seal_after = false);
137
138 /// @brief Seals stream-open batch. No more events will be appended.
139 void seal_stream();
140
141 /// @brief Marks stream-open batch as truncated and wakes blocked readers.
143
144 private:
145 /// @brief Checks if currently decompressing the internal stream (TPLE).
146 ///
147 /// @return true if decompressing, false otherwise.
148 bool decompressing() const;
149
150 /// @brief Fetches the next event from the stream.
151 ///
152 /// @return Managed_event or empty optional in case of stream end or error.
153 std::optional<Managed_event> fetch_from_stream();
154
155 /// @brief Waits until stream has at least one more event available.
156 ///
157 /// @retval true At least one more event can be fetched.
158 /// @retval false Stream is sealed/truncated/error and no more fetch is
159 /// possible.
161
162 /// @brief Safely closes the reader if it is open.
163 void safe_close_reader();
164 /// @brief Safely opens the reader (closes if already open and reopens).
165 void safe_open_reader();
166 /// @brief Starts reading from the file by opening it and creating the input
167 /// stream.
168 void start_reading();
169
170 /// @brief Flag indicating if the reader is open.
171 bool m_is_initialized = false;
172 /// @brief Flag indicating if processing is done (finished or error).
173 bool m_is_done = false;
174 /// @brief Name of the relay log file to read from.
175 std::string m_file_name{""};
176 /// @brief Starting file position for reading.
177 std::size_t m_start_file_pos{0};
178 /// @brief Detailed error message if any.
179 std::string m_failure_msg{""};
180 /// @brief Status of the object.
182 /// @brief Handle to the relay log deleter.
184 /// @brief Decompressing stream object for handling compressed events.
186 /// @brief Relay log file reader used by the decompressing stream.
188 /// @brief Flag indicating if this is a transaction.
189 bool m_is_trx{false};
190 /// @brief Flag indicating if currently decompressing an internal event
191 /// (TPLE).
192 bool m_decompressing{false};
193 /// @brief owning pointer to FDE.
195 /// @brief non-owning pointer to FDE.
197
198 /// @brief Stream synchronization state.
199 mutable std::mutex m_stream_mutex;
200 std::condition_variable m_stream_cv;
202 bool m_stream_open{false};
203 bool m_stream_sealed{true};
205};
206
207} // namespace mysql::csa
208
209#endif // MYSQL_CSA_EVENT_SET_FETCHABLE_RELAY_LOG_H
For binlog version 4.
Definition: log_event.h:1558
Stream class that yields Log_event objects from a source.
Definition: decompressing_event_object_istream.h:67
Implementation of Event_set_fetchable that fetches events from the relay log.
Definition: event_set_fetchable_relay_log.h:51
void safe_close_reader()
Safely closes the reader if it is open.
Definition: event_set_fetchable_relay_log.cpp:278
bool m_is_trx
Flag indicating if this is a transaction.
Definition: event_set_fetchable_relay_log.h:189
bool wait_for_event_availability()
Waits until stream has at least one more event available.
Definition: event_set_fetchable_relay_log.cpp:103
std::mutex m_stream_mutex
Stream synchronization state.
Definition: event_set_fetchable_relay_log.h:199
bool m_decompressing
Flag indicating if currently decompressing an internal event (TPLE).
Definition: event_set_fetchable_relay_log.h:192
bool m_is_done
Flag indicating if processing is done (finished or error).
Definition: event_set_fetchable_relay_log.h:173
std::size_t m_published_end_file_pos
Definition: event_set_fetchable_relay_log.h:201
const std::string & get_error_str() const override
Retrieves the error message if any error occurred.
Definition: event_set_fetchable_relay_log.cpp:226
std::string to_string() const
Returns a string representation of this batch's information.
Definition: event_set_fetchable_relay_log.cpp:216
bool is_done() const override
Checks if the fetchable stream has finished without error.
Definition: event_set_fetchable_relay_log.cpp:230
Fde_ptr get_fde() override
Obtains non-owning pointer to current transaction FDE.
Definition: event_set_fetchable_relay_log.cpp:65
Log_event_ptr m_fde_base
owning pointer to FDE.
Definition: event_set_fetchable_relay_log.h:194
void set_stream_truncated()
Marks stream-open batch as truncated and wakes blocked readers.
Definition: event_set_fetchable_relay_log.cpp:94
bool wait_next() override
Fetches the next event from the initialized internal stream.
Definition: event_set_fetchable_relay_log.cpp:121
bool is_error() const override
Checks if an error occurred in the fetchable stream.
Definition: event_set_fetchable_relay_log.cpp:234
bool m_stream_sealed
Definition: event_set_fetchable_relay_log.h:203
Return_status m_status
Status of the object.
Definition: event_set_fetchable_relay_log.h:181
bool m_is_initialized
Flag indicating if the reader is open.
Definition: event_set_fetchable_relay_log.h:171
void set_success() override
Callback notifying that task was executed successfully.
Definition: event_set_fetchable_relay_log.cpp:57
std::unique_ptr< Stream_type > Stream_ptr
Unique pointer to the decompressing stream.
Definition: event_set_fetchable_relay_log.h:60
bool m_stream_open
Definition: event_set_fetchable_relay_log.h:202
bool is_trx() const override
Checks if this event set represents a transaction.
Definition: event_set_fetchable_relay_log.cpp:285
std::optional< Managed_event > fetch_from_stream()
Fetches the next event from the stream.
Definition: event_set_fetchable_relay_log.cpp:136
void start_reading()
Starts reading from the file by opening it and creating the input stream.
Definition: event_set_fetchable_relay_log.cpp:242
Relay_log_deleter_handle m_delete_file_handle
Handle to the relay log deleter.
Definition: event_set_fetchable_relay_log.h:183
bool decompressing() const
Checks if currently decompressing the internal stream (TPLE).
Definition: event_set_fetchable_relay_log.cpp:61
std::size_t m_start_file_pos
Starting file position for reading.
Definition: event_set_fetchable_relay_log.h:177
Relaylog_file_reader m_reader
Relay log file reader used by the decompressing stream.
Definition: event_set_fetchable_relay_log.h:187
Fde_ptr m_fde
non-owning pointer to FDE.
Definition: event_set_fetchable_relay_log.h:196
std::optional< Managed_event > fetch_next() override
Fetches the next event if possible.
Definition: event_set_fetchable_relay_log.cpp:209
std::string m_file_name
Name of the relay log file to read from.
Definition: event_set_fetchable_relay_log.h:175
Event_set_fetchable_relay_log(std::string filename, std::size_t start_file_pos, std::size_t end_file_pos, Relay_log_deleter_handle deleter, bool checksum_validation, bool is_trx, Log_event_ptr fde, bool streaming_open=false)
Constructs an Event_set_fetchable_relay_log with relay log coordinates.
Definition: event_set_fetchable_relay_log.cpp:38
void append_event_end(std::size_t end_file_pos, bool seal_after=false)
Appends one published event boundary for stream-open batch.
Definition: event_set_fetchable_relay_log.cpp:69
std::condition_variable m_stream_cv
Definition: event_set_fetchable_relay_log.h:200
void safe_open_reader()
Safely opens the reader (closes if already open and reopens).
Definition: event_set_fetchable_relay_log.cpp:265
Stream_ptr m_input_stream
Decompressing stream object for handling compressed events.
Definition: event_set_fetchable_relay_log.h:185
void reset(bool reset_events) override
Resets the state to allow fetching again, clearing error state.
Definition: event_set_fetchable_relay_log.cpp:257
virtual ~Event_set_fetchable_relay_log() override
Destructor.
Definition: event_set_fetchable_relay_log.cpp:238
std::string m_failure_msg
Detailed error message if any.
Definition: event_set_fetchable_relay_log.h:179
bool m_stream_truncated
Definition: event_set_fetchable_relay_log.h:204
void seal_stream()
Seals stream-open batch. No more events will be appended.
Definition: event_set_fetchable_relay_log.cpp:86
Represents metadata of a set of events, capable of being fetched from a storage.
Definition: event_set_fetchable.h:55
std::shared_ptr< Log_event > Log_event_ptr
Definition: event_set_fetchable.h:59
Stream class that yields Log_event objects, including events contained in Transaction_payload_log_eve...
Definition: channel.cpp:28
std::shared_ptr< Relay_log_deleter > Relay_log_deleter_handle
Represents a shared reference to Relay_log_deleter.
Definition: relay_log_deleter.h:40
Return_status
Simple, strongly-typed enumeration to indicate internal status: ok, error.
Definition: return_status.h:40
const char * filename
Definition: pfs_example_component_population.cc:67
Experimental API header.