MySQL 26.7.0
Source Code Documentation
job.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_H
25#define MYSQL_CSA_JOB_H
26
27#include <my_systime.h>
28#include <atomic>
29#include <deque>
30#include <functional>
31#include <iostream>
34
35namespace mysql::csa {
36
37/// Forward declaration of the Job class.
38class Job;
39
40/// Type alias for a pointer to Job.
41using Job_ptr = Job *;
42
43/// A job represents a single unit of work applied by worker pool threads.
44/// Job defines execution path, the number of times the job runs, the number
45/// of retries, how job attaches to applier context and detaches from it.
46/// This is an abstract class for a job.
47class Job {
48 public:
49 /// Deleted copy constructor.
50 Job(const Job &) = delete;
51 /// Deleted assignment operator.
52 Job &operator=(const Job &) = delete;
53 /// Type alias for thread identifier.
54 using Thread_id = unsigned int;
55
56 /// @brief Constructor.
57 /// @param max_retries The number of times a job can be retried.
58 Job(unsigned int max_retries);
59 /// @brief Destructor.
60 virtual ~Job();
61 /// @brief Obtains unique instance id to gather statistics separately for
62 /// different "instances"
63 /// @return Instance unique identifier
64 virtual unsigned int get_instance_id() const;
65 /// @brief Checks whether handled job is a transaction (supports two phases)
66 /// @return True if handled job is a transaction
67 virtual bool is_trx() const;
68 /// @brief Resets the job for retrying.
69 /// This must be called before retrying, so that the internal
70 /// pointers are reset to the proper place.
71 /// @return True on error. False on success.
72 virtual bool restart();
73 /// @brief Implements execution of the full job or a single job phase.
74 /// @param thread_id Thread pool worker identifier
75 /// @return False on success. True on error.
76 virtual bool run(Thread_id thread_id) = 0;
77 /// @brief Attaches the job.
78 /// @param thread_id Thread pool worker identifier
79 /// @return True on error. False on success.
80 virtual bool attach(Thread_id thread_id) = 0;
81 /// @brief Returns an id based on which the job attaches to applier context
82 /// @return Attach id
84 /// @brief Detaches the job.
85 /// @param thread_id Thread pool worker identifier
86 /// @return True on error. False on success.
87 virtual bool detach(Thread_id thread_id) = 0;
88 /// @brief Gets the job identifier.
89 /// @return Job identifier - job sequence number.
90 virtual std::size_t get_id() const;
91 /// @brief Increments the number of retries for the job.
92 virtual void inc_retries();
93 /// @brief Checks whether this job can be retried.
94 /// @return True if the job can be retried. False otherwise.
95 virtual bool can_be_retried();
96 /// @brief Gets the number of retries for this job.
97 /// @return The number of times this job was retried.
98 virtual unsigned int get_retries() const;
99 /// @brief Checks whether this job is in error state.
100 /// @return True if the job errored out. False otherwise.
101 virtual bool is_error();
102 /// @brief Checks whether applier stop has been requested for this job.
103 /// @return True if stop was requested. False otherwise.
104 virtual bool is_stopped() const;
105 /// @brief Checks whether job has a fatal, non-recoverable error.
106 /// @return True when fatal error detected. False otherwise.
107 virtual bool is_fatal_error();
108 /// @brief Injects the applier stop flag for this job.
109 /// @param applier_stop Stop flag reference.
110 virtual void set_applier_stop(std::atomic<bool> &applier_stop);
111 /// @brief Global job success callback.
112 virtual void set_success();
113 /// @brief Global job failure callback.
114 virtual void set_failure();
115 /// @brief Sets fatal job error.
116 virtual void set_fatal_error();
117 /// @brief Sets job error.
118 virtual void set_error();
119 /// @brief Sets this job as done.
120 virtual void set_done();
121 /// @brief Checks whether the job is done.
122 /// @return True if the job is done, false otherwise.
123 virtual bool is_done();
124 /// @brief Checks whether the job is currently attached.
125 /// @return True if attached, false if detached.
126 virtual bool is_attached() const = 0;
127 /// @brief Converts the job to a string representation.
128 /// @return String representation of the job.
129 virtual std::string to_string();
130 /// Skip this job, considered complete and done without failure
131 virtual void skip() {}
132
133 protected:
134 /// Error flag for job
135 bool m_is_error{false};
136 /// Specifies if this job has a fatal error - we cannot retry it
137 bool m_is_fatal_error{false};
138 /// When set to true, job is done (successfully or not)
139 bool m_is_done{false};
140 /// Maximum number of retries for this job
141 unsigned int m_max_retries;
142 /// Next job id. Used to generate ids for jobs
143 static std::atomic<std::size_t> next_id;
144 /// This job sequence id
145 std::size_t m_id{0};
146 /// Worker pool thread id
148 /// The number of times this job was retried
149 std::size_t m_trx_retries{0};
150 /// Channel stop flag used to interrupt wait loops.
151 std::reference_wrapper<std::atomic<bool>> m_applier_stop;
152};
153
154} // namespace mysql::csa
155
156#endif
A job represents a single unit of work applied by worker pool threads.
Definition: job.h:47
virtual bool is_fatal_error()
Checks whether job has a fatal, non-recoverable error.
Definition: job.cpp:80
virtual std::size_t get_id() const
Gets the job identifier.
Definition: job.cpp:64
bool m_is_error
Error flag for job.
Definition: job.h:135
virtual bool restart()
Resets the job for retrying.
Definition: job.cpp:55
Job & operator=(const Job &)=delete
Deleted assignment operator.
std::size_t m_id
This job sequence id.
Definition: job.h:145
virtual bool is_error()
Checks whether this job is in error state.
Definition: job.cpp:74
virtual void set_failure()
Global job failure callback.
Definition: job.cpp:88
virtual void inc_retries()
Increments the number of retries for the job.
Definition: job.cpp:66
bool m_is_fatal_error
Specifies if this job has a fatal error - we cannot retry it.
Definition: job.h:137
virtual void set_applier_stop(std::atomic< bool > &applier_stop)
Injects the applier stop flag for this job.
Definition: job.cpp:82
Thread_id m_thread_id
Worker pool thread id.
Definition: job.h:147
virtual bool detach(Thread_id thread_id)=0
Detaches the job.
virtual bool attach(Thread_id thread_id)=0
Attaches the job.
virtual unsigned int get_retries() const
Gets the number of retries for this job.
Definition: job.cpp:72
std::reference_wrapper< std::atomic< bool > > m_applier_stop
Channel stop flag used to interrupt wait loops.
Definition: job.h:151
Thread_id get_attach_id() const
Returns an id based on which the job attaches to applier context.
Definition: job.cpp:62
virtual bool is_attached() const =0
Checks whether the job is currently attached.
virtual void set_success()
Global job success callback.
Definition: job.cpp:86
unsigned int Thread_id
Type alias for thread identifier.
Definition: job.h:54
virtual bool is_done()
Checks whether the job is done.
Definition: job.cpp:103
virtual bool run(Thread_id thread_id)=0
Implements execution of the full job or a single job phase.
Job(const Job &)=delete
Deleted copy constructor.
virtual bool can_be_retried()
Checks whether this job can be retried.
Definition: job.cpp:68
std::size_t m_trx_retries
The number of times this job was retried.
Definition: job.h:149
virtual bool is_stopped() const
Checks whether applier stop has been requested for this job.
Definition: job.cpp:76
virtual unsigned int get_instance_id() const
Obtains unique instance id to gather statistics separately for different "instances".
Definition: job.cpp:51
virtual void skip()
Skip this job, considered complete and done without failure.
Definition: job.h:131
virtual void set_fatal_error()
Sets fatal job error.
Definition: job.cpp:90
bool m_is_done
When set to true, job is done (successfully or not)
Definition: job.h:139
virtual void set_error()
Sets job error.
Definition: job.cpp:96
virtual void set_done()
Sets this job as done.
Definition: job.cpp:101
static std::atomic< std::size_t > next_id
Next job id. Used to generate ids for jobs.
Definition: job.h:143
virtual ~Job()
Destructor.
Definition: job.cpp:49
virtual bool is_trx() const
Checks whether handled job is a transaction (supports two phases)
Definition: job.cpp:53
virtual std::string to_string()
Converts the job to a string representation.
Definition: job.cpp:105
unsigned int m_max_retries
Maximum number of retries for this job.
Definition: job.h:141
Defines for getting and processing the current system type programmatically.
static my_thread_id thread_id
Definition: my_thr_init.cc:60
Definition: channel.cpp:28