MySQL  8.0.16
Source Code Documentation
srv_session.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef SRV_SESSION_H
24 #define SRV_SESSION_H
25 
26 #include <stdint.h>
27 
28 #include "lex_string.h"
29 #include "m_ctype.h"
30 #include "my_command.h"
31 #include "my_psi_config.h"
32 #include "my_thread_local.h"
34 #include "mysql/service_command.h"
36 #include "sql/protocol_callback.h"
37 #include "sql/sql_class.h"
38 #include "sql/sql_error.h"
39 #include "violite.h" /* enum_vio_type */
40 
41 struct st_plugin_int;
42 
43 /**
44  @file
45  Header file for the Srv_session class that wraps THD, DA in one bundle
46  for easy use of internal APIs.
47  Srv_session also provides means for physical thread initialization and
48  respective deinitialization.
49 */
50 
51 #ifdef HAVE_PSI_STATEMENT_INTERFACE
53 #endif
54 
55 class Srv_session {
56  public:
57  /**
58  Initializes the module.
59 
60 
61  This method has to be called at server startup.
62 
63  @return
64  false success
65  true failure
66  */
67  static bool module_init();
68 
69  /**
70  Deinitializes the module
71 
72 
73  This method has to be called at server shutdown.
74 
75  @return
76  false success
77  true failure
78  */
79  static bool module_deinit();
80 
81  /**
82  Initializes the current physical thread for use with this class.
83 
84  @param plugin Pointer to the plugin structure, passed to the plugin over
85  the plugin init function.
86 
87  @return
88  false success
89  true failure
90  */
91  static bool init_thread(const void *plugin);
92 
93  /**
94  Deinitializes the current physical thread for use with session service
95  */
96  static void deinit_thread();
97 
98  /**
99  Checks if a plugin has left threads and sessions
100 
101  @param plugin The plugin to be checked
102  */
103  static void check_for_stale_threads(const st_plugin_int *plugin);
104 
105  /**
106  Checks if the session is valid.
107 
108  Checked is if session is NULL, or in the list of opened sessions. If the
109  session is not in this list it was either closed or the address is invalid.
110 
111  @return
112  true valid
113  false not valid
114  */
115  static bool is_valid(const Srv_session *session);
116 
117  /**
118  Returns the number opened sessions in thread initialized by this class.
119  */
120  static unsigned int session_count();
121 
122  /**
123  Returns the number currently running threads initialized by this class.
124  */
125  static unsigned int thread_count(const void *plugin_name);
126 
127  /**
128  Check if current physical thread was created to be used with this class.
129  */
130  static bool is_srv_session_thread();
131 
132  /* Non-static members follow */
133 
134  /**
135  Enum for the state of the session
136  */
142  };
143 
144  /**
145  Constructs a server session
146 
147  @param err_cb Default completion callback
148  @param err_cb_ctx Plugin's context, opaque pointer that would
149  be provided to callbacks. Might be NULL.
150  */
151  Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx);
152 
153  /**
154  Opens a server session
155 
156  @return
157  session on success
158  NULL on failure
159  */
160  bool open();
161 
162  /**
163  Attaches the session to the current physical thread
164 
165  @returns
166  false success
167  true failure
168  */
169  bool attach();
170 
171  /**
172  Detaches the session from current physical thread.
173 
174  @returns
175  false success
176  true failure
177  */
178  bool detach();
179 
180  /**
181  Closes the session
182 
183  @returns
184  false Session successfully closed
185  true Session wasn't found or key doesn't match
186  */
187  bool close();
188 
189  /**
190  Returns if the session is in attached state
191 
192  @returns
193  false Not attached
194  true Attached
195  */
196  inline bool is_attached() const { return state == SRV_SESSION_ATTACHED; }
197 
198  /**
199  Executes a server command.
200 
201  @param command Command to be executed
202  @param data Command's arguments
203  @param client_cs The charset for the string data input (COM_QUERY
204  for example)
205  @param command_callbacks Callbacks to be used by the server to encode data
206  and to communicate with the client (plugin) side.
207  @param text_or_binary See enum cs_text_or_binary
208  @param callbacks_context Context passed to the callbacks
209 
210  @returns
211  1 error
212  0 success
213  */
215  const union COM_DATA *data, const CHARSET_INFO *client_cs,
216  const struct st_command_service_cbs *command_callbacks,
217  enum cs_text_or_binary text_or_binary,
218  void *callbacks_context);
219 
220  /**
221  Returns the internal THD object
222  */
223  inline THD *get_thd() { return &thd; }
224 
225  /**
226  Returns the ID of a session.
227 
228  The value returned from THD::thread_id()
229  */
230  my_thread_id get_session_id() const { return thd.thread_id(); }
231 
232  /**
233  Returns the client port.
234 
235  @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
236  This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
237  */
238  uint16_t get_client_port() const { return thd.peer_port; }
239 
240  /**
241  Sets the client port.
242 
243  @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
244  This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
245 
246  @param port Port number
247  */
248  void set_client_port(uint16_t port);
249 
250  /**
251  Returns the current database of a session.
252 
253  @note This call is not thread-safe. Don't invoke the method from a thread
254  different than the one in which the invocation happens. This means
255  that the call should NOT happen during run_command(). The value
256  returned is valid until the next run_command() call, which may
257  change it.
258  */
259  LEX_CSTRING get_current_database() const { return thd.db(); }
260 
261  /**
262  Sets the connection type.
263 
264  @see enum_vio_type
265 
266  @note If NO_VIO_TYPE passed as type the call will fail.
267 
268  @return
269  false success
270  true failure
271  */
273 
276  : handler(h), handler_context(h_ctx) {}
277 
280  };
281 
282  private:
283  /**
284  Sets session's state to attached
285 
286  @param stack New stack address
287  */
288  void set_attached(const char *stack);
289 
290  /**
291  Changes the state of a session to detached
292  */
293  void set_detached();
294 
299 
302 
304  public:
305  /**
306  Constructs a session state object. Saves state then attaches a session.
307  Uses RAII.
308 
309  @param sess Session to backup
310  @param is_in_close_session Wheather session needs to be closed.
311  */
312  Session_backup_and_attach(Srv_session *sess, bool is_in_close_session);
313 
314  /**
315  Destructs the session state object. In other words it restores to
316  previous state.
317  */
319 
320  private:
322  Srv_session *old_session; /* used in srv_session threads */
325 
326  public:
328  };
329 };
330 
331 #endif /* SRV_SESSION_H */
Definition: sql_plugin_ref.h:44
LEX_STRING * plugin_name(st_plugin_int **ref)
Definition: sql_plugin_ref.h:94
Definition: srv_session.h:274
st_err_protocol_ctx(srv_session_error_cb h, void *h_ctx)
Definition: srv_session.h:275
Session_backup_and_attach(Srv_session *sess, bool is_in_close_session)
Constructs a session state object.
Definition: srv_session.cc:457
Srv_session * session
Definition: srv_session.h:321
void(* srv_session_error_cb)(void *ctx, unsigned int sql_errno, const char *err_msg)
Definition: service_srv_session.h:44
static bool module_deinit()
Deinitializes the module.
Definition: srv_session.cc:732
const LEX_CSTRING & db() const
Definition: sql_class.h:3383
srv_session_error_cb handler
Definition: srv_session.h:278
void * handler_context
Definition: srv_session.h:279
MYSQL_SESSION session[9]
Definition: test_sql_9_sessions.cc:65
Definition: mysql_lex_string.h:39
static struct st_mysql_daemon plugin
Definition: test_services_host_application_signal.cc:130
static void deinit_thread()
Deinitializes the current physical thread for use with session service.
Definition: srv_session.cc:667
Definition: com_data.h:106
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:3913
Definition: srv_session.h:55
bool close()
Closes the session.
Definition: srv_session.cc:976
my_thread_id get_session_id() const
Returns the ID of a session.
Definition: srv_session.h:230
THD * backup_thd
Definition: srv_session.h:323
THD thd
Definition: srv_session.h:295
static unsigned int session_count()
Returns the number opened sessions in thread initialized by this class.
Definition: srv_session.cc:1200
Definition: srv_session.h:141
Definition: srv_session.h:140
static in_port_t port
Definition: testapp.c:33
my_thread_id thread_id() const
Definition: sql_class.h:2101
bool is_attached() const
Returns if the session is in attached state.
Definition: srv_session.h:196
bool set_connection_type(enum_vio_type type)
Sets the connection type.
Definition: srv_session.cc:1124
LEX_CSTRING get_current_database() const
Returns the current database of a session.
Definition: srv_session.h:259
void set_client_port(uint16_t port)
Sets the client port.
Definition: srv_session.cc:1191
Definition: protocol_callback.h:52
bool in_close_session
Definition: srv_session.h:324
Stores status of the currently executed statement.
Definition: sql_error.h:268
enum_vio_type vio_type
Definition: srv_session.h:301
Interface of the Protocol_callback class, which is used by the Command service as proxy protocol...
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Statement instrument information.
Definition: psi_statement_bits.h:108
bool detach()
Detaches the session from current physical thread.
Definition: srv_session.cc:931
bool open()
Opens a server session.
Definition: srv_session.cc:787
enum_vio_type
Definition: violite.h:75
Performance schema instrumentation interface.
Definition: srv_session.h:139
cs_text_or_binary
Definition: service_command.h:375
srv_session_state state
Definition: srv_session.h:300
Definition: m_ctype.h:358
srv_session_state
Enum for the state of the session.
Definition: srv_session.h:137
~Session_backup_and_attach()
Destructs the session state object.
Definition: srv_session.cc:485
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
uint16 peer_port
Definition: sql_class.h:1294
bool attach()
Attaches the session to the current physical thread.
Definition: srv_session.cc:854
Definition: srv_session.h:138
Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx)
Constructs a server session.
Definition: srv_session.cc:770
uint32 my_thread_id
Definition: my_thread_local.h:33
command
Definition: version_token.cc:278
static void check_for_stale_threads(const st_plugin_int *plugin)
Checks if a plugin has left threads and sessions.
Definition: srv_session.cc:689
int type
Definition: http_common.h:411
Definition: srv_session.h:303
int execute_command(enum enum_server_command command, const union COM_DATA *data, const CHARSET_INFO *client_cs, const struct st_command_service_cbs *command_callbacks, enum cs_text_or_binary text_or_binary, void *callbacks_context)
Executes a server command.
Definition: srv_session.cc:1056
Vio Lite.
Diagnostics_area da
Definition: srv_session.h:296
static bool is_valid(const Srv_session *session)
Checks if the session is valid.
Definition: srv_session.cc:758
static bool module_init()
Initializes the module.
Definition: srv_session.cc:712
void set_attached(const char *stack)
Sets session&#39;s state to attached.
Definition: srv_session.cc:1043
static bool init_thread(const void *plugin)
Initializes the current physical thread for use with this class.
Definition: srv_session.cc:612
A better implementation of the UNIX ctype(3) library.
static bool is_srv_session_thread()
Check if current physical thread was created to be used with this class.
Definition: srv_session.cc:1212
THD * get_thd()
Returns the internal THD object.
Definition: srv_session.h:223
Header file for the Command service.
Definition: service_command.h:317
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:48
void set_detached()
Changes the state of a session to detached.
Definition: srv_session.cc:1051
Protocol_callback protocol_error
Definition: srv_session.h:298
static unsigned int thread_count(const void *plugin_name)
Returns the number currently running threads initialized by this class.
Definition: srv_session.cc:1205
Header file for the Server session service.
bool attach_error
Definition: srv_session.h:327
st_err_protocol_ctx err_protocol_ctx
Definition: srv_session.h:297
uint16_t get_client_port() const
Returns the client port.
Definition: srv_session.h:238
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
Srv_session * old_session
Definition: srv_session.h:322