MySQL 9.1.0
Source Code Documentation
rpl_context.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RPL_SESSION_H
25#define RPL_SESSION_H
26
27#include <sys/types.h>
28#include <memory>
29
30#include "my_inttypes.h" // IWYU pragma: keep
31#include "mysql/binlog/event/compression/compressor.h" // mysql::binlog::event::compression::Compressor
33
37#include "sql/psi_memory_key.h"
38#include "sql/resource_blocker.h" // resource_blocker::User
40
41#include <functional>
42#include <vector>
43
44class Gtid_set;
45class Tsid_map;
46class THD;
47struct Gtid;
48
49/** Type of replication channel thread/transaction might be associated to*/
51 NO_CHANNEL_INFO = 0, // No information exists about the channel
52 RPL_STANDARD_CHANNEL = 1, // It is a standard replication channel
53 GR_APPLIER_CHANNEL = 2, // It is a GR applier channel
54 GR_RECOVERY_CHANNEL = 3 // It is a GR recovery channel
55};
56
57/**
58 This class is an interface for session consistency instrumentation
59 in the server. It holds the context information for a given session.
60
61 It does not require locking since access to this content is mutually
62 exclusive by design (only one thread reading or writing to this object
63 at a time).
64 */
66 public:
67 /**
68 This is an interface to be implemented by classes that want to listen
69 to changes to this context. This can be used, for instance, by the
70 session tracker gtids to become aware of ctx modifications.
71 */
73 public:
75 virtual ~Ctx_change_listener() = default;
77
78 private:
79 // not implemented
82 };
83
84 private:
85 /*
86 Local tsid_map to enable a lock free m_gtid_set.
87 */
89
90 /**
91 Set holding the transaction identifiers of the gtids
92 to reply back on the response packet.
93
94 Lifecycle: Emptied after the reply is sent back to the application. Remains
95 empty until:
96 - a RW transaction commits and a GTID is written to the binary log.
97 - a RO transaction is issued, the consistency level is set to "Check
98 Potential Writes" and the transaction is committed.
99 */
101
102 /**
103 If a listener is registered, e.g., the session track gtids, then this
104 points to an instance of such listener.
105
106 Since this context is valid only for one session, there is no need
107 to protect this with locks.
108 */
110
111 /**
112 Keeps track of the current session track gtids, so that we capture
113 according to what was set before. For instance, if the user does:
114 SET @@SESSION.SESSION_TRACK_GTIDS='ALL_GTIDS';
115 ...
116 SET @@SESSION.SESSION_TRACK_GTIDS='OWN_GTID';
117
118 The last statement should return a set of GTIDs.
119 */
121
122 protected:
123 /*
124 Auxiliary function to determine if GTID collection should take place
125 when it is invoked. It takes into consideration the gtid_mode and
126 the current session context.
127
128 @param thd the thread context.
129 @return true if should collect gtids, false otherwise.
130 */
131 inline bool shall_collect(const THD *thd);
132
133 /**
134 Auxiliary function that allows notification of ctx change listeners.
135 */
138 }
139
140 public:
141 /**
142 Simple constructor.
143 */
145
146 /**
147 The destructor. Deletes the m_gtid_set and the tsid_map.
148 */
150
151 /**
152 Registers the listener. The pointer MUST not be NULL.
153
154 @param listener a pointer to the listener to register.
155 @param thd THD context associated to this listener.
156 */
159
160 /**
161 Unregisters the listener. The listener MUST have registered previously.
162
163 @param listener a pointer to the listener to register.
164 */
167
168 /**
169 This member function MUST return a reference to the set of collected
170 GTIDs so far.
171
172 @return the set of collected GTIDs so far.
173 */
174 inline Gtid_set *state() { return m_gtid_set; }
175
176 /**
177 This function MUST be called after the response packet is set to the
178 client connected. The implementation may act on the collected state
179 for instance to do garbage collection.
180
181 @param thd The thread context.
182 * @return true on error, false otherwise.
183 */
184 virtual bool notify_after_response_packet(const THD *thd);
185
186 /**
187 This function SHALL be called once the GTID for the given transaction has
188 has been added to GTID_EXECUTED.
189
190 This function SHALL store the data if the
191 thd->variables.session_track_gtids is set to a value other than NONE.
192
193 @param thd The thread context.
194 @return true on error, false otherwise.
195 */
196 virtual bool notify_after_gtid_executed_update(const THD *thd);
197
198 /**
199 This function MUST be called after a transaction is committed
200 in the server. It should be called regardless whether it is a
201 RO or RW transaction. Also, DDLs, DDS are considered transaction
202 for what is worth.
203
204 This function SHALL store relevant data for the session consistency.
205
206 @param thd The thread context.
207 @return true on error, false otherwise.
208 */
209 virtual bool notify_after_transaction_commit(const THD *thd);
210
211 virtual bool notify_after_xa_prepare(const THD *thd) {
213 }
214
215 /**
216 Update session tracker (m_curr_session_track_gtids) from thd.
217 */
219
220 private:
221 // not implemented
225};
226
227/**
228 This class tracks the last used GTID per session.
229*/
231 public:
234
235 /**
236 Set the last used GTID the session.
237
238 @param[in] gtid the used gtid.
239 @param[in] sid the used sid.
240 */
241 void set_last_used_gtid(const Gtid &gtid, const mysql::gtid::Tsid &sid);
242
243 /**
244 Get the last used GTID the session.
245
246 @param[out] gtid the used gtid.
247 */
248 void get_last_used_gtid(Gtid &gtid);
249
250 /**
251 Get the last used TSID of the session.
252
253 @param[out] tsid the used tsid.
254 */
256
257 private:
258 std::unique_ptr<Gtid> m_last_used_gtid;
260};
261
266
267 public:
268 using Compressor_ptr_t = std::shared_ptr<Compressor_t>;
271
273
274 /// Return the compressor.
275 ///
276 /// This constructs the compressor on the first invocation and
277 /// returns the same compressor on subsequent invocations.
279
280 /// Return reference to the buffer sequence holding compressed
281 /// bytes.
283
284 private:
288};
289
290/**
291 Keeps the THD session context to be used with the
292 `Bgc_ticket_manager`. In particular, manages the value of the ticket the
293 current THD session has been assigned to.
294 */
296 public:
298 virtual ~Binlog_group_commit_ctx() = default;
299
300 /**
301 Retrieves the ticket that the THD session has been assigned to. If
302 it hasn't been assigned to any yet, returns '0'.
303
304 @return The ticket the THD session has been assigned to, if
305 any. Returns `0` if it hasn't.
306 */
308 /**
309 Sets the THD session's ticket to the given value.
310
311 @param ticket The ticket to set the THD session to.
312 */
314 /**
315 Assigns the THD session to the ticket accepting assignments in the
316 ticket manager. The method is idem-potent within the execution of a
317 statement. This means that it can be invoked several times during the
318 execution of a command within the THD session that only once will the
319 session be assign to a ticket.
320 */
321 void assign_ticket();
322 /**
323 Whether or not the session already waited on the ticket.
324
325 @return true if the session already waited, false otherwise.
326 */
327 bool has_waited();
328 /**
329 Marks the underlying session has already waited on the ticket.
330 */
332 /**
333 Resets the THD session's ticket context.
334 */
335 void reset();
336 /**
337 Returns the textual representation of this object;
338
339 @return a string containing the textual representation of this object.
340 */
341 std::string to_string() const;
342 /**
343 Dumps the textual representation of this object into the given output
344 stream.
345
346 @param out The stream to dump this object into.
347 */
348 void format(std::ostream &out) const;
349 /**
350 Dumps the textual representation of an instance of this class into the
351 given output stream.
352
353 @param out The output stream to dump the instance to.
354 @param to_dump The class instance to dump to the output stream.
355
356 @return The output stream to which the instance was dumped to.
357 */
358 inline friend std::ostream &operator<<(
359 std::ostream &out, Binlog_group_commit_ctx const &to_dump) {
360 to_dump.format(out);
361 return out;
362 }
363 /**
364 Retrieves the flag for determining if it should be possible to manually
365 set the session's ticket.
366
367 @return the reference for the atomic flag.
368 */
370
371 private:
372 /** The ticket the THD session has been assigned to. */
374 /** Whether or not the session already waited on the ticket. */
375 bool m_has_waited{false};
376
377 public:
378 /// Set whether binlog max size was exceeded.
379 /// The max size exceeded condition must be checked with LOCK_log held and
380 /// thus its done early during flush stage although not used until end of BGC.
381 /// This is an optimization which avoids taking LOCK_log at end of BGC when no
382 /// session has seen that the threshold has been exceeded.
383 void set_max_size_exceeded(bool value) { m_max_size_exceeded = value; }
384
385 /// Turn on forced rotate at end of BGC. Thus performing a rotate although
386 /// the max size has not been reached.
388
389 /// Aggregate the rotate requests over all sessions in queue
390 ///
391 /// @return The first element states whether any session
392 /// detected max binlog size exceeded and the second whether any session
393 /// requested forced binlog rotate.
394 static std::pair<bool, bool> aggregate_rotate_settings(THD *queue);
395
396 private:
397 /// Whether session detected that binlog max size was exceeded.
399 /// Whether session requests forced rotate
400 bool m_force_rotate{false};
401};
402
403/*
404 This class SHALL encapsulate the replication context associated with the THD
405 object.
406 */
408 public:
409 /**
410 This structure helps to maintain state of transaction.
411 State of transaction is w.r.t delegates
412 Please refer Trans_delegate to understand states being referred.
413 */
415 // Initialized, first state
417 // begin is being called
419 // binlog cache created, transaction will be binlogged
421 // before_commit is being called
423 // before_rollback is being called
425 // transaction has ended
427 // end
428 TX_RPL_STAGE_END // Not used
429 };
430
432
433 private:
437 /** Manages interaction and keeps context w.r.t `Bgc_ticket_manager` */
439 std::vector<std::function<bool()>> m_post_filters_actions;
440 /** If this thread is a channel, what is its type*/
442
445
446 public:
450
451 /**
452 Initializers. Clears the writeset session history and re-set delegate state
453 to INIT.
454 */
455 void init();
456
458 return m_session_gtids_ctx;
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:295
void assign_ticket()
Assigns the THD session to the ticket accepting assignments in the ticket manager.
Definition: rpl_context.cc:247
bool m_has_waited
Whether or not the session already waited on the ticket.
Definition: rpl_context.h:375
void set_force_rotate()
Turn on forced rotate at end of BGC.
Definition: rpl_context.h:387
binlog::BgcTicket get_session_ticket()
Retrieves the ticket that the THD session has been assigned to.
Definition: rpl_context.cc:236
bool has_waited()
Whether or not the session already waited on the ticket.
Definition: rpl_context.cc:256
virtual ~Binlog_group_commit_ctx()=default
bool m_max_size_exceeded
Whether session detected that binlog max size was exceeded.
Definition: rpl_context.h:398
binlog::BgcTicket m_session_ticket
The ticket the THD session has been assigned to.
Definition: rpl_context.h:373
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:284
void set_max_size_exceeded(bool value)
Set whether binlog max size was exceeded.
Definition: rpl_context.h:383
void format(std::ostream &out) const
Dumps the textual representation of this object into the given output stream.
Definition: rpl_context.cc:275
static std::pair< bool, bool > aggregate_rotate_settings(THD *queue)
Aggregate the rotate requests over all sessions in queue.
Definition: rpl_context.cc:289
bool m_force_rotate
Whether session requests forced rotate.
Definition: rpl_context.h:400
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:358
std::string to_string() const
Returns the textual representation of this object;.
Definition: rpl_context.cc:269
void set_session_ticket(binlog::BgcTicket ticket)
Sets the THD session's ticket to the given value.
Definition: rpl_context.cc:240
void reset()
Resets the THD session's ticket context.
Definition: rpl_context.cc:262
void mark_as_already_waited()
Marks the underlying session has already waited on the ticket.
Definition: rpl_context.cc:258
Binlog_group_commit_ctx()=default
Represents a set of GTIDs.
Definition: rpl_gtid.h:1557
This class tracks the last used GTID per session.
Definition: rpl_context.h:230
void get_last_used_tsid(mysql::gtid::Tsid &tsid)
Get the last used TSID of the session.
Definition: rpl_context.cc:210
Last_used_gtid_tracker_ctx()
Definition: rpl_context.cc:193
mysql::gtid::Tsid m_last_used_tsid
Definition: rpl_context.h:259
void get_last_used_gtid(Gtid &gtid)
Get the last used GTID the session.
Definition: rpl_context.cc:205
void set_last_used_gtid(const Gtid &gtid, const mysql::gtid::Tsid &sid)
Set the last used GTID the session.
Definition: rpl_context.cc:199
std::unique_ptr< Gtid > m_last_used_gtid
Definition: rpl_context.h:258
virtual ~Last_used_gtid_tracker_ctx()
Definition: rpl_context.h:407
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:310
Last_used_gtid_tracker_ctx m_last_used_gtid_tracker_ctx
Definition: rpl_context.h:435
enum_transaction_rpl_delegate_status m_tx_rpl_delegate_stage_status
Definition: rpl_context.h:505
enum_transaction_rpl_delegate_status
This structure helps to maintain state of transaction.
Definition: rpl_context.h:414
@ TX_RPL_STAGE_CACHE_CREATED
Definition: rpl_context.h:420
@ TX_RPL_STAGE_CONNECTION_CLEANED
Definition: rpl_context.h:426
@ TX_RPL_STAGE_BEFORE_ROLLBACK
Definition: rpl_context.h:424
@ TX_RPL_STAGE_BEGIN
Definition: rpl_context.h:418
@ TX_RPL_STAGE_INIT
Definition: rpl_context.h:416
@ TX_RPL_STAGE_BEFORE_COMMIT
Definition: rpl_context.h:422
@ TX_RPL_STAGE_END
Definition: rpl_context.h:428
Transaction_compression_ctx m_transaction_compression_ctx
Definition: rpl_context.h:436
void set_tx_rpl_delegate_stage_status(enum_transaction_rpl_delegate_status status)
Sets the transaction states.
Definition: rpl_context.cc:304
std::vector< std::function< bool()> > m_post_filters_actions
Definition: rpl_context.h:439
Transaction_compression_ctx & transaction_compression_ctx()
Definition: rpl_context.h:480
Session_consistency_gtids_ctx m_session_gtids_ctx
Definition: rpl_context.h:434
resource_blocker::User dump_thread_user
Definition: rpl_context.h:431
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:438
void init()
Initializers.
Definition: rpl_context.cc:300
Session_consistency_gtids_ctx & session_gtids_ctx()
Definition: rpl_context.h:457
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:314
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:447
enum_rpl_channel_type rpl_channel_type
If this thread is a channel, what is its type.
Definition: rpl_context.h:441
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:72
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:65
virtual bool notify_after_xa_prepare(const THD *thd)
Definition: rpl_context.h:211
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:71
bool shall_collect(const THD *thd)
Definition: rpl_context.cc:59
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:109
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:97
Gtid_set * state()
This member function MUST return a reference to the set of collected GTIDs so far.
Definition: rpl_context.h:174
void unregister_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener)
Unregisters the listener.
Definition: rpl_context.cc:179
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:145
Gtid_set * m_gtid_set
Set holding the transaction identifiers of the gtids to reply back on the response packet.
Definition: rpl_context.h:100
void notify_ctx_change_listener()
Auxiliary function that allows notification of ctx change listeners.
Definition: rpl_context.h:136
void update_tracking_activeness_from_session_variable(const THD *thd)
Update session tracker (m_curr_session_track_gtids) from thd.
Definition: rpl_context.cc:141
virtual ~Session_consistency_gtids_ctx()
The destructor.
Definition: rpl_context.cc:47
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:120
Session_consistency_gtids_ctx(const Session_consistency_gtids_ctx &rsc)
Tsid_map * m_tsid_map
Definition: rpl_context.h:88
void register_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener, THD *thd)
Registers the listener.
Definition: rpl_context.cc:161
Session_consistency_gtids_ctx()
Simple constructor.
Definition: rpl_context.cc:41
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:36
Definition: rpl_context.h:262
Transaction_compression_ctx(PSI_memory_key key)
Definition: rpl_context.cc:214
Memory_resource_t m_managed_buffer_memory_resource
Definition: rpl_context.h:285
Compressor_ptr_t get_compressor(THD *session)
Return the compressor.
Definition: rpl_context.cc:220
Compressor_ptr_t m_compressor
Definition: rpl_context.h:287
Managed_buffer_sequence_t & managed_buffer_sequence()
Return reference to the buffer sequence holding compressed bytes.
Definition: rpl_context.cc:232
Managed_buffer_sequence_t m_managed_buffer_sequence
Definition: rpl_context.h:286
std::shared_ptr< Compressor_t > Compressor_ptr_t
Definition: rpl_context.h:268
Represents a bidirectional map between TSID and SIDNO.
Definition: rpl_gtid.h:750
Represents the Binlog Group Commit Ticket - BGC Ticket.
Definition: bgc_ticket.h:54
Templated class that encapsulates an std::atomic within a byte buffer that is padded to the processor...
Definition: aligned_atomic.h:158
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
Abstract base class for compressors.
Definition: compressor.h:80
mysql::containers::buffers::Managed_buffer_sequence<> Managed_buffer_sequence_t
Definition: compressor.h:83
Description of a heuristic to determine how much memory to allocate.
Definition: grow_calculator.h:68
Owned, non-contiguous, growable memory buffer.
Definition: managed_buffer_sequence.h:115
Represents Transaction Source Identifier which is composed of source UUID and transaction tag.
Definition: tsid.h:44
Definition: resource_blocker.h:137
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
Some integer typedefs for easier portability.
static QUEUE queue
Definition: myisampack.cc:210
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
required string key
Definition: replication_asynchronous_connection_failover.proto:60
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:50
@ NO_CHANNEL_INFO
Definition: rpl_context.h:51
@ GR_RECOVERY_CHANNEL
Definition: rpl_context.h:54
@ RPL_STANDARD_CHANNEL
Definition: rpl_context.h:52
@ GR_APPLIER_CHANNEL
Definition: rpl_context.h:53
TODO: Move this structure to mysql/binlog/event/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1101