MySQL 26.7.0
Source Code Documentation
scheduled_task.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_SCHEDULER_SCHEDULED_TASK_H
25#define MYSQL_SCHEDULER_SCHEDULED_TASK_H
26
27#include <chrono>
28#include <iostream>
29#include <memory>
30#include <string>
31
37
38namespace mysql::scheduler {
39
40/// @class Repeatable_task_state
41/// @brief Shared state for one logical repeatable scheduler task.
42///
43/// This object is shared by all scheduled representations of the same logical
44/// task, including the currently queued phase, the task handed off to a worker,
45/// and any follow-up phase enqueued by the scheduler.
46///
47/// It combines:
48/// - the repeatable task callable, executed by each task phase
49/// - the logical task lifetime accounting for @c m_scheduled_tasks_cnt
50///
51/// The scheduler increments @c m_scheduled_tasks_cnt once when the logical task
52/// is created. The counter is decremented in this object's destructor, which
53/// means the decrement happens only after the last queued or running owner of
54/// the logical task releases its shared reference.
56 public:
57 using Repeatable_task_type = std::function<bool(unsigned int)>;
58
59 /// @brief Creates shared state for a logical repeatable task.
60 /// @param scheduled_tasks_cnt Reference to the scheduler task counter.
61 /// @param repeatable_task Repeatable task callable shared across phases.
62 Repeatable_task_state(std::atomic<std::size_t> &scheduled_tasks_cnt,
63 Repeatable_task_type &&repeatable_task);
64
65 /// @brief Decrements the logical scheduled task count.
67
70
71 /// @brief Executes the repeatable task body for one phase dispatch.
72 /// @param thread_id Worker or scheduler thread identifier.
73 /// @return True if task execution reported an error, false otherwise.
74 bool execute(unsigned int thread_id) { return m_repeatable_task(thread_id); }
75
76 private:
77 /// Reference to the scheduler's logical task counter.
78 std::atomic<std::size_t> &m_scheduled_tasks_cnt;
79 /// Repeatable task body shared by all scheduled task instances.
81};
82
83using Repeatable_task_state_ptr = std::shared_ptr<Repeatable_task_state>;
84
85/// @class Scheduled_task
86/// @brief Represents task that is scheduled in the priority queue
88 using Func_type = std::function<Task_result(unsigned int)>;
89
90 public:
91 /// @brief Constructor
92 /// @param id Task ID
93 /// @param task Function to execute
94 /// @param repeatable_task Shared repeatable task state
95 /// @param schedule Right reference to task schedule pointer
96 /// @param phase_id Current phase sequence number
97 Scheduled_task(const Task_id &id, Func_type &&task,
98 const Repeatable_task_state_ptr &repeatable_task,
99 const Task_schedule_ptr &schedule, unsigned int phase_id);
100
101 /// @brief Returns task with a lower priority - operator needed for the
102 /// priority queue
103 bool operator<(const Scheduled_task &arg) const;
104
105 /// @brief Gets the task delay time point.
106 /// @return The time point for the task delay.
108 return m_schedule->get_phase_delay(m_phase);
109 }
110
111 /// @brief Gets the task ID.
112 /// @return The task ID.
113 Task_id get_id() const { return m_task_id; }
114
115 /// @brief Checks if the current phase is ready.
116 /// @return true if phase is ready, false otherwise.
117 bool is_phase_ready() const { return m_schedule->is_phase_ready(); }
118
119 /// @brief Sets the repeatable task and marks as enqueued by scheduler.
120 void set_enqueued_by_scheduler() { m_schedule->set_enqueued_by_scheduler(); }
121
122 /// @brief Checks if enqueued by scheduler.
123 /// @return true if enqueued by scheduler, false otherwise.
125 return m_schedule->is_enqueued_by_scheduler();
126 }
127
128 /// @brief Gets the shared repeatable task state that owns task lifetime.
129 /// @return The shared repeatable task state pointer.
131
132 /// @brief Gets the dispatch reason for this task instance.
133 /// @return Dispatch reason assigned by the scheduler.
135
136 /// @brief Sets the dispatch reason for this task instance.
137 /// @param dispatch_reason Reason assigned by the scheduler.
138 void set_dispatch_reason(Dispatch_reason dispatch_reason) {
139 m_dispatch_reason = dispatch_reason;
140 }
141
142 friend class Scheduler;
143
144 protected:
145 /// Task identifier (local to scheduler)
147 /// Task function
149 /// Shared repeatable task state carrying execution and logical lifetime.
151 /// Task schedule
153 /// Current phase counter
154 int m_phase{0};
155 /// Dispatch reason assigned when task is released to the worker pool.
157};
158
159} // namespace mysql::scheduler
160
161#endif // MYSQL_SCHEDULER_SCHEDULED_TASK_H
Shared state for one logical repeatable scheduler task.
Definition: scheduled_task.h:55
Repeatable_task_state & operator=(const Repeatable_task_state &)=delete
Repeatable_task_type m_repeatable_task
Repeatable task body shared by all scheduled task instances.
Definition: scheduled_task.h:80
bool execute(unsigned int thread_id)
Executes the repeatable task body for one phase dispatch.
Definition: scheduled_task.h:74
Repeatable_task_state(const Repeatable_task_state &)=delete
Repeatable_task_state(std::atomic< std::size_t > &scheduled_tasks_cnt, Repeatable_task_type &&repeatable_task)
Creates shared state for a logical repeatable task.
Definition: scheduled_task.cpp:30
std::atomic< std::size_t > & m_scheduled_tasks_cnt
Reference to the scheduler's logical task counter.
Definition: scheduled_task.h:78
~Repeatable_task_state()
Decrements the logical scheduled task count.
Definition: scheduled_task.cpp:36
std::function< bool(unsigned int)> Repeatable_task_type
Definition: scheduled_task.h:57
Represents task that is scheduled in the priority queue.
Definition: scheduled_task.h:87
Repeatable_task_state_ptr get_repeatable_task()
Gets the shared repeatable task state that owns task lifetime.
Definition: scheduled_task.h:130
Task_schedule_ptr m_schedule
Task schedule.
Definition: scheduled_task.h:152
Func_type m_task
Task function.
Definition: scheduled_task.h:148
Dispatch_reason get_dispatch_reason() const
Gets the dispatch reason for this task instance.
Definition: scheduled_task.h:134
std::function< Task_result(unsigned int)> Func_type
Definition: scheduled_task.h:88
void set_enqueued_by_scheduler()
Sets the repeatable task and marks as enqueued by scheduler.
Definition: scheduled_task.h:120
bool operator<(const Scheduled_task &arg) const
Returns task with a lower priority - operator needed for the priority queue.
Definition: scheduled_task.cpp:49
Task_id get_id() const
Gets the task ID.
Definition: scheduled_task.h:113
void set_dispatch_reason(Dispatch_reason dispatch_reason)
Sets the dispatch reason for this task instance.
Definition: scheduled_task.h:138
Scheduler_clock::Time_point_t get_task_delay() const
Gets the task delay time point.
Definition: scheduled_task.h:107
bool is_enqueued_by_scheduler()
Checks if enqueued by scheduler.
Definition: scheduled_task.h:124
Scheduled_task(const Task_id &id, Func_type &&task, const Repeatable_task_state_ptr &repeatable_task, const Task_schedule_ptr &schedule, unsigned int phase_id)
Constructor.
Definition: scheduled_task.cpp:38
Task_id m_task_id
Task identifier (local to scheduler)
Definition: scheduled_task.h:146
Repeatable_task_state_ptr m_repeatable_task
Shared repeatable task state carrying execution and logical lifetime.
Definition: scheduled_task.h:150
int m_phase
Current phase counter.
Definition: scheduled_task.h:154
bool is_phase_ready() const
Checks if the current phase is ready.
Definition: scheduled_task.h:117
Dispatch_reason m_dispatch_reason
Dispatch reason assigned when task is released to the worker pool.
Definition: scheduled_task.h:156
uint64_t Time_point_t
Definition: scheduler_clock.h:54
Main scheduling class.
Definition: scheduler.h:66
Represents the identifier of a task ingested by the scheduler,.
Definition: task_id.h:41
static my_thread_id thread_id
Definition: my_thr_init.cc:60
Definition: base_dependency_tracker.h:41
Dispatch_reason
Definition: dispatch_reason.h:29
Task_result
Acceptable task state after its execution.
Definition: task_result.h:32
std::shared_ptr< Task_schedule > Task_schedule_ptr
Definition: task_schedule.h:41
std::shared_ptr< Repeatable_task_state > Repeatable_task_state_ptr
Definition: scheduled_task.h:83