MySQL  8.0.27
Source Code Documentation
srv_session.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
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  */
143  };
144 
145  /**
146  Constructs a server session
147 
148  @param err_cb Default completion callback
149  @param err_cb_ctx Plugin's context, opaque pointer that would
150  be provided to callbacks. Might be NULL.
151  */
152  Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx);
153 
154  /**
155  Opens a server session
156 
157  @return
158  session on success
159  NULL on failure
160  */
161  bool open();
162 
163  /**
164  Attaches the session to the current physical thread
165 
166  @returns
167  false success
168  true failure
169  */
170  bool attach();
171 
172  /**
173  Detaches the session from current physical thread.
174 
175  @returns
176  false success
177  true failure
178  */
179  bool detach();
180 
181  /**
182  Closes the session
183 
184  @returns
185  false Session successfully closed
186  true Session wasn't found or key doesn't match
187  */
188  bool close();
189 
190  /**
191  Returns if the session is in attached state
192 
193  @returns
194  false Not attached
195  true Attached
196  */
197  inline bool is_attached() const { return state == SRV_SESSION_ATTACHED; }
198 
199  /**
200  Executes a server command.
201 
202  @param command Command to be executed
203  @param data Command's arguments
204  @param client_cs The charset for the string data input (COM_QUERY
205  for example)
206  @param command_callbacks Callbacks to be used by the server to encode data
207  and to communicate with the client (plugin) side.
208  @param text_or_binary See enum cs_text_or_binary
209  @param callbacks_context Context passed to the callbacks
210 
211  @returns
212  1 error
213  0 success
214  */
216  const union COM_DATA *data, const CHARSET_INFO *client_cs,
217  const struct st_command_service_cbs *command_callbacks,
218  enum cs_text_or_binary text_or_binary,
219  void *callbacks_context);
220 
221  /**
222  Returns the internal THD object
223  */
224  inline THD *get_thd() { return &thd; }
225 
226  /**
227  Returns the ID of a session.
228 
229  The value returned from THD::thread_id()
230  */
231  my_thread_id get_session_id() const { return thd.thread_id(); }
232 
233  /**
234  Returns the client port.
235 
236  @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
237  This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
238  */
239  uint16_t get_client_port() const { return thd.peer_port; }
240 
241  /**
242  Sets the client port.
243 
244  @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
245  This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
246 
247  @param port Port number
248  */
249  void set_client_port(uint16_t port);
250 
251  /**
252  Returns the current database of a session.
253 
254  @note This call is not thread-safe. Don't invoke the method from a thread
255  different than the one in which the invocation happens. This means
256  that the call should NOT happen during run_command(). The value
257  returned is valid until the next run_command() call, which may
258  change it.
259  */
260  LEX_CSTRING get_current_database() const { return thd.db(); }
261 
262  /**
263  Sets the connection type.
264 
265  @see enum_vio_type
266 
267  @note If NO_VIO_TYPE passed as type the call will fail.
268 
269  @return
270  false success
271  true failure
272  */
274 
277  : handler(h), handler_context(h_ctx) {}
278 
281  };
282 
283  private:
284  /**
285  Sets session's state to attached
286 
287  @param stack New stack address
288  */
289  void set_attached(const char *stack);
290 
291  /**
292  Changes the state of a session to detached
293  */
294  void set_detached();
295 
300 
303 
305  public:
306  /**
307  Constructs a session state object. Saves state then attaches a session.
308  Uses RAII.
309 
310  @param sess Session to backup
311  @param is_in_close_session Wheather session needs to be closed.
312  */
313  Session_backup_and_attach(Srv_session *sess, bool is_in_close_session);
314 
315  /**
316  Destructs the session state object. In other words it restores to
317  previous state.
318  */
320 
321  private:
323  Srv_session *old_session; /* used in srv_session threads */
326 
327  public:
329  };
330 };
331 
332 #endif /* SRV_SESSION_H */
Stores status of the currently executed statement.
Definition: sql_error.h:265
Definition: protocol_callback.h:51
Definition: srv_session.h:304
THD * backup_thd
Definition: srv_session.h:324
Session_backup_and_attach(Srv_session *sess, bool is_in_close_session)
Constructs a session state object.
Definition: srv_session.cc:456
~Session_backup_and_attach()
Destructs the session state object.
Definition: srv_session.cc:484
bool in_close_session
Definition: srv_session.h:325
Srv_session * session
Definition: srv_session.h:322
bool attach_error
Definition: srv_session.h:328
Srv_session * old_session
Definition: srv_session.h:323
Definition: srv_session.h:55
srv_session_state
Enum for the state of the session.
Definition: srv_session.h:137
@ SRV_SESSION_CLOSED
Definition: srv_session.h:142
@ SRV_SESSION_OPENED
Definition: srv_session.h:139
@ SRV_SESSION_CREATED
Definition: srv_session.h:138
@ SRV_SESSION_ATTACHED
Definition: srv_session.h:140
@ SRV_SESSION_DETACHED
Definition: srv_session.h:141
static bool module_deinit()
Deinitializes the module.
Definition: srv_session.cc:734
void set_detached()
Changes the state of a session to detached.
Definition: srv_session.cc:1063
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:1068
bool detach()
Detaches the session from current physical thread.
Definition: srv_session.cc:937
void set_client_port(uint16_t port)
Sets the client port.
Definition: srv_session.cc:1161
Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx)
Constructs a server session.
Definition: srv_session.cc:781
bool close()
Closes the session.
Definition: srv_session.cc:982
Diagnostics_area da
Definition: srv_session.h:297
bool is_attached() const
Returns if the session is in attached state.
Definition: srv_session.h:197
enum_vio_type vio_type
Definition: srv_session.h:302
bool attach()
Attaches the session to the current physical thread.
Definition: srv_session.cc:862
static void check_for_stale_threads(const st_plugin_int *plugin)
Checks if a plugin has left threads and sessions.
Definition: srv_session.cc:691
static unsigned int thread_count(const void *plugin_name)
Returns the number currently running threads initialized by this class.
Definition: srv_session.cc:1176
LEX_CSTRING get_current_database() const
Returns the current database of a session.
Definition: srv_session.h:260
uint16_t get_client_port() const
Returns the client port.
Definition: srv_session.h:239
bool set_connection_type(enum_vio_type type)
Sets the connection type.
Definition: srv_session.cc:1136
static bool init_thread(const void *plugin)
Initializes the current physical thread for use with this class.
Definition: srv_session.cc:614
bool open()
Opens a server session.
Definition: srv_session.cc:799
my_thread_id get_session_id() const
Returns the ID of a session.
Definition: srv_session.h:231
Protocol_callback protocol_error
Definition: srv_session.h:299
static bool module_init()
Initializes the module.
Definition: srv_session.cc:714
static bool is_valid(const Srv_session *session)
Checks if the session is valid.
Definition: srv_session.cc:760
void set_attached(const char *stack)
Sets session's state to attached.
Definition: srv_session.cc:1055
st_err_protocol_ctx err_protocol_ctx
Definition: srv_session.h:298
static unsigned int session_count()
Returns the number opened sessions in thread initialized by this class.
Definition: srv_session.cc:1171
static bool is_srv_session_thread()
Check if current physical thread was created to be used with this class.
Definition: srv_session.cc:1183
srv_session_state state
Definition: srv_session.h:301
static void deinit_thread()
Deinitializes the current physical thread for use with session service.
Definition: srv_session.cc:669
THD thd
Definition: srv_session.h:296
THD * get_thd()
Returns the internal THD object.
Definition: srv_session.h:224
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
my_thread_id thread_id() const
Definition: sql_class.h:2253
const LEX_CSTRING & db() const
Definition: sql_class.h:3662
uint16 peer_port
Definition: sql_class.h:1389
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4131
A better implementation of the UNIX ctype(3) library.
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:47
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
uint32 my_thread_id
Definition: my_thread_local.h:33
Interface of the Protocol_callback class, which is used by the Command service as proxy protocol.
Performance schema instrumentation interface.
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
required string type
Definition: replication_group_member_actions.proto:33
Header file for the Command service.
cs_text_or_binary
Definition: service_command.h:388
Header file for the Server session service.
void(* srv_session_error_cb)(void *ctx, unsigned int sql_errno, const char *err_msg)
Definition: service_srv_session_bits.h:42
LEX_CSTRING * plugin_name(st_plugin_int **ref)
Definition: sql_plugin_ref.h:94
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:49
Definition: m_ctype.h:354
Definition: mysql_lex_string.h:39
Statement instrument information.
Definition: psi_statement_bits.h:110
Definition: srv_session.h:275
srv_session_error_cb handler
Definition: srv_session.h:279
st_err_protocol_ctx(srv_session_error_cb h, void *h_ctx)
Definition: srv_session.h:276
void * handler_context
Definition: srv_session.h:280
Definition: service_command.h:327
Definition: sql_plugin_ref.h:44
task_env * stack
Definition: task.cc:875
Definition: com_data.h:111
command
Definition: version_token.cc:279
Vio Lite.
enum_vio_type
Definition: violite.h:77