24#ifndef MYSQL_SCHEDULER_SCHEDULER_H
25#define MYSQL_SCHEDULER_SCHEDULER_H
111 template <
typename FType,
typename... Args>
124 template <
typename FType,
typename... Args>
126 FType &&task, Args &&...args);
134 bool synchronize(
bool force =
true,
bool print_checkpoint =
false);
190 template <
typename FType,
typename... Args>
219 template <
typename Repeatable_task_type>
223 std::shared_ptr<Repeatable_task_type> repeatable_task,
224 unsigned int current_thread_id);
232 unsigned int phase_id);
240 template <
typename Repeatable_task_type>
243 std::shared_ptr<Repeatable_task_type> repeatable_task,
244 unsigned int current_thread_id,
unsigned int phase_id);
Lock-free, fixed-size bounded, multiple-producer (MP), multiple-consumer (MC), circular FIFO queue fo...
Definition: integrals_lockfree_queue.h:130
MySQL wrapper for a condition variable, using mysql_cond_t as implementation of a condition variable ...
Definition: condition_variable_wrapper.h:37
MySQL wrapper for a mutex, template which may be specialized with a specific implementation of a mute...
Definition: mutex_wrapper.h:38
Wrapper to mysql thread, which matches interface of std::thread.
Definition: thread_srv.h:46
Represents task that is scheduled in the priority queue.
Definition: scheduled_task.h:87
uint64_t Time_point_t
Definition: scheduler_clock.h:54
Main scheduling class.
Definition: scheduler.h:66
Scheduler_clock_ptr m_scheduler_clock
Scheduler clock.
Definition: scheduler.h:354
std::size_t get_timeouts() const
Access "timeouts" observability statistics - the number of times scheduler timed out while waiting fo...
Definition: scheduler.cpp:380
std::priority_queue< Scheduled_task > Task_queue_type
Definition: scheduler.h:73
Thread_pool_ptr m_thread_pool
Thread pool with threads that execute tasks.
Definition: scheduler.h:373
bool m_wait_for_task_limit
Information on whether enqueueing thread is waiting for available task count limit.
Definition: scheduler.h:388
void deinit()
Ends the scheduler if stop was not yet requested.
Definition: scheduler.cpp:32
Scheduler(const Scheduler &src)=delete
Copying constructor is deleted.
void wait_for_scheduler_thread_to_stop()
Waits for scheduler thread to finish its work.
Definition: scheduler.cpp:76
Task_result callback(Task_schedule_ptr schedule, Scheduler_clock::Time_point_t task_delay, bool task_error, std::shared_ptr< Repeatable_task_type > repeatable_task, unsigned int current_thread_id)
Function used to notify that task ended its execution.
Definition: scheduler_impl.hpp:168
void request_unblock()
This function requests unblocking of the scheduler.
Definition: scheduler.cpp:429
void notify_scheduler()
Function used to notify scheduler thread by the other thread (enqueueing thread / worker poll thread)
Definition: scheduler.cpp:382
concurrency::Condition_variable m_cv_scheduler
Cv used by the scheduler main thread to wait on, when no task is available or tasks in m_task queue a...
Definition: scheduler.h:324
std::shared_ptr< Thread_pool_type > Thread_pool_ptr
Definition: scheduler.h:70
bool is_task_ready() const
Helper function to check if the task at the top of the task queue is ready.
Definition: scheduler.cpp:121
container::Integrals_lockfree_queue< uint64_t > m_notified_tasks
Lock-free queue of task IDs that are ready for execution.
Definition: scheduler.h:336
std::atomic< bool > m_scheduler_active
Variable that checks whether scheduler should run (true) or end its execution (false)
Definition: scheduler.h:344
std::unordered_map< Scheduler_clock_ptr, Mutex_type > m_task_phases_locks
Definition: scheduler.h:365
Dependency_tracker_ptr m_dependencies
Task dependencies, used mainly to implement dependencies between events in one transaction or between...
Definition: scheduler.h:371
bool is_error() const
Internal function to check on whether error has been set.
Definition: scheduler.cpp:252
std::atomic_flag m_is_error
Definition: scheduler.h:374
Mutex_type m_mutex_notification
Mutex protecting access to scheduler notifications.
Definition: scheduler.h:311
void stop_now()
Instruct the scheduler to stop now and withdraw all work.
Definition: scheduler.cpp:411
std::list< Task_future > Futures_list
Definition: scheduler.h:170
Mutex_type m_mutex_tasks
Mutex protecting access to m_tasks, data structure populated by the enqueuing thread.
Definition: scheduler.h:314
concurrency::Condition_variable m_cv_end
cv used by a thread requesting scheduler to finish its work
Definition: scheduler.h:329
std::atomic< std::size_t > m_scheduled_tasks_cnt
The number of scheduled tasks.
Definition: scheduler.h:351
bool register_phase(Scheduler_clock_ptr phase_clock)
Register task phase run according to the given clock.
Definition: scheduler.cpp:295
std::atomic< Scheduler_status > m_status
Observability variables.
Definition: scheduler.h:376
bool enqueue_helper(Scheduled_task &&task)
Enqueue helper.
Definition: scheduler.cpp:310
std::atomic< std::size_t > m_timeouts
Definition: scheduler.h:377
Task_result callback_phase(Task_schedule_ptr schedule, bool task_error, unsigned int phase_id)
Function used to notify that task phase ended its execution.
Definition: scheduler.cpp:390
bool add_dependency(const Task_id &predecessor, const Task_id &successor)
add_dependency
Definition: scheduler.cpp:86
void synchronize_active(bool print_checkpoint, std::size_t expected, bool force)
Synchronizes the scheduler by actively waiting for finished tasks count.
Definition: scheduler.cpp:322
bool synchronize(bool force=true, bool print_checkpoint=false)
This function waits for currently fired tasks to finish (blocking)
Definition: scheduler.cpp:368
std::unordered_map< Task_id, Scheduled_task > m_tasks_waiting_for_deps
Internal map that keeps tasks waiting for dependencies defined in the Dependency Graph (distinct from...
Definition: scheduler.h:334
std::priority_queue< Scheduled_task > m_tasks
This is a queue which holds task ready for scheduling.
Definition: scheduler.h:305
std::size_t get_scheduled_tasks_count() const
Obtain the number of scheduled, ongoing tasks.
Definition: scheduler.cpp:407
Statistics_instance_monitor_ref m_stat_monitor
Statistics monitoring object for the current instance.
Definition: scheduler.h:381
std::unordered_map< Scheduler_clock_ptr, Task_queue_type > m_task_phases
Tasks may be divided into phases, each phase works according to a defined clock, registered with "reg...
Definition: scheduler.h:364
bool is_task_phase_ready() const
Checks whether any phase of the task is ready for execution.
Definition: scheduler.cpp:109
std::atomic< std::size_t > m_allowed_task_count
Maximum allowed number of tasks active in the scheduler.
Definition: scheduler.h:384
void synchronize_partial(bool print_checkpoint=false)
Waits for percent of tasks to finish.
Definition: scheduler.cpp:376
unsigned int m_instance_id
Instance id.
Definition: scheduler.h:379
bool are_phase_queues_empty() const
Checks phase queues and return true if all are empty.
Definition: scheduler.cpp:91
std::future< Task_result > Task_future
Definition: scheduler.h:169
std::atomic< bool > m_stop_now
Variable that instructs scheduler to withdraw all work and stop.
Definition: scheduler.h:348
bool m_scheduler_thread_active
Variable indicating that scheduler thread finished its execution, protected with m_mutex_end.
Definition: scheduler.h:308
bool enqueue_internal(Task_schedule_ptr schedule, FType &&task, Args &&...args)
Enqueues a task identified by the task_id.
Definition: scheduler_impl.hpp:55
Mutex_type m_mutex_end
Mutex used to finish execution of the Scheduler, protects m_scheduler_thread_active and it is used to...
Definition: scheduler.h:327
bool enqueue_phase(Task_schedule_ptr schedule, Scheduler_clock::Time_point_t task_delay, std::shared_ptr< Repeatable_task_type > repeatable_task, unsigned int current_thread_id, unsigned int phase_id)
Function that enqueues next task phase.
Definition: scheduler_impl.hpp:117
static constexpr Sync_method sync_method
Definition: scheduler.h:76
Mutex_type & get_phase_queue_lock(Scheduler_clock_ptr phase_clock)
Definition: scheduler.cpp:288
std::atomic< bool > m_notification
True if scheduler has been notified.
Definition: scheduler.h:321
std::atomic< bool > m_unblock_request
Stores unblock requests from external threads, will allow N task to enter phase.
Definition: scheduler.h:391
virtual ~Scheduler()
Destructor.
Definition: scheduler.cpp:30
Mutex_type m_mutex_phases
This mutex protect access to phase.
Definition: scheduler.h:316
bool enqueue(Task_schedule_ptr schedule, FType &&task, Args &&...args)
Enqueues a task.
Definition: scheduler_impl.hpp:204
Scheduler_psi m_psi
PSI parameters.
Definition: scheduler.h:393
void handle_error()
Worker error handling function.
Definition: scheduler.cpp:276
void scheduler_clean_up()
Scheduler error handling function.
Definition: scheduler.cpp:254
void synchronize_active_partial(bool print_checkpoint)
Actively waits for percent of tasks to finish.
Definition: scheduler.cpp:359
void run_main_thread()
Scheduler run function.
Definition: scheduler.cpp:126
bool ensure_space()
Ensure space for the next task (eliminates possible wait in the enqueue)
Definition: scheduler.cpp:418
bool enqueue_after(const Task_id &predecessor, Task_schedule_ptr schedule, FType &&task, Args &&...args)
Enqueues a task that should execute after a predecessor task finishes.
Definition: scheduler_impl.hpp:215
Task_queue_type & get_phase_queue(Scheduler_clock_ptr phase_clock)
Definition: scheduler.cpp:281
void end_execution()
end_execution
Definition: scheduler.cpp:81
bool is_stop_requested() const
Internal function to check if immediate stop was requested, externally or due to an error.
Definition: scheduler.cpp:416
Thread_type m_scheduler_thread
Scheduler main thread.
Definition: scheduler.h:341
std::atomic< bool > m_deinitialized
Ensures deinit() is executed only once.
Definition: scheduler.h:346
Represents the identifier of a task ingested by the scheduler,.
Definition: task_id.h:41
MySQL wrapper for a condition variable, template which may be specialized with a specific implementat...
Definition: thread_pool.h:48
ValueType max(X &&first)
Definition: gtid.h:103
Mutex_wrapper Mutex
Definition: mutex_srv.h:40
std::thread Thread
Definition: thread_stl.h:42
Definition: base_dependency_tracker.h:41
std::unique_ptr< Base_dependency_tracker > Dependency_tracker_ptr
Definition: base_dependency_tracker.h:89
Sync_method
Definition: scheduler.h:51
std::reference_wrapper< Statistics_instance_monitor > Statistics_instance_monitor_ref
Definition: statistics_instance_monitor.h:43
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< Scheduler_clock > Scheduler_clock_ptr
Definition: scheduler_clock.h:36
Scheduler_status
Definition: scheduler.h:53
@ exiting_scheduler_thread
@ exited_scheduler_thread
Scheduler instrumentation parameters, packed into this structure to simplify construction of a Schedu...
Definition: scheduler_psi.h:38