MySQL  8.0.20
Source Code Documentation
rpl_context.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef RPL_SESSION_H
24 #define RPL_SESSION_H
25 
26 #include <sys/types.h>
27 #include <memory>
28 
29 #include "my_inttypes.h" // IWYU pragma: keep
30 
31 #include "sql/system_variables.h"
32 
33 class Gtid_set;
34 class Sid_map;
35 class THD;
36 struct Gtid;
37 
38 /** Type of replication channel thread/transaction might be associated to*/
40  NO_CHANNEL_INFO = 0, // No information exists about the channel
41  RPL_STANDARD_CHANNEL = 1, // It is a standard replication channel
42  GR_APPLIER_CHANNEL = 2, // It is a GR applier channel
43  GR_RECOVERY_CHANNEL = 3 // It is a GR recovery channel
44 };
45 
46 /**
47  This class is an interface for session consistency instrumentation
48  in the server. It holds the context information for a given session.
49 
50  It does not require locking since access to this content is mutually
51  exclusive by design (only one thread reading or writing to this object
52  at a time).
53  */
55  public:
56  /**
57  This is an interface to be implemented by classes that want to listen
58  to changes to this context. This can be used, for instance, by the
59  session tracker gtids to become aware of ctx modifications.
60  */
62  public:
64  virtual ~Ctx_change_listener() {}
65  virtual void notify_session_gtids_ctx_change() = 0;
66 
67  private:
68  // not implemented
71  };
72 
73  private:
74  /*
75  Local sid_map to enable a lock free m_gtid_set.
76  */
78 
79  /**
80  Set holding the transaction identifiers of the gtids
81  to reply back on the response packet.
82 
83  Lifecycle: Emptied after the reply is sent back to the application. Remains
84  empty until:
85  - a RW transaction commits and a GTID is written to the binary log.
86  - a RO transaction is issued, the consistency level is set to "Check
87  Potential Writes" and the transaction is committed.
88  */
90 
91  /**
92  If a listener is registered, e.g., the session track gtids, then this
93  points to an instance of such listener.
94 
95  Since this context is valid only for one session, there is no need
96  to protect this with locks.
97  */
99 
100  /**
101  Keeps track of the current session track gtids, so that we capture
102  according to what was set before. For instance, if the user does:
103  SET @@SESSION.SESSION_TRACK_GTIDS='ALL_GTIDS';
104  ...
105  SET @@SESSION.SESSION_TRACK_GTIDS='OWN_GTID';
106 
107  The last statement should return a set of GTIDs.
108  */
110 
111  protected:
112  /*
113  Auxiliary function to determine if GTID collection should take place
114  when it is invoked. It takes into consideration the gtid_mode and
115  the current session context.
116 
117  @param thd the thread context.
118  @return true if should collect gtids, false otherwise.
119  */
120  inline bool shall_collect(const THD *thd);
121 
122  /**
123  Auxiliary function that allows notification of ctx change listeners.
124  */
126  m_listener->notify_session_gtids_ctx_change();
127  }
128 
129  public:
130  /**
131  Simple constructor.
132  */
134 
135  /**
136  The destructor. Deletes the m_gtid_set and the sid_map.
137  */
139 
140  /**
141  Registers the listener. The pointer MUST not be NULL.
142 
143  @param listener a pointer to the listener to register.
144  @param thd THD context associated to this listener.
145  */
148 
149  /**
150  Unregisters the listener. The listener MUST have registered previously.
151 
152  @param listener a pointer to the listener to register.
153  */
156 
157  /**
158  This member function MUST return a reference to the set of collected
159  GTIDs so far.
160 
161  @return the set of collected GTIDs so far.
162  */
163  inline Gtid_set *state() { return m_gtid_set; }
164 
165  /**
166  This function MUST be called after the response packet is set to the
167  client connected. The implementation may act on the collected state
168  for instance to do garbage collection.
169 
170  @param thd The thread context.
171  * @return true on error, false otherwise.
172  */
173  virtual bool notify_after_response_packet(const THD *thd);
174 
175  /**
176  This function SHALL be called once the GTID for the given transaction has
177  has been added to GTID_EXECUTED.
178 
179  This function SHALL store the data if the
180  thd->variables.session_track_gtids is set to a value other than NONE.
181 
182  @param thd The thread context.
183  @return true on error, false otherwise.
184  */
185  virtual bool notify_after_gtid_executed_update(const THD *thd);
186 
187  /**
188  This function MUST be called after a transaction is committed
189  in the server. It should be called regardless whether it is a
190  RO or RW transaction. Also, DDLs, DDS are considered transaction
191  for what is worth.
192 
193  This function SHALL store relevant data for the session consistency.
194 
195  @param thd The thread context.
196  @return true on error, false otherwise.
197  */
198  virtual bool notify_after_transaction_commit(const THD *thd);
199 
200  virtual bool notify_after_xa_prepare(const THD *thd) {
202  }
203 
204  private:
205  // not implemented
208  const Session_consistency_gtids_ctx &rsc);
209 };
210 
211 /*
212  This object encapsulates the state kept between transactions of the same
213  client in order to compute logical timestamps based on WRITESET_SESSION.
214 */
216  public:
217  Dependency_tracker_ctx() : m_last_session_sequence_number(0) {}
218 
219  void set_last_session_sequence_number(int64 sequence_number) {
220  m_last_session_sequence_number = sequence_number;
221  }
222 
224  return m_last_session_sequence_number;
225  }
226 
227  private:
229 };
230 
231 /**
232  This class tracks the last used GTID per session.
233 */
235  public:
237  virtual ~Last_used_gtid_tracker_ctx();
238 
239  /**
240  Set the last used GTID the session.
241 
242  @param[in] gtid the used gtid.
243  */
244  void set_last_used_gtid(const Gtid &gtid);
245 
246  /**
247  Get the last used GTID the session.
248 
249  @param[out] gtid the used gtid.
250  */
251  void get_last_used_gtid(Gtid &gtid);
252 
253  private:
254  std::unique_ptr<Gtid> m_last_used_gtid;
255 };
256 
258  public:
259  static const size_t DEFAULT_COMPRESSION_BUFFER_SIZE;
260 
262  virtual ~Transaction_compression_ctx();
263 
265  THD *session);
266 
267  protected:
269 };
270 
271 /*
272  This class SHALL encapsulate the replication context associated with the THD
273  object.
274  */
276  private:
281  /** If this thread is a channel, what is its type*/
283 
284  Rpl_thd_context(const Rpl_thd_context &rsc);
286 
287  public:
288  Rpl_thd_context() : rpl_channel_type(NO_CHANNEL_INFO) {}
289 
291  return m_session_gtids_ctx;
292  }
293 
295  return m_dependency_tracker_ctx;
296  }
297 
299  return m_last_used_gtid_tracker_ctx;
300  }
301 
302  enum_rpl_channel_type get_rpl_channel_type() { return rpl_channel_type; }
303 
304  void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg) {
305  rpl_channel_type = rpl_channel_type_arg;
306  }
307 
309  return m_transaction_compression_ctx;
310  }
311 };
312 
313 #endif /* RPL_SESSION_H */
virtual bool notify_after_response_packet(const THD *thd)
This function MUST be called after the response packet is set to the client connected.
Definition: rpl_context.cc:135
This class tracks the last used GTID per session.
Definition: rpl_context.h:234
bool shall_collect(const THD *thd)
Definition: rpl_context.cc:55
Definition: rpl_context.h:41
int64 m_last_session_sequence_number
Definition: rpl_context.h:228
enum_rpl_channel_type get_rpl_channel_type()
Definition: rpl_context.h:302
enum_rpl_channel_type rpl_channel_type
If this thread is a channel, what is its type.
Definition: rpl_context.h:282
Session_consistency_gtids_ctx m_session_gtids_ctx
Definition: rpl_context.h:277
Dependency_tracker_ctx & dependency_tracker_ctx()
Definition: rpl_context.h:294
void unregister_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener)
Unregisters the listener.
Definition: rpl_context.cc:169
Some integer typedefs for easier portability.
This is an interface to be implemented by classes that want to listen to changes to this context...
Definition: rpl_context.h:61
Definition: rpl_context.h:40
ulong m_curr_session_track_gtids
Keeps track of the current session track gtids, so that we capture according to what was set before...
Definition: rpl_context.h:109
Rpl_thd_context()
Definition: rpl_context.h:288
Definition: rpl_context.h:257
MYSQL_SESSION session[9]
Definition: test_sql_9_sessions.cc:65
Definition: rpl_context.h:275
This class is an interface for session consistency instrumentation in the server. ...
Definition: rpl_context.h:54
virtual bool notify_after_xa_prepare(const THD *thd)
Definition: rpl_context.h:200
Gtid_set * m_gtid_set
Set holding the transaction identifiers of the gtids to reply back on the response packet...
Definition: rpl_context.h:89
virtual bool notify_after_gtid_executed_update(const THD *thd)
This function SHALL be called once the GTID for the given transaction has has been added to GTID_EXEC...
Definition: rpl_context.cc:92
Session_consistency_gtids_ctx()
Simple constructor.
Definition: rpl_context.cc:37
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11...
Definition: rpl_gtid.h:926
Represents a bidirectional map between SID and SIDNO.
Definition: rpl_gtid.h:584
void notify_ctx_change_listener()
Auxiliary function that allows notification of ctx change listeners.
Definition: rpl_context.h:125
virtual ~Session_consistency_gtids_ctx()
The destructor.
Definition: rpl_context.cc:43
Last_used_gtid_tracker_ctx m_last_used_gtid_tracker_ctx
Definition: rpl_context.h:279
Definition: rpl_context.h:215
Dependency_tracker_ctx m_dependency_tracker_ctx
Definition: rpl_context.h:278
The base compressor abstract class.
Definition: base.h:145
int64_t int64
Definition: my_inttypes.h:67
Gtid_set * state()
This member function MUST return a reference to the set of collected GTIDs so far.
Definition: rpl_context.h:163
Last_used_gtid_tracker_ctx & last_used_gtid_tracker_ctx()
Definition: rpl_context.h:298
int64 get_last_session_sequence_number()
Definition: rpl_context.h:223
Definition: rpl_context.h:43
virtual ~Ctx_change_listener()
Definition: rpl_context.h:64
static const size_t DEFAULT_COMPRESSION_BUFFER_SIZE
Definition: rpl_context.h:259
Transaction_compression_ctx m_transaction_compression_ctx
Definition: rpl_context.h:280
Represents a set of GTIDs.
Definition: rpl_gtid.h:1313
void set_last_session_sequence_number(int64 sequence_number)
Definition: rpl_context.h:219
Sid_map * m_sid_map
Definition: rpl_context.h:77
virtual bool notify_after_transaction_commit(const THD *thd)
This function MUST be called after a transaction is committed in the server.
Definition: rpl_context.cc:66
Definition: rpl_context.h:42
void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg)
Definition: rpl_context.h:304
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:39
Ctx_change_listener & operator=(const Ctx_change_listener &rsc)
Ctx_change_listener()
Definition: rpl_context.h:63
Session_consistency_gtids_ctx & session_gtids_ctx()
Definition: rpl_context.h:290
Dependency_tracker_ctx()
Definition: rpl_context.h:217
std::unique_ptr< Gtid > m_last_used_gtid
Definition: rpl_context.h:254
Session_consistency_gtids_ctx::Ctx_change_listener * m_listener
If a listener is registered, e.g., the session track gtids, then this points to an instance of such l...
Definition: rpl_context.h:98
void register_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener, THD *thd)
Registers the listener.
Definition: rpl_context.cc:151
Transaction_compression_ctx & transaction_compression_ctx()
Definition: rpl_context.h:308
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:765