MySQL 8.0.30
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
32
33class Gtid_set;
34class Sid_map;
35class THD;
36struct 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() = default;
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 */
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 /**
205 Update session tracker (m_curr_session_track_gtids) from thd.
206 */
208
209 private:
210 // not implemented
214};
215
216/*
217 This object encapsulates the state kept between transactions of the same
218 client in order to compute logical timestamps based on WRITESET_SESSION.
219*/
221 public:
223
225 m_last_session_sequence_number = sequence_number;
226 }
227
230 }
231
232 private:
234};
235
236/**
237 This class tracks the last used GTID per session.
238*/
240 public:
243
244 /**
245 Set the last used GTID the session.
246
247 @param[in] gtid the used gtid.
248 */
249 void set_last_used_gtid(const Gtid &gtid);
250
251 /**
252 Get the last used GTID the session.
253
254 @param[out] gtid the used gtid.
255 */
256 void get_last_used_gtid(Gtid &gtid);
257
258 private:
259 std::unique_ptr<Gtid> m_last_used_gtid;
260};
261
263 public:
265
268
270 THD *session);
271
272 protected:
274};
275
276/*
277 This class SHALL encapsulate the replication context associated with the THD
278 object.
279 */
281 public:
282 /**
283 This structure helps to maintain state of transaction.
284 State of transaction is w.r.t delegates
285 Please refer Trans_delegate to understand states being referred.
286 */
288 // Initialized, first state
290 // begin is being called
292 // binlog cache created, transaction will be binlogged
294 // before_commit is being called
296 // before_rollback is being called
298 // transaction has ended
300 // end
301 TX_RPL_STAGE_END // Not used
302 };
303
304 private:
309 /** If this thread is a channel, what is its type*/
311
314
315 public:
317
318 /**
319 Initializers. Clears the writeset session history and re-set delegate state
320 to INIT.
321 */
322 void init();
323
325 return m_session_gtids_ctx;
326 }
327
330 }
331
334 }
335
337
338 void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg) {
339 rpl_channel_type = rpl_channel_type_arg;
340 }
341
344 }
345
346 /**
347 Sets the transaction states
348
349 @param[in] status state to which THD is progressing
350 */
353
354 /**
355 Returns the transaction state.
356
357 @return status transaction status is returned
358 */
360
361 private:
362 /* Maintains transaction status of Trans_delegate. */
365};
366
367#endif /* RPL_SESSION_H */
Definition: rpl_context.h:220
void set_last_session_sequence_number(int64 sequence_number)
Definition: rpl_context.h:224
int64 m_last_session_sequence_number
Definition: rpl_context.h:233
Dependency_tracker_ctx()
Definition: rpl_context.h:222
int64 get_last_session_sequence_number()
Definition: rpl_context.h:228
Represents a set of GTIDs.
Definition: rpl_gtid.h:1454
This class tracks the last used GTID per session.
Definition: rpl_context.h:239
Last_used_gtid_tracker_ctx()
Definition: rpl_context.cc:189
void get_last_used_gtid(Gtid &gtid)
Get the last used GTID the session.
Definition: rpl_context.cc:199
std::unique_ptr< Gtid > m_last_used_gtid
Definition: rpl_context.h:259
void set_last_used_gtid(const Gtid &gtid)
Set the last used GTID the session.
Definition: rpl_context.cc:195
virtual ~Last_used_gtid_tracker_ctx()
Definition: rpl_context.h:280
enum_transaction_rpl_delegate_status get_tx_rpl_delegate_stage_status()
Returns the transaction state.
Definition: rpl_context.cc:272
Last_used_gtid_tracker_ctx m_last_used_gtid_tracker_ctx
Definition: rpl_context.h:307
enum_transaction_rpl_delegate_status m_tx_rpl_delegate_stage_status
Definition: rpl_context.h:363
Dependency_tracker_ctx & dependency_tracker_ctx()
Definition: rpl_context.h:328
enum_transaction_rpl_delegate_status
This structure helps to maintain state of transaction.
Definition: rpl_context.h:287
@ TX_RPL_STAGE_CACHE_CREATED
Definition: rpl_context.h:293
@ TX_RPL_STAGE_CONNECTION_CLEANED
Definition: rpl_context.h:299
@ TX_RPL_STAGE_BEFORE_ROLLBACK
Definition: rpl_context.h:297
@ TX_RPL_STAGE_BEGIN
Definition: rpl_context.h:291
@ TX_RPL_STAGE_INIT
Definition: rpl_context.h:289
@ TX_RPL_STAGE_BEFORE_COMMIT
Definition: rpl_context.h:295
@ TX_RPL_STAGE_END
Definition: rpl_context.h:301
Transaction_compression_ctx m_transaction_compression_ctx
Definition: rpl_context.h:308
Dependency_tracker_ctx m_dependency_tracker_ctx
Definition: rpl_context.h:306
void set_tx_rpl_delegate_stage_status(enum_transaction_rpl_delegate_status status)
Sets the transaction states.
Definition: rpl_context.cc:266
Transaction_compression_ctx & transaction_compression_ctx()
Definition: rpl_context.h:342
Session_consistency_gtids_ctx m_session_gtids_ctx
Definition: rpl_context.h:305
enum_rpl_channel_type get_rpl_channel_type()
Definition: rpl_context.h:336
void init()
Initializers.
Definition: rpl_context.cc:261
Session_consistency_gtids_ctx & session_gtids_ctx()
Definition: rpl_context.h:324
Rpl_thd_context()
Definition: rpl_context.h:316
void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg)
Definition: rpl_context.h:338
Rpl_thd_context(const Rpl_thd_context &rsc)
Last_used_gtid_tracker_ctx & last_used_gtid_tracker_ctx()
Definition: rpl_context.h:332
enum_rpl_channel_type rpl_channel_type
If this thread is a channel, what is its type.
Definition: rpl_context.h:310
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:61
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:54
virtual bool notify_after_xa_prepare(const THD *thd)
Definition: rpl_context.h:200
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:67
bool shall_collect(const THD *thd)
Definition: rpl_context.cc:55
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
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:93
Gtid_set * state()
This member function MUST return a reference to the set of collected GTIDs so far.
Definition: rpl_context.h:163
void unregister_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener)
Unregisters the listener.
Definition: rpl_context.cc:175
Sid_map * m_sid_map
Definition: rpl_context.h:77
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:141
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
void notify_ctx_change_listener()
Auxiliary function that allows notification of ctx change listeners.
Definition: rpl_context.h:125
void update_tracking_activeness_from_session_variable(const THD *thd)
Update session tracker (m_curr_session_track_gtids) from thd.
Definition: rpl_context.cc:137
virtual ~Session_consistency_gtids_ctx()
The destructor.
Definition: rpl_context.cc:43
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
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:157
Session_consistency_gtids_ctx()
Simple constructor.
Definition: rpl_context.cc:37
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_class.h:922
Definition: rpl_context.h:262
virtual ~Transaction_compression_ctx()
Definition: rpl_context.cc:210
binary_log::transaction::compression::Compressor * get_compressor(THD *session)
Definition: rpl_context.cc:220
binary_log::transaction::compression::Compressor * m_compressor
Definition: rpl_context.h:273
static const size_t DEFAULT_COMPRESSION_BUFFER_SIZE
Definition: rpl_context.h:264
Transaction_compression_ctx()
Definition: rpl_context.cc:207
The base compressor abstract class.
Definition: base.h:145
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:67
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:39
@ NO_CHANNEL_INFO
Definition: rpl_context.h:40
@ GR_RECOVERY_CHANNEL
Definition: rpl_context.h:43
@ RPL_STANDARD_CHANNEL
Definition: rpl_context.h:41
@ GR_APPLIER_CHANNEL
Definition: rpl_context.h:42
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11.
Definition: rpl_gtid.h:1065