MySQL 9.0.0
Source Code Documentation
session_tracker.h
Go to the documentation of this file.
1#ifndef SESSION_TRACKER_INCLUDED
2#define SESSION_TRACKER_INCLUDED
3
4/* Copyright (c) 2014, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <stddef.h>
28#include <sys/types.h>
29
30#include "lex_string.h"
31#include "thr_lock.h" // thr_lock_type
32
33class String;
34class THD;
35class set_var;
36
37struct CHARSET_INFO;
38
40 SESSION_SYSVARS_TRACKER, /* Session system variables */
41 CURRENT_SCHEMA_TRACKER, /* Current schema */
43 SESSION_GTIDS_TRACKER, /* Tracks GTIDs */
44 TRANSACTION_INFO_TRACKER, /* Transaction state */
45 /*
46 There should be a one-to-one mapping between this enum members and the
47 members from enum enum_session_state_type defined in mysql_com.h.
48 TRANSACTION_INFO_TRACKER maps to 2 types of tracker which are:
49 SESSION_TRACK_TRANSACTION_CHARACTERISTICS, SESSION_TRACK_TRANSACTION_STATE.
50 Thus introduced a dummy tracker type on server side to keep trackers in sync
51 between client and server.
52 */
54};
55
56#define SESSION_TRACKER_END TRACK_TRANSACTION_STATE
57
58#define TX_TRACKER_GET(a) \
59 Transaction_state_tracker *a = \
60 (Transaction_state_tracker *)thd->session_tracker.get_tracker( \
61 TRANSACTION_INFO_TRACKER)
62
63/**
64 State_tracker
65 -------------
66 An abstract class that defines the interface for any of the server's
67 'session state change tracker'. A tracker, however, is a sub- class of
68 this class which takes care of tracking the change in value of a part-
69 icular session state type and thus defines various methods listed in this
70 interface. The change information is later serialized and transmitted to
71 the client through protocol's OK packet.
72
73 Tracker system variables :-
74 A tracker is normally mapped to a system variable. So in order to enable,
75 disable or modify the sub-entities of a tracker, the user needs to modify
76 the respective system variable either through SET command or via command
77 line option. As required in system variable handling, this interface also
78 includes two functions to help in the verification of the supplied value
79 (ON_CHECK) and the updation (ON_UPDATE) of the tracker system variable,
80 namely - check() and update().
81*/
82
84 protected:
85 /** Is tracking enabled for a particular session state type ? */
87
88 /** Has the session state type changed ? */
90
91 public:
92 /** Constructor */
93 State_tracker() : m_enabled(false), m_changed(false) {}
94
95 /** Destructor */
96 virtual ~State_tracker() = default;
97
98 /** Getters */
99 bool is_enabled() const { return m_enabled; }
100
101 bool is_changed() const { return m_changed; }
102
103 /** Called in the constructor of THD*/
104 virtual bool enable(THD *thd) = 0;
105
106 /** To be invoked when the tracker's system variable is checked (ON_CHECK). */
107 virtual bool check(THD *thd, set_var *var) = 0;
108
109 /** To be invoked when the tracker's system variable is updated (ON_UPDATE).*/
110 virtual bool update(THD *thd) = 0;
111
112 /** Store changed data into the given buffer. */
113 virtual bool store(THD *thd, String &buf) = 0;
114
115 /** Mark the entity as changed. */
116 virtual void mark_as_changed(THD *thd, LEX_CSTRING name) = 0;
117
118 virtual void claim_memory_ownership(bool claim [[maybe_unused]]) {}
119};
120
121/**
122 Session_tracker
123 ---------------
124 This class holds an object each for all tracker classes and provides
125 methods necessary for systematic detection and generation of session
126 state change information.
127*/
128
130 private:
132
133 public:
135
137
138 /** Constructor */
139 Session_tracker() = default;
140
141 /** Destructor */
142 ~Session_tracker() = default;
143 /**
144 Initialize Session_tracker objects and enable them based on the
145 tracker_xxx variables' value that the session inherit from global
146 variables at the time of session initialization (see plugin_thdvar_init).
147 */
148 void init(const CHARSET_INFO *char_set);
149 void enable(THD *thd);
150 bool server_boot_verify(const CHARSET_INFO *char_set, LEX_STRING var_list);
151
152 /** Returns the pointer to the tracker object for the specified tracker. */
154
155 /** Checks if m_enabled flag is set for any of the tracker objects. */
156 bool enabled_any();
157
158 /** Checks if m_changed flag is set for any of the tracker objects. */
159 bool changed_any();
160
161 /**
162 Stores the session state change information of all changes session state
163 type entities into the specified buffer.
164 */
165 void store(THD *thd, String &main_buf);
166 void deinit() {
167 for (int i = 0; i <= SESSION_TRACKER_END; i++) delete m_trackers[i];
168 }
169
170 void claim_memory_ownership(bool claim);
171};
172
173/*
174 Session_state_change_tracker
175 ----------------------------
176 This is a boolean tracker class that will monitor any change that contributes
177 to a session state change.
178 Attributes that contribute to session state change include:
179 - Successful change to System variables
180 - User defined variables assignments
181 - temporary tables created, altered or deleted
182 - prepared statements added or removed
183 - change in current database
184*/
185
187 private:
188 void reset();
189
190 public:
192 bool enable(THD *thd) override;
193 bool check(THD *, set_var *) override { return false; }
194 bool update(THD *thd) override;
195 bool store(THD *, String &buf) override;
196 void mark_as_changed(THD *thd, LEX_CSTRING tracked_item_name) override;
197 bool is_state_changed();
198};
199
200/**
201 Transaction_state_tracker
202 ----------------------
203 This is a tracker class that enables & manages the tracking of
204 current transaction info for a particular connection.
205*/
206
207/**
208 Transaction state (no transaction, transaction active, work attached, etc.)
209*/
211 TX_EMPTY = 0, ///< "none of the below"
212 TX_EXPLICIT = 1, ///< an explicit transaction is active
213 TX_IMPLICIT = 2, ///< an implicit transaction is active
214 TX_READ_TRX = 4, ///< transactional reads were done
215 TX_READ_UNSAFE = 8, ///< non-transaction reads were done
216 TX_WRITE_TRX = 16, ///< transactional writes were done
217 TX_WRITE_UNSAFE = 32, ///< non-transactional writes were done
218 TX_STMT_UNSAFE = 64, ///< "unsafe" (non-deterministic like UUID()) stmts
219 TX_RESULT_SET = 128, ///< result-set was sent
220 TX_WITH_SNAPSHOT = 256, ///< WITH CONSISTENT SNAPSHOT was used
221 TX_LOCKED_TABLES = 512, ///< LOCK TABLES is active
222 TX_STMT_DML = 1024, ///< a DML statement (known before data is accessed)
223 TX_STMT_DDL = 2048 ///< a DDL statement
225
226/**
227 Transaction access mode
228*/
230 TX_READ_INHERIT = 0, ///< not explicitly set, inherit session.tx_read_only
231 TX_READ_ONLY = 1, ///< START TRANSACTION READ ONLY, or tx_read_only=1
232 TX_READ_WRITE = 2, ///< START TRANSACTION READ WRITE, or tx_read_only=0
233};
234
235/**
236 Transaction isolation level
237*/
239 TX_ISOL_INHERIT = 0, ///< not explicitly set, inherit session.tx_isolation
245
246/**
247 Transaction tracking level
248*/
250 TX_TRACK_NONE = 0, ///< do not send tracker items on transaction info
251 TX_TRACK_STATE = 1, ///< track transaction status
252 TX_TRACK_CHISTICS = 2 ///< track status and characteristics
254
256 public:
257 /** Constructor */
259 bool enable(THD *thd) override { return update(thd); }
260 bool check(THD *, set_var *) override { return false; }
261 bool update(THD *thd) override;
262 bool store(THD *thd, String &buf) override;
263 void mark_as_changed(THD *thd, LEX_CSTRING tracked_item_name) override;
264
265 /** Change transaction characteristics */
267 void set_isol_level(THD *thd, enum enum_tx_isol_level level);
268
269 /** Change transaction state */
270 void clear_trx_state(THD *thd, uint clear);
271 void add_trx_state(THD *thd, uint add);
272 void add_trx_state_from_thd(THD *thd);
273 void end_trx(THD *thd);
274
275 /** Helper function: turn table info into table access flag */
277
278 /** Get (possibly still incomplete) state */
279 uint get_trx_state() const { return tx_curr_state; }
280
281 private:
283 TX_CHG_NONE = 0, ///< no changes from previous stmt
284 TX_CHG_STATE = 1, ///< state has changed from previous stmt
285 TX_CHG_CHISTICS = 2 ///< characteristics have changed from previous stmt
286 };
287
288 /** any trackable changes caused by this statement? */
290
291 /** transaction state */
293
294 /** r/w or r/o set? session default? */
296
297 /** isolation level */
299
300 void reset();
301
302 inline void update_change_flags(THD *thd) {
303 tx_changed &= ~TX_CHG_STATE;
304 // Flag state changes other than "is DDL/DML"
308 : 0;
309 if (tx_changed != TX_CHG_NONE) mark_as_changed(thd, {});
310 }
311};
312
313#endif /* SESSION_TRACKER_INCLUDED */
Definition: session_tracker.h:186
bool update(THD *thd) override
Enable/disable the tracker based on @session_track_state_change value.
Definition: session_tracker.cc:1442
void reset()
Reset the m_changed flag for next statement.
Definition: session_tracker.cc:1502
Session_state_change_tracker()
Constructor.
Definition: session_tracker.cc:1416
bool enable(THD *thd) override
Initiate the value of m_enabled based on @session_track_state_change value.
Definition: session_tracker.cc:1429
bool store(THD *, String &buf) override
Store the 1byte boolean flag in the specified buffer.
Definition: session_tracker.cc:1457
bool is_state_changed()
find if there is a session state change
Definition: session_tracker.cc:1512
bool check(THD *, set_var *) override
To be invoked when the tracker's system variable is checked (ON_CHECK).
Definition: session_tracker.h:193
void mark_as_changed(THD *thd, LEX_CSTRING tracked_item_name) override
Mark the tracker as changed and associated session attributes accordingly.
Definition: session_tracker.cc:1484
Definition: session_tracker.h:129
void deinit()
Definition: session_tracker.h:166
bool changed_any()
Checks if m_changed flag is set for any of the tracker objects.
Definition: session_tracker.cc:1622
State_tracker * get_tracker(enum_session_tracker tracker) const
Returns the pointer to the tracker object for the specified tracker.
Definition: session_tracker.cc:1592
Session_tracker & operator=(Session_tracker const &)=delete
void claim_memory_ownership(bool claim)
Definition: session_tracker.cc:1550
bool enabled_any()
Checks if m_enabled flag is set for any of the tracker objects.
Definition: session_tracker.cc:1606
bool server_boot_verify(const CHARSET_INFO *char_set, LEX_STRING var_list)
Method called during the server startup to verify the contents of @session_track_system_variables.
Definition: session_tracker.cc:1574
void enable(THD *thd)
Enables the tracker objects.
Definition: session_tracker.cc:1560
Session_tracker(Session_tracker const &)=delete
void init(const CHARSET_INFO *char_set)
Initialize Session_tracker objects and enable them based on the tracker_xxx variables' value that the...
Definition: session_tracker.cc:1536
Session_tracker()=default
Constructor.
void store(THD *thd, String &main_buf)
Stores the session state change information of all changes session state type entities into the speci...
Definition: session_tracker.cc:1637
~Session_tracker()=default
Destructor.
State_tracker * m_trackers[SESSION_TRACKER_END+1]
Definition: session_tracker.h:131
Definition: session_tracker.h:83
bool m_changed
Has the session state type changed ?
Definition: session_tracker.h:89
virtual bool check(THD *thd, set_var *var)=0
To be invoked when the tracker's system variable is checked (ON_CHECK).
State_tracker()
Constructor.
Definition: session_tracker.h:93
virtual bool enable(THD *thd)=0
Called in the constructor of THD.
virtual bool update(THD *thd)=0
To be invoked when the tracker's system variable is updated (ON_UPDATE).
virtual void claim_memory_ownership(bool claim)
Definition: session_tracker.h:118
bool m_enabled
Is tracking enabled for a particular session state type ?
Definition: session_tracker.h:86
bool is_enabled() const
Getters.
Definition: session_tracker.h:99
bool is_changed() const
Definition: session_tracker.h:101
virtual void mark_as_changed(THD *thd, LEX_CSTRING name)=0
Mark the entity as changed.
virtual ~State_tracker()=default
Destructor.
virtual bool store(THD *thd, String &buf)=0
Store changed data into the given buffer.
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: session_tracker.h:255
uint get_trx_state() const
Get (possibly still incomplete) state.
Definition: session_tracker.h:279
bool check(THD *, set_var *) override
To be invoked when the tracker's system variable is checked (ON_CHECK).
Definition: session_tracker.h:260
Transaction_state_tracker()
Constructor.
Definition: session_tracker.cc:911
enum_tx_state calc_trx_state(thr_lock_type l, bool has_trx)
Helper function: turn table info into table access flag.
Definition: session_tracker.cc:1252
uint tx_reported_state
Definition: session_tracker.h:292
void add_trx_state(THD *thd, uint add)
Add flags pertaining to the current statement or transaction.
Definition: session_tracker.cc:1304
bool enable(THD *thd) override
Called in the constructor of THD.
Definition: session_tracker.h:259
void reset()
Reset the m_changed flag for next statement.
Definition: session_tracker.cc:1235
void end_trx(THD *thd)
Register the end of an (implicit or explicit) transaction.
Definition: session_tracker.cc:1270
enum enum_tx_isol_level tx_isol_level
isolation level
Definition: session_tracker.h:298
void set_read_flags(THD *thd, enum enum_tx_read_flags flags)
Change transaction characteristics.
Definition: session_tracker.cc:1385
enum enum_tx_read_flags tx_read_flags
r/w or r/o set? session default?
Definition: session_tracker.h:295
uint tx_changed
any trackable changes caused by this statement?
Definition: session_tracker.h:289
void clear_trx_state(THD *thd, uint clear)
Change transaction state.
Definition: session_tracker.cc:1289
void set_isol_level(THD *thd, enum enum_tx_isol_level level)
Set isolation level pertaining to the next transaction.
Definition: session_tracker.cc:1402
bool update(THD *thd) override
Enable/disable the tracker based on @session_track_transaction_info's value.
Definition: session_tracker.cc:929
uint tx_curr_state
transaction state
Definition: session_tracker.h:292
bool store(THD *thd, String &buf) override
Store the transaction state (and, optionally, characteristics) as length-encoded string in the specif...
Definition: session_tracker.cc:963
void mark_as_changed(THD *thd, LEX_CSTRING tracked_item_name) override
Mark the tracker as changed.
Definition: session_tracker.cc:1227
enum_tx_changed
Definition: session_tracker.h:282
@ TX_CHG_CHISTICS
characteristics have changed from previous stmt
Definition: session_tracker.h:285
@ TX_CHG_NONE
no changes from previous stmt
Definition: session_tracker.h:283
@ TX_CHG_STATE
state has changed from previous stmt
Definition: session_tracker.h:284
void add_trx_state_from_thd(THD *thd)
Add "unsafe statement" flag if applicable.
Definition: session_tracker.cc:1374
void update_change_flags(THD *thd)
Definition: session_tracker.h:302
set_var_base descendant for assignments to the system variables.
Definition: set_var.h:983
static int flags[50]
Definition: hp_test1.cc:40
Definition: buf0block_hint.cc:30
static mysql_service_status_t clear(reference_caching_channel channel) noexcept
Definition: component.cc:146
static mysql_service_status_t add(reference_caching_channel channel, const char *implementation_name) noexcept
Definition: component.cc:127
#define SESSION_TRACKER_END
Definition: session_tracker.h:56
enum_session_tracker
Definition: session_tracker.h:39
@ SESSION_GTIDS_TRACKER
Definition: session_tracker.h:43
@ SESSION_STATE_CHANGE_TRACKER
Definition: session_tracker.h:42
@ CURRENT_SCHEMA_TRACKER
Definition: session_tracker.h:41
@ TRACK_TRANSACTION_STATE
Definition: session_tracker.h:53
@ TRANSACTION_INFO_TRACKER
Definition: session_tracker.h:44
@ SESSION_SYSVARS_TRACKER
Definition: session_tracker.h:40
enum_tx_isol_level
Transaction isolation level.
Definition: session_tracker.h:238
@ TX_ISOL_INHERIT
not explicitly set, inherit session.tx_isolation
Definition: session_tracker.h:239
@ TX_ISOL_UNCOMMITTED
Definition: session_tracker.h:240
@ TX_ISOL_COMMITTED
Definition: session_tracker.h:241
@ TX_ISOL_REPEATABLE
Definition: session_tracker.h:242
@ TX_ISOL_SERIALIZABLE
Definition: session_tracker.h:243
enum_session_track_transaction_info
Transaction tracking level.
Definition: session_tracker.h:249
@ TX_TRACK_STATE
track transaction status
Definition: session_tracker.h:251
@ TX_TRACK_CHISTICS
track status and characteristics
Definition: session_tracker.h:252
@ TX_TRACK_NONE
do not send tracker items on transaction info
Definition: session_tracker.h:250
enum_tx_read_flags
Transaction access mode.
Definition: session_tracker.h:229
@ TX_READ_INHERIT
not explicitly set, inherit session.tx_read_only
Definition: session_tracker.h:230
@ TX_READ_ONLY
START TRANSACTION READ ONLY, or tx_read_only=1.
Definition: session_tracker.h:231
@ TX_READ_WRITE
START TRANSACTION READ WRITE, or tx_read_only=0.
Definition: session_tracker.h:232
enum_tx_state
Transaction state (no transaction, transaction active, work attached, etc.)
Definition: session_tracker.h:210
@ TX_READ_UNSAFE
non-transaction reads were done
Definition: session_tracker.h:215
@ TX_STMT_DML
a DML statement (known before data is accessed)
Definition: session_tracker.h:222
@ TX_STMT_DDL
a DDL statement
Definition: session_tracker.h:223
@ TX_EMPTY
"none of the below"
Definition: session_tracker.h:211
@ TX_EXPLICIT
an explicit transaction is active
Definition: session_tracker.h:212
@ TX_WRITE_UNSAFE
non-transactional writes were done
Definition: session_tracker.h:217
@ TX_WRITE_TRX
transactional writes were done
Definition: session_tracker.h:216
@ TX_LOCKED_TABLES
LOCK TABLES is active.
Definition: session_tracker.h:221
@ TX_WITH_SNAPSHOT
WITH CONSISTENT SNAPSHOT was used.
Definition: session_tracker.h:220
@ TX_IMPLICIT
an implicit transaction is active
Definition: session_tracker.h:213
@ TX_RESULT_SET
result-set was sent
Definition: session_tracker.h:219
@ TX_STMT_UNSAFE
"unsafe" (non-deterministic like UUID()) stmts
Definition: session_tracker.h:218
@ TX_READ_TRX
transactional reads were done
Definition: session_tracker.h:214
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Definition: mysql_lex_string.h:40
Definition: mysql_lex_string.h:35
thr_lock_type
Definition: thr_lock.h:51