MySQL 8.0.32
Source Code Documentation
srv_session.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2022, 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"
37#include "sql/sql_class.h"
38#include "sql/sql_error.h"
39#include "violite.h" /* enum_vio_type */
40
41struct 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
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 Following are the states
144 while using the existing
145 THD with the session.
146 */
147 SRV_SESSION_ASSOCIATE, /* session using the THD provided explicitly */
148 SRV_SESSION_ASSOCIATED, /* explicit THD, is installed */
149 SRV_SESSION_DISASSOCIATED, /* Changes the state of a session
150 to disassociate */
152 };
153
154 /**
155 Constructs a server session. That means This session object owns the THD.
156
157 @note May throw if it fails to construct server session.
158
159 @param err_cb Default completion callback
160 @param err_cb_ctx Plugin's context, opaque pointer that would
161 be provided to callbacks. Might be NULL.
162 */
163 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx);
164
165 /**
166 Have a THD object and wish to associate the same to session object.
167 Session object will use the thd. It won't own it.
168
169 @param err_cb Default completion callback
170 @param err_cb_ctx Plugin's context, opaque pointer that would
171 be provided to callbacks. Might be NULL.
172 @param thd A valid THD
173 */
174 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx, THD *thd);
175
176 ~Srv_session();
177
178 /**
179 Opens a server session
180
181 @return
182 session on success
183 NULL on failure
184 */
185 bool open();
186
187 /**
188 Attaches the session to the current physical thread
189
190 @returns
191 false success
192 true failure
193 */
194 bool attach();
195
196 /**
197 Detaches the session from current physical thread.
198
199 @returns
200 false success
201 true failure
202 */
203 bool detach();
204
205 /**
206 Closes the session
207
208 @returns
209 false Session successfully closed
210 true Session wasn't found or key doesn't match
211 */
212 bool close();
213
214 /**
215 Returns if the session is in attached state
216
217 @returns
218 false Not attached
219 true Attached
220 */
221 inline bool is_attached() const { return m_state == SRV_SESSION_ATTACHED; }
222
223 /**
224 Executes a server command.
225
226 @param command Command to be executed
227 @param data Command's arguments
228 @param client_cs The charset for the string data input (COM_QUERY
229 for example)
230 @param command_callbacks Callbacks to be used by the server to encode data
231 and to communicate with the client (plugin) side.
232 @param text_or_binary See enum cs_text_or_binary
233 @param callbacks_context Context passed to the callbacks
234
235 @returns
236 1 error
237 0 success
238 */
240 const union COM_DATA *data, const CHARSET_INFO *client_cs,
241 const struct st_command_service_cbs *command_callbacks,
242 enum cs_text_or_binary text_or_binary,
243 void *callbacks_context);
244
245 /**
246 Returns the internal THD object
247 */
248 inline THD *get_thd() { return m_thd; }
249
250 /**
251 Returns the ID of a session.
252
253 The value returned from THD::thread_id()
254 */
256
257 /**
258 Returns the client port.
259
260 @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
261 This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
262 */
263 uint16_t get_client_port() const { return m_thd->peer_port; }
264
265 /**
266 Sets the client port.
267
268 @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
269 This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
270
271 @param port Port number
272 */
273 void set_client_port(uint16_t port);
274
275 /**
276 Returns the current database of a session.
277
278 @note This call is not thread-safe. Don't invoke the method from a thread
279 different than the one in which the invocation happens. This means
280 that the call should NOT happen during run_command(). The value
281 returned is valid until the next run_command() call, which may
282 change it.
283 */
285
286 /**
287 Sets the connection type.
288
289 @see enum_vio_type
290
291 @note If NO_VIO_TYPE passed as type the call will fail.
292
293 @return
294 false success
295 true failure
296 */
298
301 : handler(h), handler_context(h_ctx) {}
302
305 };
306
307 private:
308 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx,
309 srv_session_state state, bool free_resources, THD *thd);
310
311 /**
312 Sets session's state to attached
313
314 @param stack New stack address
315 */
316 void set_attached(const char *stack);
317
318 /**
319 Changes the state of a session to detached
320 */
321 void set_detached();
322
323 /**
324 Changes the state of a session to associate
325 */
326 void set_associate();
327
328 /**
329 Changes the state of a session to disassociate
330 */
331 void set_disassociate();
332
333 /**
334 Check is the session state is associate.
335 In other words, session is using the THD provided explicitly.
336
337 @returns
338 true session state is associate
339 false Otherwise
340 */
341 bool is_associate() const;
342
343 /**
344 Check if the session state is associated.
345 In other words, explicit thd which pointed by this, is installed
346
347 @returns
348 true session state is associated
349 false Otherwise
350 */
351 bool is_associated() const;
352
353 /**
354 Installs the thd pointed by the session object as the current_thd
355
356 @return
357 false success
358 true failure
359 */
360 bool associate();
361
362 /**
363 Uninstall the thd pointed by the session object as the current_thd
364
365 @return
366 false success
367 true failure
368 */
369 bool disassociate();
370
374
379
381 public:
382 /**
383 Constructs a session state object. Saves state then attaches a session.
384 Uses RAII.
385
386 @param sess Session to backup
387 @param is_in_close_session Whether session needs to be closed.
388 */
389 Session_backup_and_attach(Srv_session *sess, bool is_in_close_session);
390
391 /**
392 Destructs the session state object. In other words it restores to
393 previous state.
394 */
396
397 private:
399 Srv_session *old_session; /* used in srv_session threads */
402
403 public:
405 };
406};
407
408#endif /* SRV_SESSION_H */
Stores status of the currently executed statement.
Definition: sql_error.h:268
Definition: protocol_callback.h:51
Definition: srv_session.h:380
THD * backup_thd
Definition: srv_session.h:400
Session_backup_and_attach(Srv_session *sess, bool is_in_close_session)
Constructs a session state object.
Definition: srv_session.cc:458
~Session_backup_and_attach()
Destructs the session state object.
Definition: srv_session.cc:492
bool in_close_session
Definition: srv_session.h:401
Srv_session * session
Definition: srv_session.h:398
bool attach_error
Definition: srv_session.h:404
Srv_session * old_session
Definition: srv_session.h:399
Definition: srv_session.h:55
~Srv_session()
Definition: srv_session.cc:824
srv_session_state
Enum for the state of the session.
Definition: srv_session.h:137
@ SRV_SESSION_CLOSED
Definition: srv_session.h:151
@ SRV_SESSION_OPENED
Definition: srv_session.h:139
@ SRV_SESSION_ASSOCIATE
Definition: srv_session.h:147
@ SRV_SESSION_CREATED
Definition: srv_session.h:138
@ SRV_SESSION_DISASSOCIATED
Definition: srv_session.h:149
@ SRV_SESSION_ATTACHED
Definition: srv_session.h:140
@ SRV_SESSION_ASSOCIATED
Definition: srv_session.h:148
@ SRV_SESSION_DETACHED
Definition: srv_session.h:141
static bool module_deinit()
Deinitializes the module.
Definition: srv_session.cc:745
void set_detached()
Changes the state of a session to detached.
Definition: srv_session.cc:1106
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:1111
bool detach()
Detaches the session from current physical thread.
Definition: srv_session.cc:979
void set_client_port(uint16_t port)
Sets the client port.
Definition: srv_session.cc:1254
Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx)
Constructs a server session.
Definition: srv_session.cc:791
enum_vio_type m_vio_type
Definition: srv_session.h:376
bool close()
Closes the session.
Definition: srv_session.cc:1025
bool is_associated() const
Check if the session state is associated.
Definition: srv_session.cc:1208
bool is_attached() const
Returns if the session is in attached state.
Definition: srv_session.h:221
bool disassociate()
Uninstall the thd pointed by the session object as the current_thd.
Definition: srv_session.cc:1223
bool attach()
Attaches the session to the current physical thread.
Definition: srv_session.cc:905
static void check_for_stale_threads(const st_plugin_int *plugin)
Checks if a plugin has left threads and sessions.
Definition: srv_session.cc:702
static unsigned int thread_count(const void *plugin_name)
Returns the number currently running threads initialized by this class.
Definition: srv_session.cc:1269
LEX_CSTRING get_current_database() const
Returns the current database of a session.
Definition: srv_session.h:284
uint16_t get_client_port() const
Returns the client port.
Definition: srv_session.h:263
THD * get_thd()
Returns the internal THD object.
Definition: srv_session.h:248
bool is_associate() const
Check is the session state is associate.
Definition: srv_session.cc:1204
srv_session_state m_state
Definition: srv_session.h:375
bool set_connection_type(enum_vio_type type)
Sets the connection type.
Definition: srv_session.cc:1190
static bool init_thread(const void *plugin)
Initializes the current physical thread for use with this class.
Definition: srv_session.cc:625
bool open()
Opens a server session.
Definition: srv_session.cc:842
my_thread_id get_session_id() const
Returns the ID of a session.
Definition: srv_session.h:255
static bool module_init()
Initializes the module.
Definition: srv_session.cc:725
const bool m_free_resources
Definition: srv_session.h:378
Diagnostics_area m_da
Definition: srv_session.h:371
static bool is_valid(const Srv_session *session)
Checks if the session is valid.
Definition: srv_session.cc:771
void set_attached(const char *stack)
Sets session's state to attached.
Definition: srv_session.cc:1098
Protocol_callback m_protocol_error
Definition: srv_session.h:373
void set_associate()
Changes the state of a session to associate.
Definition: srv_session.cc:1200
THD * m_thd
Definition: srv_session.h:377
static unsigned int session_count()
Returns the number opened sessions in thread initialized by this class.
Definition: srv_session.cc:1264
static bool is_srv_session_thread()
Check if current physical thread was created to be used with this class.
Definition: srv_session.cc:1276
bool associate()
Installs the thd pointed by the session object as the current_thd.
Definition: srv_session.cc:1212
st_err_protocol_ctx m_err_protocol_ctx
Definition: srv_session.h:372
void set_disassociate()
Changes the state of a session to disassociate.
Definition: srv_session.cc:1202
static void deinit_thread()
Deinitializes the current physical thread for use with session service.
Definition: srv_session.cc:680
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
my_thread_id thread_id() const
Definition: sql_class.h:2466
uint16 peer_port
Definition: sql_class.h:1570
const LEX_CSTRING & db() const
Definition: sql_class.h:3874
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4354
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
static void free_resources()
Definition: mysql_secure_installation.cc:125
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:382
Definition: mysql_lex_string.h:39
Statement instrument information.
Definition: psi_statement_bits.h:124
Definition: srv_session.h:299
srv_session_error_cb handler
Definition: srv_session.h:303
st_err_protocol_ctx(srv_session_error_cb h, void *h_ctx)
Definition: srv_session.h:300
void * handler_context
Definition: srv_session.h:304
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:78