MySQL  8.0.12
Source Code Documentation
mtr0mtr.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 1995, 2018, Oracle and/or its affiliates. All Rights Reserved.
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25  The lines above are intentionally left blank
26 */
27 
28 /** @file include/mtr0mtr.h
29  Mini-transaction buffer
30 
31  Created 11/26/1995 Heikki Tuuri
32  *******************************************************/
33 
34 #ifndef mtr0mtr_h
35 #define mtr0mtr_h
36 
37 #include <stddef.h>
38 
39 #include "buf0types.h"
40 #include "dyn0buf.h"
41 #include "fil0fil.h"
42 #include "srv0srv.h"
43 
44 /** Start a mini-transaction. */
45 #define mtr_start(m) (m)->start()
46 
47 /** Start a synchronous mini-transaction */
48 #define mtr_start_sync(m) (m)->start(true)
49 
50 /** Start an asynchronous read-only mini-transaction */
51 #define mtr_start_ro(m) (m)->start(true, true)
52 
53 /** Commit a mini-transaction. */
54 #define mtr_commit(m) (m)->commit()
55 
56 /** Set and return a savepoint in mtr.
57 @return savepoint */
58 #define mtr_set_savepoint(m) (m)->get_savepoint()
59 
60 /** Release the (index tree) s-latch stored in an mtr memo after a
61 savepoint. */
62 #define mtr_release_s_latch_at_savepoint(m, s, l) \
63  (m)->release_s_latch_at_savepoint((s), (l))
64 
65 /** Get the logging mode of a mini-transaction.
66 @return logging mode: MTR_LOG_NONE, ... */
67 #define mtr_get_log_mode(m) (m)->get_log_mode()
68 
69 /** Change the logging mode of a mini-transaction.
70 @return old mode */
71 #define mtr_set_log_mode(m, d) (m)->set_log_mode((d))
72 
73 /** Get the flush observer of a mini-transaction.
74 @return flush observer object */
75 #define mtr_get_flush_observer(m) (m)->get_flush_observer()
76 
77 /** Set the flush observer of a mini-transaction. */
78 #define mtr_set_flush_observer(m, d) (m)->set_flush_observer((d))
79 
80 /** Read 1 - 4 bytes from a file page buffered in the buffer pool.
81 @return value read */
82 #define mtr_read_ulint(p, t, m) (m)->read_ulint((p), (t))
83 
84 /** Release an object in the memo stack.
85 @return true if released */
86 #define mtr_memo_release(m, o, t) (m)->memo_release((o), (t))
87 
88 #ifdef UNIV_DEBUG
89 
90 /** Check if memo contains the given item ignore if table is intrinsic
91 @return true if contains or table is intrinsic. */
92 #define mtr_is_block_fix(m, o, t, table) \
93  (mtr_memo_contains(m, o, t) || table->is_intrinsic())
94 
95 /** Check if memo contains the given page ignore if table is intrinsic
96 @return true if contains or table is intrinsic. */
97 #define mtr_is_page_fix(m, p, t, table) \
98  (mtr_memo_contains_page(m, p, t) || table->is_intrinsic())
99 
100 /** Check if memo contains the given item.
101 @return true if contains */
102 #define mtr_memo_contains(m, o, t) (m)->memo_contains((m)->get_memo(), (o), (t))
103 
104 /** Check if memo contains the given page.
105 @return true if contains */
106 #define mtr_memo_contains_page(m, p, t) \
107  (m)->memo_contains_page_flagged((p), (t))
108 #endif /* UNIV_DEBUG */
109 
110 /** Print info of an mtr handle. */
111 #define mtr_print(m) (m)->print()
112 
113 /** Return the log object of a mini-transaction buffer.
114 @return log */
115 #define mtr_get_log(m) (m)->get_log()
116 
117 /** Push an object to an mtr memo stack. */
118 #define mtr_memo_push(m, o, t) (m)->memo_push(o, t)
119 
120 /** Lock an rw-lock in s-mode. */
121 #define mtr_s_lock(l, m) (m)->s_lock((l), __FILE__, __LINE__)
122 
123 /** Lock an rw-lock in x-mode. */
124 #define mtr_x_lock(l, m) (m)->x_lock((l), __FILE__, __LINE__)
125 
126 /** Lock a tablespace in x-mode. */
127 #define mtr_x_lock_space(s, m) (m)->x_lock_space((s), __FILE__, __LINE__)
128 
129 /** Lock an rw-lock in sx-mode. */
130 #define mtr_sx_lock(l, m) (m)->sx_lock((l), __FILE__, __LINE__)
131 
132 #define mtr_memo_contains_flagged(m, p, l) (m)->memo_contains_flagged((p), (l))
133 
134 #define mtr_memo_contains_page_flagged(m, p, l) \
135  (m)->memo_contains_page_flagged((p), (l))
136 
137 #define mtr_release_block_at_savepoint(m, s, b) \
138  (m)->release_block_at_savepoint((s), (b))
139 
140 #define mtr_block_sx_latch_at_savepoint(m, s, b) \
141  (m)->sx_latch_at_savepoint((s), (b))
142 
143 #define mtr_block_x_latch_at_savepoint(m, s, b) \
144  (m)->x_latch_at_savepoint((s), (b))
145 
146 /** Check if a mini-transaction is dirtying a clean page.
147 @param b block being x-fixed
148 @return true if the mtr is dirtying a clean page. */
149 #define mtr_block_dirtied(b) mtr_t::is_block_dirtied((b))
150 
151 /** Forward declaration of a tablespace object */
152 struct fil_space_t;
153 
154 /** Mini-transaction memo stack slot. */
156  /** pointer to the object */
157  void *object;
158 
159  /** type of the stored object (MTR_MEMO_S_LOCK, ...) */
160  ulint type;
161 };
162 
163 /** Mini-transaction handle and buffer */
164 struct mtr_t {
165  /** State variables of the mtr */
166  struct Impl {
167  /** memo stack for locks etc. */
169 
170  /** mini-transaction log */
172 
173  /** true if mtr has made at least one buffer pool page dirty */
175 
176  /** true if inside ibuf changes */
178 
179  /** true if the mini-transaction modified buffer pool pages */
181 
182  /** Count of how many page initial log records have been
183  written to the mtr log */
184  ib_uint32_t m_n_log_recs;
185 
186  /** specifies which operations should be logged; default
187  value MTR_LOG_ALL */
189 
190  /** State of the transaction */
192 
193  /** Flush Observer */
195 
196 #ifdef UNIV_DEBUG
197  /** For checking corruption. */
198  ulint m_magic_n;
199 #endif /* UNIV_DEBUG */
200 
201  /** Owning mini-transaction */
203  };
204 
205  mtr_t() { m_impl.m_state = MTR_STATE_INIT; }
206 
207  ~mtr_t() {
208 #ifdef UNIV_DEBUG
209  switch (m_impl.m_state) {
210  case MTR_STATE_ACTIVE:
211  ut_ad(m_impl.m_memo.size() == 0);
212  break;
213  case MTR_STATE_INIT:
214  case MTR_STATE_COMMITTED:
215  break;
217  ut_error;
218  }
219 #endif /* UNIV_DEBUG */
220  }
221 
222  /** Start a mini-transaction.
223  @param sync true if it is a synchronous mini-transaction
224  @param read_only true if read only mini-transaction */
225  void start(bool sync = true, bool read_only = false);
226 
227  /** @return whether this is an asynchronous mini-transaction. */
228  bool is_async() const { return (!m_sync); }
229 
230  /** Request a future commit to be synchronous. */
231  void set_sync() { m_sync = true; }
232 
233  /** Commit the mini-transaction. */
234  void commit();
235 
236  /** Return current size of the buffer.
237  @return savepoint */
238  ulint get_savepoint() const MY_ATTRIBUTE((warn_unused_result)) {
239  ut_ad(is_active());
240  ut_ad(m_impl.m_magic_n == MTR_MAGIC_N);
241 
242  return (m_impl.m_memo.size());
243  }
244 
245  /** Release the (index tree) s-latch stored in an mtr memo after a
246  savepoint.
247  @param savepoint value returned by @see set_savepoint.
248  @param lock latch to release */
249  inline void release_s_latch_at_savepoint(ulint savepoint, rw_lock_t *lock);
250 
251  /** Release the block in an mtr memo after a savepoint. */
252  inline void release_block_at_savepoint(ulint savepoint, buf_block_t *block);
253 
254  /** SX-latch a not yet latched block after a savepoint. */
255  inline void sx_latch_at_savepoint(ulint savepoint, buf_block_t *block);
256 
257  /** X-latch a not yet latched block after a savepoint. */
258  inline void x_latch_at_savepoint(ulint savepoint, buf_block_t *block);
259 
260  /** Get the logging mode.
261  @return logging mode */
262  inline mtr_log_t get_log_mode() const MY_ATTRIBUTE((warn_unused_result));
263 
264  /** Change the logging mode.
265  @param mode logging mode
266  @return old mode */
267  inline mtr_log_t set_log_mode(mtr_log_t mode);
268 
269  /** Read 1 - 4 bytes from a file page buffered in the buffer pool.
270  @param ptr pointer from where to read
271  @param type MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES
272  @return value read */
273  inline uint32_t read_ulint(const byte *ptr, mlog_id_t type) const
274  MY_ATTRIBUTE((warn_unused_result));
275 
276  /** Locks a rw-latch in S mode.
277  NOTE: use mtr_s_lock().
278  @param lock rw-lock
279  @param file file name from where called
280  @param line line number in file */
281  inline void s_lock(rw_lock_t *lock, const char *file, ulint line);
282 
283  /** Locks a rw-latch in X mode.
284  NOTE: use mtr_x_lock().
285  @param lock rw-lock
286  @param file file name from where called
287  @param line line number in file */
288  inline void x_lock(rw_lock_t *lock, const char *file, ulint line);
289 
290  /** Locks a rw-latch in X mode.
291  NOTE: use mtr_sx_lock().
292  @param lock rw-lock
293  @param file file name from where called
294  @param line line number in file */
295  inline void sx_lock(rw_lock_t *lock, const char *file, ulint line);
296 
297  /** Acquire a tablespace X-latch.
298  NOTE: use mtr_x_lock_space().
299  @param[in] space tablespace instance
300  @param[in] file file name from where called
301  @param[in] line line number in file */
302  void x_lock_space(fil_space_t *space, const char *file, ulint line);
303 
304  /** Release an object in the memo stack.
305  @param object object
306  @param type object type: MTR_MEMO_S_LOCK, ... */
307  void memo_release(const void *object, ulint type);
308 
309  /** Release a page latch.
310  @param[in] ptr pointer to within a page frame
311  @param[in] type object type: MTR_MEMO_PAGE_X_FIX, ... */
312  void release_page(const void *ptr, mtr_memo_type_t type);
313 
314  /** Note that the mini-transaction has modified data. */
315  void set_modified() { m_impl.m_modifications = true; }
316 
317  /** Set the state to not-modified. This will not log the
318  changes. This is only used during redo log apply, to avoid
319  logging the changes. */
320  void discard_modifications() { m_impl.m_modifications = false; }
321 
322  /** Get the LSN of commit().
323  @return the commit LSN
324  @retval 0 if the transaction only modified temporary tablespaces */
325  lsn_t commit_lsn() const MY_ATTRIBUTE((warn_unused_result)) {
326  ut_ad(has_committed());
327  ut_ad(m_impl.m_log_mode == MTR_LOG_ALL);
328  return (m_commit_lsn);
329  }
330 
331  /** Note that we are inside the change buffer code. */
332  void enter_ibuf() { m_impl.m_inside_ibuf = true; }
333 
334  /** Note that we have exited from the change buffer code. */
335  void exit_ibuf() { m_impl.m_inside_ibuf = false; }
336 
337  /** @return true if we are inside the change buffer code */
338  bool is_inside_ibuf() const MY_ATTRIBUTE((warn_unused_result)) {
339  return (m_impl.m_inside_ibuf);
340  }
341 
342  /*
343  @return true if the mini-transaction is active */
344  bool is_active() const MY_ATTRIBUTE((warn_unused_result)) {
345  return (m_impl.m_state == MTR_STATE_ACTIVE);
346  }
347 
348  /** Get flush observer
349  @return flush observer */
350  FlushObserver *get_flush_observer() const MY_ATTRIBUTE((warn_unused_result)) {
351  return (m_impl.m_flush_observer);
352  }
353 
354  /** Set flush observer
355  @param[in] observer flush observer */
357  ut_ad(observer == NULL || m_impl.m_log_mode == MTR_LOG_NO_REDO);
358 
359  m_impl.m_flush_observer = observer;
360  }
361 
362 #ifdef UNIV_DEBUG
363  /** Check if memo contains the given item.
364  @param memo memo stack
365  @param object object to search
366  @param type type of object
367  @return true if contains */
368  static bool memo_contains(mtr_buf_t *memo, const void *object, ulint type)
369  MY_ATTRIBUTE((warn_unused_result));
370 
371  /** Check if memo contains the given item.
372  @param ptr object to search
373  @param flags specify types of object (can be ORred) of
374  MTR_MEMO_PAGE_S_FIX ... values
375  @return true if contains */
376  bool memo_contains_flagged(const void *ptr, ulint flags) const
377  MY_ATTRIBUTE((warn_unused_result));
378 
379  /** Check if memo contains the given page.
380  @param[in] ptr pointer to within buffer frame
381  @param[in] flags specify types of object with OR of
382  MTR_MEMO_PAGE_S_FIX... values
383  @return the block
384  @retval NULL if not found */
385  buf_block_t *memo_contains_page_flagged(const byte *ptr, ulint flags) const
386  MY_ATTRIBUTE((warn_unused_result));
387 
388  /** Mark the given latched page as modified.
389  @param[in] ptr pointer to within buffer frame */
390  void memo_modify_page(const byte *ptr);
391 
392  /** Print info of an mtr handle. */
393  void print() const;
394 
395  /** @return true if the mini-transaction has committed */
396  bool has_committed() const MY_ATTRIBUTE((warn_unused_result)) {
397  return (m_impl.m_state == MTR_STATE_COMMITTED);
398  }
399 
400  /** @return true if the mini-transaction is committing */
401  bool is_committing() const {
402  return (m_impl.m_state == MTR_STATE_COMMITTING);
403  }
404 
405  /** @return true if mini-transaction contains modifications. */
406  bool has_modifications() const MY_ATTRIBUTE((warn_unused_result)) {
407  return (m_impl.m_modifications);
408  }
409 
410  /** @return the memo stack */
411  const mtr_buf_t *get_memo() const MY_ATTRIBUTE((warn_unused_result)) {
412  return (&m_impl.m_memo);
413  }
414 
415  /** @return the memo stack */
416  mtr_buf_t *get_memo() MY_ATTRIBUTE((warn_unused_result)) {
417  return (&m_impl.m_memo);
418  }
419 #endif /* UNIV_DEBUG */
420 
421  /** @return true if a record was added to the mini-transaction */
422  bool is_dirty() const MY_ATTRIBUTE((warn_unused_result)) {
423  return (m_impl.m_made_dirty);
424  }
425 
426  /** Note that a record has been added to the log */
427  void added_rec() { ++m_impl.m_n_log_recs; }
428 
429  /** Get the buffered redo log of this mini-transaction.
430  @return redo log */
431  const mtr_buf_t *get_log() const MY_ATTRIBUTE((warn_unused_result)) {
432  ut_ad(m_impl.m_magic_n == MTR_MAGIC_N);
433 
434  return (&m_impl.m_log);
435  }
436 
437  /** Get the buffered redo log of this mini-transaction.
438  @return redo log */
439  mtr_buf_t *get_log() MY_ATTRIBUTE((warn_unused_result)) {
440  ut_ad(m_impl.m_magic_n == MTR_MAGIC_N);
441 
442  return (&m_impl.m_log);
443  }
444 
445  /** Push an object to an mtr memo stack.
446  @param object object
447  @param type object type: MTR_MEMO_S_LOCK, ... */
448  inline void memo_push(void *object, mtr_memo_type_t type);
449 
450  /** Check if this mini-transaction is dirtying a clean page.
451  @param block block being x-fixed
452  @return true if the mtr is dirtying a clean page. */
453  static bool is_block_dirtied(const buf_block_t *block)
454  MY_ATTRIBUTE((warn_unused_result));
455 
456  private:
458 
459  /** LSN at commit time */
461 
462  /** true if it is synchronous mini-transaction */
463  bool m_sync;
464 
465  class Command;
466 
467  friend class Command;
468 };
469 
470 #include "mtr0mtr.ic"
471 
472 #endif /* mtr0mtr_h */
unsigned char byte
Blob class.
Definition: common.h:159
mtr_buf_t m_memo
memo stack for locks etc.
Definition: mtr0mtr.h:168
static void print(const char *key, int keylen, const char *val, int vallen)
Print the key value pair.
Definition: mcstat.c:106
The structure used in the spin lock implementation of a read-write lock.
Definition: sync0rw.h:554
uint64_t lsn_t
Type used for all log sequence number storage and arithmetics.
Definition: log0types.h:59
mtr_t()
Definition: mtr0mtr.h:205
Default mode: log all operations modifying disk-based data.
Definition: mtr0types.h:43
The low-level file system.
static bool commit(THD *thd)
Commit the current statement and transaction.
Definition: sql_cmd_srs.cc:149
The dynamically allocated buffer implementation.
void enter_ibuf()
Note that we are inside the change buffer code.
Definition: mtr0mtr.h:332
bool is_active(space_id_t space_id)
Definition: trx0purge.cc:873
void exit_ibuf()
Note that we have exited from the change buffer code.
Definition: mtr0mtr.h:335
The buffer control block structure.
Definition: buf0buf.h:1301
mlog_id_t
Definition: mtr0types.h:59
void added_rec()
Note that a record has been added to the log.
Definition: mtr0mtr.h:427
void discard_modifications()
Set the state to not-modified.
Definition: mtr0mtr.h:320
Tablespace or log data space.
Definition: fil0fil.h:180
State variables of the mtr.
Definition: mtr0mtr.h:166
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
void set_sync()
Request a future commit to be synchronous.
Definition: mtr0mtr.h:231
mtr_state_t m_state
State of the transaction.
Definition: mtr0mtr.h:191
bool has_modifications() const
Definition: mtr0mtr.h:406
mtr_state_t
Definition: mtr0types.h:276
Impl m_impl
Definition: mtr0mtr.h:457
bool is_inside_ibuf() const
Definition: mtr0mtr.h:338
mtr_buf_t * get_memo()
Definition: mtr0mtr.h:416
mtr_log_t
Logging modes for a mini-transaction.
Definition: mtr0types.h:41
FlushObserver * get_flush_observer() const
Get flush observer.
Definition: mtr0mtr.h:350
The server main program.
lsn_t m_commit_lsn
LSN at commit time.
Definition: mtr0mtr.h:460
Definition: mtr0types.h:279
void set_modified()
Note that the mini-transaction has modified data.
Definition: mtr0mtr.h:315
bool read_only
Definition: mysqld.cc:889
bool has_committed() const
Definition: mtr0mtr.h:396
mtr_memo_type_t
Types for the mlock objects to store in the mtr memo; NOTE that the first 3 values must be RW_S_LATCH...
Definition: mtr0types.h:252
mtr_t * m_mtr
Owning mini-transaction.
Definition: mtr0mtr.h:202
Mini-transaction memo stack slot.
Definition: mtr0mtr.h:155
void set_flush_observer(FlushObserver *observer)
Set flush observer.
Definition: mtr0mtr.h:356
The database buffer pool global types for the directory.
bool m_inside_ibuf
true if inside ibuf changes
Definition: mtr0mtr.h:177
Definition: mtr0mtr.cc:300
We use FlushObserver to track flushing of non-redo logged pages in bulk create index(BtrBulk.cc).Since we disable redo logging during a index build, we need to make sure that all dirty pages modifed by the index build are flushed to disk before any redo logged operations go to the index.
Definition: buf0flu.h:262
bool is_active() const
Definition: mtr0mtr.h:344
Don&#39;t generate REDO log but add dirty pages to flush list.
Definition: mtr0types.h:49
Definition: mtr0types.h:277
ulint type
type of the stored object (MTR_MEMO_S_LOCK, ...)
Definition: mtr0mtr.h:160
mtr_buf_t m_log
mini-transaction log
Definition: mtr0mtr.h:171
FlushObserver * m_flush_observer
Flush Observer.
Definition: mtr0mtr.h:194
Definition: mtr0types.h:278
ib_uint32_t m_n_log_recs
Count of how many page initial log records have been written to the mtr log.
Definition: mtr0mtr.h:184
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:60
bool is_dirty() const
Definition: mtr0mtr.h:422
#define ut_error
Abort execution.
Definition: ut0dbg.h:56
mtr_buf_t * get_log()
Get the buffered redo log of this mini-transaction.
Definition: mtr0mtr.h:439
mtr_log_t m_log_mode
specifies which operations should be logged; default value MTR_LOG_ALL
Definition: mtr0mtr.h:188
bool m_modifications
true if the mini-transaction modified buffer pool pages
Definition: mtr0mtr.h:180
Definition: mtr0types.h:280
lsn_t commit_lsn() const
Get the LSN of commit().
Definition: mtr0mtr.h:325
#define MTR_MAGIC_N
Definition: mtr0types.h:273
const mtr_buf_t * get_log() const
Get the buffered redo log of this mini-transaction.
Definition: mtr0mtr.h:431
bool is_async() const
Definition: mtr0mtr.h:228
static int flags[50]
Definition: hp_test1.cc:39
Mini-transaction handle and buffer.
Definition: mtr0mtr.h:164
const mtr_buf_t * get_memo() const
Definition: mtr0mtr.h:411
void * object
pointer to the object
Definition: mtr0mtr.h:157
~mtr_t()
Definition: mtr0mtr.h:207
ulint m_magic_n
For checking corruption.
Definition: mtr0mtr.h:198
bool m_sync
true if it is synchronous mini-transaction
Definition: mtr0mtr.h:463
bool is_committing() const
Definition: mtr0mtr.h:401
ulint get_savepoint() const
Return current size of the buffer.
Definition: mtr0mtr.h:238
bool m_made_dirty
true if mtr has made at least one buffer pool page dirty
Definition: mtr0mtr.h:174