MySQL  8.0.18
Source Code Documentation
tc_log.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2017, 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 TC_LOG_H
24 #define TC_LOG_H
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "my_dbug.h"
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() {}
68  virtual ~TC_LOG() {}
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:
138  int open(const char *) { return 0; }
139  void close() {}
140  enum_result commit(THD *thd, bool all);
141  int rollback(THD *thd, bool all);
142  int prepare(THD *thd, bool all);
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
151  } PAGE_STATE;
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);
194  void close();
195  enum_result commit(THD *thd, bool all);
196  int rollback(THD *thd, bool all);
197  int prepare(THD *thd, bool all);
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  */
227  /* searching for an empty slot */
228  while (*p->ptr) {
229  p->ptr++;
230  DBUG_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  @return
248  @retval false Success
249  @retval true Failure
250  */
252  p->waiters++;
253  while (p->state == PS_DIRTY && syncing) {
254  mysql_cond_wait(&p->cond, &LOCK_tc);
255  }
256  p->waiters--;
257 
258  return p->state == PS_ERROR;
259  }
260 
261  /*
262  the following friend declaration is to grant access from TCLogMMapTest
263  to methods log_xid()/unlog() that are private.
264  */
265  friend class TCLogMMapTest;
266 };
267 
268 extern TC_LOG *tc_log;
269 extern TC_LOG_MMAP tc_log_mmap;
271 
272 #endif // TC_LOG_H
PAGE * pages
Definition: tc_log.h:174
unsigned long long int ulonglong
Definition: my_inttypes.h:55
void overflow()
Definition: tc_log.cc:263
enum_result commit(THD *thd, bool all)
Commit the transaction.
Definition: tc_log.cc:289
void unlog(ulong cookie, my_xid xid)
erase xid from the page, update page free space counters/pointers.
Definition: tc_log.cc:430
unsigned char uchar
Definition: my_inttypes.h:51
PAGE * pool
Definition: tc_log.h:174
Definition: tc_log.h:70
TC_LOG_MMAP()
Definition: tc_log.h:192
my_xid * start
Definition: tc_log.h:156
File fd
Definition: tc_log.h:170
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
Some integer typedefs for easier portability.
Transaction Coordinator Log.
Definition: tc_log.h:54
int recover()
Definition: tc_log.cc:480
int rollback(THD *thd, bool all)
Log a rollback record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:62
virtual int rollback(THD *thd, bool all)=0
Log a rollback record of the transaction to the transaction coordinator log.
virtual int prepare(THD *thd, bool all)=0
Log a prepare record of the transaction to the storage engines.
virtual enum_result commit(THD *thd, bool all)=0
Log a commit record of the transaction to the transaction coordinator log.
#define mysql_cond_wait(C, M)
Definition: mysql_cond.h:43
Instrumentation helpers for conditions.
int free
Definition: tc_log.h:158
ulonglong my_xid
Definition: tc_log.h:37
PAGE * syncing
Definition: tc_log.h:174
int rollback(THD *thd, bool all)
Log a rollback record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:307
bool using_heuristic_recover()
Perform heuristic recovery, if –tc-heuristic-recover was used.
Definition: tc_log.cc:524
int size
Definition: tc_log.h:158
Definition: tc_log.h:148
Definition: tc_log.h:70
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
uint inited
Definition: tc_log.h:172
my_xid * end
Definition: tc_log.h:156
int prepare(THD *thd, bool all)
Log a prepare record of the transaction to the storage engines.
Definition: tc_log.cc:66
void close()
Close the transaction coordinator log and free any resources.
Definition: tc_log.cc:449
TC_LOG()
Definition: tc_log.h:67
PAGE_STATE
Definition: tc_log.h:147
TC_LOG_DUMMY()
Definition: tc_log.h:137
my_xid * ptr
Definition: tc_log.h:157
virtual ~TC_LOG()
Definition: tc_log.h:68
int prepare(THD *thd, bool all)
Log a prepare record of the transaction to the storage engines.
Definition: tc_log.cc:311
ulong log_xid(my_xid xid)
Record that transaction XID is committed on the persistent storage.
Definition: tc_log.cc:341
ulonglong my_xid
recover() step of xa.
Definition: handler.h:6788
virtual void close()=0
Close the transaction coordinator log and free any resources.
unsigned int uint
Definition: uca-dump.cc:29
#define FN_REFLEN
Definition: my_io.h:82
PAGE * get_active_from_pool()
there is no active page, let&#39;s got one from the pool.
Definition: tc_log.cc:230
int open(const char *)
Initialize and open the coordinator log.
Definition: tc_log.h:138
enum_result
Definition: tc_log.h:70
TC_LOG * tc_log
Definition: tc_log.cc:520
int open(const char *opt_name)
Initialize and open the coordinator log.
Definition: tc_log.cc:117
PAGE * next
Definition: tc_log.h:155
Definition: tc_log.h:154
bool wait_sync_completion(PAGE *p)
Wait for until page data will be written to the disk.
Definition: tc_log.h:251
Common header for many mysys elements.
uchar * data
Definition: tc_log.h:173
char logname[FN_REFLEN]
Definition: tc_log.h:169
Instrumentation helpers for conditions.
int File
Definition: my_io_bits.h:50
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
virtual int open(const char *opt_name)=0
Initialize and open the coordinator log.
uint size() const
Get the total amount of potentially usable slots for XIDs in TC log.
Definition: tc_log.cc:210
unsigned long my_off_t
Definition: my_inttypes.h:77
Definition: tc_log.h:150
void close()
Close the transaction coordinator log and free any resources.
Definition: tc_log.h:139
int waiters
Definition: tc_log.h:159
PAGE * active
Definition: tc_log.h:174
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
friend class TCLogMMapTest
Definition: tc_log.h:265
TC_LOG_MMAP tc_log_mmap
Definition: tc_log.cc:522
ABI for instrumented mutexes.
Common #defines and includes for file and socket I/O.
mysql_cond_t COND_active
Signalled when active PAGE is moved to syncing state, thus member "active" becomes 0...
Definition: tc_log.h:184
Definition: tc_log.h:149
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
TC_LOG_DUMMY tc_log_dummy
Definition: tc_log.cc:521
const char * p
Definition: ctype-mb.cc:1232
int my_msync(int, void *, size_t, int)
bool sync()
Write the page data being synchronized to the disk.
Definition: tc_log.cc:395
my_off_t file_length
Definition: tc_log.h:171
enum_result commit(THD *thd, bool all)
Log a commit record of the transaction to the transaction coordinator log.
Definition: tc_log.cc:58
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
static int flags[50]
Definition: hp_test1.cc:39
PAGE_STATE state
Definition: tc_log.h:160
mysql_mutex_t LOCK_tc
Definition: tc_log.h:179
PAGE ** pool_last_ptr
Definition: tc_log.h:174
unsigned long ulong
Definition: my_inttypes.h:48
Definition: tc_log.h:70
Definition: tc_log.h:134
uint npages
Definition: tc_log.h:172
virtual int do_msync_and_fsync(int fd_arg, void *addr, size_t len, int flags)
Definition: tc_log.h:210
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
Definition: tc_log.h:145