MySQL  8.0.17
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) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
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 
32 class String;
33 class THD;
34 class set_var;
35 
36 struct 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 
46 #define SESSION_TRACKER_END TRANSACTION_INFO_TRACKER
47 
48 /**
49  State_tracker
50  -------------
51  An abstract class that defines the interface for any of the server's
52  'session state change tracker'. A tracker, however, is a sub- class of
53  this class which takes care of tracking the change in value of a part-
54  icular session state type and thus defines various methods listed in this
55  interface. The change information is later serialized and transmitted to
56  the client through protocol's OK packet.
57 
58  Tracker system variables :-
59  A tracker is normally mapped to a system variable. So in order to enable,
60  disable or modify the sub-entities of a tracker, the user needs to modify
61  the respective system variable either through SET command or via command
62  line option. As required in system variable handling, this interface also
63  includes two functions to help in the verification of the supplied value
64  (ON_CHECK) and the updation (ON_UPDATE) of the tracker system variable,
65  namely - check() and update().
66 */
67 
69  protected:
70  /** Is tracking enabled for a particular session state type ? */
71  bool m_enabled;
72 
73  /** Has the session state type changed ? */
74  bool m_changed;
75 
76  public:
77  /** Constructor */
79 
80  /** Destructor */
81  virtual ~State_tracker() {}
82 
83  /** Getters */
84  bool is_enabled() const { return m_enabled; }
85 
86  bool is_changed() const { return m_changed; }
87 
88  /** Called in the constructor of THD*/
89  virtual bool enable(THD *thd) = 0;
90 
91  /** To be invoked when the tracker's system variable is checked (ON_CHECK). */
92  virtual bool check(THD *thd, set_var *var) = 0;
93 
94  /** To be invoked when the tracker's system variable is updated (ON_UPDATE).*/
95  virtual bool update(THD *thd) = 0;
96 
97  /** Store changed data into the given buffer. */
98  virtual bool store(THD *thd, String &buf) = 0;
99 
100  /** Mark the entity as changed. */
101  virtual void mark_as_changed(THD *thd, LEX_CSTRING *name) = 0;
102 
103  virtual void claim_memory_ownership() {}
104 };
105 
106 /**
107  Session_tracker
108  ---------------
109  This class holds an object each for all tracker classes and provides
110  methods necessary for systematic detection and generation of session
111  state change information.
112 */
113 
115  private:
117 
118  public:
119  Session_tracker(Session_tracker const &) = delete;
120 
121  Session_tracker &operator=(Session_tracker const &) = delete;
122 
123  /** Constructor */
125 
126  /** Destructor */
128  /**
129  Initialize Session_tracker objects and enable them based on the
130  tracker_xxx variables' value that the session inherit from global
131  variables at the time of session initialization (see plugin_thdvar_init).
132  */
133  void init(const CHARSET_INFO *char_set);
134  void enable(THD *thd);
135  bool server_boot_verify(const CHARSET_INFO *char_set, LEX_STRING var_list);
136 
137  /** Returns the pointer to the tracker object for the specified tracker. */
139 
140  /** Checks if m_enabled flag is set for any of the tracker objects. */
141  bool enabled_any();
142 
143  /** Checks if m_changed flag is set for any of the tracker objects. */
144  bool changed_any();
145 
146  /**
147  Stores the session state change information of all changes session state
148  type entities into the specified buffer.
149  */
150  void store(THD *thd, String &main_buf);
151  void deinit() {
152  for (int i = 0; i <= SESSION_TRACKER_END; i++) delete m_trackers[i];
153  }
154 
155  void claim_memory_ownership();
156 };
157 
158 /*
159  Session_state_change_tracker
160  ----------------------------
161  This is a boolean tracker class that will monitor any change that contributes
162  to a session state change.
163  Attributes that contribute to session state change include:
164  - Successful change to System variables
165  - User defined variables assignments
166  - temporary tables created, altered or deleted
167  - prepared statements added or removed
168  - change in current database
169 */
170 
172  private:
173  void reset();
174 
175  public:
177  bool enable(THD *thd);
178  bool check(THD *, set_var *) { return false; }
179  bool update(THD *thd);
180  bool store(THD *, String &buf);
181  void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name);
182  bool is_state_changed();
183 };
184 
185 /**
186  Transaction_state_tracker
187  ----------------------
188  This is a tracker class that enables & manages the tracking of
189  current transaction info for a particular connection.
190 */
191 
192 /**
193  Transaction state (no transaction, transaction active, work attached, etc.)
194 */
196  TX_EMPTY = 0, ///< "none of the below"
197  TX_EXPLICIT = 1, ///< an explicit transaction is active
198  TX_IMPLICIT = 2, ///< an implicit transaction is active
199  TX_READ_TRX = 4, ///< transactional reads were done
200  TX_READ_UNSAFE = 8, ///< non-transaction reads were done
201  TX_WRITE_TRX = 16, ///< transactional writes were done
202  TX_WRITE_UNSAFE = 32, ///< non-transactional writes were done
203  TX_STMT_UNSAFE = 64, ///< "unsafe" (non-deterministic like UUID()) stmts
204  TX_RESULT_SET = 128, ///< result-set was sent
205  TX_WITH_SNAPSHOT = 256, ///< WITH CONSISTENT SNAPSHOT was used
206  TX_LOCKED_TABLES = 512 ///< LOCK TABLES is active
207 };
208 
209 /**
210  Transaction access mode
211 */
213  TX_READ_INHERIT = 0, ///< not explicitly set, inherit session.tx_read_only
214  TX_READ_ONLY = 1, ///< START TRANSACTION READ ONLY, or tx_read_only=1
215  TX_READ_WRITE = 2, ///< START TRANSACTION READ WRITE, or tx_read_only=0
216 };
217 
218 /**
219  Transaction isolation level
220 */
222  TX_ISOL_INHERIT = 0, ///< not explicitly set, inherit session.tx_isolation
227 };
228 
229 /**
230  Transaction tracking level
231 */
233  TX_TRACK_NONE = 0, ///< do not send tracker items on transaction info
234  TX_TRACK_STATE = 1, ///< track transaction status
235  TX_TRACK_CHISTICS = 2 ///< track status and characteristics
236 };
237 
239  public:
240  /** Constructor */
242  bool enable(THD *thd) { return update(thd); }
243  bool check(THD *, set_var *) { return false; }
244  bool update(THD *thd);
245  bool store(THD *thd, String &buf);
246  void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name);
247 
248  /** Change transaction characteristics */
249  void set_read_flags(THD *thd, enum enum_tx_read_flags flags);
250  void set_isol_level(THD *thd, enum enum_tx_isol_level level);
251 
252  /** Change transaction state */
253  void clear_trx_state(THD *thd, uint clear);
254  void add_trx_state(THD *thd, uint add);
255  void add_trx_state_from_thd(THD *thd);
256  void end_trx(THD *thd);
257 
258  /** Helper function: turn table info into table access flag */
259  enum_tx_state calc_trx_state(thr_lock_type l, bool has_trx);
260 
261  private:
263  TX_CHG_NONE = 0, ///< no changes from previous stmt
264  TX_CHG_STATE = 1, ///< state has changed from previous stmt
265  TX_CHG_CHISTICS = 2 ///< characteristics have changed from previous stmt
266  };
267 
268  /** any trackable changes caused by this statement? */
270 
271  /** transaction state */
273 
274  /** r/w or r/o set? session default? */
276 
277  /** isolation level */
279 
280  void reset();
281 
282  inline void update_change_flags(THD *thd) {
286  }
287 };
288 
289 #endif /* SESSION_TRACKER_INCLUDED */
characteristics have changed from previous stmt
Definition: session_tracker.h:265
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:1185
bool enable(THD *thd)
Initiate the value of m_enabled based on @session_track_state_change value.
Definition: session_tracker.cc:1330
Session_tracker & operator=(Session_tracker const &)=delete
bool m_changed
Has the session state type changed ?
Definition: session_tracker.h:74
virtual void mark_as_changed(THD *thd, LEX_CSTRING *name)=0
Mark the entity as changed.
Definition: mysql_lex_string.h:34
Definition: session_tracker.h:226
bool check(THD *, set_var *)
To be invoked when the tracker&#39;s system variable is checked (ON_CHECK).
Definition: session_tracker.h:178
START TRANSACTION READ ONLY, or tx_read_only=1.
Definition: session_tracker.h:214
bool store(THD *, String &buf)
Store the 1byte boolean flag in the specified buffer.
Definition: session_tracker.cc:1358
result-set was sent
Definition: session_tracker.h:204
const string name("\ame\)
Session_tracker()
Constructor.
Definition: session_tracker.h:124
"none of the below"
Definition: session_tracker.h:196
virtual bool update(THD *thd)=0
To be invoked when the tracker&#39;s system variable is updated (ON_UPDATE).
enum_tx_state
Transaction_state_tracker This is a tracker class that enables & manages the tracking of current tran...
Definition: session_tracker.h:195
"unsafe" (non-deterministic like UUID()) stmts
Definition: session_tracker.h:203
enum_tx_read_flags
Transaction access mode.
Definition: session_tracker.h:212
bool update(THD *thd)
Enable/disable the tracker based on @session_track_state_change value.
Definition: session_tracker.cc:1343
bool m_enabled
Is tracking enabled for a particular session state type ?
Definition: session_tracker.h:71
transactional reads were done
Definition: session_tracker.h:199
virtual bool check(THD *thd, set_var *var)=0
To be invoked when the tracker&#39;s system variable is checked (ON_CHECK).
unsigned int add(m_name) char *m_name
void enable(THD *thd)
Enables the tracker objects.
Definition: session_tracker.cc:1445
bool enable(THD *thd)
Called in the constructor of THD.
Definition: session_tracker.h:242
enum_tx_isol_level
Transaction isolation level.
Definition: session_tracker.h:221
Definition: mysql_lex_string.h:39
bool update(THD *thd)
Enable/disable the tracker based on @session_track_transaction_info&#39;s value.
Definition: session_tracker.cc:862
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
bool enabled_any()
Checks if m_enabled flag is set for any of the tracker objects.
Definition: session_tracker.cc:1491
enum enum_tx_isol_level tx_isol_level
isolation level
Definition: session_tracker.h:278
void init(const CHARSET_INFO *char_set)
Initialize Session_tracker objects and enable them based on the tracker_xxx variables&#39; value that the...
Definition: session_tracker.cc:1423
track transaction status
Definition: session_tracker.h:234
Transaction_state_tracker()
Constructor.
Definition: session_tracker.cc:844
#define SESSION_TRACKER_END
Definition: session_tracker.h:46
~Session_tracker()
Destructor.
Definition: session_tracker.h:127
uint tx_reported_state
Definition: session_tracker.h:272
void clear_trx_state(THD *thd, uint clear)
Change transaction state.
Definition: session_tracker.cc:1223
non-transaction reads were done
Definition: session_tracker.h:200
an implicit transaction is active
Definition: session_tracker.h:198
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:159
set_var_base descendant for assignments to the system variables.
Definition: set_var.h:386
WITH CONSISTENT SNAPSHOT was used.
Definition: session_tracker.h:205
track status and characteristics
Definition: session_tracker.h:235
virtual bool store(THD *thd, String &buf)=0
Store changed data into the given buffer.
do not send tracker items on transaction info
Definition: session_tracker.h:233
void add_trx_state(THD *thd, uint add)
Add flags pertaining to the current statement or transaction.
Definition: session_tracker.cc:1239
virtual void claim_memory_ownership()
Definition: session_tracker.h:103
no changes from previous stmt
Definition: session_tracker.h:263
void set_isol_level(THD *thd, enum enum_tx_isol_level level)
Set isolation level pertaining to the next transaction.
Definition: session_tracker.cc:1305
enum_session_track_transaction_info
Transaction tracking level.
Definition: session_tracker.h:232
not explicitly set, inherit session.tx_isolation
Definition: session_tracker.h:222
thr_lock_type
Definition: thr_lock.h:50
bool is_enabled() const
Getters.
Definition: session_tracker.h:84
void end_trx(THD *thd)
Register the end of an (implicit or explicit) transaction.
Definition: session_tracker.cc:1203
State_tracker()
Constructor.
Definition: session_tracker.h:78
Definition: session_tracker.h:42
bool is_state_changed()
find if there is a session state change
Definition: session_tracker.cc:1413
void add_trx_state_from_thd(THD *thd)
Add "unsafe statement" flag if applicable.
Definition: session_tracker.cc:1275
void reset()
Reset the m_changed flag for next statement.
Definition: session_tracker.cc:1168
Session_state_change_tracker()
Constructor.
Definition: session_tracker.cc:1317
void reset()
Reset the m_changed flag for next statement.
Definition: session_tracker.cc:1403
uint tx_changed
any trackable changes caused by this statement?
Definition: session_tracker.h:269
Definition: session_tracker.h:40
State_tracker An abstract class that defines the interface for any of the server&#39;s &#39;session state cha...
Definition: session_tracker.h:68
unsigned int uint
Definition: uca-dump.cc:29
non-transactional writes were done
Definition: session_tracker.h:202
Definition: session_tracker.h:238
void clear(Shards &shards)
Clear the counter - reset to 0.
Definition: ut0counter.h:294
uint tx_curr_state
transaction state
Definition: session_tracker.h:272
Definition: session_tracker.h:225
bool store(THD *thd, String &buf)
Store the transaction state (and, optionally, characteristics) as length-encoded string in the specif...
Definition: session_tracker.cc:896
Definition: m_ctype.h:359
void deinit()
Definition: session_tracker.h:151
START TRANSACTION READ WRITE, or tx_read_only=0.
Definition: session_tracker.h:215
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:1459
Definition: session_tracker.h:43
bool changed_any()
Checks if m_changed flag is set for any of the tracker objects.
Definition: session_tracker.cc:1507
Definition: session_tracker.h:171
an explicit transaction is active
Definition: session_tracker.h:197
void set_read_flags(THD *thd, enum enum_tx_read_flags flags)
Change transaction characteristics.
Definition: session_tracker.cc:1290
void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name)
Mark the tracker as changed.
Definition: session_tracker.cc:1160
bool is_changed() const
Definition: session_tracker.h:86
Definition: session_tracker.h:224
void claim_memory_ownership()
Definition: session_tracker.cc:1435
enum_tx_changed
Definition: session_tracker.h:262
Definition: session_tracker.h:223
void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name)
Mark the tracker as changed and associated session attributes accordingly.
Definition: session_tracker.cc:1386
transactional writes were done
Definition: session_tracker.h:201
Definition: session_tracker.h:41
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:1524
State_tracker * m_trackers[SESSION_TRACKER_END+1]
Definition: session_tracker.h:116
virtual ~State_tracker()
Destructor.
Definition: session_tracker.h:81
enum enum_tx_read_flags tx_read_flags
r/w or r/o set? session default?
Definition: session_tracker.h:275
LOCK TABLES is active.
Definition: session_tracker.h:206
Definition: session_tracker.h:39
State_tracker * get_tracker(enum_session_tracker tracker) const
Returns the pointer to the tracker object for the specified tracker.
Definition: session_tracker.cc:1477
static int flags[50]
Definition: hp_test1.cc:39
bool check(THD *, set_var *)
To be invoked when the tracker&#39;s system variable is checked (ON_CHECK).
Definition: session_tracker.h:243
void update_change_flags(THD *thd)
Definition: session_tracker.h:282
#define false
Definition: config_static.h:43
enum_session_tracker
Definition: session_tracker.h:38
virtual bool enable(THD *thd)=0
Called in the constructor of THD.
state has changed from previous stmt
Definition: session_tracker.h:264
not explicitly set, inherit session.tx_read_only
Definition: session_tracker.h:213
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
Session_tracker This class holds an object each for all tracker classes and provides methods necessar...
Definition: session_tracker.h:114