MySQL  8.0.17
Source Code Documentation
connection_handler_manager.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
29 #include <sys/types.h>
30 
31 #include "my_dbug.h"
32 #include "mysql/psi/mysql_cond.h" // mysql_cond_t
33 #include "mysql/psi/mysql_mutex.h"
34 #include "sql/conn_handler/connection_handler.h" // Connection_handler
35 
36 class Channel_info;
37 class THD;
38 struct mysql_cond_t;
39 struct mysql_mutex_t;
40 
41 /**
42  Functions to notify interested connection handlers
43  of events like begining 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);
49  void (*post_kill_notification)(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 
87  delete m_connection_handler;
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
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  */
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  */
161  bool valid_connection_count();
162 
163  /**
164  Increment connection count if max_connections is not exceeded.
165 
166  @retval
167  true max_connections NOT exceeded
168  false max_connections reached
169  */
170  bool check_and_incr_conn_count(bool is_admin_connection);
171 
172  /**
173  Reset the max_used_connections counter to the number of current
174  connections.
175  */
176  static void reset_max_used_connections();
177 
178  /**
179  Decrease the number of current connections.
180  */
181  static void dec_connection_count() {
184  /*
185  Notify shutdown thread when last connection is done with its job
186  */
189  }
190 
192 
194 
195  /**
196  @note This is a dirty read.
197  */
200  }
201 
202  /**
203  Dynamically load a connection handler implemented as a plugin.
204  The current connection handler will be saved so that it can
205  later be restored by unload_connection_handler().
206  */
207  void load_connection_handler(Connection_handler *conn_handler);
208 
209  /**
210  Unload the connection handler previously loaded by
211  load_connection_handler(). The previous connection handler will
212  be restored.
213 
214  @return true if unload failed (no previous connection handler was found).
215  */
217 
218  /**
219  Process a new incoming connection.
220 
221  @param channel_info Pointer to Channel_info object containing
222  connection channel information.
223  */
224  void process_new_connection(Channel_info *channel_info);
225 
226  /**
227  Waits until all connections are done with their job. In other words,
228  wat till connection_count to become zero.
229  */
230  static void wait_till_no_connection();
231 };
232 #endif // CONNECTION_HANDLER_MANAGER_INCLUDED.
bool unload_connection_handler()
Unload the connection handler previously loaded by load_connection_handler().
Definition: connection_handler_manager.cc:238
Instrumentation helpers for mutexes.
static ulong thread_handling
Definition: connection_handler_manager.h:121
Functions to notify interested connection handlers of events like begining of wait and end of wait an...
Definition: connection_handler_manager.h:46
void inc_aborted_connects()
Definition: connection_handler_manager.h:191
ulong m_saved_thread_handling
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:36
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:89
void(* post_kill_notification)(THD *thd)
Definition: connection_handler_manager.h:49
ulong m_connection_errors_max_connection
Definition: connection_handler_manager.h:74
Connection_handler_manager & operator=(const Connection_handler_manager &)
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
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:181
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:219
static ulong max_used_connections_time
Definition: connection_handler_manager.h:118
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
#define mysql_cond_signal(C)
Definition: mysql_cond.h:52
bool check_and_incr_conn_count(bool is_admin_connection)
Increment connection count if max_connections is not exceeded.
Definition: connection_handler_manager.cc:100
scheduler_types
thread_handling enumeration.
Definition: connection_handler_manager.h:109
Definition: connection_handler_manager.h:110
~Connection_handler_manager()
Definition: connection_handler_manager.h:86
static uint connection_count
Definition: connection_handler_manager.h:116
static void wait_till_no_connection()
Waits until all connections are done with their job.
Definition: connection_handler_manager.cc:200
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
Connection_handler * m_connection_handler
Definition: connection_handler_manager.h:65
static THD_event_functions * saved_event_functions
Definition: connection_handler_manager.h:126
void process_new_connection(Channel_info *channel_info)
Process a new incoming connection.
Definition: connection_handler_manager.cc:250
Definition: connection_handler_manager.h:111
ulong m_aborted_connects
Definition: connection_handler_manager.h:72
void(* thd_wait_end)(THD *thd)
Definition: connection_handler_manager.h:48
unsigned int uint
Definition: uca-dump.cc:29
ulong aborted_connects() const
Definition: connection_handler_manager.h:193
static void destroy_instance()
Destroy the singleton instance.
Definition: connection_handler_manager.cc:208
static bool init()
Initialize the connection handler manager.
Definition: connection_handler_manager.cc:141
ulong connection_errors_max_connection() const
Definition: connection_handler_manager.h:198
void(* thd_wait_begin)(THD *thd, int wait_type)
Definition: connection_handler_manager.h:47
static mysql_mutex_t LOCK_connection_count
Definition: connection_handler_manager.h:61
Instrumentation helpers for conditions.
static Connection_handler_manager * m_instance
Definition: connection_handler_manager.h:59
static ulong max_used_connections
Definition: connection_handler_manager.h:117
Connection_handler_manager(Connection_handler *connection_handler)
Constructor to instantiate an instance of this class.
Definition: connection_handler_manager.h:79
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
Connection_handler * m_saved_connection_handler
Definition: connection_handler_manager.h:67
void load_connection_handler(Connection_handler *conn_handler)
Dynamically load a connection handler implemented as a plugin.
Definition: connection_handler_manager.cc:226
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:53
static THD_event_functions * event_functions
Definition: connection_handler_manager.h:124
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:44
static mysql_cond_t COND_connection_count
Definition: connection_handler_manager.h:62
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, most importantly dispatching new connections to the currently active Connection_handler.
Definition: connection_handler_manager.h:57
unsigned long ulong
Definition: my_inttypes.h:48
static Connection_handler_manager * get_instance()
Singleton method to return an instance of this class.
Definition: connection_handler_manager.h:137
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777