MySQL 26.7.0
Source Code Documentation
csa_worker_context.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_CS_APPLY_CSA_WORKER_CONTEXT_H
25#define MYSQL_CS_APPLY_CSA_WORKER_CONTEXT_H
26
27#include <atomic>
28#include <cstdint>
29#include <future>
30#include <memory>
34
35class Relay_log_info;
36class THD;
37class MDL_context;
38
39namespace cs::apply {
40
41/// @brief Class representing the interface for parallel worker context. It
42/// accesses basic information like worker id, transaction id, channel id and
43/// other functionatlities needed by the commit order manager.
44/// @note This context is needed to disconnect Slave_worker class from
45/// execution path, Slave_worker implements Parallel_worker_context interface
47 public:
48 Csa_worker_context(Trx_id trx_seq_num, Worker_id worker_id,
49 const std::string &channel_id, THD *trx_ctx,
50 int current_retry, int retries_num);
51
54
55 /// @brief Indicates that commit order deadlock has been found
56 /// @param for_self When true, a thread reports for its own
57 void report_commit_order_deadlock(bool for_self = false) override;
58 /// @brief Checks if commit order deadlock has been found
59 /// @return True if commit order deadlock has been found, false otherwise
60 bool found_commit_order_deadlock() const override;
61 /// @brief Resets commit order deadlock
62 void reset_commit_order_deadlock() override;
63 /// @brief Checks if arg parallel worker executes transaction coming from
64 /// the same channel
65 /// @param arg Context to compare against
66 bool is_same_channel(const Parallel_worker_context *arg) const override;
67 /// @brief Returns transaction id (we use sequence number)
68 /// @return transaction id
69 THD *get_transaction_ctx() override;
70 /// @brief Returns worker id
71 /// @return Worker identifier
72 Worker_id get_worker_id() const override;
73 /// @brief Obtains worker metrics
74 /// @return Reference to worker metrics object
76 /// @brief MDL context accessor
77 /// @return MDL context obj pointer
78 MDL_context *get_mdl_context() override;
79 /// @brief Channel identifier accessor
80 /// @return channel identifier
81 const std::string &get_channel_id() const;
82 /// @brief Obtain transaction id (sequence number)
83 /// @return Transaction sequence number
84 Trx_id get_trx_id() override;
85 /// @brief Get "for channel" id. Builds string in flight when needed
86 /// @param upper_case Pass true if upper case is needed
87 /// @return "for channel" string
88 const char *get_for_channel_id(bool upper_case) const override;
89 /// Updates internal data for a new transaction
90 /// @param trx_seq_num Transaction sequence number
91 /// @param worker_id Worker identifier
92 /// @param trx_ctx Transaction THD
93 /// @param current_retry Current retry number
94 void update(Trx_id trx_seq_num, Worker_id worker_id, THD *trx_ctx,
95 int current_retry);
96 /// Updates internal data for ongoing transaction
97 /// @param worker_id Worker identifier
98 /// @param current_retry Current retry number
99 void update(Worker_id worker_id, int current_retry);
100 /// Updates all internal data for new transaction and channel
101 /// @param trx_seq_num Transaction sequence number
102 /// @param worker_id Worker identifier
103 /// @param channel_id Channel identifier
104 /// @param trx_ctx Transaction THD
105 /// @param current_retry Current retry number
106 /// @param retries_num Number of possible retries for this transaction
107 void update(Trx_id trx_seq_num, Worker_id worker_id,
108 const std::string &channel_id, THD *trx_ctx, int current_retry,
109 int retries_num);
110 /// @brief Returns information on whether THD transaction can be retried
111 /// @return true if transaction can be retried
112 bool can_be_retried(THD *thd) override;
113 /// @brief Checks whether this transaction error is temporary
114 /// @param thd THD handle
115 /// @param error Additional error information
116 bool has_temporary_error(THD *thd, int error);
117 /// Mark transaction as prepared and externally rollbackable.
118 void set_applied();
119 /// Move transaction from prepared into committing state.
120 void set_committing();
121 /// Check if external rollback is on-going
122 /// @return True when external rollback was already requested.
123 bool is_rollback_requested() const { return m_rollback.load(); }
124 /// Handle commit order deadlock - one of the callers will request
125 /// transaction rollback due to rpco deadlock
127 /// Waits for external rollback to finish
128 bool wait_for_rollback();
129 /// Worker metrics
131 /// Checks whether this worker is CSA worker
132 /// @return true for CSA parallel worker context. False otherwise
133 bool is_csa() const override;
134
135 private:
136 /// RPCO lifecycle visible to deadlock reporting:
137 /// - preparing: transaction is still in apply/prepare work
138 /// - prepared: transaction left apply and may be rescued externally
139 /// - commit: transaction entered commit ownership and must resolve there
140 enum class Rpco_state : std::uint8_t { preparing, prepared, commit };
141
142 /// Sticky indicator that this transaction was reported as an RPCO victim.
143 std::atomic<bool> m_is_commit_order_deadlock{false};
144 /// Set once an external rollback request has been enqueued.
145 std::atomic<bool> m_rollback{false};
146 /// Current RPCO lifecycle state used to decide how to handle reports.
147 std::atomic<Rpco_state> m_rpco_state{Rpco_state::preparing};
148 /// Completes when the external rollback worker finishes rescue handling.
149 std::promise<bool> m_rollback_promise;
150 /// Transaction identifier reused by commit-order logic.
152 /// Current worker slot owning this transaction context.
154 /// Replication channel this transaction belongs to.
155 std::string m_channel_id{""};
156 /// THD currently bound to this transaction context.
157 THD *m_trx_ctx{nullptr};
158 /// Retry number of the current attempt.
160 /// Maximum number of retries allowed for this transaction.
162 /// Cached "for channel" suffix, built lazily for diagnostics.
163 mutable std::string m_for_channel_id{""};
164};
165
166} // namespace cs::apply
167
168#endif // MYSQL_CS_APPLY_CSA_WORKER_CONTEXT_H
Context of the owner of metadata locks.
Definition: mdl.h:1415
Definition: rpl_rli.h:208
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Class representing the interface for parallel worker context.
Definition: csa_worker_context.h:46
bool can_be_retried(THD *thd) override
Returns information on whether THD transaction can be retried.
Definition: csa_worker_context.cc:146
Rpco_state
RPCO lifecycle visible to deadlock reporting:
Definition: csa_worker_context.h:140
MDL_context * get_mdl_context() override
MDL context accessor.
Definition: csa_worker_context.cc:119
bool wait_for_rollback()
Waits for external rollback to finish.
Definition: csa_worker_context.cc:64
Trx_id get_trx_id() override
Obtain transaction id (sequence number)
Definition: csa_worker_context.cc:169
Trx_id m_trx_id
Transaction identifier reused by commit-order logic.
Definition: csa_worker_context.h:151
std::string m_for_channel_id
Cached "for channel" suffix, built lazily for diagnostics.
Definition: csa_worker_context.h:163
Worker_id m_worker_id
Current worker slot owning this transaction context.
Definition: csa_worker_context.h:153
std::atomic< bool > m_rollback
Set once an external rollback request has been enqueued.
Definition: csa_worker_context.h:145
bool is_same_channel(const Parallel_worker_context *arg) const override
Checks if arg parallel worker executes transaction coming from the same channel.
Definition: csa_worker_context.cc:98
int m_current_retry
Retry number of the current attempt.
Definition: csa_worker_context.h:159
void reset_commit_order_deadlock() override
Resets commit order deadlock.
Definition: csa_worker_context.cc:91
Csa_worker_context(Trx_id trx_seq_num, Worker_id worker_id, const std::string &channel_id, THD *trx_ctx, int current_retry, int retries_num)
Definition: csa_worker_context.cc:33
THD * get_transaction_ctx() override
Returns transaction id (we use sequence number)
Definition: csa_worker_context.cc:113
const std::string & get_channel_id() const
Channel identifier accessor.
Definition: csa_worker_context.cc:109
void set_applied()
Mark transaction as prepared and externally rollbackable.
Definition: csa_worker_context.cc:82
Parallel_worker_context::Trx_id Trx_id
Definition: csa_worker_context.h:52
Parallel_worker_context::Worker_id Worker_id
Definition: csa_worker_context.h:53
std::atomic< bool > m_is_commit_order_deadlock
Sticky indicator that this transaction was reported as an RPCO victim.
Definition: csa_worker_context.h:143
bool has_temporary_error(THD *thd, int error)
Checks whether this transaction error is temporary.
Definition: csa_worker_context.cc:124
void update(Trx_id trx_seq_num, Worker_id worker_id, THD *trx_ctx, int current_retry)
Updates internal data for a new transaction.
Definition: csa_worker_context.cc:195
void report_commit_order_deadlock(bool for_self=false) override
Indicates that commit order deadlock has been found.
Definition: csa_worker_context.cc:44
Worker_id get_worker_id() const override
Returns worker id.
Definition: csa_worker_context.cc:115
std::promise< bool > m_rollback_promise
Completes when the external rollback worker finishes rescue handling.
Definition: csa_worker_context.h:149
std::atomic< Rpco_state > m_rpco_state
Current RPCO lifecycle state used to decide how to handle reports.
Definition: csa_worker_context.h:147
bool found_commit_order_deadlock() const override
Checks if commit order deadlock has been found.
Definition: csa_worker_context.cc:60
int m_retries_num
Maximum number of retries allowed for this transaction.
Definition: csa_worker_context.h:161
const char * get_for_channel_id(bool upper_case) const override
Get "for channel" id.
Definition: csa_worker_context.cc:179
bool is_rollback_requested() const
Check if external rollback is on-going.
Definition: csa_worker_context.h:123
std::string m_channel_id
Replication channel this transaction belongs to.
Definition: csa_worker_context.h:155
void set_committing()
Move transaction from prepared into committing state.
Definition: csa_worker_context.cc:86
bool is_csa() const override
Checks whether this worker is CSA worker.
Definition: csa_worker_context.cc:215
static instruments::Dummy_worker_metrics m_disabled_worker_metrics
Worker metrics.
Definition: csa_worker_context.h:130
void handle_commit_order_deadlock()
Handle commit order deadlock - one of the callers will request transaction rollback due to rpco deadl...
Definition: csa_worker_context.cc:70
instruments::Worker_metrics & get_worker_metrics() override
Obtains worker metrics.
Definition: csa_worker_context.cc:171
THD * m_trx_ctx
THD currently bound to this transaction context.
Definition: csa_worker_context.h:157
Class representing the interface for parallel worker context.
Definition: parallel_worker_context.h:47
int64_t Trx_id
Definition: parallel_worker_context.h:51
uint64_t Worker_id
Definition: parallel_worker_context.h:52
Class that intends to be a dummy end point for worker metrics.
Definition: dummy_worker_metrics.h:34
Abstract class for classes that contain metrics related to transaction execution in applier workers.
Definition: worker_metrics.h:34
Definition: applier_version.h:27