File: srv_session.h

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (409 lines) | stat: -rw-r--r-- 10,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*  Copyright (c) 2015, 2025, Oracle and/or its affiliates.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2.0,
    as published by the Free Software Foundation.

    This program is designed to work with certain software (including
    but not limited to OpenSSL) that is licensed under separate terms,
    as designated in a particular file or component or in included license
    documentation.  The authors of MySQL hereby grant you an additional
    permission to link the program and your derivative works with the
    separately licensed software that they have either included with
    the program or referenced in the documentation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License, version 2.0, for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef SRV_SESSION_H
#define SRV_SESSION_H

#include <stdint.h>

#include "lex_string.h"
#include "m_ctype.h"
#include "my_command.h"
#include "my_psi_config.h"
#include "my_thread_local.h"
#include "mysql/components/services/bits/psi_statement_bits.h"
#include "mysql/service_command.h"
#include "mysql/service_srv_session.h"
#include "sql/protocol_callback.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "violite.h" /* enum_vio_type */

struct st_plugin_int;

/**
  @file
  Header file for the Srv_session class that wraps THD, DA in one bundle
  for easy use of internal APIs.
  Srv_session also provides means for physical thread initialization and
  respective deinitialization.
*/

#ifdef HAVE_PSI_STATEMENT_INTERFACE
extern PSI_statement_info stmt_info_new_packet;
#endif

class Srv_session {
 public:
  /**
    Initializes the module.


    This method has to be called at server startup.

    @return
     false  success
     true   failure
  */
  static bool module_init();

  /**
    Deinitializes the module


    This method has to be called at server shutdown.

    @return
      false  success
      true   failure
  */
  static bool module_deinit();

  /**
    Initializes the current physical thread for use with this class.

    @param plugin Pointer to the plugin structure, passed to the plugin over
                  the plugin init function.

    @return
      false  success
      true   failure
  */
  static bool init_thread(const void *plugin);

  /**
    Deinitializes the current physical thread for use with session service
  */
  static void deinit_thread();

  /**
    Checks if a plugin has left threads and sessions

    @param plugin  The plugin to be checked
  */
  static void check_for_stale_threads(const st_plugin_int *plugin);

  /**
    Checks if the session is valid.

    Checked is if session is NULL, or in the list of opened sessions. If the
    session is not in this list it was either closed or the address is invalid.

    @return
      true  valid
      false not valid
  */
  static bool is_valid(const Srv_session *session);

  /**
    Returns the number opened sessions in thread initialized by this class.
  */
  static unsigned int session_count();

  /**
    Returns the number currently running threads initialized by this class.
  */
  static unsigned int thread_count(const void *plugin_name);

  /**
    Check if current physical thread was created to be used with this class.
  */
  static bool is_srv_session_thread();

  /* Non-static members follow */

  /**
    Enum for the state of the session
  */
  enum srv_session_state {
    SRV_SESSION_CREATED,
    SRV_SESSION_OPENED,
    SRV_SESSION_ATTACHED,
    SRV_SESSION_DETACHED,
    /*
      Following are the states
      while using the existing
      THD with the session.
    */
    SRV_SESSION_ASSOCIATE,     /* session using the THD provided explicitly */
    SRV_SESSION_ASSOCIATED,    /* explicit THD, is installed */
    SRV_SESSION_DISASSOCIATED, /* Changes the state of a session
                                 to disassociate */
    SRV_SESSION_CLOSED
  };

  /**
    Constructs a server session. That means This session object owns the THD.

    @note May throw if it fails to construct server session.

    @param err_cb         Default completion callback
    @param err_cb_ctx     Plugin's context, opaque pointer that would
                          be provided to callbacks. Might be NULL.
  */
  Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx);

  /**
   Have a THD object and wish to associate the same to session object.
   Session object will use the thd. It won't own it.

   @param err_cb         Default completion callback
   @param err_cb_ctx     Plugin's context, opaque pointer that would
                          be provided to callbacks. Might be NULL.
   @param thd   A valid THD
  */
  Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx, THD *thd);

  ~Srv_session();

  /**
    Opens a server session

    @return
      session  on success
      NULL     on failure
  */
  bool open();

  /**
    Attaches the session to the current physical thread

    @returns
      false   success
      true    failure
  */
  bool attach();

  /**
    Detaches the session from current physical thread.

    @returns
      false success
      true  failure
  */
  bool detach();

  /**
    Closes the session

    @returns
      false Session successfully closed
      true  Session wasn't found or key doesn't match
  */
  bool close();

  /**
    Returns if the session is in attached state

    @returns
      false   Not attached
      true    Attached
  */
  inline bool is_attached() const { return m_state == SRV_SESSION_ATTACHED; }

  /**
    Executes a server command.

    @param command  Command to be executed
    @param data     Command's arguments
    @param client_cs  The charset for the string data input (COM_QUERY
                      for example)
    @param command_callbacks  Callbacks to be used by the server to encode data
                              and to communicate with the client (plugin) side.
    @param text_or_binary     See enum cs_text_or_binary
    @param callbacks_context  Context passed to the callbacks

    @returns
      1   error
      0   success
  */
  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);

  /**
    Returns the internal THD object
  */
  inline THD *get_thd() { return m_thd; }

  /**
    Returns the ID of a session.

    The value returned from THD::thread_id()
  */
  my_thread_id get_session_id() const { return m_thd->thread_id(); }

  /**
    Returns the client port.

    @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
    This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.
  */
  uint16_t get_client_port() const { return m_thd->peer_port; }

  /**
    Sets the client port.

    @note The client port in SHOW PROCESSLIST, INFORMATION_SCHEMA.PROCESSLIST.
    This port is NOT shown in PERFORMANCE_SCHEMA.THREADS.

    @param port  Port number
  */
  void set_client_port(uint16_t port);

  /**
    Returns the current database of a session.

    @note This call is not thread-safe. Don't invoke the method from a thread
          different than the one in which the invocation happens. This means
          that the call should NOT happen during run_command(). The value
          returned is valid until the next run_command() call, which may
          change it.
  */
  LEX_CSTRING get_current_database() const { return m_thd->db(); }

  /**
    Sets the connection type.

    @see enum_vio_type

    @note If NO_VIO_TYPE passed as type the call will fail.

    @return
      false success
      true  failure
  */
  bool set_connection_type(enum_vio_type type);

  struct st_err_protocol_ctx {
    st_err_protocol_ctx(srv_session_error_cb h, void *h_ctx)
        : handler(h), handler_context(h_ctx) {}

    srv_session_error_cb handler;
    void *handler_context;
  };

 private:
  Srv_session(srv_session_error_cb err_cb, void *err_cb_ctx,
              srv_session_state state, bool free_resources, THD *thd);

  /**
    Sets session's state to attached

    @param stack  New stack address
  */
  void set_attached(const char *stack);

  /**
    Changes the state of a session to detached
  */
  void set_detached();

  /**
    Changes the state of a session to associate
  */
  void set_associate();

  /**
    Changes the state of a session to disassociate
  */
  void set_disassociate();

  /**
    Check is the session state is associate.
    In other words, session is using the THD provided explicitly.

    @returns
      true   session state is associate
      false  Otherwise
  */
  bool is_associate() const;

  /**
    Check if the session state is associated.
    In other words, explicit thd which pointed by this, is installed

    @returns
      true   session state is associated
      false  Otherwise
  */
  bool is_associated() const;

  /**
    Installs the thd pointed by the session object as the current_thd

    @return
      false success
      true  failure
  */
  bool associate();

  /**
   Uninstall the thd pointed by the session object as the current_thd

   @return
     false success
     true  failure
  */
  bool disassociate();

  Diagnostics_area m_da;
  st_err_protocol_ctx m_err_protocol_ctx;
  Protocol_callback m_protocol_error;

  srv_session_state m_state;
  enum_vio_type m_vio_type;
  THD *m_thd;
  const bool m_free_resources;

  class Session_backup_and_attach {
   public:
    /**
      Constructs a session state object. Saves state then attaches a session.
      Uses RAII.

      @param sess Session to backup
      @param is_in_close_session Whether session needs to be closed.
    */
    Session_backup_and_attach(Srv_session *sess, bool is_in_close_session);

    /**
      Destructs the session state object. In other words it restores to
      previous state.
    */
    ~Session_backup_and_attach();

   private:
    Srv_session *session;
    Srv_session *old_session; /* used in srv_session threads */
    THD *backup_thd;
    bool in_close_session;

   public:
    bool attach_error;
  };
};

#endif /* SRV_SESSION_H */