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