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