MySQL 26.7.0
Source Code Documentation
event_set_fetchable_cache.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_CACHE_H
25#define MYSQL_CSA_EVENT_SET_FETCHABLE_CACHE_H
26
27#include <condition_variable>
28#include <cstdint>
29#include <memory>
30#include <mutex>
31#include <optional>
32#include <string>
33#include <vector>
34
36#include "sql/binlog/decompressing_event_object_istream.h" // Decompressing_event_object_istream
42
43namespace mysql::csa {
44
45/// @brief Empty implementation of Event_set_fetchable that does not fetch
46/// events from the storage as they were already fetched. It returns cached
47/// event objects instead.
49 public:
50 /// @brief Shared pointer to a Log_event.
51 using Log_event_ptr = std::shared_ptr<Log_event>;
52 /// @brief Type alias for return status.
54 /// @brief Vector of Log_event_ptr representing a set of events.
55 using Event_set_type = std::vector<IReader_event_ptr>;
56 /// @brief Type alias for the decompressing event stream.
58 /// @brief Unique pointer to the decompressing stream.
59 using Stream_ptr = std::unique_ptr<Stream_type>;
60
61 /// @brief Constructs an Event_set_fetchable_cache with pre-fetched events.
62 ///
63 /// @param events The vector of events to cache (moved into the object).
64 /// @param is_trx Flag indicating if this set represents a transaction.
65 /// @param fde Shared pointer to the Format_description_event.
66 /// @param delete_file_handle Handle to the relay log deleter.
67 /// @param streaming_open If true, events may be appended concurrently.
69 Log_event_ptr fde,
70 Relay_log_deleter_handle delete_file_handle,
71 bool streaming_open = false);
72
73 /// @brief Fetches the next event from the internal cache.
74 ///
75 /// @return Managed_event or empty optional in case of stream end or error.
76 /// Error can be checked with 'is_error'. Decompresses events if
77 /// necessary.
78 bool wait_next() override;
79 std::optional<Managed_event> fetch_next() override;
80
81 /// @brief Retrieves the error message if any error occurred.
82 ///
83 /// @return Const reference to the error message string.
84 const std::string &get_error_str() const override;
85
86 /// @brief Checks if the fetchable stream has finished processing without
87 /// error.
88 ///
89 /// @return true if finished without error, false otherwise.
90 bool is_done() const override;
91
92 /// @brief Checks if an error occurred in the fetchable stream.
93 ///
94 /// @return true if an error occurred, false otherwise.
95 bool is_error() const override;
96
97 /// @brief Checks if this event set represents a transaction.
98 ///
99 /// @return true if it contains a transaction, false otherwise.
100 bool is_trx() const override;
101
102 /// @brief Resets the state to allow fetching the event set again, clearing
103 /// any error state.
104 /// @param reset_events When true events states need to be reset
105 void reset(bool reset_events) override;
106
107 /// @brief Destructor.
108 virtual ~Event_set_fetchable_cache() override;
109
110 /// @brief Obtains non-owning pointer to current transaction FDE
111 /// @return Non-owning pointer to FDE
112 Fde_ptr get_fde() override;
113
114 /// @brief Callback notifying that task was executed successfully
115 void set_success() override;
116
117 /// @brief Appends one event to a stream-open cache batch.
118 /// @param event Event to append
119 /// @param seal_after When true, the batch is sealed together with publish.
120 void append_event(IReader_event_ptr event, bool seal_after = false);
121
122 /// @brief Seals stream-open cache batch.
123 void seal_stream();
124
125 /// @brief Marks stream-open cache batch as truncated.
127
128 private:
129 /// @brief Cached vector of events.
131
132 /// @brief Decompresses and returns the next event from the TPLE stream.
133 ///
134 /// @return Optional Log_event_ptr if successful, empty if failed or stream
135 /// ended.
136 std::optional<Log_event_ptr> decompress();
137 /// @brief Helper to deinitialize the decompression stream and update status.
138 void end_decompression();
139 /// @brief Helper to initialize the decompression stream and update status.
140 void start_decompression();
141
142 /// @brief Flag indicating if processing is done (finished or error).
143 bool m_is_done = false;
144 /// @brief Index of the next event to fetch.
145 std::size_t m_event_id{0};
146 /// @brief Detailed error message if any.
147 std::string m_failure_msg{""};
148 /// @brief Status of the object.
150 /// @brief Flag indicating if this is a transaction.
151 bool m_is_trx{false};
152 /// @brief Flag indicating if currently decompressing a TPLE.
153 bool m_decompressing{false};
154 /// @brief Decompressing stream created from TPLE if any.
156 /// @brief Non-owning pointer to compressed event casted to
157 /// Transaction_payload_log_event.
159 /// @brief Compressed event used during decompression.
161 /// @brief Owning pointer to Format_description_event.
163 /// @brief Non-owning pointer to Format_description_event.
165 /// @brief Handle to relay log deleter, relay log will be removed when
166 /// last living reference to this file is released.
168
169 /// @brief Stream synchronization state.
170 mutable std::mutex m_stream_mutex;
171 std::condition_variable m_stream_cv;
172 bool m_stream_open{false};
173 bool m_stream_sealed{true};
175};
176
177} // namespace mysql::csa
178
179#endif // MYSQL_CSA_EVENT_SET_FETCHABLE_CACHE_H
For binlog version 4.
Definition: log_event.h:1558
Definition: log_event.h:3901
Stream class that yields Log_event objects from a source.
Definition: decompressing_event_object_istream.h:67
Empty implementation of Event_set_fetchable that does not fetch events from the storage as they were ...
Definition: event_set_fetchable_cache.h:48
Log_event_ptr m_fde_base
Owning pointer to Format_description_event.
Definition: event_set_fetchable_cache.h:162
bool wait_next() override
Fetches the next event from the internal cache.
Definition: event_set_fetchable_cache.cpp:153
virtual ~Event_set_fetchable_cache() override
Destructor.
Definition: event_set_fetchable_cache.cpp:237
bool m_is_trx
Flag indicating if this is a transaction.
Definition: event_set_fetchable_cache.h:151
const std::string & get_error_str() const override
Retrieves the error message if any error occurred.
Definition: event_set_fetchable_cache.cpp:225
Event_set_fetchable_cache(Event_set_type &&events, bool is_trx, Log_event_ptr fde, Relay_log_deleter_handle delete_file_handle, bool streaming_open=false)
Constructs an Event_set_fetchable_cache with pre-fetched events.
Definition: event_set_fetchable_cache.cpp:36
std::condition_variable m_stream_cv
Definition: event_set_fetchable_cache.h:171
std::optional< Log_event_ptr > decompress()
Decompresses and returns the next event from the TPLE stream.
Definition: event_set_fetchable_cache.cpp:118
void end_decompression()
Helper to deinitialize the decompression stream and update status.
Definition: event_set_fetchable_cache.cpp:110
void append_event(IReader_event_ptr event, bool seal_after=false)
Appends one event to a stream-open cache batch.
Definition: event_set_fetchable_cache.cpp:64
bool m_stream_open
Definition: event_set_fetchable_cache.h:172
std::size_t m_event_id
Index of the next event to fetch.
Definition: event_set_fetchable_cache.h:145
void set_success() override
Callback notifying that task was executed successfully.
Definition: event_set_fetchable_cache.cpp:54
Return_status m_status
Status of the object.
Definition: event_set_fetchable_cache.h:149
void set_stream_truncated()
Marks stream-open cache batch as truncated.
Definition: event_set_fetchable_cache.cpp:87
Relay_log_deleter_handle m_delete_file_handle
Handle to relay log deleter, relay log will be removed when last living reference to this file is rel...
Definition: event_set_fetchable_cache.h:167
std::optional< Managed_event > fetch_next() override
Fetches the next event if possible.
Definition: event_set_fetchable_cache.cpp:177
Stream_ptr m_decompressing_stream
Decompressing stream created from TPLE if any.
Definition: event_set_fetchable_cache.h:155
void reset(bool reset_events) override
Resets the state to allow fetching the event set again, clearing any error state.
Definition: event_set_fetchable_cache.cpp:239
Event_set_type m_events
Cached vector of events.
Definition: event_set_fetchable_cache.h:130
void start_decompression()
Helper to initialize the decompression stream and update status.
Definition: event_set_fetchable_cache.cpp:96
bool m_stream_sealed
Definition: event_set_fetchable_cache.h:173
Transaction_payload_log_event * m_compressed_event_ptr
Non-owning pointer to compressed event casted to Transaction_payload_log_event.
Definition: event_set_fetchable_cache.h:158
Fde_ptr get_fde() override
Obtains non-owning pointer to current transaction FDE.
Definition: event_set_fetchable_cache.cpp:58
bool m_stream_truncated
Definition: event_set_fetchable_cache.h:174
std::mutex m_stream_mutex
Stream synchronization state.
Definition: event_set_fetchable_cache.h:170
bool is_trx() const override
Checks if this event set represents a transaction.
Definition: event_set_fetchable_cache.cpp:62
std::unique_ptr< Stream_type > Stream_ptr
Unique pointer to the decompressing stream.
Definition: event_set_fetchable_cache.h:59
std::string m_failure_msg
Detailed error message if any.
Definition: event_set_fetchable_cache.h:147
Log_event_ptr m_compressed_event
Compressed event used during decompression.
Definition: event_set_fetchable_cache.h:160
bool is_error() const override
Checks if an error occurred in the fetchable stream.
Definition: event_set_fetchable_cache.cpp:233
bool is_done() const override
Checks if the fetchable stream has finished processing without error.
Definition: event_set_fetchable_cache.cpp:229
bool m_is_done
Flag indicating if processing is done (finished or error).
Definition: event_set_fetchable_cache.h:143
std::vector< IReader_event_ptr > Event_set_type
Vector of Log_event_ptr representing a set of events.
Definition: event_set_fetchable_cache.h:55
Fde_ptr m_fde
Non-owning pointer to Format_description_event.
Definition: event_set_fetchable_cache.h:164
void seal_stream()
Seals stream-open cache batch.
Definition: event_set_fetchable_cache.cpp:79
bool m_decompressing
Flag indicating if currently decompressing a TPLE.
Definition: event_set_fetchable_cache.h:153
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
std::shared_ptr< IReader_event > IReader_event_ptr
Definition: ireader_event.h:33
Return_status
Simple, strongly-typed enumeration to indicate internal status: ok, error.
Definition: return_status.h:40
Experimental API header.