MySQL 8.0.31
Source Code Documentation
log0test.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2017, 2022, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2.0,
7as published by the Free Software Foundation.
8
9This program is also distributed with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have included with MySQL.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License, version 2.0, for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/**************************************************/ /**
28 @file include/log0test.h
29
30 Redo log - helper for unit tests.
31
32 *******************************************************/
33
34#ifndef log0test_h
35#define log0test_h
36
37#include <memory>
38
39/* lsn_t */
40#include "log0types.h"
41
42#ifdef UNIV_DEBUG
43
44/* DBUG_EXECUTE_IF */
45#include "my_dbug.h"
46
47/* DEBUG_SYNC_C */
48#include "my_sys.h"
49
50/* current_thd */
51#include "sql/current_thd.h"
52
53/* debug_sync_set_action */
54#include "sql/debug_sync.h"
55
56#endif /* UNIV_DEBUG */
57
58#ifndef UNIV_HOTBACKUP
59
60/** It is a environment for tests of redo log. It contains a mock, which
61replaces real buffer pool during the test. */
62class Log_test {
63 public:
64 typedef page_no_t Key;
65 typedef int64_t Value;
66
67 struct Page {
72 };
73
74 typedef std::map<Key, Page> Pages;
75
76 class Sync_point {
77 public:
78 virtual void sync() = 0;
79
80 virtual ~Sync_point() = default;
81 };
82
83 enum class Options {
86 };
87
88 typedef std::map<std::string, std::unique_ptr<Sync_point>> Sync_points;
89
90 /** Calculates oldest_modification of the earliest added dirty page
91 during the test in log0log-t. It is basically a replacement for the
92 log_buf_get_oldest_modification_approx() during the test.
93 @return oldest_modification lsn */
95
96 void add_dirty_page(const Page &page);
97
99
100 void purge(lsn_t max_dirty_page_age);
101
102 static byte *create_mlog_rec(byte *rec, Key key, Value value, size_t payload);
103
104 static byte *create_mlog_rec(byte *rec, Key key, Value value);
105
106 static byte *parse_mlog_rec(byte *begin, byte *end, Key &key, Value &value,
107 lsn_t &start_lsn, lsn_t &end_lsn);
108
109 byte *parse_mlog_rec(byte *begin, byte *end);
110
111 const Pages &flushed() const;
112
113 const Pages &recovered() const;
114
115 void sync_point(const std::string &sync_point_name);
116
118 const std::string &sync_point_name,
119 std::unique_ptr<Sync_point> &&sync_point_handler);
120
121 bool enabled(Options option) const;
122
123 void set_enabled(Options option, bool enabled);
124
125 int flush_every() const;
126
128
129 int verbosity() const;
130
131 void set_verbosity(int level);
132
133 private:
134 void recovered_reset(Key key, lsn_t oldest_modification,
135 lsn_t newest_modification);
136
137 void recovered_add(Key key, Value value, lsn_t oldest_modification,
138 lsn_t newest_modification);
139
140 mutable std::mutex m_mutex;
141 mutable std::mutex m_purge_mutex;
142 std::map<lsn_t, Page> m_buf;
147 uint64_t m_options_enabled = 0;
148 int m_verbosity = 0;
150};
151
152/** Represents currently running test of redo log, nullptr otherwise. */
153extern std::unique_ptr<Log_test> log_test;
154
155/** This function is responsible for three actions:
156
1571. Defines a conditional sync point with name = sync_point_name
158 (@see CONDITIONAL_SYNC_POINT).
159
1602. Crashes MySQL if debug variable with name = "crash_" + sync_poit_name is
161 defined. You could use following approach to crash it:
162 SET GLOBAL DEBUG = '+d,crash_foo' (if sync_point_name='foo')
163
1643. Notifies log_test (unless it's nullptr) about the sync point.
165
166@param[in] sync_point_name name of syncpoint, must be a string literal */
167template <size_t N>
168static void log_sync_point(const char (&sync_point_name)[N]) {
169#ifdef UNIV_DEBUG
170 CONDITIONAL_SYNC_POINT(sync_point_name);
171 const std::string crash_var_name = std::string{"crash_"} + sync_point_name;
172 DBUG_EXECUTE_IF(crash_var_name.c_str(), DBUG_SUICIDE(););
173#endif /* UNIV_DEBUG */
174 if (log_test != nullptr) {
175 log_test->sync_point(sync_point_name);
176 }
177}
178
179#endif /* !UNIV_HOTBACKUP */
180
181#endif /* log0test_h */
uint32_t page_no_t
Page number.
Definition: api0api.h:48
Definition: log0test.h:76
virtual void sync()=0
virtual ~Sync_point()=default
It is a environment for tests of redo log.
Definition: log0test.h:62
void purge(lsn_t max_dirty_page_age)
Definition: log0test.cc:75
const Pages & flushed() const
Definition: log0test.cc:240
std::mutex m_purge_mutex
Definition: log0test.h:141
std::map< Key, Page > Pages
Definition: log0test.h:74
int verbosity() const
Definition: log0test.cc:276
void sync_point(const std::string &sync_point_name)
Definition: log0test.cc:244
void set_verbosity(int level)
Definition: log0test.cc:278
void recovered_add(Key key, Value value, lsn_t oldest_modification, lsn_t newest_modification)
Definition: log0test.cc:228
uint64_t m_options_enabled
Definition: log0test.h:147
Pages m_flushed
Definition: log0test.h:144
void fsync_written_pages()
Definition: log0test.cc:65
void register_sync_point_handler(const std::string &sync_point_name, std::unique_ptr< Sync_point > &&sync_point_handler)
Definition: log0test.cc:254
Pages m_recovered
Definition: log0test.h:145
void set_enabled(Options option, bool enabled)
Definition: log0test.cc:264
Pages m_written
Definition: log0test.h:143
int flush_every() const
Definition: log0test.cc:272
std::mutex m_mutex
Definition: log0test.h:140
void recovered_reset(Key key, lsn_t oldest_modification, lsn_t newest_modification)
Definition: log0test.cc:217
static byte * create_mlog_rec(byte *rec, Key key, Value value, size_t payload)
Definition: log0test.cc:122
std::map< lsn_t, Page > m_buf
Definition: log0test.h:142
int m_flush_every
Definition: log0test.h:149
bool enabled(Options option) const
Definition: log0test.cc:260
const Pages & recovered() const
Definition: log0test.cc:242
Sync_points m_sync_points
Definition: log0test.h:146
int64_t Value
Definition: log0test.h:65
static byte * parse_mlog_rec(byte *begin, byte *end, Key &key, Value &value, lsn_t &start_lsn, lsn_t &end_lsn)
Definition: log0test.cc:161
int m_verbosity
Definition: log0test.h:148
void add_dirty_page(const Page &page)
Definition: log0test.cc:54
void set_flush_every(int flush_every)
Definition: log0test.cc:274
Options
Definition: log0test.h:83
lsn_t oldest_modification_approx() const
Calculates oldest_modification of the earliest added dirty page during the test in log0log-t.
Definition: log0test.cc:48
std::map< std::string, std::unique_ptr< Sync_point > > Sync_points
Definition: log0test.h:88
page_no_t Key
Definition: log0test.h:64
int page
Definition: ctype-mb.cc:1235
Declarations for the Debug Sync Facility.
#define CONDITIONAL_SYNC_POINT(NAME)
Definition: debug_sync.h:127
std::unique_ptr< Log_test > log_test
Represents currently running test of redo log, nullptr otherwise.
Definition: log0test.cc:46
static void log_sync_point(const char(&sync_point_name)[N])
This function is responsible for three actions:
Definition: log0test.h:168
Redo log basic types.
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:62
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:170
#define DBUG_SUICIDE()
Definition: my_dbug.h:227
Common header for many mysys elements.
std::atomic< Type > N
Definition: ut0counter.h:224
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Definition: log0test.h:67
lsn_t oldest_modification
Definition: log0test.h:70
Key key
Definition: log0test.h:68
Value value
Definition: log0test.h:69
lsn_t newest_modification
Definition: log0test.h:71