MySQL  8.0.27
Source Code Documentation
event_queue.h
Go to the documentation of this file.
1 #ifndef _EVENT_QUEUE_H_
2 #define _EVENT_QUEUE_H_
3 /* Copyright (c) 2004, 2021, Oracle and/or its affiliates.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation. The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 
25 /**
26 
27  @addtogroup Event_Scheduler
28  @{
29 
30  @file event_queue.h
31 
32  Queue of events awaiting execution.
33 */
34 
35 #include <sys/types.h>
36 #include <time.h>
37 #include <atomic>
38 #include <vector>
39 
40 #include "lex_string.h"
41 #include "my_psi_config.h"
42 #include "my_time.h"
48 #include "priority_queue.h" // Priority_queue
49 #include "sql/event_data_objects.h" // Event_queue_element
50 #include "sql/event_parse_data.h" // Event_parse_data
51 #include "sql/malloc_allocator.h" // IWYU pragma: keep
52 
53 class THD;
54 
55 #ifdef HAVE_PSI_INTERFACE
58 #endif /* HAVE_PSI_INTERFACE */
59 
61  /// Maps compare function to strict weak ordering required by Priority_queue.
63  return event_queue_element_compare_q(left, right) > 0;
64  }
65 
66  /**
67  Compares the execute_at members of two Event_queue_element instances.
68  Used as compare operator for the prioritized queue when shifting
69  elements inside.
70 
71  SYNOPSIS
72  event_queue_element_compare_q()
73  @param left First Event_queue_element object
74  @param right Second Event_queue_element object
75 
76  @retval
77  -1 left->execute_at < right->execute_at
78  0 left->execute_at == right->execute_at
79  1 left->execute_at > right->execute_at
80 
81  @remark
82  execute_at.second_part is not considered during comparison
83  */
85  Event_queue_element *right) {
87  return right->m_status != Event_parse_data::DISABLED;
88 
89  if (right->m_status == Event_parse_data::DISABLED) return 1;
90 
91  my_time_t lhs = left->m_execute_at;
92  my_time_t rhs = right->m_execute_at;
93  return (lhs < rhs ? -1 : (lhs > rhs ? 1 : 0));
94  }
95 };
96 
97 /**
98  Queue of active events awaiting execution.
99 */
100 
101 class Event_queue {
102  public:
103  Event_queue();
104  ~Event_queue();
105 
106  bool init_queue();
107 
108  /* Methods for queue management follow */
109 
110  bool create_event(THD *thd, Event_queue_element *new_element, bool *created);
111 
112  void update_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name,
113  Event_queue_element *new_element);
114 
115  void drop_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name);
116 
117  void drop_schema_events(LEX_CSTRING schema);
118 
120 
122  Event_queue_element_for_exec **event_name);
123 
124  void dump_internal_status();
125 
126  private:
127  void empty_queue();
128 
129  void deinit_queue();
130  /* helper functions for working with mutexes & conditionals */
131  void lock_data(const char *func, uint line);
132 
133  void unlock_data(const char *func, uint line);
134 
135  void cond_wait(THD *thd, struct timespec *abstime,
136  const PSI_stage_info *stage, const char *src_func,
137  const char *src_file, uint src_line);
138 
140 
141  void drop_matching_events(LEX_CSTRING pattern,
142  bool (*)(LEX_CSTRING, Event_basic *));
143 
144  void dbug_dump_queue(time_t now);
145 
146  /* LOCK_event_queue is the mutex which protects the access to the queue. */
149 
150  /* The sorted queue with the Event_queue_element objects */
156 
158 
164  std::atomic<const char *> mutex_last_attempted_lock_in_func;
168 };
169 /**
170  @} (End of group Event_Scheduler)
171 */
172 
173 #endif /* _EVENT_QUEUE_H_ */
Definition: event_data_objects.h:77
@ DISABLED
Definition: event_parse_data.h:50
Definition: event_data_objects.h:52
Definition: event_data_objects.h:93
my_time_t m_execute_at
Definition: event_data_objects.h:100
int m_status
Definition: event_data_objects.h:96
Queue of active events awaiting execution.
Definition: event_queue.h:101
uint mutex_last_unlocked_at_line
Definition: event_queue.h:160
bool waiting_on_cond
Definition: event_queue.h:167
mysql_cond_t COND_queue_state
Definition: event_queue.h:148
bool mutex_queue_data_locked
Definition: event_queue.h:165
uint mutex_last_locked_at_line
Definition: event_queue.h:159
my_time_t next_activation_at
Definition: event_queue.h:157
std::atomic< const char * > mutex_last_attempted_lock_in_func
Definition: event_queue.h:164
const char * mutex_last_locked_in_func
Definition: event_queue.h:162
const char * mutex_last_unlocked_in_func
Definition: event_queue.h:163
std::atomic< uint > mutex_last_attempted_lock_at_line
Definition: event_queue.h:161
mysql_mutex_t LOCK_event_queue
Definition: event_queue.h:147
std::atomic< bool > mutex_queue_data_attempting_lock
Definition: event_queue.h:166
Priority_queue< Event_queue_element *, std::vector< Event_queue_element *, Malloc_allocator< Event_queue_element * > >, Event_queue_less > queue
Definition: event_queue.h:155
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:62
Implements a priority queue using a vector-based max-heap.
Definition: priority_queue.h:91
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
bool get_top_for_execution_if_time(THD *thd, Event_queue_element_for_exec **event_name)
Definition: event_queue.cc:530
void update_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name, Event_queue_element *new_element)
Definition: event_queue.cc:204
void lock_data(const char *func, uint line)
Definition: event_queue.cc:657
Event_queue()
Definition: event_queue.cc:75
void recalculate_activation_times(THD *thd)
Definition: event_queue.cc:375
void drop_matching_events(LEX_CSTRING pattern, bool(*)(LEX_CSTRING, Event_basic *))
Definition: event_queue.cc:276
void find_n_remove_event(LEX_CSTRING db, LEX_CSTRING name)
Definition: event_queue.cc:347
void cond_wait(THD *thd, struct timespec *abstime, const PSI_stage_info *stage, const char *src_func, const char *src_file, uint src_line)
Definition: event_queue.cc:704
void empty_queue()
Definition: event_queue.cc:475
PSI_cond_key key_COND_queue_state
Definition: events.cc:1007
~Event_queue()
Definition: event_queue.cc:93
void dbug_dump_queue(time_t now)
Definition: event_queue.cc:493
void unlock_data(const char *func, uint line)
Definition: event_queue.cc:683
void drop_schema_events(LEX_CSTRING schema)
Definition: event_queue.cc:327
bool init_queue()
Definition: event_queue.cc:115
PSI_mutex_key key_LOCK_event_queue
Definition: events.cc:997
bool create_event(THD *thd, Event_queue_element *new_element, bool *created)
Adds an event to the queue.
Definition: event_queue.cc:168
void dump_internal_status()
Definition: event_queue.cc:744
void deinit_queue()
Definition: event_queue.cc:142
void drop_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name)
Definition: event_queue.cc:245
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:43
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:51
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Interface for low level time utilities.
[] long int my_time_t
Portable time_t replacement.
Instrumentation helpers for conditions.
ABI for instrumented mutexes.
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2162
Performance schema instrumentation interface.
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
case opt name
Definition: sslopt-case.h:32
Definition: event_queue.h:60
int event_queue_element_compare_q(Event_queue_element *left, Event_queue_element *right)
Compares the execute_at members of two Event_queue_element instances.
Definition: event_queue.h:84
bool operator()(Event_queue_element *left, Event_queue_element *right)
Maps compare function to strict weak ordering required by Priority_queue.
Definition: event_queue.h:62
Definition: mysql_lex_string.h:39
Stage instrument information.
Definition: psi_stage_bits.h:73
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
Include file for Sun RPC to compile out of the box.
unsigned int uint
Definition: uca-dump.cc:29