MySQL  8.0.18
Source Code Documentation
rpl_context.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2018, 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 class Gtid_set;
32 class Sid_map;
33 class THD;
34 struct Gtid;
35 
36 /** Type of replication channel thread/transaction might be associated to*/
38  NO_CHANNEL_INFO = 0, // No information exists about the channel
39  RPL_STANDARD_CHANNEL = 1, // It is a standard replication channel
40  GR_APPLIER_CHANNEL = 2, // It is a GR applier channel
41  GR_RECOVERY_CHANNEL = 3 // It is a GR recovery channel
42 };
43 
44 /**
45  This class is an interface for session consistency instrumentation
46  in the server. It holds the context information for a given session.
47 
48  It does not require locking since access to this content is mutually
49  exclusive by design (only one thread reading or writing to this object
50  at a time).
51  */
53  public:
54  /**
55  This is an interface to be implemented by classes that want to listen
56  to changes to this context. This can be used, for instance, by the
57  session tracker gtids to become aware of ctx modifications.
58  */
60  public:
62  virtual ~Ctx_change_listener() {}
63  virtual void notify_session_gtids_ctx_change() = 0;
64 
65  private:
66  // not implemented
69  };
70 
71  private:
72  /*
73  Local sid_map to enable a lock free m_gtid_set.
74  */
76 
77  /**
78  Set holding the transaction identifiers of the gtids
79  to reply back on the response packet.
80 
81  Lifecycle: Emptied after the reply is sent back to the application. Remains
82  empty until:
83  - a RW transaction commits and a GTID is written to the binary log.
84  - a RO transaction is issued, the consistency level is set to "Check
85  Potential Writes" and the transaction is committed.
86  */
88 
89  /**
90  If a listener is registered, e.g., the session track gtids, then this
91  points to an instance of such listener.
92 
93  Since this context is valid only for one session, there is no need
94  to protect this with locks.
95  */
97 
98  /**
99  Keeps track of the current session track gtids, so that we capture
100  according to what was set before. For instance, if the user does:
101  SET @@SESSION.SESSION_TRACK_GTIDS='ALL_GTIDS';
102  ...
103  SET @@SESSION.SESSION_TRACK_GTIDS='OWN_GTID';
104 
105  The last statement should return a set of GTIDs.
106  */
108 
109  protected:
110  /*
111  Auxiliary function to determine if GTID collection should take place
112  when it is invoked. It takes into consideration the gtid_mode and
113  the current session context.
114 
115  @param thd the thread context.
116  @return true if should collect gtids, false otherwise.
117  */
118  inline bool shall_collect(const THD *thd);
119 
120  /**
121  Auxiliary function that allows notification of ctx change listeners.
122  */
125  }
126 
127  public:
128  /**
129  Simple constructor.
130  */
132 
133  /**
134  The destructor. Deletes the m_gtid_set and the sid_map.
135  */
137 
138  /**
139  Registers the listener. The pointer MUST not be NULL.
140 
141  @param listener a pointer to the listener to register.
142  @param thd THD context associated to this listener.
143  */
146 
147  /**
148  Unregisters the listener. The listener MUST have registered previously.
149 
150  @param listener a pointer to the listener to register.
151  */
154 
155  /**
156  This member function MUST return a reference to the set of collected
157  GTIDs so far.
158 
159  @return the set of collected GTIDs so far.
160  */
161  inline Gtid_set *state() { return m_gtid_set; }
162 
163  /**
164  This function MUST be called after the response packet is set to the
165  client connected. The implementation may act on the collected state
166  for instance to do garbage collection.
167 
168  @param thd The thread context.
169  * @return true on error, false otherwise.
170  */
171  virtual bool notify_after_response_packet(const THD *thd);
172 
173  /**
174  This function SHALL be called once the GTID for the given transaction has
175  has been added to GTID_EXECUTED.
176 
177  This function SHALL store the data if the
178  thd->variables.session_track_gtids is set to a value other than NONE.
179 
180  @param thd The thread context.
181  @return true on error, false otherwise.
182  */
183  virtual bool notify_after_gtid_executed_update(const THD *thd);
184 
185  /**
186  This function MUST be called after a transaction is committed
187  in the server. It should be called regardless whether it is a
188  RO or RW transaction. Also, DDLs, DDS are considered transaction
189  for what is worth.
190 
191  This function SHALL store relevant data for the session consistency.
192 
193  @param thd The thread context.
194  @return true on error, false otherwise.
195  */
196  virtual bool notify_after_transaction_commit(const THD *thd);
197 
198  virtual bool notify_after_xa_prepare(const THD *thd) {
200  }
201 
202  private:
203  // not implemented
206  const Session_consistency_gtids_ctx &rsc);
207 };
208 
209 /*
210  This object encapsulates the state kept between transactions of the same
211  client in order to compute logical timestamps based on WRITESET_SESSION.
212 */
214  public:
216 
217  void set_last_session_sequence_number(int64 sequence_number) {
218  m_last_session_sequence_number = sequence_number;
219  }
220 
223  }
224 
225  private:
227 };
228 
229 /**
230  This class tracks the last used GTID per session.
231 */
233  public:
235  virtual ~Last_used_gtid_tracker_ctx();
236 
237  /**
238  Set the last used GTID the session.
239 
240  @param[in] gtid the used gtid.
241  */
242  void set_last_used_gtid(const Gtid &gtid);
243 
244  /**
245  Get the last used GTID the session.
246 
247  @param[out] gtid the used gtid.
248  */
249  void get_last_used_gtid(Gtid &gtid);
250 
251  private:
252  std::unique_ptr<Gtid> m_last_used_gtid;
253 };
254 
255 /*
256  This class SHALL encapsulate the replication context associated with the THD
257  object.
258  */
260  private:
264  /** If this thread is a channel, what is its type*/
266 
267  Rpl_thd_context(const Rpl_thd_context &rsc);
269 
270  public:
272 
274  return m_session_gtids_ctx;
275  }
276 
279  }
280 
283  }
284 
286 
287  void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg) {
288  rpl_channel_type = rpl_channel_type_arg;
289  }
290 };
291 
292 #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:133
This class tracks the last used GTID per session.
Definition: rpl_context.h:232
bool shall_collect(const THD *thd)
Definition: rpl_context.cc:53
Definition: rpl_context.h:39
int64 m_last_session_sequence_number
Definition: rpl_context.h:226
enum_rpl_channel_type get_rpl_channel_type()
Definition: rpl_context.h:285
enum_rpl_channel_type rpl_channel_type
If this thread is a channel, what is its type.
Definition: rpl_context.h:265
Session_consistency_gtids_ctx m_session_gtids_ctx
Definition: rpl_context.h:261
Dependency_tracker_ctx & dependency_tracker_ctx()
Definition: rpl_context.h:277
void unregister_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener)
Unregisters the listener.
Definition: rpl_context.cc:167
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:59
Definition: rpl_context.h:38
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:107
Rpl_thd_context()
Definition: rpl_context.h:271
Definition: rpl_context.h:259
This class is an interface for session consistency instrumentation in the server. ...
Definition: rpl_context.h:52
virtual bool notify_after_xa_prepare(const THD *thd)
Definition: rpl_context.h:198
Gtid_set * m_gtid_set
Set holding the transaction identifiers of the gtids to reply back on the response packet...
Definition: rpl_context.h:87
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:90
Session_consistency_gtids_ctx()
Simple constructor.
Definition: rpl_context.cc:35
TODO: Move this structure to libbinlogevents/include/control_events.h when we start using C++11...
Definition: rpl_gtid.h:924
Represents a bidirectional map between SID and SIDNO.
Definition: rpl_gtid.h:582
void notify_ctx_change_listener()
Auxiliary function that allows notification of ctx change listeners.
Definition: rpl_context.h:123
virtual ~Session_consistency_gtids_ctx()
The destructor.
Definition: rpl_context.cc:41
Last_used_gtid_tracker_ctx m_last_used_gtid_tracker_ctx
Definition: rpl_context.h:263
Definition: rpl_context.h:213
Dependency_tracker_ctx m_dependency_tracker_ctx
Definition: rpl_context.h:262
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:161
Rpl_thd_context & operator=(const Rpl_thd_context &rsc)
void get_last_used_gtid(Gtid &gtid)
Get the last used GTID the session.
Definition: rpl_context.cc:191
void set_last_used_gtid(const Gtid &gtid)
Set the last used GTID the session.
Definition: rpl_context.cc:187
Last_used_gtid_tracker_ctx & last_used_gtid_tracker_ctx()
Definition: rpl_context.h:281
Session_consistency_gtids_ctx & operator=(const Session_consistency_gtids_ctx &rsc)
int64 get_last_session_sequence_number()
Definition: rpl_context.h:221
Definition: rpl_context.h:41
virtual ~Ctx_change_listener()
Definition: rpl_context.h:62
Last_used_gtid_tracker_ctx()
Definition: rpl_context.cc:181
virtual ~Last_used_gtid_tracker_ctx()
Definition: rpl_context.cc:185
Represents a set of GTIDs.
Definition: rpl_gtid.h:1302
void set_last_session_sequence_number(int64 sequence_number)
Definition: rpl_context.h:217
Sid_map * m_sid_map
Definition: rpl_context.h:75
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:64
Definition: rpl_context.h:40
void set_rpl_channel_type(enum_rpl_channel_type rpl_channel_type_arg)
Definition: rpl_context.h:287
enum_rpl_channel_type
Type of replication channel thread/transaction might be associated to.
Definition: rpl_context.h:37
Ctx_change_listener & operator=(const Ctx_change_listener &rsc)
Ctx_change_listener()
Definition: rpl_context.h:61
Session_consistency_gtids_ctx & session_gtids_ctx()
Definition: rpl_context.h:273
Dependency_tracker_ctx()
Definition: rpl_context.h:215
std::unique_ptr< Gtid > m_last_used_gtid
Definition: rpl_context.h:252
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 ...
Definition: rpl_context.h:96
unsigned long ulong
Definition: my_inttypes.h:48
void register_ctx_change_listener(Session_consistency_gtids_ctx::Ctx_change_listener *listener, THD *thd)
Registers the listener.
Definition: rpl_context.cc:149
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778