MySQL 8.0.32
Source Code Documentation
rpl_context.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2022, Oracle and/or its affiliates.
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
34
35#include <functional>
36#include <vector>
37
38class Gtid_set;
39class Sid_map;
40class THD;
41struct Gtid;
42
43/** Type of replication channel thread/transaction might be associated to*/
45 NO_CHANNEL_INFO = 0, // No information exists about the channel
46 RPL_STANDARD_CHANNEL = 1, // It is a standard replication channel
47 GR_APPLIER_CHANNEL = 2, // It is a GR applier channel
48 GR_RECOVERY_CHANNEL = 3 // It is a GR recovery channel
49};
50
51/**
52 This class is an interface for session consistency instrumentation
53 in the server. It holds the context information for a given session.
54
55 It does not require locking since access to this content is mutually
56 exclusive by design (only one thread reading or writing to this object
57 at a time).
58 */
60 public:
61 /**
62 This is an interface to be implemented by classes that want to listen
63 to changes to this context. This can be used, for instance, by the
64 session tracker gtids to become aware of ctx modifications.
65 */
67 public:
69 virtual ~Ctx_change_listener() = default;
71
72 private:
73 // not implemented
76 };
77
78 private:
79 /*
80 Local sid_map to enable a lock free m_gtid_set.
81 */
83
84 /**
85 Set holding the transaction identifiers of the gtids
86 to reply back on the response packet.
87
88 Lifecycle: Emptied after the reply is sent back to the application. Remains
89 empty until:
90 - a RW transaction commits and a GTID is written to the binary log.
91 - a RO transaction is issued, the consistency level is set to "Check
92 Potential Writes" and the transaction is committed.
93 */
95
96 /**
97 If a listener is registered, e.g., the session track gtids, then this
98 points to an instance of such listener.
99
100 Since this context is valid only for one session, there is no need
101 to protect this with locks.
102 */
104
105 /**
106 Keeps track of the current session track gtids, so that we capture
107 according to what was set before. For instance, if the user does:
108 SET @@SESSION.SESSION_TRACK_GTIDS='ALL_GTIDS';
109 ...
110 SET @@SESSION.SESSION_TRACK_GTIDS='OWN_GTID';
111
112 The last statement should return a set of GTIDs.
113 */
115
116 protected:
117 /*
118 Auxiliary function to determine if GTID collection should take place
119 when it is invoked. It takes into consideration the gtid_mode and
120 the current session context.
121
122 @param thd the thread context.
123 @return true if should collect gtids, false otherwise.
124 */
125 inline bool shall_collect(const THD *thd);
126
127 /**
128 Auxiliary function that allows notification of ctx change listeners.
129 */
132 }
133
134 public:
135 /**
136 Simple constructor.
137 */
139
140 /**
141 The destructor. Deletes the m_gtid_set and the sid_map.
142 */
144
145 /**
146 Registers the listener. The pointer MUST not be NULL.
147
148 @param listener a pointer to the listener to register.
149 @param thd THD context associated to this listener.
150 */
153
154 /**
155 Unregisters the listener. The listener MUST have registered previously.
156
157 @param listener a pointer to the listener to register.
158 */
161
162 /**
163 This member function MUST return a reference to the set of collected
164 GTIDs so far.
165
166 @return the set of collected GTIDs so far.
167 */
168 inline Gtid_set *state() { return m_gtid_set; }
169
170 /**
171 This function MUST be called after the response packet is set to the
172 client connected. The implementation may act on the collected state
173 for instance to do garbage collection.
174
175 @param thd The thread context.
176 * @return true on error, false otherwise.
177 */
178 virtual bool notify_after_response_packet(const THD *thd);
179
180 /**
181 This function SHALL be called once the GTID for the given transaction has
182 has been added to GTID_EXECUTED.
183
184 This function SHALL store the data if the
185 thd->variables.session_track_gtids is set to a value other than NONE.
186
187 @param thd The thread context.
188 @return true on error, false otherwise.
189 */
190 virtual bool notify_after_gtid_executed_update(const THD *thd);
191
192 /**
193 This function MUST be called after a transaction is committed
194 in the server. It should be called regardless whether it is a
195 RO or RW transaction. Also, DDLs, DDS are considered transaction
196 for what is worth.
197
198 This function SHALL store relevant data for the session consistency.
199
200 @param thd The thread context.
201 @return true on error, false otherwise.
202 */
203 virtual bool notify_after_transaction_commit(const THD *thd);
204
205 virtual bool notify_after_xa_prepare(const THD *thd) {
207 }
208
209 /**
210 Update session tracker (m_curr_session_track_gtids) from thd.
211 */
213
214 private:
215 // not implemented
219};
220
221/*
222 This object encapsulates the state kept between transactions of the same
223 client in order to compute logical timestamps based on WRITESET_SESSION.
224*/
226 public:
228
230 m_last_session_sequence_number = sequence_number;
231 }
232
235 }
236
237 private:
239};
240
241/**
242 This class tracks the last used GTID per session.
243*/
245 public:
248
249 /**
250 Set the last used GTID the session.
251
252 @param[in] gtid the used gtid.
253 */
254 void set_last_used_gtid(const Gtid &gtid);
255
256 /**
257 Get the last used GTID the session.
258
259 @param[out] gtid the used gtid.
260 */
261 void get_last_used_gtid(Gtid &gtid);
262
263 private:
264 std::unique_ptr<Gtid> m_last_used_gtid;
265};
266
268 public:
270
273
275 THD *session);
276
277 protected:
279};
280
281/**
282 Keeps the THD session context to be used with the
283 `Bgc_ticket_manager`. In particular, manages the value of the ticket the
284 current THD session has been assigned to.
285 */
287 public:
289 virtual ~Binlog_group_commit_ctx() = default;
290
291 /**
292 Retrieves the ticket that the THD session has been assigned to. If
293 it hasn't been assigned to any yet, returns '0'.
294
295 @return The ticket the THD session has been assigned to, if
296 any. Returns `0` if it hasn't.
297 */
299 /**
300 Sets the THD session's ticket to the given value.
301
302 @param ticket The ticket to set the THD session to.
303 */
305 /**
306 Assigns the THD session to the ticket accepting assignments in the
307 ticket manager. The method is idem-potent within the execution of a
308 statement. This means that it can be invoked several times during the
309 execution of a command within the THD session that only once will the
310 session be assign to a ticket.
311 */
312 void assign_ticket();
313 /**
314 Whether or not the session already waited on the ticket.
315
316 @return true if the session already waited, false otherwise.
317 */
318 bool has_waited();
319 /**
320 Marks the underlying session has already waited on the ticket.
321 */
323 /**
324 Resets the THD session's ticket context.
325 */
326 void reset();
327 /**
328 Returns the textual representation of this object;
329
330 @return a string containing the textual representation of this object.
331 */
332 std::string to_string() const;
333 /**
334 Dumps the textual representation of this object into the given output
335 stream.
336
337 @param out The stream to dump this object into.
338 */
339 void format(std::ostream &out) const;
340 /**
341 Dumps the textual representation of an instance of this class into the
342 given output stream.
343
344 @param out The output stream to dump the instance to.
345 @param to_dump The class instance to dump to the output stream.
346
347 @return The output stream to which the instance was dumped to.
348 */
349 inline friend std::ostream &operator<<(
350 std::ostream &out, Binlog_group_commit_ctx const &to_dump) {
351 to_dump.format(out);
352 return out;
353 }
354 /**
355 Retrieves the flag for determining if it should be possible to manually
356 set the session's ticket.
357
358 @return the reference for the atomic flag.
359 */
361
362 private:
363 /** The ticket the THD session has been assigned to. */
365 /** Whether or not the session already waited on the ticket. */
366 bool m_has_waited{false};
367};
368
369/*
370 This class SHALL encapsulate the replication context associated with the THD
371 object.
372 */
374 public:
375 /**
376 This structure helps to maintain state of transaction.
377 State of transaction is w.r.t delegates
378 Please refer Trans_delegate to understand states being referred.
379 */
381 // Initialized, first state
383 // begin is being called
385 // binlog cache created, transaction will be binlogged
387 // before_commit is being called
389 // before_rollback is being called
391 // transaction has ended
393 // end
394 TX_RPL_STAGE_END // Not used
395 };
396
397 private:
402 /** Manages interaction and keeps context w.r.t `Bgc_ticket_manager` */
404 std::vector<std::function<bool()>> m_post_filters_actions;
405 /** If this thread is a channel, what is its type*/
407
410
411 public:
413
414 /**
415 Initializers. Clears the writeset session history and re-set delegate state
416 to INIT.
417 */
418 void init();
419
421 return m_session_gtids_ctx;
422 }
423
426 }
427
430 }
431
432 /**
433 Retrieves the class member responsible for managing the interaction
434 with `Bgc_ticket_manager`.
435
436 @return The class member responsible for managing the interaction
437 with `Bgc_ticket_manager`.
438 */
440
442
443 void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg) {
444 rpl_channel_type = rpl_channel_type_arg;
445 }
446
449 }
450
451 std::vector<std::function<bool()>> &post_filters_actions() {
453 }
454
455 /**
456 Sets the transaction states
457
458 @param[in] status state to which THD is progressing
459 */
462
463 /**
464 Returns the transaction state.
465
466 @return status transaction status is returned
467 */
469
470 private:
471 /* Maintains transaction status of Trans_delegate. */
474};
475
476#endif /* RPL_SESSION_H */
Keeps the THD session context to be used with the Bgc_ticket_manager.
Definition: rpl_context.h:286
void assign_ticket()
Assigns the THD session to the ticket accepting assignments in the ticket manager.
Definition: rpl_context.cc:273
bool m_has_waited
Whether or not the session already waited on the ticket.
Definition: rpl_context.h:366
binlog::BgcTicket get_session_ticket()
Retrieves the ticket that the THD session has been assigned to.
Definition: rpl_context.cc:262
bool has_waited()
Whether or not the session already waited on the ticket.
Definition: rpl_context.cc:282
virtual ~Binlog_group_commit_ctx()=default
binlog::BgcTicket m_session_ticket
The ticket the THD session has been assigned to.
Definition: rpl_context.h:364
static memory::Aligned_atomic< bool > & manual_ticket_setting()
Retrieves the flag for determining if it should be possible to manually set the session's ticket.
Definition: rpl_context.cc:308
void format(std::ostream &out) const
Dumps the textual representation of this object into the given output stream.
Definition: rpl_context.cc:299
friend std::ostream & operator<<(std::ostream &out, Binlog_group_commit_ctx const &to_dump)
Dumps the textual representation of an instance of this class into the given output stream.
Definition: rpl_context.h:349
std::string to_string() const
Returns the textual representation of this object;.
Definition: rpl_context.cc:293
void set_session_ticket(binlog::BgcTicket ticket)
Sets the THD session's ticket to the given value.
Definition: rpl_context.cc:266
void reset()
Resets the THD session's ticket context.
Definition: rpl_context.cc:288
void mark_as_already_waited()
Marks the underlying session has already waited on the ticket.
Definition: rpl_context.cc:284
Binlog_group_commit_ctx()=default
Definition: rpl_context.h:225
void set_last_session_sequence_number(int64 sequence_number)
Definition: rpl_context.h:229
int64 m_last_session_sequence_number
Definition: rpl_context.h:238
Dependency_tracker_ctx()
Definition: rpl_context.h:227
int64 get_last_session_sequence_number()
Definition: rpl_context.h:233
Represents a set of GTIDs.
Definition: rpl_gtid.h:1454
This class tracks the last used GTID per session.
Definition: rpl_context.h:244
Last_used_gtid_tracker_ctx()
Definition: rpl_context.cc:190
void get_last_used_gtid(Gtid &gtid)
Get the last used GTID the session.
Definition: rpl_context.cc:200
std::unique_ptr< Gtid > m_last_used_gtid
Definition: rpl_context.h:264
void set_last_used_gtid(const Gtid &gtid)
Set the last used GTID the session.
Definition: rpl_context.cc:196
virtual ~Last_used_gtid_tracker_ctx()
Definition: rpl_context.h:373
std::vector< std::function< bool()> > & post_filters_actions()
Definition: rpl_context.h:451
enum_transaction_rpl_delegate_status get_tx_rpl_delegate_stage_status()
Returns the transaction state.
Definition: rpl_context.cc:324
Last_used_gtid_tracker_ctx m_last_used_gtid_tracker_ctx
Definition: rpl_context.h:400
enum_transaction_rpl_delegate_status m_tx_rpl_delegate_stage_status
Definition: rpl_context.h:472
Dependency_tracker_ctx & dependency_tracker_ctx()
Definition: rpl_context.h:424
enum_transaction_rpl_delegate_status
This structure helps to maintain state of transaction.
Definition: rpl_context.h:380
@ TX_RPL_STAGE_CACHE_CREATED
Definition: rpl_context.h:386
@ TX_RPL_STAGE_CONNECTION_CLEANED
Definition: rpl_context.h:392
@ TX_RPL_STAGE_BEFORE_ROLLBACK
Definition: rpl_context.h:390
@ TX_RPL_STAGE_BEGIN
Definition: rpl_context.h:384
@ TX_RPL_STAGE_INIT
Definition: rpl_context.h:382
@ TX_RPL_STAGE_BEFORE_COMMIT
Definition: rpl_context.h:388
@ TX_RPL_STAGE_END
Definition: rpl_context.h:394
Transaction_compression_ctx m_transaction_compression_ctx
Definition: rpl_context.h:401
Dependency_tracker_ctx m_dependency_tracker_ctx
Definition: rpl_context.h:399
void set_tx_rpl_delegate_stage_status(enum_transaction_rpl_delegate_status status)
Sets the transaction states.
Definition: rpl_context.cc:318
std::vector< std::function< bool()> > m_post_filters_actions
Definition: rpl_context.h:404
Transaction_compression_ctx & transaction_compression_ctx()
Definition: rpl_context.h:447
Session_consistency_gtids_ctx m_session_gtids_ctx
Definition: rpl_context.h:398
enum_rpl_channel_type get_rpl_channel_type()
Definition: rpl_context.h:441
Binlog_group_commit_ctx m_binlog_group_commit_ctx
Manages interaction and keeps context w.r.t Bgc_ticket_manager
Definition: rpl_context.h:403
void init()
Initializers.
Definition: rpl_context.cc:313
Session_consistency_gtids_ctx & session_gtids_ctx()
Definition: rpl_context.h:420
Rpl_thd_context()
Definition: rpl_context.h:412
Binlog_group_commit_ctx & binlog_group_commit_ctx()
Retrieves the class member responsible for managing the interaction with Bgc_ticket_manager.
Definition: rpl_context.cc:328
void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg)
Definition: rpl_context.h:443
Rpl_thd_context(const Rpl_thd_context &rsc)
Last_used_gtid_tracker_ctx & last_used_gtid_tracker_ctx()
Definition: rpl_context.h:428
enum_rpl_channel_type rpl_channel_type
If this thread is a channel, what is its type.
Definition: rpl_context.h:406
Rpl_thd_context & operator=(const Rpl_thd_context &rsc)
This is an interface to be implemented by classes that want to listen to changes to this context.
Definition: rpl_context.h:66
Ctx_change_listener & operator=(const Ctx_change_listener &rsc)
Ctx_change_listener(const Ctx_change_listener &rsc)
This class is an interface for session consistency instrumentation in the server.
Definition: rpl_context.h:59
virtual bool notify_after_xa_prepare(const THD *thd)
Definition: rpl_context.h:205
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:68
bool shall_collect(const THD *thd)
Definition: rpl_context.cc:56
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:103
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:94
Gtid_set * state()
This member function MUST return a reference to the set of collected GTIDs so far.
Definition: rpl_context.h:168
void unregister_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener)
Unregisters the listener.
Definition: rpl_context.cc:176
Sid_map * m_sid_map
Definition: rpl_context.h:82
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:142
Gtid_set * m_gtid_set
Set holding the transaction identifiers of the gtids to reply back on the response packet.
Definition: rpl_context.h:94
void notify_ctx_change_listener()
Auxiliary function that allows notification of ctx change listeners.
Definition: rpl_context.h:130
void update_tracking_activeness_from_session_variable(const THD *thd)
Update session tracker (m_curr_session_track_gtids) from thd.
Definition: rpl_context.cc:138
virtual ~Session_consistency_gtids_ctx()
The destructor.
Definition: rpl_context.cc:44
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:114
Session_consistency_gtids_ctx(const Session_consistency_gtids_ctx &rsc)
void register_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener, THD *thd)
Registers the listener.
Definition: rpl_context.cc:158
Session_consistency_gtids_ctx()
Simple constructor.
Definition: rpl_context.cc:38
Session_consistency_gtids_ctx & operator=(const Session_consistency_gtids_ctx &rsc)
Represents a bidirectional map between SID and SIDNO.
Definition: rpl_gtid.h:723
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Definition: rpl_context.h:267
virtual ~Transaction_compression_ctx()
Definition: rpl_context.cc:211
binary_log::transaction::compression::Compressor * get_compressor(THD *session)
Definition: rpl_context.cc:221
binary_log::transaction::compression::Compressor * m_compressor
Definition: rpl_context.h:278
static const size_t DEFAULT_COMPRESSION_BUFFER_SIZE
Definition: rpl_context.h:269
Transaction_compression_ctx()
Definition: rpl_context.cc:208
The base compressor abstract class.
Definition: base.h:145
Represents the Binlog Group Commit Ticket - BGC Ticket.
Definition: bgc_ticket.h:52
Templated class that encapsulates an std::atomic within a byte buffer that is padded to the processor...
Definition: aligned_atomic.h:151
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:67
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2872
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:60
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:44
@ NO_CHANNEL_INFO
Definition: rpl_context.h:45
@ GR_RECOVERY_CHANNEL
Definition: rpl_context.h:48
@ RPL_STANDARD_CHANNEL
Definition: rpl_context.h:46
@ GR_APPLIER_CHANNEL
Definition: rpl_context.h:47
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1065