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