MySQL 26.7.0
Source Code Documentation
csa_service.h
Go to the documentation of this file.
1// Copyright (c) 2024, 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_SERVICE_H
25#define MYSQL_CSA_SERVICE_H
26
27#include <mysql/plugin.h>
28#include <memory>
29#include <optional>
30#include <string>
31#include <type_traits>
50
51namespace mysql::csa {
52
53/// @brief Service class for the Change Streams Applier (CSA)
54class Csa_service : public Module {
55 public:
56 /// @brief Result type for tasks
58 /// @brief Thread pool type
62 /// @brief Shared pointer to Thread_pool
63 using Thread_pool_ptr = std::shared_ptr<Thread_pool>;
64 /// @brief Clock type used in the scheduler
66 /// @brief Shared pointer to Scheduler_clock
67 using Clock_ptr = std::shared_ptr<mysql::scheduler::Scheduler_clock>;
68 /// @brief Scheduler type
70 /// @brief Shared pointer to Scheduler_type
71 using Scheduler_ptr = std::shared_ptr<Scheduler_type>;
72 /// @brief Schedule factory type
74 /// @brief Shared pointer to Schedule_factory
75 using Schedule_factory_ptr = std::shared_ptr<Schedule_factory>;
76 /// @brief Dependency tracker type
78 /// @brief Shared pointer to Dependency_tracker
80 /// @brief Shared pointer to Session_service
84
85 /// @brief Constructor
87 /// @brief Destructor
88 virtual ~Csa_service() override;
89 /// @brief Synchronous method that runs the channel applier
90 /// @param rli Channel RLI object
91 /// @return True if run succeeds, false otherwise
92 bool run(Relay_log_info *rli);
93 /// @brief Synchronous method that removes data and destroys
94 /// objects related to the channel
95 /// @param rli Channel RLI object
96 void remove(Relay_log_info *rli);
97 /// @brief Obtains the number of workers configured for the channel
98 std::size_t get_workers_number(const char *channel);
99 /// @brief Obtains session legacy statistics kept in the RLI object
100 /// @param channel Channel id
101 /// @param worker_id Id of the worker for which we want to extract the session
102 /// @return Upon success, returns corresponding legacy stats. If they are
103 /// unavailable due to e.g. channel reconfiguration, returns an empty object
104 std::optional<Session_legacy_stats> get_session_legacy_stats(
105 const char *channel, std::size_t worker_id);
106 /// Checks whether applier for the channel has applied all of the work and
107 /// is waiting for more tasks
108 /// @param channel Applier channel name
109 /// @return When true - channel is waiting for more work. When false - channel
110 /// is currently applying. When no value - no channel or channel is inactive
111 std::optional<bool> has_applied_all_work(const char *channel);
112
113 /// Checks whether CSA contains session with a given THD thread_id
114 /// @param channel Selected channel
115 /// @param thread_id THD thread identifier
116 bool is_csa_event_applier(const char *channel, unsigned int thread_id);
117
118 /// This function is used only to initialize channel legacy statistics and
119 /// to make empty statistics available in case channel was not activated
120 /// @param channel Channel name
121 /// @param channel_unique_id Channel unique integer identifier
122 /// @param worker_num Workers number currently configured for this channel
123 /// @return True on success, false on failure
124 [[nodiscard]] bool initialize_channel_data(const char *channel,
125 std::size_t channel_unique_id,
126 std::size_t worker_num);
127
128 /// This function is used only to remove cached channel statistics upon
129 /// channel deletion
130 /// @param channel Channel name
131 /// @param remove When true, erases channel data. Otherwise, clears values.
132 void clear_channel_data(const char *channel, bool remove);
133
134 /// @brief Stops applier now
135 /// @param channel Channel name
136 /// @param force_kill When true, awakes all sessions
137 void stop(const char *channel, bool force_kill);
138
139 private:
140 /// @brief Obtains session legacy statistics kept in the RLI object, no lock
141 /// @param channel Channel id
142 /// @param worker_id Id of the worker for which we want to extract the session
143 /// @return Upon success, returns corresponding legacy stats. If they are
144 /// unavailable due to e.g. channel reconfiguration, returns an empty object
145 std::optional<Session_legacy_stats> get_session_legacy_stats_internal(
146 const char *channel, std::size_t worker_id);
147 /// @brief Initialization
148 /// @retval 0 Success
149 /// @retval 1 Failure
150 bool do_init() override;
151 /// @brief Deinitialization
152 /// @retval 0 Success
153 /// @retval 1 Failure
154 bool do_deinit() override;
155 /// @brief Helper function to clean up channel data upon destruction
156 /// @param channel Channel identifier
157 void clean_up_context(const std::string &channel);
158
159 /// The number of references (channels), protected with internal lock
160 unsigned int m_kernel_ref_count{0};
161 /// Channel data
162 std::unordered_map<std::string, Csa_channel> csa_channels;
163 /// Cached statistics for inactive channels
164 std::unordered_map<std::string, std::vector<Session_legacy_stats>>
166 /// When true, channels will execute soft stop after stop of the
167 /// channel was requested
168 std::atomic<bool> m_soft_stop{false};
169};
170
171} // namespace mysql::csa
172
173#endif // MYSQL_CSA_SERVICE_H
Definition: rpl_rli.h:208
Service class for the Change Streams Applier (CSA)
Definition: csa_service.h:54
std::shared_ptr< Thread_pool > Thread_pool_ptr
Shared pointer to Thread_pool.
Definition: csa_service.h:63
mysql::scheduler::Dependency_tracker_ptr Dependency_tracker_ptr
Shared pointer to Dependency_tracker.
Definition: csa_service.h:79
std::optional< Session_legacy_stats > get_session_legacy_stats(const char *channel, std::size_t worker_id)
Obtains session legacy statistics kept in the RLI object.
Definition: csa_service.cpp:549
void stop(const char *channel, bool force_kill)
Stops applier now.
Definition: csa_service.cpp:437
bool is_csa_event_applier(const char *channel, unsigned int thread_id)
Checks whether CSA contains session with a given THD thread_id.
Definition: csa_service.cpp:587
mysql::csa::Session_service_ptr Session_service_ptr
Shared pointer to Session_service.
Definition: csa_service.h:81
void remove(Relay_log_info *rli)
Synchronous method that removes data and destroys objects related to the channel.
Definition: csa_service.cpp:370
bool initialize_channel_data(const char *channel, std::size_t channel_unique_id, std::size_t worker_num)
This function is used only to initialize channel legacy statistics and to make empty statistics avail...
Definition: csa_service.cpp:486
std::optional< Session_legacy_stats > get_session_legacy_stats_internal(const char *channel, std::size_t worker_id)
Obtains session legacy statistics kept in the RLI object, no lock.
Definition: csa_service.cpp:505
std::unordered_map< std::string, Csa_channel > csa_channels
Channel data.
Definition: csa_service.h:162
std::size_t get_workers_number(const char *channel)
Obtains the number of workers configured for the channel.
Definition: csa_service.cpp:470
virtual ~Csa_service() override
Destructor.
Definition: csa_service.cpp:55
bool do_deinit() override
Deinitialization.
Definition: csa_service.cpp:62
std::shared_ptr< mysql::scheduler::Scheduler_clock > Clock_ptr
Shared pointer to Scheduler_clock.
Definition: csa_service.h:67
mysql::scheduler::Task_result Task_result
Result type for tasks.
Definition: csa_service.h:57
std::atomic< bool > m_soft_stop
When true, channels will execute soft stop after stop of the channel was requested.
Definition: csa_service.h:168
std::shared_ptr< Schedule_factory > Schedule_factory_ptr
Shared pointer to Schedule_factory.
Definition: csa_service.h:75
std::shared_ptr< Scheduler_type > Scheduler_ptr
Shared pointer to Scheduler_type.
Definition: csa_service.h:71
bool run(Relay_log_info *rli)
Synchronous method that runs the channel applier.
Definition: csa_service.cpp:115
std::unordered_map< std::string, std::vector< Session_legacy_stats > > m_inactive_channel_stats
Cached statistics for inactive channels.
Definition: csa_service.h:165
std::optional< bool > has_applied_all_work(const char *channel)
Checks whether applier for the channel has applied all of the work and is waiting for more tasks.
Definition: csa_service.cpp:575
bool do_init() override
Initialization.
Definition: csa_service.cpp:57
void clean_up_context(const std::string &channel)
Helper function to clean up channel data upon destruction.
Definition: csa_service.cpp:385
void clear_channel_data(const char *channel, bool remove)
This function is used only to remove cached channel statistics upon channel deletion.
Definition: csa_service.cpp:558
Csa_service()
Constructor.
Definition: csa_service.cpp:52
unsigned int m_kernel_ref_count
The number of references (channels), protected with internal lock.
Definition: csa_service.h:160
Represents a lockable module.
Definition: module.h:40
Clock implementation that computes LWM (Low Water Mark) based on executed tasks.
Definition: clock_lwm_registry.h:85
Definition: commit_order_clock.h:38
Dependency tracker stub: empty tracker, does not check dependencies.
Definition: dependency_tracker_stub.h:33
Schedule factory class - creates task schedule object pointer based on input parameters.
Definition: schedule_factory.h:38
Main scheduling class.
Definition: scheduler.h:66
Supported statistics:
Definition: statistics_map.h:43
MySQL wrapper for a condition variable, template which may be specialized with a specific implementat...
Definition: thread_pool.h:48
mysql::csa::Statistics_map Statistics_map
Definition: job_applier.cpp:33
static my_thread_id thread_id
Definition: my_thr_init.cc:60
constexpr std::size_t scheduler_tp_queue_size
Definition: tune.h:37
Definition: channel.cpp:28
std::shared_ptr< Session_service > Session_service_ptr
Shared pointer to Session_service.
Definition: session_service.h:40
std::unique_ptr< Base_dependency_tracker > Dependency_tracker_ptr
Definition: base_dependency_tracker.h:89
Task_result
Acceptable task state after its execution.
Definition: task_result.h:32
Definition: task.h:427