MySQL 8.0.39
Source Code Documentation
srv_session.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
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#ifndef SRV_SESSION_H
25#define SRV_SESSION_H
26
27#include <stdint.h>
28
29#include "lex_string.h"
30#include "m_ctype.h"
31#include "my_command.h"
32#include "my_psi_config.h"
33#include "my_thread_local.h"
38#include "sql/sql_class.h"
39#include "sql/sql_error.h"
40#include "violite.h" /* enum_vio_type */
41
42struct st_plugin_int;
43
44/**
45 @file
46 Header file for the Srv_session class that wraps THD, DA in one bundle
47 for easy use of internal APIs.
48 Srv_session also provides means for physical thread initialization and
49 respective deinitialization.
50*/
51
52#ifdef HAVE_PSI_STATEMENT_INTERFACE
54#endif
55
57 public:
58 /**
59 Initializes the module.
60
61
62 This method has to be called at server startup.
63
64 @return
65 false success
66 true failure
67 */
68 static bool module_init();
69
70 /**
71 Deinitializes the module
72
73
74 This method has to be called at server shutdown.
75
76 @return
77 false success
78 true failure
79 */
80 static bool module_deinit();
81
82 /**
83 Initializes the current physical thread for use with this class.
84
85 @param plugin Pointer to the plugin structure, passed to the plugin over
86 the plugin init function.
87
88 @return
89 false success
90 true failure
91 */
92 static bool init_thread(const void *plugin);
93
94 /**
95 Deinitializes the current physical thread for use with session service
96 */
97 static void deinit_thread();
98
99 /**
100 Checks if a plugin has left threads and sessions
101
102 @param plugin The plugin to be checked
103 */
104 static void check_for_stale_threads(const st_plugin_int *plugin);
105
106 /**
107 Checks if the session is valid.
108
109 Checked is if session is NULL, or in the list of opened sessions. If the
110 session is not in this list it was either closed or the address is invalid.
111
112 @return
113 true valid
114 false not valid
115 */
116 static bool is_valid(const Srv_session *session);
117
118 /**
119 Returns the number opened sessions in thread initialized by this class.
120 */
121 static unsigned int session_count();
122
123 /**
124 Returns the number currently running threads initialized by this class.
125 */
126 static unsigned int thread_count(const void *plugin_name);
127
128 /**
129 Check if current physical thread was created to be used with this class.
130 */
131 static bool is_srv_session_thread();
132
133 /* Non-static members follow */
134
135 /**
136 Enum for the state of the session
137 */
143 /*
144 Following are the states
145 while using the existing
146 THD with the session.
147 */
148 SRV_SESSION_ASSOCIATE, /* session using the THD provided explicitly */
149 SRV_SESSION_ASSOCIATED, /* explicit THD, is installed */
150 SRV_SESSION_DISASSOCIATED, /* Changes the state of a session
151 to disassociate */
153 };
154
155 /**
156 Constructs a server session. That means This session object owns the THD.
157
158 @note May throw if it fails to construct server session.
159
160 @param err_cb Default completion callback
161 @param err_cb_ctx Plugin's context, opaque pointer that would
162 be provided to callbacks. Might be NULL.
163 */
164 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx);
165
166 /**
167 Have a THD object and wish to associate the same to session object.
168 Session object will use the thd. It won't own it.
169
170 @param err_cb Default completion callback
171 @param err_cb_ctx Plugin's context, opaque pointer that would
172 be provided to callbacks. Might be NULL.
173 @param thd A valid THD
174 */
175 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx, THD *thd);
176
177 ~Srv_session();
178
179 /**
180 Opens a server session
181
182 @return
183 session on success
184 NULL on failure
185 */
186 bool open();
187
188 /**
189 Attaches the session to the current physical thread
190
191 @returns
192 false success
193 true failure
194 */
195 bool attach();
196
197 /**
198 Detaches the session from current physical thread.
199
200 @returns
201 false success
202 true failure
203 */
204 bool detach();
205
206 /**
207 Closes the session
208
209 @returns
210 false Session successfully closed
211 true Session wasn't found or key doesn't match
212 */
213 bool close();
214
215 /**
216 Returns if the session is in attached state
217
218 @returns
219 false Not attached
220 true Attached
221 */
222 inline bool is_attached() const { return m_state == SRV_SESSION_ATTACHED; }
223
224 /**
225 Executes a server command.
226
227 @param command Command to be executed
228 @param data Command's arguments
229 @param client_cs The charset for the string data input (COM_QUERY
230 for example)
231 @param command_callbacks Callbacks to be used by the server to encode data
232 and to communicate with the client (plugin) side.
233 @param text_or_binary See enum cs_text_or_binary
234 @param callbacks_context Context passed to the callbacks
235
236 @returns
237 1 error
238 0 success
239 */
241 const union COM_DATA *data, const CHARSET_INFO *client_cs,
242 const struct st_command_service_cbs *command_callbacks,
243 enum cs_text_or_binary text_or_binary,
244 void *callbacks_context);
245
246 /**
247 Returns the internal THD object
248 */
249 inline THD *get_thd() { return m_thd; }
250
251 /**
252 Returns the ID of a session.
253
254 The value returned from THD::thread_id()
255 */
257
258 /**
259 Returns the client port.
260
261 @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
262 This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
263 */
264 uint16_t get_client_port() const { return m_thd->peer_port; }
265
266 /**
267 Sets the client port.
268
269 @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
270 This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
271
272 @param port Port number
273 */
274 void set_client_port(uint16_t port);
275
276 /**
277 Returns the current database of a session.
278
279 @note This call is not thread-safe. Don't invoke the method from a thread
280 different than the one in which the invocation happens. This means
281 that the call should NOT happen during run_command(). The value
282 returned is valid until the next run_command() call, which may
283 change it.
284 */
286
287 /**
288 Sets the connection type.
289
290 @see enum_vio_type
291
292 @note If NO_VIO_TYPE passed as type the call will fail.
293
294 @return
295 false success
296 true failure
297 */
299
302 : handler(h), handler_context(h_ctx) {}
303
306 };
307
308 private:
309 Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx,
310 srv_session_state state, bool free_resources, THD *thd);
311
312 /**
313 Sets session's state to attached
314
315 @param stack New stack address
316 */
317 void set_attached(const char *stack);
318
319 /**
320 Changes the state of a session to detached
321 */
322 void set_detached();
323
324 /**
325 Changes the state of a session to associate
326 */
327 void set_associate();
328
329 /**
330 Changes the state of a session to disassociate
331 */
332 void set_disassociate();
333
334 /**
335 Check is the session state is associate.
336 In other words, session is using the THD provided explicitly.
337
338 @returns
339 true session state is associate
340 false Otherwise
341 */
342 bool is_associate() const;
343
344 /**
345 Check if the session state is associated.
346 In other words, explicit thd which pointed by this, is installed
347
348 @returns
349 true session state is associated
350 false Otherwise
351 */
352 bool is_associated() const;
353
354 /**
355 Installs the thd pointed by the session object as the current_thd
356
357 @return
358 false success
359 true failure
360 */
361 bool associate();
362
363 /**
364 Uninstall the thd pointed by the session object as the current_thd
365
366 @return
367 false success
368 true failure
369 */
370 bool disassociate();
371
375
380
382 public:
383 /**
384 Constructs a session state object. Saves state then attaches a session.
385 Uses RAII.
386
387 @param sess Session to backup
388 @param is_in_close_session Whether session needs to be closed.
389 */
390 Session_backup_and_attach(Srv_session *sess, bool is_in_close_session);
391
392 /**
393 Destructs the session state object. In other words it restores to
394 previous state.
395 */
397
398 private:
400 Srv_session *old_session; /* used in srv_session threads */
403
404 public:
406 };
407};
408
409#endif /* SRV_SESSION_H */
Stores status of the currently executed statement.
Definition: sql_error.h:269
Definition: protocol_callback.h:52
Definition: srv_session.h:381
THD * backup_thd
Definition: srv_session.h:401
Session_backup_and_attach(Srv_session *sess, bool is_in_close_session)
Constructs a session state object.
Definition: srv_session.cc:459
~Session_backup_and_attach()
Destructs the session state object.
Definition: srv_session.cc:493
bool in_close_session
Definition: srv_session.h:402
Srv_session * session
Definition: srv_session.h:399
bool attach_error
Definition: srv_session.h:405
Srv_session * old_session
Definition: srv_session.h:400
Definition: srv_session.h:56
~Srv_session()
Definition: srv_session.cc:832
srv_session_state
Enum for the state of the session.
Definition: srv_session.h:138
@ SRV_SESSION_CLOSED
Definition: srv_session.h:152
@ SRV_SESSION_OPENED
Definition: srv_session.h:140
@ SRV_SESSION_ASSOCIATE
Definition: srv_session.h:148
@ SRV_SESSION_CREATED
Definition: srv_session.h:139
@ SRV_SESSION_DISASSOCIATED
Definition: srv_session.h:150
@ SRV_SESSION_ATTACHED
Definition: srv_session.h:141
@ SRV_SESSION_ASSOCIATED
Definition: srv_session.h:149
@ SRV_SESSION_DETACHED
Definition: srv_session.h:142
static bool module_deinit()
Deinitializes the module.
Definition: srv_session.cc:753
void set_detached()
Changes the state of a session to detached.
Definition: srv_session.cc:1114
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:1119
bool detach()
Detaches the session from current physical thread.
Definition: srv_session.cc:987
void set_client_port(uint16_t port)
Sets the client port.
Definition: srv_session.cc:1263
Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx)
Constructs a server session.
Definition: srv_session.cc:799
enum_vio_type m_vio_type
Definition: srv_session.h:377
bool close()
Closes the session.
Definition: srv_session.cc:1033
bool is_associated() const
Check if the session state is associated.
Definition: srv_session.cc:1217
bool is_attached() const
Returns if the session is in attached state.
Definition: srv_session.h:222
bool disassociate()
Uninstall the thd pointed by the session object as the current_thd.
Definition: srv_session.cc:1232
bool attach()
Attaches the session to the current physical thread.
Definition: srv_session.cc:913
static void check_for_stale_threads(const st_plugin_int *plugin)
Checks if a plugin has left threads and sessions.
Definition: srv_session.cc:710
static unsigned int thread_count(const void *plugin_name)
Returns the number currently running threads initialized by this class.
Definition: srv_session.cc:1278
LEX_CSTRING get_current_database() const
Returns the current database of a session.
Definition: srv_session.h:285
uint16_t get_client_port() const
Returns the client port.
Definition: srv_session.h:264
THD * get_thd()
Returns the internal THD object.
Definition: srv_session.h:249
bool is_associate() const
Check is the session state is associate.
Definition: srv_session.cc:1213
srv_session_state m_state
Definition: srv_session.h:376
bool set_connection_type(enum_vio_type type)
Sets the connection type.
Definition: srv_session.cc:1199
static bool init_thread(const void *plugin)
Initializes the current physical thread for use with this class.
Definition: srv_session.cc:626
bool open()
Opens a server session.
Definition: srv_session.cc:850
my_thread_id get_session_id() const
Returns the ID of a session.
Definition: srv_session.h:256
static bool module_init()
Initializes the module.
Definition: srv_session.cc:733
const bool m_free_resources
Definition: srv_session.h:379
Diagnostics_area m_da
Definition: srv_session.h:372
static bool is_valid(const Srv_session *session)
Checks if the session is valid.
Definition: srv_session.cc:779
void set_attached(const char *stack)
Sets session's state to attached.
Definition: srv_session.cc:1106
Protocol_callback m_protocol_error
Definition: srv_session.h:374
void set_associate()
Changes the state of a session to associate.
Definition: srv_session.cc:1209
THD * m_thd
Definition: srv_session.h:378
static unsigned int session_count()
Returns the number opened sessions in thread initialized by this class.
Definition: srv_session.cc:1273
static bool is_srv_session_thread()
Check if current physical thread was created to be used with this class.
Definition: srv_session.cc:1285
bool associate()
Installs the thd pointed by the session object as the current_thd.
Definition: srv_session.cc:1221
st_err_protocol_ctx m_err_protocol_ctx
Definition: srv_session.h:373
void set_disassociate()
Changes the state of a session to disassociate.
Definition: srv_session.cc:1211
static void deinit_thread()
Deinitializes the current physical thread for use with session service.
Definition: srv_session.cc:685
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
my_thread_id thread_id() const
Definition: sql_class.h:2462
uint16 peer_port
Definition: sql_class.h:1561
const LEX_CSTRING & db() const
Definition: sql_class.h:3864
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4414
A better implementation of the UNIX ctype(3) library.
enum_server_command
A list of all MySQL protocol commands.
Definition: my_command.h:48
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
uint32 my_thread_id
Definition: my_thread_local.h:34
static void free_resources()
Definition: mysql_secure_installation.cc:126
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:33
required string type
Definition: replication_group_member_actions.proto:34
Header file for the Command service.
cs_text_or_binary
Definition: service_command.h:389
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:43
LEX_CSTRING * plugin_name(st_plugin_int **ref)
Definition: sql_plugin_ref.h:95
PSI_statement_info stmt_info_new_packet
Definition: init_net_server_extension.cc:50
Definition: m_ctype.h:385
Definition: mysql_lex_string.h:40
Statement instrument information.
Definition: psi_statement_bits.h:133
Definition: srv_session.h:300
srv_session_error_cb handler
Definition: srv_session.h:304
st_err_protocol_ctx(srv_session_error_cb h, void *h_ctx)
Definition: srv_session.h:301
void * handler_context
Definition: srv_session.h:305
Definition: service_command.h:328
Definition: sql_plugin_ref.h:45
task_env * stack
Definition: task.cc:876
Definition: com_data.h:112
command
Definition: version_token.cc:280
Vio Lite.
enum_vio_type
Definition: violite.h:79