MySQL 26.7.0
Source Code Documentation
relay_log_adaptive_reader.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_RELAY_LOG_ADAPTIVE_READER_H
25#define MYSQL_CSA_RELAY_LOG_ADAPTIVE_READER_H
26
27#include <memory>
28#include <vector>
42#include "sql/log_event.h" // Format_description_log_event
44#include "sql/rpl_rli.h" // Relay_log_info
45
46namespace mysql::csa {
47
48/// @brief Iterates over relay log and returns fetchable Jobs
49/// This reader reads consecutive events from the relay log and caches events.
50/// When 'max_read_event_bytes' is reached for a single transaction, the
51/// reader switches to "metadata" mode. From this point, it will read
52/// only event metadata from the relay log and supply event set batches
53/// that are able to fetch themselves from the relay log on demand (durring
54/// apply)
56 public:
57 /// @param instance_id Instance (channel) id
58 /// @param rli RLI for the channel
59 /// @param max_read_event_bytes The maximum number of bytes in a transaction
60 /// which reader can read, decode and cache
61 /// @param max_read_payload_bytes The maximum number of bytes in a transaction
62 /// which reader can read and cache payload
63 Relay_log_adaptive_reader(int instance_id, Relay_log_info *rli,
64 std::size_t max_read_event_bytes,
65 std::size_t max_read_payload_bytes);
66 virtual ~Relay_log_adaptive_reader() override;
67
70 delete;
73
74 /// @brief Reads the next Job (full transaction) from the relay log and
75 /// supplies a fetchable job object. This function may block for
76 /// a while in case it is reading from an active relay log and when being
77 /// outside of transaction boundary.
78 /// @return Pointer to Job in case reading succeeded. Empty pointer in case
79 /// of failure or stop of the reader
80 Job_ptr read() override;
81
82 /// Checks whether CSA is stopped
83 /// @brief True if stop was requested, false otherwise
84 bool is_stopped() const override;
85
86 /// Checks whether reader errored out
87 /// True - reader errored out. False - no error.
88 bool is_error() const override;
89
90 /// Awakes and stops the reader
91 void stop() override;
92
93 private:
94 /// Tunes reader parameters based on CSA statistics
95 void tune();
96
97 /// @brief Pointer to the relay log context of applier thread that launches
98 /// CSA. It contains pointer to the actual relay log object needed for
99 /// reading transactions
101 /// Applier reader, object used to read event METADATA (non owning pointer)
103 /// Owning pointer of applier reader
105 /// Deleter for the current file
107
109
110 /// @brief Current FDE pointer. Source's FDEs are attached to transactions
111 std::shared_ptr<Log_event> m_current_fde;
112
117
118 // Stateful metadata stream for currently assembled transaction.
119 std::shared_ptr<Fetchable_transaction> m_active_fetchable_transaction;
120 // Start metadata for current on-demand batch.
122 // Current read mode and next read mode computed from GTID trx length.
126 // Currently open stream batch for cached events.
128 // Currently open stream batch for metadata-mode events.
130 // Maximum event size seen in currently assembled transaction.
132 // Reader-side transaction boundary state persisted across read() calls.
133 bool m_is_in_trx{false};
134
135 /// Previous sequence number recorded to validate timestamps
136 int64_t m_prev_seq{-1};
137 /// Internal error flag
138 bool m_is_error{false};
139 /// Stop flag, set externally or by reached until condition
140 bool m_stopped{false};
141 /// Unique instance id for statistics monitoring
142 unsigned int m_instance_id{0};
143 /// Statistics monitoring object for the current instance
145 /// Resource monitoring object for the current instance
147 /// @brief The maximum number of bytes that this reader can cache for a single
148 /// transaction with decoding. When this limit is reached, reader switches to
149 /// payload cache read mode.
151 /// @brief The maximum number of bytes that this reader can cache for a single
152 /// transaction without decoding. When this limit is reached, reader
153 /// switches to reading of transaction metadata
155 /// Owning pointer to channel object
156 std::unique_ptr<Channel> m_channel;
157 /// Threshold below which we ask workers to read transactions in order to
158 /// increase reader and receiver throughput
160 /// Threshold above which we go back to previous settings
162 /// Used to calculate workers load
164 /// Used to calculate workers load
166 /// Time point at which we tuned parameters for the last time
167 std::chrono::time_point<std::chrono::system_clock> m_last_tune_time{
168 std::chrono::system_clock::now()};
169 /// We tune parameters each m_tune_period_ms milliseconds
170 long int m_tune_period_ms{5000};
171};
172
173} // namespace mysql::csa
174
175#endif // MYSQL_CSA_RELAY_LOG_ADAPTIVE_READER_H
Definition: rpl_rli.h:208
This is the base class for verifying transaction boundaries.
Definition: trx_boundary_parser.h:50
@ TRX_BOUNDARY_PARSER_APPLIER
Definition: trx_boundary_parser.h:59
Represents basic event metadata: type and length.
Definition: event_file_metadata.h:35
The Event Reader / Controller class.
Definition: event_reader_controller.h:80
Empty implementation of Event_set_fetchable that does not fetch events from the storage as they were ...
Definition: event_set_fetchable_cache.h:48
Implementation of Event_set_fetchable that fetches events from the relay log.
Definition: event_set_fetchable_relay_log.h:51
A job represents a single unit of work applied by worker pool threads.
Definition: job.h:47
Definition: reader.h:40
Iterates over relay log and returns fetchable Jobs This reader reads consecutive events from the rela...
Definition: relay_log_adaptive_reader.h:55
std::unique_ptr< Channel > m_channel
Owning pointer to channel object.
Definition: relay_log_adaptive_reader.h:156
Relay_log_adaptive_reader(Relay_log_adaptive_reader &&)=delete
Relay_log_adaptive_reader & operator=(const Relay_log_adaptive_reader &)=delete
double m_worker_min_load_threshold
Threshold below which we ask workers to read transactions in order to increase reader and receiver th...
Definition: relay_log_adaptive_reader.h:159
std::size_t m_current_transaction_max_event_length
Definition: relay_log_adaptive_reader.h:131
Sliding_window_counter m_thp_worker_exec_time
Used to calculate workers load.
Definition: relay_log_adaptive_reader.h:165
bool m_is_in_trx
Definition: relay_log_adaptive_reader.h:133
std::shared_ptr< Fetchable_transaction > m_active_fetchable_transaction
Definition: relay_log_adaptive_reader.h:119
Reader_controller_read_type m_next_read_type
Definition: relay_log_adaptive_reader.h:124
void tune()
Tunes reader parameters based on CSA statistics.
Definition: relay_log_adaptive_reader.cpp:76
Relay_log_adaptive_reader(const Relay_log_adaptive_reader &)=delete
double m_worker_max_load_threshold
Threshold above which we go back to previous settings.
Definition: relay_log_adaptive_reader.h:161
std::size_t m_max_read_payload_bytes
The maximum number of bytes that this reader can cache for a single transaction without decoding.
Definition: relay_log_adaptive_reader.h:154
unsigned int m_instance_id
Unique instance id for statistics monitoring.
Definition: relay_log_adaptive_reader.h:142
void stop() override
Awakes and stops the reader.
Definition: relay_log_adaptive_reader.cpp:485
Relay_log_info * m_rli
Pointer to the relay log context of applier thread that launches CSA.
Definition: relay_log_adaptive_reader.h:100
virtual ~Relay_log_adaptive_reader() override
Definition: relay_log_adaptive_reader.cpp:74
Relay_log_adaptive_reader(int instance_id, Relay_log_info *rli, std::size_t max_read_event_bytes, std::size_t max_read_payload_bytes)
Definition: relay_log_adaptive_reader.cpp:45
Relay_log_adaptive_reader & operator=(Relay_log_adaptive_reader &&)=delete
Event_reader_controller * m_reader
Applier reader, object used to read event METADATA (non owning pointer)
Definition: relay_log_adaptive_reader.h:102
Relay_log_deleter_handle m_delete_handler
Deleter for the current file.
Definition: relay_log_adaptive_reader.h:106
Job_ptr read() override
Reads the next Job (full transaction) from the relay log and supplies a fetchable job object.
Definition: relay_log_adaptive_reader.cpp:149
Reader_controller_read_type m_read_type
Definition: relay_log_adaptive_reader.h:123
std::size_t m_max_read_event_bytes
The maximum number of bytes that this reader can cache for a single transaction with decoding.
Definition: relay_log_adaptive_reader.h:150
bool m_is_error
Internal error flag.
Definition: relay_log_adaptive_reader.h:138
bool is_error() const override
Checks whether reader errored out True - reader errored out.
Definition: relay_log_adaptive_reader.cpp:483
long int m_tune_period_ms
We tune parameters each m_tune_period_ms milliseconds.
Definition: relay_log_adaptive_reader.h:170
scheduler::Statistics_instance_monitor_ref m_stat_monitor
Statistics monitoring object for the current instance.
Definition: relay_log_adaptive_reader.h:144
std::shared_ptr< Log_event > m_current_fde
Current FDE pointer. Source's FDEs are attached to transactions.
Definition: relay_log_adaptive_reader.h:111
std::chrono::time_point< std::chrono::system_clock > m_last_tune_time
Time point at which we tuned parameters for the last time.
Definition: relay_log_adaptive_reader.h:167
bool m_stopped
Stop flag, set externally or by reached until condition.
Definition: relay_log_adaptive_reader.h:140
Event_set_fetchable_cache * m_open_cache_batch
Definition: relay_log_adaptive_reader.h:127
int64_t m_prev_seq
Previous sequence number recorded to validate timestamps.
Definition: relay_log_adaptive_reader.h:136
Event_set_fetchable_relay_log * m_open_stream_batch
Definition: relay_log_adaptive_reader.h:129
Log_prefetcher_sptr m_prefetcher
Definition: relay_log_adaptive_reader.h:108
Event_file_metadata m_start_batch_metadata
Definition: relay_log_adaptive_reader.h:121
Resource_instance_monitor_ref m_resource_monitor
Resource monitoring object for the current instance.
Definition: relay_log_adaptive_reader.h:146
bool is_stopped() const override
Checks whether CSA is stopped.
Definition: relay_log_adaptive_reader.cpp:116
Log_purge_controller_sptr m_shared_controller
Owning pointer of applier reader.
Definition: relay_log_adaptive_reader.h:104
Sliding_window_counter m_thp_task_exec_time
Used to calculate workers load.
Definition: relay_log_adaptive_reader.h:163
Transaction_boundary_parser m_transaction_boundary_parser
Definition: relay_log_adaptive_reader.h:115
Binary log event definitions.
constexpr std::size_t provider_max_read_payload_bytes
Definition: tune.h:47
constexpr std::size_t provider_max_read_event_bytes
Definition: tune.h:46
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< Log_prefetcher > Log_prefetcher_sptr
Definition: log_prefetcher.h:48
std::shared_ptr< Log_purge_controller > Log_purge_controller_sptr
Definition: log_purge_controller.h:35
std::reference_wrapper< Resource_instance_monitor > Resource_instance_monitor_ref
Definition: resource_monitor.h:43
Reader_controller_read_type
Definition: reader_controller_read_type.h:31
@ event
Reads and decodes event.
std::reference_wrapper< Statistics_instance_monitor > Statistics_instance_monitor_ref
Definition: statistics_instance_monitor.h:43
Experimental API header.
Calculates statistic value in a time window.
Definition: sliding_window_counter.h:30
Transaction boundary parser definitions.