MySQL 26.7.0
Source Code Documentation
transaction_conflict_manager.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_TRANSACTION_CONFLICT_MANAGER_H
25#define MYSQL_CSA_TRANSACTION_CONFLICT_MANAGER_H
26
27#include <array>
28#include <fstream>
29#include <functional>
30#include <memory>
31
39
40namespace mysql::csa {
41
42class Transaction_conflict_manager;
44 std::unique_ptr<Transaction_conflict_manager>;
45
46/// Rescue thread for solving conflicts between transactions
47/// Main methods are:
48/// - start : Runs asynchronous thread
49/// - stop : Stops execution and blocks until thread is joined
50/// - rollback: Schedules transaction to rollback
51/// - is_stopped : Checks whether stop has been requested
53 public:
55 /// Starts asynchronous thread that decodes jobs from the stream
56 void start();
57 /// Stops asynchronous thread, notifies it and and waits for notification that
58 /// thread has been stopped and may be joined. Next, joins the thread.
59 void stop();
60 /// Checks if stop has been requested
61 /// @return True if stop has been requested internally (error) or externally
62 /// (log wait for update)
63 bool is_stopped() const;
64 /// @brief Check if provider has an error
65 /// @return True in case an error occurred, false otherwise
66 bool is_error() const;
67 /// @brief Preempt this transaction
68 void enqueue(Rescue_task &&task);
69
70 private:
71 /// Function executed by m_thread
72 void run_thread();
73
74 /// Thread type
77
78 /// Queue into which decoder thread puts data jobs
80 /// Decoder thread object
82 /// Notification atomic for end of execution
83 std::atomic<bool> m_end{false};
84 /// Variable to gracefully stop the thread
85 std::atomic<bool> m_is_stopped{false};
86 /// The number of scheduled tasks
87 std::atomic<std::size_t> m_scheduled_tasks{0};
88};
89
90/// @brief Singleton Transaction_conflict_manager for all of the registered
91/// "instances". We typically track statistics separately for user-defined
92/// channels
94 public:
95 /// Obtain Transaction_conflict_manager for unique instance id
96 /// @param instance_id Unique instance id for the replication channel
97 static Transaction_conflict_manager_sptr &get(std::size_t instance_id);
98
99 protected:
100 /// @brief This initialization function is called by "get" if needed
101 static void init();
103 static inline constexpr int max_instances =
106 std::array<Transaction_conflict_manager_sptr, max_instances>;
108 static std::atomic<bool> m_init;
109 static std::atomic<bool> m_ready;
110};
111
112} // namespace mysql::csa
113
114#endif // MYSQL_CSA_TRANSACTION_CONFLICT_MANAGER_H
Wrapper to mysql thread, which matches interface of std::thread.
Definition: thread_srv.h:46
Rescue task to rollback conflicting transaction.
Definition: rescue_task.h:39
Rescue thread for solving conflicts between transactions Main methods are:
Definition: transaction_conflict_manager.h:52
Queue_type m_cache
Queue into which decoder thread puts data jobs.
Definition: transaction_conflict_manager.h:79
std::atomic< bool > m_is_stopped
Variable to gracefully stop the thread.
Definition: transaction_conflict_manager.h:85
bool is_stopped() const
Checks if stop has been requested.
Definition: transaction_conflict_manager.cpp:43
void run_thread()
Function executed by m_thread.
Definition: transaction_conflict_manager.cpp:45
void start()
Starts asynchronous thread that decodes jobs from the stream.
Definition: transaction_conflict_manager.cpp:30
void enqueue(Rescue_task &&task)
Preempt this transaction.
Definition: transaction_conflict_manager.cpp:60
std::atomic< bool > m_end
Notification atomic for end of execution.
Definition: transaction_conflict_manager.h:83
bool is_error() const
Check if provider has an error.
Thread_type m_thread
Decoder thread object.
Definition: transaction_conflict_manager.h:81
void stop()
Stops asynchronous thread, notifies it and and waits for notification that thread has been stopped an...
Definition: transaction_conflict_manager.cpp:36
std::atomic< std::size_t > m_scheduled_tasks
The number of scheduled tasks.
Definition: transaction_conflict_manager.h:87
Singleton Transaction_conflict_manager for all of the registered "instances".
Definition: transaction_conflict_manager.h:93
static Instances_map m_instances
Definition: transaction_conflict_manager.h:107
static constexpr int max_instances
Definition: transaction_conflict_manager.h:103
static std::atomic< bool > m_ready
Definition: transaction_conflict_manager.h:109
static Transaction_conflict_manager_sptr & get(std::size_t instance_id)
Obtain Transaction_conflict_manager for unique instance id.
Definition: transaction_conflict_manager.cpp:76
static std::atomic< bool > m_init
Definition: transaction_conflict_manager.h:108
std::array< Transaction_conflict_manager_sptr, max_instances > Instances_map
Definition: transaction_conflict_manager.h:106
static void init()
This initialization function is called by "get" if needed.
Definition: transaction_conflict_manager.cpp:89
std::thread Thread
Definition: thread_stl.h:42
Definition: channel.cpp:28
std::unique_ptr< Transaction_conflict_manager > Transaction_conflict_manager_sptr
Definition: transaction_conflict_manager.h:44
Experimental API header.
static constexpr unsigned int max_instances
The maximum number of scheduler instances supported This value should be aligned with the 'MAX_CHANNE...
Definition: constants.h:34