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