MySQL  8.0.27
Source Code Documentation
tc_log.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2021, 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 TC_LOG_H
24 #define TC_LOG_H
25 
26 #include <assert.h>
27 #include <stddef.h>
28 #include <sys/types.h>
29 
30 #include "my_inttypes.h"
31 #include "my_io.h"
32 #include "my_sys.h" // my_msync
35 #include "mysql/psi/mysql_cond.h"
36 
37 class THD;
38 
39 typedef ulonglong my_xid;
40 
41 #define TC_LOG_MIN_PAGES 6
42 
43 /**
44  Transaction Coordinator Log.
45 
46  A base abstract class for three different implementations of the
47  transaction coordinator.
48 
49  The server uses the transaction coordinator to order transactions
50  correctly and there are three different implementations: one using
51  an in-memory structure, one dummy that does not do anything, and one
52  using the binary log for transaction coordination.
53 */
54 class TC_LOG {
55  public:
56  /**
57  Perform heuristic recovery, if --tc-heuristic-recover was used.
58 
59  @note no matter whether heuristic recovery was successful or not
60  mysqld must exit. So, return value is the same in both cases.
61 
62  @retval false no heuristic recovery was requested
63  @retval true heuristic recovery was performed
64  */
66 
67  TC_LOG() = default;
68  virtual ~TC_LOG() = default;
69 
71 
72  /**
73  Initialize and open the coordinator log.
74  Do recovery if necessary. Called during server startup.
75 
76  @param opt_name Name of logfile.
77 
78  @retval 0 sucess
79  @retval 1 failed
80  */
81  virtual int open(const char *opt_name) = 0;
82 
83  /**
84  Close the transaction coordinator log and free any resources.
85  Called during server shutdown.
86  */
87  virtual void close() = 0;
88 
89  /**
90  Log a commit record of the transaction to the transaction
91  coordinator log.
92 
93  When the function returns, the transaction commit is properly
94  logged to the transaction coordinator log and can be committed in
95  the storage engines.
96 
97  @param thd Session to log transaction for.
98  @param all @c True if this is a "real" commit, @c false if it is a
99  "statement" commit.
100 
101  @return Error code on failure, zero on success.
102  */
103  virtual enum_result commit(THD *thd, bool all) = 0;
104 
105  /**
106  Log a rollback record of the transaction to the transaction
107  coordinator log.
108 
109  When the function returns, the transaction have been aborted in
110  the transaction coordinator log.
111 
112  @param thd Session to log transaction record for.
113 
114  @param all @c true if an explicit commit or an implicit commit
115  for a statement, @c false if an internal commit of the statement.
116 
117  @return Error code on failure, zero on success.
118  */
119  virtual int rollback(THD *thd, bool all) = 0;
120 
121  /**
122  Log a prepare record of the transaction to the storage engines.
123 
124  @param thd Session to log transaction record for.
125 
126  @param all @c true if an explicit commit or an implicit commit
127  for a statement, @c false if an internal commit of the statement.
128 
129  @return Error code on failure, zero on success.
130  */
131  virtual int prepare(THD *thd, bool all) = 0;
132 };
133 
134 class TC_LOG_DUMMY : public TC_LOG // use it to disable the logging
135 {
136  public:
137  TC_LOG_DUMMY() = default;
138  int open(const char *) override { return 0; }
139  void close() override {}
140  enum_result commit(THD *thd, bool all) override;
141  int rollback(THD *thd, bool all) override;
142  int prepare(THD *thd, bool all) override;
143 };
144 
145 class TC_LOG_MMAP : public TC_LOG {
146  public: // only to keep Sun Forte on sol9x86 happy
147  typedef enum {
148  PS_POOL, // page is in pool
149  PS_ERROR, // last sync failed
150  PS_DIRTY // new xids added since last sync
152 
153  private:
154  struct PAGE {
155  PAGE *next; // pages are linked in a fifo queue
156  my_xid *start, *end; // usable area of a page
157  my_xid *ptr; // next xid will be written here
158  int size, free; // max and current number of free xid slots on the page
159  int waiters; // number of waiters on condition
160  PAGE_STATE state; // see above
161  /**
162  Signalled when syncing of this page is done or when
163  this page is in "active" slot and syncing slot just
164  became free.
165  */
167  };
168 
175  /*
176  LOCK_tc is used to protect access both to data members 'syncing',
177  'active', 'pool' and to the content of PAGE objects.
178  */
180  /**
181  Signalled when active PAGE is moved to syncing state,
182  thus member "active" becomes 0.
183  */
185  /**
186  Signalled when one more page becomes available in the
187  pool which we might select as active.
188  */
190 
191  public:
192  TC_LOG_MMAP() : inited(0) {}
193  int open(const char *opt_name) override;
194  void close() override;
195  enum_result commit(THD *thd, bool all) override;
196  int rollback(THD *thd, bool all) override;
197  int prepare(THD *thd, bool all) override;
198  int recover();
199  uint size() const;
200 
201  private:
202  ulong log_xid(my_xid xid);
203  void unlog(ulong cookie, my_xid xid);
204  PAGE *get_active_from_pool();
205  bool sync();
206  void overflow();
207 
208  protected:
209  // We want to mock away syncing to disk in unit tests.
210  virtual int do_msync_and_fsync(int fd_arg, void *addr, size_t len,
211  int flags) {
212  return my_msync(fd_arg, addr, len, flags);
213  }
214 
215  private:
216  /**
217  Find empty slot in the page and write xid value there.
218 
219  @param xid value of xid to store in the page
220  @param p pointer to the page where to store xid
221  @param data_arg pointer to the top of the mapped to memory file
222  to calculate offset value (cookie)
223 
224  @return offset value from the top of the page where the xid was stored.
225  */
226  ulong store_xid_in_empty_slot(my_xid xid, PAGE *p, uchar *data_arg) {
227  /* searching for an empty slot */
228  while (*p->ptr) {
229  p->ptr++;
230  assert(p->ptr < p->end); // because p->free > 0
231  }
232 
233  /* found! store xid there and mark the page dirty */
234  ulong cookie = (ulong)((uchar *)p->ptr - data_arg); // can never be zero
235  *p->ptr++ = xid;
236  p->free--;
237  p->state = PS_DIRTY;
238 
239  return cookie;
240  }
241 
242  /**
243  Wait for until page data will be written to the disk.
244 
245  @param p pointer to the PAGE to store to the disk
246 
247  @retval false Success
248  @retval true Failure
249  */
251  p->waiters++;
252  while (p->state == PS_DIRTY && syncing) {
253  mysql_cond_wait(&p->cond, &LOCK_tc);
254  }
255  p->waiters--;
256 
257  return p->state == PS_ERROR;
258  }
259 
260  /*
261  the following friend declaration is to grant access from TCLogMMapTest
262  to methods log_xid()/unlog() that are private.
263  */
264  friend class TCLogMMapTest;
265 };
266 
267 extern TC_LOG *tc_log;
268 extern TC_LOG_MMAP tc_log_mmap;
270 
271 #endif // TC_LOG_H
Definition: tc_log.h:135
TC_LOG_DUMMY()=default
int prepare(THD *thd, bool all) override
Log a prepare record of the transaction to the storage engines.
Definition: tc_log.cc:68
void close() override
Close the transaction coordinator log and free any resources.
Definition: tc_log.h:139
int rollback(THD *thd, bool all) override
Log a rollback record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:64
enum_result commit(THD *thd, bool all) override
Log a commit record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:60
int open(const char *) override
Initialize and open the coordinator log.
Definition: tc_log.h:138
Definition: tc_log.h:145
ulong store_xid_in_empty_slot(my_xid xid, PAGE *p, uchar *data_arg)
Find empty slot in the page and write xid value there.
Definition: tc_log.h:226
PAGE * get_active_from_pool()
there is no active page, let's got one from the pool.
Definition: tc_log.cc:232
enum_result commit(THD *thd, bool all) override
Commit the transaction.
Definition: tc_log.cc:292
PAGE * pages
Definition: tc_log.h:174
uint size() const
Get the total amount of potentially usable slots for XIDs in TC log.
Definition: tc_log.cc:212
my_off_t file_length
Definition: tc_log.h:171
bool wait_sync_completion(PAGE *p)
Wait for until page data will be written to the disk.
Definition: tc_log.h:250
PAGE * pool
Definition: tc_log.h:174
File fd
Definition: tc_log.h:170
void unlog(ulong cookie, my_xid xid)
erase xid from the page, update page free space counters/pointers.
Definition: tc_log.cc:432
void close() override
Close the transaction coordinator log and free any resources.
Definition: tc_log.cc:451
virtual int do_msync_and_fsync(int fd_arg, void *addr, size_t len, int flags)
Definition: tc_log.h:210
uchar * data
Definition: tc_log.h:173
uint npages
Definition: tc_log.h:172
mysql_mutex_t LOCK_tc
Definition: tc_log.h:179
PAGE * syncing
Definition: tc_log.h:174
char logname[FN_REFLEN]
Definition: tc_log.h:169
TC_LOG_MMAP()
Definition: tc_log.h:192
void overflow()
Definition: tc_log.cc:266
PAGE_STATE
Definition: tc_log.h:147
@ PS_DIRTY
Definition: tc_log.h:150
@ PS_POOL
Definition: tc_log.h:148
@ PS_ERROR
Definition: tc_log.h:149
mysql_cond_t COND_pool
Signalled when one more page becomes available in the pool which we might select as active.
Definition: tc_log.h:189
friend class TCLogMMapTest
Definition: tc_log.h:264
PAGE ** pool_last_ptr
Definition: tc_log.h:174
int recover()
Definition: tc_log.cc:482
int open(const char *opt_name) override
Initialize and open the coordinator log.
Definition: tc_log.cc:119
PAGE * active
Definition: tc_log.h:174
uint inited
Definition: tc_log.h:172
ulong log_xid(my_xid xid)
Record that transaction XID is committed on the persistent storage.
Definition: tc_log.cc:344
mysql_cond_t COND_active
Signalled when active PAGE is moved to syncing state, thus member "active" becomes 0.
Definition: tc_log.h:184
int rollback(THD *thd, bool all) override
Log a rollback record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:310
int prepare(THD *thd, bool all) override
Log a prepare record of the transaction to the storage engines.
Definition: tc_log.cc:314
bool sync()
Write the page data being synchronized to the disk.
Definition: tc_log.cc:397
Transaction Coordinator Log.
Definition: tc_log.h:54
virtual int open(const char *opt_name)=0
Initialize and open the coordinator log.
virtual enum_result commit(THD *thd, bool all)=0
Log a commit record of the transaction to the transaction coordinator log.
virtual ~TC_LOG()=default
bool using_heuristic_recover()
Perform heuristic recovery, if –tc-heuristic-recover was used.
Definition: tc_log.cc:526
virtual void close()=0
Close the transaction coordinator log and free any resources.
TC_LOG()=default
virtual int prepare(THD *thd, bool all)=0
Log a prepare record of the transaction to the storage engines.
virtual int rollback(THD *thd, bool all)=0
Log a rollback record of the transaction to the transaction coordinator log.
enum_result
Definition: tc_log.h:70
@ RESULT_INCONSISTENT
Definition: tc_log.h:70
@ RESULT_SUCCESS
Definition: tc_log.h:70
@ RESULT_ABORTED
Definition: tc_log.h:70
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
#define mysql_cond_wait(C, M)
Definition: mysql_cond.h:47
const char * p
Definition: ctype-mb.cc:1236
int my_msync(int, void *, size_t, int)
Definition: my_mmap.cc:50
static int flags[50]
Definition: hp_test1.cc:39
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
ulonglong my_off_t
Definition: my_inttypes.h:71
unsigned char uchar
Definition: my_inttypes.h:51
Common #defines and includes for file and socket I/O.
#define FN_REFLEN
Definition: my_io.h:82
int File
Definition: my_io_bits.h:50
Common header for many mysys elements.
Instrumentation helpers for conditions.
ABI for instrumented mutexes.
Instrumentation helpers for conditions.
ulonglong my_xid
recover() step of xa.
Definition: handler.h:6931
Definition: tc_log.h:154
PAGE * next
Definition: tc_log.h:155
my_xid * start
Definition: tc_log.h:156
int waiters
Definition: tc_log.h:159
my_xid * end
Definition: tc_log.h:156
int free
Definition: tc_log.h:158
PAGE_STATE state
Definition: tc_log.h:160
int size
Definition: tc_log.h:158
mysql_cond_t cond
Signalled when syncing of this page is done or when this page is in "active" slot and syncing slot ju...
Definition: tc_log.h:166
my_xid * ptr
Definition: tc_log.h:157
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
TC_LOG_DUMMY tc_log_dummy
Definition: tc_log.cc:523
TC_LOG_MMAP tc_log_mmap
Definition: tc_log.cc:524
TC_LOG * tc_log
Definition: tc_log.cc:522
ulonglong my_xid
Definition: tc_log.h:37
unsigned int uint
Definition: uca-dump.cc:29
static int all(site_def const *s, node_no node)
Definition: xcom_transport.cc:858