MySQL 26.7.0
Source Code Documentation
job_binlog.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_BINLOG_H
25#define MYSQL_CSA_JOB_BINLOG_H
26
27#include <mysql/psi/mysql_mutex.h> // mysql_mutex_t
28#include <map>
29#include <string>
30#include <tuple>
31#include <vector>
32
39
40namespace mysql::csa {
41
42/// Lists all phases the Job_binlog can be in
44 prepare = 0, /// Transaction prepare
45 commit_register = 1, ///< Register for commit phase
46 commit_binlog = 2, ///< Committing phase
47 retry_commit = 3, ///< Retrying commit
48 done = 4 ///< Done, successfully or with failure
49};
50
51/// @brief The Job_binlog class is a container that holds a buffer of fetchable
52/// events and context for the transaction to be applied in change streams.
53///
54/// The context includes the originating channel and parallelization window
55/// information.
56class Job_binlog : public Job {
57 public:
58 /// @brief Alias to Statistics_instance_monitor_ref contained in the scheduler
59 /// library
61 /// @brief Deleted copy constructor.
62 Job_binlog(const Job_binlog &) = delete;
63
64 /// @brief Deleted assignment operator.
65 Job_binlog &operator=(const Job_binlog &) = delete;
66
67 /// @brief Constructor for Job_binlog.
68 /// @param channel The channel this job is coming from.
69 /// @param max_retries The maximum number of retries for this job.
70 /// @param fetch_object The object to fetch job data from.
71 /// @param stat_monitor Object for monitoring statistics
72 Job_binlog(Channel *channel, unsigned int max_retries,
73 std::shared_ptr<Fetchable_transaction> fetch_object,
74 Stat_monitor_ref stat_monitor);
75 /// @brief Destructor for Job_binlog.
76 virtual ~Job_binlog() override;
77 /// @brief Gets the channel this job comes from.
78 /// @return Pointer to the channel this job comes from.
79 Channel *get_channel() const;
80 /// @brief Gets the transaction identifier as a string.
81 /// @return The transaction identifier as a string.
82 const std::string &get_channel_id() const;
83 /// Get transaction GTID
84 /// @return Transaction GTID
85 std::string get_trx_id() const;
86 /// @brief Gets the transaction GTID.
87 /// @return The transaction GTID.
88 const mysql::gtid::Gtid &get_trx_gtid() const { return m_trx_gtid; }
89 /// @brief Gets the last committed value to determine when this transaction
90 /// can run.
91 /// @return The transaction's last committed value.
92 unsigned long long get_last_committed() const;
93 /// @brief Gets the sequence number of this transaction to determine when it
94 /// can run and commit.
95 /// @return The transaction's sequence number.
96 unsigned long long get_sequence_number() const;
97 /// @brief Gets the transaction length.
98 /// @return The transaction length.
99 unsigned long long get_trx_length() const;
100 /// Run prepare phase of this transaction
101 /// @param thread_id Thread pool worker identifier
102 /// @return False on success. True on failure
103 virtual bool prepare(Thread_id thread_id) = 0;
104 /// Run commit phase of this transaction
105 /// @param thread_id Thread pool worker identifier
106 /// @return False on success. True on failure
107 virtual bool commit(Thread_id thread_id) = 0;
108 /// Register the transaction for commit phase
109 /// @param thread_id Thread pool worker identifier
110 /// @return False on success. True on failure
112 /// Mark the transaction as done (done with failure or done successfully
113 void set_done() override;
114 /// @brief Obtains unique instance id to gather statistics separately for
115 /// different "instances".
116 /// @details Since we want to gather statistics separately for different.
117 /// channels, this is set to channel id. The instance ID is used as a key to
118 /// aggregate statistics for each channel.
119 /// @return int Unique identifier for the instance (channel ID).
120 unsigned int get_instance_id() const override;
121 /// @brief Checks if this transaction has finished fetching.
122 /// @return True if complete, false otherwise.
123 virtual bool is_complete();
124 /// @brief Restarts the job and prepares for retry.
125 /// @return False on success, true on failure.
126 bool restart() override;
127 /// @brief Binlog job needs to be called twice to apply transaction:
128 /// - 1st run - prepare
129 /// - 2nd run - commit
130 /// Applier job may run up to slave_trans_retries times. If we retry
131 /// "prepare" phase, nothing changes. If we retry "commit", we run
132 /// both phases, prepare + commit.
133 /// @param thread_id Thread pool worker identifier
134 bool run(Thread_id thread_id) override;
135 /// @brief Checks whether handled job is a transaction (supports two phases)
136 /// @return True if handled job is a transaction
137 bool is_trx() const override;
138 /// @brief success callback
139 void set_success() override;
140 /// Skip this job, considered complete and done without failure
141 void skip() override;
142
143 protected:
144 /// Restarts job with fetch metadata
145 /// @param all When true, this is a retry and all object states must be
146 /// restarted
147 bool restart_internal(bool all);
148 /// @brief The GTID of the transaction. "<unknown>" if not available.
150 /// @brief The channel this transaction is coming from.
152 /// @brief The first event fetched to obtain GTID information.
154 /// @brief The cursor for the events to process.
155 uint32_t m_next_event{0};
156 /// Statistics monitoring object for the current instance
158 /// Information on how to fetch transaction data
159 std::shared_ptr<Fetchable_transaction> m_fetch_metadata;
160 /// Transaction phase indicator:
161 /// - prepare - this state means that transaction needs to be prepared
162 /// - commit - this state means that transaction needs to be committed
163 /// - done - transaction is committed
164 /// - retry_commit - transaction prepare phase succeeded, but commit did not
165 /// and we need to retry full transaction in one run.
167};
168
169} // namespace mysql::csa
170
171#endif
Represents a channel in the Change Stream Applier (CSA).
Definition: channel.h:33
The Job_binlog class is a container that holds a buffer of fetchable events and context for the trans...
Definition: job_binlog.h:56
Channel * m_channel
The channel this transaction is coming from.
Definition: job_binlog.h:151
virtual bool commit_register(Thread_id thread_id)=0
Register the transaction for commit phase.
const std::string & get_channel_id() const
Gets the transaction identifier as a string.
Definition: job_binlog.cpp:52
virtual bool prepare(Thread_id thread_id)=0
Run prepare phase of this transaction.
uint32_t m_next_event
The cursor for the events to process.
Definition: job_binlog.h:155
virtual ~Job_binlog() override
Destructor for Job_binlog.
Definition: job_binlog.cpp:46
const mysql::gtid::Gtid & get_trx_gtid() const
Gets the transaction GTID.
Definition: job_binlog.h:88
unsigned int get_instance_id() const override
Obtains unique instance id to gather statistics separately for different "instances".
Definition: job_binlog.cpp:66
virtual bool is_complete()
Checks if this transaction has finished fetching.
Definition: job_binlog.cpp:90
bool is_trx() const override
Checks whether handled job is a transaction (supports two phases)
Definition: job_binlog.cpp:138
mysql::gtid::Gtid m_trx_gtid
The GTID of the transaction. "<unknown>" if not available.
Definition: job_binlog.h:149
Channel * get_channel() const
Gets the channel this job comes from.
Definition: job_binlog.cpp:48
mysql::csa::Managed_event m_first_event
The first event fetched to obtain GTID information.
Definition: job_binlog.h:153
bool restart() override
Restarts the job and prepares for retry.
Definition: job_binlog.cpp:140
Stat_monitor_ref m_stat_monitor
Statistics monitoring object for the current instance.
Definition: job_binlog.h:157
Transaction_phase m_phase
Transaction phase indicator:
Definition: job_binlog.h:166
unsigned long long get_last_committed() const
Gets the last committed value to determine when this transaction can run.
Definition: job_binlog.cpp:56
Job_binlog(const Job_binlog &)=delete
Deleted copy constructor.
bool restart_internal(bool all)
Restarts job with fetch metadata.
Definition: job_binlog.cpp:142
std::shared_ptr< Fetchable_transaction > m_fetch_metadata
Information on how to fetch transaction data.
Definition: job_binlog.h:159
std::string get_trx_id() const
Get transaction GTID.
Definition: job_binlog.cpp:50
unsigned long long get_trx_length() const
Gets the transaction length.
Definition: job_binlog.cpp:170
bool run(Thread_id thread_id) override
Binlog job needs to be called twice to apply transaction:
Definition: job_binlog.cpp:104
void skip() override
Skip this job, considered complete and done without failure.
Definition: job_binlog.cpp:98
void set_done() override
Mark the transaction as done (done with failure or done successfully.
Definition: job_binlog.cpp:92
void set_success() override
success callback
Definition: job_binlog.cpp:70
virtual bool commit(Thread_id thread_id)=0
Run commit phase of this transaction.
Job_binlog & operator=(const Job_binlog &)=delete
Deleted assignment operator.
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 long long get_sequence_number() const
Gets the sequence number of this transaction to determine when it can run and commit.
Definition: job_binlog.cpp:80
A job represents a single unit of work applied by worker pool threads.
Definition: job.h:47
unsigned int Thread_id
Type alias for thread identifier.
Definition: job.h:54
Managed event.
Definition: managed_event.h:35
Represents a MySQL Global Transaction Identifier.
Definition: gtid.h:47
static my_thread_id thread_id
Definition: my_thr_init.cc:60
Definition: channel.cpp:28
Transaction_phase
Lists all phases the Job_binlog can be in.
Definition: job_binlog.h:43
@ commit_register
Transaction prepare.
@ done
Done, successfully or with failure.
@ commit_binlog
Committing phase.
@ retry_commit
Retrying commit.
std::reference_wrapper< Statistics_instance_monitor > Statistics_instance_monitor_ref
Definition: statistics_instance_monitor.h:43
Instrumentation helpers for mutexes.
Definition: task.h:427