MySQL 26.7.0
Source Code Documentation
job_applier.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_JOB_APPLIER_H
25#define MYSQL_CSA_JOB_APPLIER_H
26
31
32namespace mysql::csa {
33
34/// @brief Concrete class representing job applied by the applier. Contains
35/// logic to execute (prepare, commit, retry) transaction.
36class Job_applier : public Job_binlog {
37 public:
39 /// @brief Constructor.
40 /// @param channel Channel this job comes from.
41 /// @param max_retries Maximum number of retries for this job.
42 /// @param fetch_object Object to fetch job data.
43 /// @param stat_monitor Object for monitoring statistics.
44 /// @param res_monitor Object for monitoring resources.
45 Job_applier(Channel *channel, unsigned int max_retries,
46 std::shared_ptr<Fetchable_transaction> fetch_object,
47 Stat_monitor_ref stat_monitor, Resource_monitor_ref res_monitor);
48 /// @brief Destructor.
49 virtual ~Job_applier() override;
50 /// @brief Attaches to relay context.
51 /// @param thread_id Thread pool worker identifier
52 /// @return False on success, true otherwise.
53 bool attach(Thread_id thread_id) override;
54 /// @brief Detaches from relay context.
55 /// @param thread_id Thread pool worker identifier
56 /// @return False on success, true otherwise.
57 bool detach(Thread_id thread_id) override;
58 /// @brief Checks whether this job is attached to relay log context.
59 /// @return True if attached. False otherwise.
60 bool is_attached() const override;
61 /// @brief Restarts internal state. Prepares for retry.
62 /// @return False on success, true otherwise.
63 bool restart() override;
64 /// @brief Presents job identifier.
65 /// @return String with job identifier.
66 std::string to_string() override;
67 /// @brief Sets job failure.
68 void set_failure() override;
69 /// @brief Prepares this job to be applied
70 /// @param ss Pointer to session service.
72 /// @brief Internal function to wait for rollback and restart transaction in
73 /// case deadlock has been found
74 /// @return True if transaction cannot be rolled back or retried. False
75 /// otherwise
77
78 protected:
79 /// @brief Type alias for log event pointer.
81 /// @brief Transaction prepare phase, control events execute fully in
82 /// prepare phase
83 /// @param thread_id Thread pool worker identifier
84 bool prepare(Thread_id thread_id) override;
85 /// @brief Transaction commit phase, noop for control events
86 /// @param thread_id Thread pool worker identifier
87 bool commit(Thread_id thread_id) override;
88 /// @brief Transaction "register for commit" phase, noop for control events
89 /// @param thread_id Thread pool worker identifier
90 /// @return False on success, true on error
91 bool commit_register(Thread_id thread_id) override;
92 /// Applies a single event (internal helper)
93 /// @param ev Event to apply
94 /// @param thd THD session to apply an event
95 /// @return False on success, true on error
96 bool apply_event(const Log_event_ptr &ev, THD *thd);
97 /// @brief Apply events from phase until phase ends or transaction ends
98 /// @param thread_id Thread pool worker identifier
99 /// @return False on success, true otherwise
101 /// Checks and handles RPCO conflict if designated.
102 /// @param thread_id Thread pool worker identifier
103 /// @retval True RPCO conflict detected
104 /// @retval False No RPCO conflict
106 /// Acquires session if not already obtained. Prepares parallel worker context
107 /// @param thread_id Thread pool worker identifier
108 void ensure_session(uint thread_id);
109 /// Called to clean up when a transaction is skipped (skipping commit phase):
110 /// - unregister from commit order manager
112 /// Check if transaction can be retried
113 /// @return True if transaction can be retried, false otherwise
114 bool can_be_retried() override;
115 /// Internal function that starts the transaction telemetry tracking
116 void start_telemetry();
117 /// Internal function that finishes the transaction telemetry tracking
118 void finish_telemetry();
119 /// Keeps ROWS_QUERY event memory alive until Relay_log_info cleanup clears
120 /// the query pointers that reference it.
122
123 /// @brief Relay context used to apply transaction.
125 /// @brief The session service to secure a THD and RLI objects when
126 /// applying the job.
128 /// @brief Attach flag. Set to true when job is attached to the relay context.
129 bool m_is_attached{false};
130 /// Commit event, saved here for the commit phase in case of retry
132 /// The currently active ROWS_QUERY event for statement-level processlist
133 /// visibility.
135 /// Resource monitoring object for the current channel instance
137 /// Internal flag for skipping transaction used to check if we need
138 /// to wait for unregistering from COM queue in commit phase.
139 bool m_skip{false};
140 /// Internal flag to skip transaction rollback, used by the
141 /// restart
142 bool m_skip_rollback{false};
143 /// Flag indicating that trx registered in the COM queue
144 bool m_co_registered{false};
145};
146
147} // namespace mysql::csa
148
149#endif
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Represents a channel in the Change Stream Applier (CSA).
Definition: channel.h:33
Concrete class representing job applied by the applier.
Definition: job_applier.h:36
void sync_rows_query_event_retention()
Keeps ROWS_QUERY event memory alive until Relay_log_info cleanup clears the query pointers that refer...
Definition: job_applier.cpp:497
bool can_be_retried() override
Check if transaction can be retried.
Definition: job_applier.cpp:472
Resource_instance_monitor_ref Resource_monitor_ref
Definition: job_applier.h:38
bool m_skip_rollback
Internal flag to skip transaction rollback, used by the restart.
Definition: job_applier.h:142
bool commit_register(Thread_id thread_id) override
Transaction "register for commit" phase, noop for control events.
Definition: job_applier.cpp:226
bool prepare(Thread_id thread_id) override
Transaction prepare phase, control events execute fully in prepare phase.
Definition: job_applier.cpp:277
void ensure_session(uint thread_id)
Acquires session if not already obtained.
Definition: job_applier.cpp:71
virtual ~Job_applier() override
Destructor.
Definition: job_applier.cpp:46
void finish_telemetry()
Internal function that finishes the transaction telemetry tracking.
Definition: job_applier.cpp:488
bool commit(Thread_id thread_id) override
Transaction commit phase, noop for control events.
Definition: job_applier.cpp:191
void finish_before_commit()
Called to clean up when a transaction is skipped (skipping commit phase):
Definition: job_applier.cpp:298
void set_failure() override
Sets job failure.
Definition: job_applier.cpp:510
mysql::csa::Relay_context_ptr m_relay_context
Relay context used to apply transaction.
Definition: job_applier.h:124
Job_applier(Channel *channel, unsigned int max_retries, std::shared_ptr< Fetchable_transaction > fetch_object, Stat_monitor_ref stat_monitor, Resource_monitor_ref res_monitor)
Constructor.
Definition: job_applier.cpp:39
Log_event_ptr m_rows_query_event
The currently active ROWS_QUERY event for statement-level processlist visibility.
Definition: job_applier.h:134
bool run_phase(Thread_id thread_id)
Apply events from phase until phase ends or transaction ends.
Definition: job_applier.cpp:308
void prepare_for_apply(Session_service_ptr ss)
Prepares this job to be applied.
Definition: job_applier.cpp:67
bool wait_for_rollback_and_restart()
Internal function to wait for rollback and restart transaction in case deadlock has been found.
Definition: job_applier.cpp:431
bool m_skip
Internal flag for skipping transaction used to check if we need to wait for unregistering from COM qu...
Definition: job_applier.h:139
bool check_rpco_conflict(Thread_id thread_id)
Checks and handles RPCO conflict if designated.
Definition: job_applier.cpp:283
bool m_co_registered
Flag indicating that trx registered in the COM queue.
Definition: job_applier.h:144
bool attach(Thread_id thread_id) override
Attaches to relay context.
Definition: job_applier.cpp:99
Log_event_ptr m_commit_event
Commit event, saved here for the commit phase in case of retry.
Definition: job_applier.h:131
bool apply_event(const Log_event_ptr &ev, THD *thd)
Applies a single event (internal helper)
Definition: job_applier.cpp:258
bool detach(Thread_id thread_id) override
Detaches from relay context.
Definition: job_applier.cpp:169
bool m_is_attached
Attach flag. Set to true when job is attached to the relay context.
Definition: job_applier.h:129
void start_telemetry()
Internal function that starts the transaction telemetry tracking.
Definition: job_applier.cpp:479
std::string to_string() override
Presents job identifier.
Definition: job_applier.cpp:466
bool is_attached() const override
Checks whether this job is attached to relay log context.
Definition: job_applier.cpp:464
Managed_event::Log_event_ptr Log_event_ptr
Type alias for log event pointer.
Definition: job_applier.h:80
Resource_monitor_ref m_resource_monitor
Resource monitoring object for the current channel instance.
Definition: job_applier.h:136
Session_service_ptr m_session_service
The session service to secure a THD and RLI objects when applying the job.
Definition: job_applier.h:127
bool restart() override
Restarts internal state.
Definition: job_applier.cpp:450
The Job_binlog class is a container that holds a buffer of fetchable events and context for the trans...
Definition: job_binlog.h:56
scheduler::Statistics_instance_monitor_ref Stat_monitor_ref
Alias to Statistics_instance_monitor_ref contained in the scheduler library.
Definition: job_binlog.h:60
unsigned int Thread_id
Type alias for thread identifier.
Definition: job.h:54
std::shared_ptr< Log_event > Log_event_ptr
Definition: managed_event.h:37
static my_thread_id thread_id
Definition: my_thr_init.cc:60
Definition: channel.cpp:28
std::shared_ptr< Session_service > Session_service_ptr
Shared pointer to Session_service.
Definition: session_service.h:40
std::reference_wrapper< Resource_instance_monitor > Resource_instance_monitor_ref
Definition: resource_monitor.h:43
std::shared_ptr< Relay_context > Relay_context_ptr
Definition: relay_context.h:39
Definition: task.h:427