MySQL 8.4.0
Source Code Documentation
connection_handler_manager.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2013, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
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#ifndef CONNECTION_HANDLER_MANAGER_INCLUDED
27#define CONNECTION_HANDLER_MANAGER_INCLUDED
28
29#include <assert.h>
30#include <stddef.h>
31#include <sys/types.h>
32
33#include "mysql/psi/mysql_cond.h" // mysql_cond_t
35#include "sql/conn_handler/connection_handler.h" // Connection_handler
36
37class Channel_info;
38class THD;
39struct mysql_cond_t;
40struct mysql_mutex_t;
41
42/**
43 Functions to notify interested connection handlers
44 of events like beginning of wait and end of wait and post-kill
45 notification events.
46*/
48 void (*thd_wait_begin)(THD *thd, int wait_type);
49 void (*thd_wait_end)(THD *thd);
51};
52
53/**
54 This is a singleton class that provides various connection management
55 related functionalities, most importantly dispatching new connections
56 to the currently active Connection_handler.
57*/
59 // Singleton instance to Connection_handler_manager
61
64
65 // Pointer to current connection handler in use
67 // Pointer to saved connection handler
69 // Saved scheduler_type
71
72 // Status variables
74 ulong
75 m_connection_errors_max_connection; // Protected by LOCK_connection_count
76
77 /**
78 Constructor to instantiate an instance of this class.
79 */
81 : m_connection_handler(connection_handler),
86
90 }
91
92 /* Make this class non-copyable */
95
96 public:
97 /**
98 thread_handling enumeration.
99
100 The default of --thread-handling is the first one in the
101 thread_handling_names array, this array has to be consistent with
102 the order in this array, so to change default one has to change the
103 first entry in this enum and the first entry in the
104 thread_handling_names array.
105
106 @note The last entry of the enumeration is also used to mark the
107 thread handling as dynamic. In this case the name of the thread
108 handling is fetched from the name of the plugin that implements it.
109 */
114 };
115
116 // Status variables. Must be static as they are used by the signal handler.
117 static uint connection_count; // Protected by LOCK_connection_count
118 static ulong max_used_connections; // Protected by LOCK_connection_count
119 static ulong max_used_connections_time; // Protected by LOCK_connection_count
120
121 // System variable
122 static ulong thread_handling;
123
124 // Functions for lock wait and post-kill notification events
126 // Saved event functions
128
129 /**
130 Maximum number of threads that can be created by the current
131 connection handler. Must be static as it is used by the signal handler.
132 */
133 static uint max_threads;
134
135 /**
136 Singleton method to return an instance of this class.
137 */
139 assert(m_instance != nullptr);
140 return m_instance;
141 }
142
143 /**
144 Initialize the connection handler manager.
145 Must be called before get_instance() can be used.
146
147 @return true if initialization failed, false otherwise.
148 */
149 static bool init();
150
151 /**
152 Destroy the singleton instance.
153 */
154 static void destroy_instance();
155
156 /**
157 Check if the current number of connections are below or equal
158 the value given by the max_connections server system variable.
159
160 @return true if a new connection can be accepted, false otherwise.
161 */
163
164 /**
165 Increment connection count if max_connections is not exceeded.
166
167 @param ignore_max_connection_count true if checking for a limit
168 specified by the max-connections
169 server option should be skipped
170
171 @retval
172 true max_connections NOT exceeded
173 false max_connections reached
174 */
175 bool check_and_incr_conn_count(bool ignore_max_connection_count);
176
177 /**
178 Reset the max_used_connections counter to the number of current
179 connections.
180 */
181 static void reset_max_used_connections();
182
183 /**
184 Decrease the number of current connections.
185 */
186 static void dec_connection_count() {
189 /*
190 Notify shutdown thread when last connection is done with its job
191 */
194 }
195
197
198 ulong aborted_connects() const { return m_aborted_connects; }
199
200 /**
201 @note This is a dirty read.
202 */
205 }
206
207 /**
208 Dynamically load a connection handler implemented as a plugin.
209 The current connection handler will be saved so that it can
210 later be restored by unload_connection_handler().
211 */
213
214 /**
215 Unload the connection handler previously loaded by
216 load_connection_handler(). The previous connection handler will
217 be restored.
218
219 @return true if unload failed (no previous connection handler was found).
220 */
222
223 /**
224 Process a new incoming connection.
225
226 @param channel_info Pointer to Channel_info object containing
227 connection channel information.
228 */
229 void process_new_connection(Channel_info *channel_info);
230
231 /**
232 Waits until all connections are done with their job. In other words,
233 wat till connection_count to become zero.
234 */
235 static void wait_till_no_connection();
236
237 /**
238 Return number of connections in thread-safe way.
239 */
240 static uint get_connection_count() {
242 const uint res = connection_count;
244 return res;
245 }
246};
247#endif // CONNECTION_HANDLER_MANAGER_INCLUDED.
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
This abstract base class represents connection channel information about a new connection.
Definition: channel_info.h:47
This is a singleton class that provides various connection management related functionalities,...
Definition: connection_handler_manager.h:58
static uint connection_count
Definition: connection_handler_manager.h:117
Connection_handler_manager(const Connection_handler_manager &)
static mysql_mutex_t LOCK_connection_count
Definition: connection_handler_manager.h:62
ulong m_saved_thread_handling
Definition: connection_handler_manager.h:70
void inc_aborted_connects()
Definition: connection_handler_manager.h:196
static uint max_threads
Maximum number of threads that can be created by the current connection handler.
Definition: connection_handler_manager.h:133
static void dec_connection_count()
Decrease the number of current connections.
Definition: connection_handler_manager.h:186
bool valid_connection_count()
Check if the current number of connections are below or equal the value given by the max_connections ...
Definition: connection_handler_manager.cc:93
void load_connection_handler(Connection_handler *conn_handler)
Dynamically load a connection handler implemented as a plugin.
Definition: connection_handler_manager.cc:231
static THD_event_functions * saved_event_functions
Definition: connection_handler_manager.h:127
static ulong thread_handling
Definition: connection_handler_manager.h:122
static bool init()
Initialize the connection handler manager.
Definition: connection_handler_manager.cc:145
static void destroy_instance()
Destroy the singleton instance.
Definition: connection_handler_manager.cc:213
static mysql_cond_t COND_connection_count
Definition: connection_handler_manager.h:63
bool check_and_incr_conn_count(bool ignore_max_connection_count)
Increment connection count if max_connections is not exceeded.
Definition: connection_handler_manager.cc:104
void process_new_connection(Channel_info *channel_info)
Process a new incoming connection.
Definition: connection_handler_manager.cc:254
static Connection_handler_manager * m_instance
Definition: connection_handler_manager.h:60
Connection_handler_manager(Connection_handler *connection_handler)
Constructor to instantiate an instance of this class.
Definition: connection_handler_manager.h:80
~Connection_handler_manager()
Definition: connection_handler_manager.h:87
ulong m_aborted_connects
Definition: connection_handler_manager.h:73
static Connection_handler_manager * get_instance()
Singleton method to return an instance of this class.
Definition: connection_handler_manager.h:138
static void wait_till_no_connection()
Waits until all connections are done with their job.
Definition: connection_handler_manager.cc:204
Connection_handler_manager & operator=(const Connection_handler_manager &)
static THD_event_functions * event_functions
Definition: connection_handler_manager.h:125
static ulong max_used_connections
Definition: connection_handler_manager.h:118
ulong m_connection_errors_max_connection
Definition: connection_handler_manager.h:75
bool unload_connection_handler()
Unload the connection handler previously loaded by load_connection_handler().
Definition: connection_handler_manager.cc:242
ulong connection_errors_max_connection() const
Definition: connection_handler_manager.h:203
static ulong max_used_connections_time
Definition: connection_handler_manager.h:119
static uint get_connection_count()
Return number of connections in thread-safe way.
Definition: connection_handler_manager.h:240
scheduler_types
thread_handling enumeration.
Definition: connection_handler_manager.h:110
@ SCHEDULER_TYPES_COUNT
Definition: connection_handler_manager.h:113
@ SCHEDULER_NO_THREADS
Definition: connection_handler_manager.h:112
@ SCHEDULER_ONE_THREAD_PER_CONNECTION
Definition: connection_handler_manager.h:111
static void reset_max_used_connections()
Reset the max_used_connections counter to the number of current connections.
Definition: connection_handler_manager.cc:224
ulong aborted_connects() const
Definition: connection_handler_manager.h:198
Connection_handler * m_connection_handler
Definition: connection_handler_manager.h:66
Connection_handler * m_saved_connection_handler
Definition: connection_handler_manager.h:68
This abstract base class represents how connections are processed, most importantly how they map to O...
Definition: connection_handler.h:37
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
#define mysql_cond_signal(C)
Definition: mysql_cond.h:56
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:50
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:57
wait_type
Definition: socket_constants.h:86
Instrumentation helpers for conditions.
Instrumentation helpers for mutexes.
Functions to notify interested connection handlers of events like beginning of wait and end of wait a...
Definition: connection_handler_manager.h:47
void(* post_kill_notification)(THD *thd)
Definition: connection_handler_manager.h:50
void(* thd_wait_end)(THD *thd)
Definition: connection_handler_manager.h:49
void(* thd_wait_begin)(THD *thd, int wait_type)
Definition: connection_handler_manager.h:48
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50