MySQL 9.1.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#include <atomic>
33
34#include "mysql/psi/mysql_cond.h" // mysql_cond_t
36#include "sql/conn_handler/connection_handler.h" // Connection_handler
37
38class Channel_info;
39class THD;
40struct mysql_cond_t;
41struct mysql_mutex_t;
42
43/**
44 Functions to notify interested connection handlers
45 of events like beginning of wait and end of wait and post-kill
46 notification events.
47*/
49 void (*thd_wait_begin)(THD *thd, int wait_type);
50 void (*thd_wait_end)(THD *thd);
52};
53
54/**
55 This is a singleton class that provides various connection management
56 related functionalities, most importantly dispatching new connections
57 to the currently active Connection_handler.
58*/
60 // Singleton instance to Connection_handler_manager
62
65
66 // Pointer to current connection handler in use
68 // Pointer to saved connection handler
70 // Saved scheduler_type
72
73 // Status variables
75 ulong
76 m_connection_errors_max_connection; // Protected by LOCK_connection_count
77
78 /**
79 Constructor to instantiate an instance of this class.
80 */
82 : m_connection_handler(connection_handler),
87
91 }
92
93 /* Make this class non-copyable */
96
97 public:
98 /**
99 thread_handling enumeration.
100
101 The default of --thread-handling is the first one in the
102 thread_handling_names array, this array has to be consistent with
103 the order in this array, so to change default one has to change the
104 first entry in this enum and the first entry in the
105 thread_handling_names array.
106
107 @note The last entry of the enumeration is also used to mark the
108 thread handling as dynamic. In this case the name of the thread
109 handling is fetched from the name of the plugin that implements it.
110 */
115 };
116
117 // Status variables. Must be static as they are used by the signal handler.
118 static uint connection_count; // Protected by LOCK_connection_count
119 static ulong max_used_connections; // Protected by LOCK_connection_count
120 static ulong max_used_connections_time; // Protected by LOCK_connection_count
121
122 // Atomic status variables.
123 static std::atomic_ulong
124 incoming_connection_count; // Only external sessions.
125
126 // System variable
127 static ulong thread_handling;
128
129 // Functions for lock wait and post-kill notification events
131 // Saved event functions
133
134 /**
135 Maximum number of threads that can be created by the current
136 connection handler. Must be static as it is used by the signal handler.
137 */
138 static uint max_threads;
139
140 /**
141 Singleton method to return an instance of this class.
142 */
144 assert(m_instance != nullptr);
145 return m_instance;
146 }
147
148 /**
149 Initialize the connection handler manager.
150 Must be called before get_instance() can be used.
151
152 @return true if initialization failed, false otherwise.
153 */
154 static bool init();
155
156 /**
157 Destroy the singleton instance.
158 */
159 static void destroy_instance();
160
161 /**
162 Check if the current number of connections are below or equal
163 the value given by the max_connections server system variable.
164
165 @return true if a new connection can be accepted, false otherwise.
166 */
168
169 /**
170 Increment connection count if max_connections is not exceeded.
171
172 @param ignore_max_connection_count true if checking for a limit
173 specified by the max-connections
174 server option should be skipped.
175
176 @param internal_session true if this is an internal session created
177 by the session service. If this is the case,
178 we will not count this as an incoming connection.
179
180 @retval
181 true max_connections NOT exceeded
182 false max_connections reached
183 */
184 bool check_and_incr_conn_count(bool ignore_max_connection_count,
185 bool internal_session);
186
187 /**
188 Reset the max_used_connections counter to the number of current
189 connections.
190 */
191 static void reset_max_used_connections();
192
193 /**
194 Decrease the number of current connections.
195 */
196 static void dec_connection_count() {
199 /*
200 Notify shutdown thread when last connection is done with its job
201 */
204 }
205
206 static ulong get_incoming_connects() {
207 return incoming_connection_count.load();
208 }
209
211
212 ulong aborted_connects() const { return m_aborted_connects; }
213
214 /**
215 @note This is a dirty read.
216 */
219 }
220
221 /**
222 Dynamically load a connection handler implemented as a plugin.
223 The current connection handler will be saved so that it can
224 later be restored by unload_connection_handler().
225 */
227
228 /**
229 Unload the connection handler previously loaded by
230 load_connection_handler(). The previous connection handler will
231 be restored.
232
233 @return true if unload failed (no previous connection handler was found).
234 */
236
237 /**
238 Process a new incoming connection.
239
240 @param channel_info Pointer to Channel_info object containing
241 connection channel information.
242 */
243 void process_new_connection(Channel_info *channel_info);
244
245 /**
246 Waits until all connections are done with their job. In other words,
247 wat till connection_count to become zero.
248 */
249 static void wait_till_no_connection();
250
251 /**
252 Return number of connections in thread-safe way.
253 */
254 static uint get_connection_count() {
256 const uint res = connection_count;
258 return res;
259 }
260};
261#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:59
static uint connection_count
Definition: connection_handler_manager.h:118
Connection_handler_manager(const Connection_handler_manager &)
static mysql_mutex_t LOCK_connection_count
Definition: connection_handler_manager.h:63
ulong m_saved_thread_handling
Definition: connection_handler_manager.h:71
static ulong get_incoming_connects()
Definition: connection_handler_manager.h:206
void inc_aborted_connects()
Definition: connection_handler_manager.h:210
static uint max_threads
Maximum number of threads that can be created by the current connection handler.
Definition: connection_handler_manager.h:138
static void dec_connection_count()
Decrease the number of current connections.
Definition: connection_handler_manager.h:196
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:95
void load_connection_handler(Connection_handler *conn_handler)
Dynamically load a connection handler implemented as a plugin.
Definition: connection_handler_manager.cc:233
static THD_event_functions * saved_event_functions
Definition: connection_handler_manager.h:132
static ulong thread_handling
Definition: connection_handler_manager.h:127
static std::atomic_ulong incoming_connection_count
Definition: connection_handler_manager.h:124
static bool init()
Initialize the connection handler manager.
Definition: connection_handler_manager.cc:147
static void destroy_instance()
Destroy the singleton instance.
Definition: connection_handler_manager.cc:215
static mysql_cond_t COND_connection_count
Definition: connection_handler_manager.h:64
void process_new_connection(Channel_info *channel_info)
Process a new incoming connection.
Definition: connection_handler_manager.cc:256
static Connection_handler_manager * m_instance
Definition: connection_handler_manager.h:61
Connection_handler_manager(Connection_handler *connection_handler)
Constructor to instantiate an instance of this class.
Definition: connection_handler_manager.h:81
~Connection_handler_manager()
Definition: connection_handler_manager.h:88
ulong m_aborted_connects
Definition: connection_handler_manager.h:74
static Connection_handler_manager * get_instance()
Singleton method to return an instance of this class.
Definition: connection_handler_manager.h:143
static void wait_till_no_connection()
Waits until all connections are done with their job.
Definition: connection_handler_manager.cc:206
Connection_handler_manager & operator=(const Connection_handler_manager &)
static THD_event_functions * event_functions
Definition: connection_handler_manager.h:130
static ulong max_used_connections
Definition: connection_handler_manager.h:119
bool check_and_incr_conn_count(bool ignore_max_connection_count, bool internal_session)
Increment connection count if max_connections is not exceeded.
Definition: connection_handler_manager.cc:106
ulong m_connection_errors_max_connection
Definition: connection_handler_manager.h:76
bool unload_connection_handler()
Unload the connection handler previously loaded by load_connection_handler().
Definition: connection_handler_manager.cc:244
ulong connection_errors_max_connection() const
Definition: connection_handler_manager.h:217
static ulong max_used_connections_time
Definition: connection_handler_manager.h:120
static uint get_connection_count()
Return number of connections in thread-safe way.
Definition: connection_handler_manager.h:254
scheduler_types
thread_handling enumeration.
Definition: connection_handler_manager.h:111
@ SCHEDULER_TYPES_COUNT
Definition: connection_handler_manager.h:114
@ SCHEDULER_NO_THREADS
Definition: connection_handler_manager.h:113
@ SCHEDULER_ONE_THREAD_PER_CONNECTION
Definition: connection_handler_manager.h:112
static void reset_max_used_connections()
Reset the max_used_connections counter to the number of current connections.
Definition: connection_handler_manager.cc:226
ulong aborted_connects() const
Definition: connection_handler_manager.h:212
Connection_handler * m_connection_handler
Definition: connection_handler_manager.h:67
Connection_handler * m_saved_connection_handler
Definition: connection_handler_manager.h:69
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:48
void(* post_kill_notification)(THD *thd)
Definition: connection_handler_manager.h:51
void(* thd_wait_end)(THD *thd)
Definition: connection_handler_manager.h:50
void(* thd_wait_begin)(THD *thd, int wait_type)
Definition: connection_handler_manager.h:49
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50