MySQL 8.4.0
Source Code Documentation
rpl_trx_tracking.h
Go to the documentation of this file.
1#ifndef RPL_TRX_TRACKING_INCLUDED
2/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#define RPL_TRX_TRACKING_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29#include <atomic>
30#include <map>
31
33
34#include "my_inttypes.h"
35
36class THD;
37
38/**
39 Logical timestamp generator for logical timestamping binlog transactions.
40 A transaction is associated with two sequence numbers see
41 @c Transaction_ctx::last_committed and @c Transaction_ctx::sequence_number.
42 The class provides necessary interfaces including that of
43 generating a next consecutive value for the latter.
44*/
46 private:
47 std::atomic<int64> state;
48 /*
49 Offset is subtracted from the actual "absolute time" value at
50 logging a replication event. That is the event holds logical
51 timestamps in the "relative" format. They are meaningful only in
52 the context of the current binlog.
53 The member is updated (incremented) per binary log rotation.
54 */
56
57 public:
60 : state(other.state.load()), offset(other.offset) {}
61
62 int64 step();
63 int64 set_if_greater(int64 new_val);
65 int64 get_offset() { return offset; }
66 /*
67 Updates the offset.
68 This operation is invoked when binlog rotates and at that time
69 there can't any concurrent step() callers so no need to guard
70 the assignment.
71 */
72 void update_offset(int64 new_offset) {
73 assert(offset <= new_offset);
74
75 offset = new_offset;
76 }
77 ~Logical_clock() = default;
78};
79
80/**
81 Generate logical timestamps for MTS using COMMIT_ORDER
82 binlog transaction dependency tracking algorithm.
83
84 Tracks dependencies based on the commit order of transactions.
85 The time intervals during which any transaction holds all its locks are
86 tracked (the interval ends just before storage engine commit, when locks
87 are released. For an autocommit transaction it begins just before storage
88 engine prepare. For BEGIN..COMMIT transactions it begins at the end of the
89 last statement before COMMIT). Two transactions are marked as
90 non-conflicting if their respective intervals overlap. In other words,
91 if trx1 appears before trx2 in the binlog, and trx2 had acquired all its
92 locks before trx1 released its locks, then trx2 is marked such that the
93 replica can schedule it in parallel with trx1.
94*/
96 public:
97 /**
98 Main function that gets the dependencies using the COMMIT_ORDER tracker.
99
100 @param [in] thd THD of the caller.
101 @param [in,out] sequence_number sequence_number initialized and returned.
102 @param [in,out] commit_parent commit_parent to be returned.
103 */
104 void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
105
106 void update_max_committed(int64 sequence_number);
107
110 }
111
112 int64 step();
113 void rotate();
114
115 private:
116 /* Committed transactions timestamp */
118
119 /* "Prepared" transactions timestamp */
121
122 /*
123 Stores the last sequence_number of the transaction which breaks the rule of
124 lock based logical clock. commit_parent of the following transactions
125 will be set to m_last_blocking_transaction if their last_committed is
126 smaller than m_last_blocking_transaction.
127 */
129};
130
131/**
132 Generate logical timestamps for MTS using WRITESET
133 binlog transaction dependency tracking algorithm.
134
135 Tracks dependencies based on the set of rows updated. Any two transactions
136 that change disjoint sets of rows, are said concurrent and non-contending.
137*/
139 public:
140 Writeset_trx_dependency_tracker(ulong max_history_size)
141 : m_opt_max_history_size(max_history_size), m_writeset_history_start(0) {}
142
143 /**
144 Main function that gets the dependencies using the WRITESET tracker.
145
146 @param [in] thd THD of the caller.
147 @param [in,out] sequence_number sequence_number initialized and returned.
148 @param [in,out] commit_parent commit_parent to be returned.
149 */
150 void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
151
152 void rotate(int64 start);
153
154 /* Atomic variable - opt_binlog_transaction_dependency_history_size */
155 std::atomic<ulong> m_opt_max_history_size;
156
157 private:
158 /*
159 Monitor the last transaction with write-set to use as the minimal
160 commit parent when logical clock source is WRITE_SET, i.e., the most recent
161 transaction that is not in the history, or 0 when the history is empty.
162
163 The m_writeset_history_start must to be set to 0 initially and the history
164 is cleared, so that it is updated to the first transaction for which the
165 dependencies are checked.
166 */
168
169 /*
170 Track the last transaction sequence number that changed each row
171 in the database, using row hashes from the writeset as the index.
172 */
173 typedef std::map<uint64, int64> Writeset_history;
175};
176
177/**
178 Dependency tracker is a container singleton that dispatches between the three
179 methods associated with the binlog transaction dependency tracking algorithm.
180 There is a singleton instance of each of these classes.
181*/
183 public:
185
186 void get_dependency(THD *thd, int64 &sequence_number, int64 &commit_parent);
187
188 void update_max_committed(THD *thd);
190
191 int64 step();
192 void rotate();
193
194 public:
196
197 private:
200};
201
202#endif /* RPL_TRX_TRACKING_INCLUDED */
Contains the classes representing events occurring in the replication stream.
Generate logical timestamps for MTS using COMMIT_ORDER binlog transaction dependency tracking algorit...
Definition: rpl_trx_tracking.h:95
Logical_clock get_max_committed_transaction()
Definition: rpl_trx_tracking.h:108
void rotate()
Definition: rpl_trx_tracking.cc:181
int64 step()
Definition: rpl_trx_tracking.cc:177
int64 m_last_blocking_transaction
Definition: rpl_trx_tracking.h:128
void update_max_committed(int64 sequence_number)
Definition: rpl_trx_tracking.cc:188
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:146
Logical_clock m_max_committed_transaction
Definition: rpl_trx_tracking.h:117
Logical_clock m_transaction_counter
Definition: rpl_trx_tracking.h:120
Logical timestamp generator for logical timestamping binlog transactions.
Definition: rpl_trx_tracking.h:45
Logical_clock(const Logical_clock &other)
Definition: rpl_trx_tracking.h:59
int64 step()
Steps the absolute value of the clock (state) to return an updated value.
Definition: rpl_trx_tracking.cc:65
int64 set_if_greater(int64 new_val)
To try setting the clock forward.
Definition: rpl_trx_tracking.cc:83
void update_offset(int64 new_offset)
Definition: rpl_trx_tracking.h:72
std::atomic< int64 > state
Definition: rpl_trx_tracking.h:47
Logical_clock()
Definition: rpl_trx_tracking.cc:44
int64 offset
Definition: rpl_trx_tracking.h:55
int64 get_timestamp()
Atomically fetch the current state.
Definition: rpl_trx_tracking.cc:50
~Logical_clock()=default
int64 get_offset()
Definition: rpl_trx_tracking.h:65
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Dependency tracker is a container singleton that dispatches between the three methods associated with...
Definition: rpl_trx_tracking.h:182
Transaction_dependency_tracker()
Definition: rpl_trx_tracking.h:184
void rotate()
Definition: rpl_trx_tracking.cc:336
Commit_order_trx_dependency_tracker m_commit_order
Definition: rpl_trx_tracking.h:199
void update_max_committed(THD *thd)
The method is to be executed right before committing time.
Definition: rpl_trx_tracking.cc:321
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:302
Writeset_trx_dependency_tracker m_writeset
Definition: rpl_trx_tracking.h:198
int64 get_max_committed_timestamp()
Definition: rpl_trx_tracking.cc:346
int64 step()
Definition: rpl_trx_tracking.cc:334
Writeset_trx_dependency_tracker * get_writeset()
Definition: rpl_trx_tracking.h:195
Generate logical timestamps for MTS using WRITESET binlog transaction dependency tracking algorithm.
Definition: rpl_trx_tracking.h:138
void rotate(int64 start)
Definition: rpl_trx_tracking.cc:293
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
Writeset_trx_dependency_tracker(ulong max_history_size)
Definition: rpl_trx_tracking.h:140
std::map< uint64, int64 > Writeset_history
Definition: rpl_trx_tracking.h:173
Writeset_history m_writeset_history
Definition: rpl_trx_tracking.h:174
std::atomic< ulong > m_opt_max_history_size
Definition: rpl_trx_tracking.h:155
int64 m_writeset_history_start
Definition: rpl_trx_tracking.h:167
const int64_t SEQ_UNINIT
Uninitialized timestamp value (for either last committed or sequence number).
Definition: binlog_event.h:158
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:68
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:308