MySQL  8.0.17
Source Code Documentation
rpl_trx_tracking.h
Go to the documentation of this file.
1 #ifndef RPL_TRX_TRACKING_INCLUDED
2 /* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License, version 2.0,
6  as published by the Free Software Foundation.
7 
8  This program is also distributed with certain software (including
9  but not limited to OpenSSL) that is licensed under separate terms,
10  as designated in a particular file or component or in included license
11  documentation. The authors of MySQL hereby grant you an additional
12  permission to link the program and your derivative works with the
13  separately licensed software that they have included with MySQL.
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 #define RPL_TRX_TRACKING_INCLUDED
25 
26 #include <sys/types.h>
27 #include <atomic>
28 #include <map>
29 
30 #include "binlog_event.h"
31 #include "my_dbug.h"
32 #include "my_inttypes.h"
33 
34 class THD;
35 
36 /**
37  Logical timestamp generator for logical timestamping binlog transactions.
38  A transaction is associated with two sequence numbers see
39  @c Transaction_ctx::last_committed and @c Transaction_ctx::sequence_number.
40  The class provides necessary interfaces including that of
41  generating a next consecutive value for the latter.
42 */
44  private:
45  std::atomic<int64> state;
46  /*
47  Offset is subtracted from the actual "absolute time" value at
48  logging a replication event. That is the event holds logical
49  timestamps in the "relative" format. They are meaningful only in
50  the context of the current binlog.
51  The member is updated (incremented) per binary log rotation.
52  */
54 
55  public:
56  Logical_clock();
58  : state(other.state.load()), offset(other.offset) {}
59 
60  int64 step();
61  int64 set_if_greater(int64 new_val);
63  int64 get_offset() { return offset; }
64  /*
65  Updates the offset.
66  This operation is invoked when binlog rotates and at that time
67  there can't any concurrent step() callers so no need to guard
68  the assignement.
69  */
70  void update_offset(int64 new_offset) {
71  DBUG_ASSERT(offset <= new_offset);
72 
73  offset = new_offset;
74  }
76 };
77 
78 /**
79  Generate logical timestamps for MTS using COMMIT_ORDER
80  in the binlog-transaction-dependency-tracking option.
81 */
83  public:
84  /**
85  Main function that gets the dependencies using the COMMIT_ORDER tracker.
86 
87  @param [in] thd THD of the caller.
88  @param [in,out] sequence_number sequence_number initialized and returned.
89  @param [in,out] commit_parent commit_parent to be returned.
90  */
91  void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
92 
93  void update_max_committed(int64 sequence_number);
94 
97  }
98 
99  int64 step();
100  void rotate();
101 
102  private:
103  /* Committed transactions timestamp */
105 
106  /* "Prepared" transactions timestamp */
108 
109  /*
110  Stores the last sequence_number of the transaction which breaks the rule of
111  lock based logical clock. commit_parent of the following transactions
112  will be set to m_last_blocking_transaction if their last_committed is
113  smaller than m_last_blocking_transaction.
114  */
116 };
117 
118 /**
119  Generate logical timestamps for MTS using WRITESET
120  in the binlog-transaction-dependency-tracking option.
121 */
123  public:
125  : m_opt_max_history_size(max_history_size), m_writeset_history_start(0) {}
126 
127  /**
128  Main function that gets the dependencies using the WRITESET tracker.
129 
130  @param [in] thd THD of the caller.
131  @param [in,out] sequence_number sequence_number initialized and returned.
132  @param [in,out] commit_parent commit_parent to be returned.
133  */
134  void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
135 
136  void rotate(int64 start);
137 
138  /* option opt_binlog_transaction_dependency_history_size */
140 
141  private:
142  /*
143  Monitor the last transaction with write-set to use as the minimal
144  commit parent when logical clock source is WRITE_SET, i.e., the most recent
145  transaction that is not in the history, or 0 when the history is empty.
146 
147  The m_writeset_history_start must to be set to 0 initially and whenever the
148  binlog_transaction_dependency_tracking variable is changed or the history
149  is cleared, so that it is updated to the first transaction for which the
150  dependencies are checked.
151  */
153 
154  /*
155  Track the last transaction sequence number that changed each row
156  in the database, using row hashes from the writeset as the index.
157  */
158  typedef std::map<uint64, int64> Writeset_history;
160 };
161 
162 /**
163  Generate logical timestamps for MTS using WRITESET_SESSION
164  in the binlog-transaction-dependency-tracking option.
165 */
167  public:
168  /**
169  Main function that gets the dependencies using the WRITESET_SESSION tracker.
170 
171  @param [in] thd THD of the caller.
172  @param [in,out] sequence_number sequence_number initialized and returned.
173  @param [in,out] commit_parent commit_parent to be returned.
174  */
175  void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
176 };
177 
178 /**
179  Modes for parallel transaction dependency tracking
180 */
182  /**
183  Tracks dependencies based on the commit order of transactions.
184  The time intervals during which any transaction holds all its locks are
185  tracked (the interval ends just before storage engine commit, when locks
186  are released. For an autocommit transaction it begins just before storage
187  engine prepare. For BEGIN..COMMIT transactions it begins at the end of the
188  last statement before COMMIT). Two transactions are marked as
189  non-conflicting if their respective intervals overlap. In other words,
190  if trx1 appears before trx2 in the binlog, and trx2 had acquired all its
191  locks before trx1 released its locks, then trx2 is marked such that the
192  slave can schedule it in parallel with trx1.
193  */
195  /**
196  Tracks dependencies based on the set of rows updated. Any two transactions
197  that change disjoint sets of rows, are said concurrent and non-contending.
198  */
200  /**
201  Tracks dependencies based on the set of rows updated per session. Any two
202  transactions that change disjoint sets of rows, on different sessions,
203  are said concurrent and non-contending. Transactions from the same session
204  are always said to be dependent, i.e., are never concurrent and
205  non-contending.
206  */
208 };
209 
210 /**
211  Dependency tracker is a container singleton that dispatches between the three
212  methods associated with the binlog-transaction-dependency-tracking option.
213  There is a singleton instance of each of these classes.
214 */
216  public:
219  m_writeset(25000) {}
220 
221  void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
222 
223  void tracking_mode_changed();
224 
225  void update_max_committed(THD *thd);
227 
228  int64 step();
229  void rotate();
230 
231  public:
232  /* option opt_binlog_transaction_dependency_tracking */
234 
236 
237  private:
241 };
242 
243 #endif /* RPL_TRX_TRACKING_INCLUDED */
Writeset_history m_writeset_history
Definition: rpl_trx_tracking.h:159
Dependency tracker is a container singleton that dispatches between the three methods associated with...
Definition: rpl_trx_tracking.h:215
bool load(THD *, const dd::String_type &fname, dd::String_type *buf)
Read an sdi file from disk and store in a buffer.
Definition: sdi_file.cc:306
int64 get_max_committed_timestamp()
Definition: rpl_trx_tracking.cc:397
Some integer typedefs for easier portability.
Contains the classes representing events occurring in the replication stream.
void update_max_committed(int64 sequence_number)
Definition: rpl_trx_tracking.cc:188
std::map< uint64, int64 > Writeset_history
Definition: rpl_trx_tracking.h:158
Tracks dependencies based on the set of rows updated.
Definition: rpl_trx_tracking.h:199
Commit_order_trx_dependency_tracker m_commit_order
Definition: rpl_trx_tracking.h:239
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
Tracks dependencies based on the commit order of transactions.
Definition: rpl_trx_tracking.h:194
void rotate()
Definition: rpl_trx_tracking.cc:387
void rotate(int64 start)
Definition: rpl_trx_tracking.cc:295
long m_opt_tracking_mode
Definition: rpl_trx_tracking.h:233
Generate logical timestamps for MTS using WRITESET_SESSION in the binlog-transaction-dependency-track...
Definition: rpl_trx_tracking.h:166
int64 step()
Definition: rpl_trx_tracking.cc:385
Tracks dependencies based on the set of rows updated per session.
Definition: rpl_trx_tracking.h:207
Writeset_session_trx_dependency_tracker m_writeset_session
Definition: rpl_trx_tracking.h:240
Generate logical timestamps for MTS using WRITESET in the binlog-transaction-dependency-tracking opti...
Definition: rpl_trx_tracking.h:122
Transaction_dependency_tracker()
Definition: rpl_trx_tracking.h:217
void rotate()
Definition: rpl_trx_tracking.cc:181
void update_offset(int64 new_offset)
Definition: rpl_trx_tracking.h:70
int64 m_last_blocking_transaction
Definition: rpl_trx_tracking.h:115
Writeset_trx_dependency_tracker * get_writeset()
Definition: rpl_trx_tracking.h:235
std::atomic< int64 > state
Definition: rpl_trx_tracking.h:45
void update_max_committed(THD *thd)
The method is to be executed right before committing time.
Definition: rpl_trx_tracking.cc:373
Writeset_trx_dependency_tracker(ulong max_history_size)
Definition: rpl_trx_tracking.h:124
enum_binlog_transaction_dependency_tracking
Modes for parallel transaction dependency tracking.
Definition: rpl_trx_tracking.h:181
int64 offset
Definition: rpl_trx_tracking.h:53
Logical_clock()
Definition: rpl_trx_tracking.cc:43
int64 step()
Steps the absolute value of the clock (state) to return an updated value.
Definition: rpl_trx_tracking.cc:64
const int64_t SEQ_UNINIT
Uninitialized timestamp value (for either last committed or sequence number).
Definition: binlog_event.h:137
Generate logical timestamps for MTS using COMMIT_ORDER in the binlog-transaction-dependency-tracking ...
Definition: rpl_trx_tracking.h:82
Logical_clock get_max_committed_transaction()
Definition: rpl_trx_tracking.h:95
~Logical_clock()
Definition: rpl_trx_tracking.h:75
Logical_clock m_max_committed_transaction
Definition: rpl_trx_tracking.h:104
int64 m_writeset_history_start
Definition: rpl_trx_tracking.h:152
void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent)
Main function that gets the dependencies using the WRITESET tracker.
Definition: rpl_trx_tracking.cc:209
Logical_clock m_transaction_counter
Definition: rpl_trx_tracking.h:107
void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent)
Main function that gets the dependencies using the WRITESET_SESSION tracker.
Definition: rpl_trx_tracking.cc:310
long long int64
Definition: my_inttypes.h:68
ulong m_opt_max_history_size
Definition: rpl_trx_tracking.h:139
int64 step()
Definition: rpl_trx_tracking.cc:177
static void start(PluginFuncEnv *env)
Definition: http_server_plugin.cc:572
Logical_clock(const Logical_clock &other)
Definition: rpl_trx_tracking.h:57
int64 get_timestamp()
Atomically fetch the current state.
Definition: rpl_trx_tracking.cc:49
Writeset_trx_dependency_tracker m_writeset
Definition: rpl_trx_tracking.h:238
void tracking_mode_changed()
Definition: rpl_trx_tracking.cc:353
void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent)
Get the dependencies in a transaction, the main entry point for the dependency tracking work...
Definition: rpl_trx_tracking.cc:326
void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent)
Main function that gets the dependencies using the COMMIT_ORDER tracker.
Definition: rpl_trx_tracking.cc:145
Logical timestamp generator for logical timestamping binlog transactions.
Definition: rpl_trx_tracking.h:43
int64 get_offset()
Definition: rpl_trx_tracking.h:63
unsigned long ulong
Definition: my_inttypes.h:48
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
int64 set_if_greater(int64 new_val)
To try setting the clock forward.
Definition: rpl_trx_tracking.cc:82