MySQL 8.0.39
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 byte *parse_mlog_rec(byte *begin, byte *end, Key &key, Value &value,
108 lsn_t &start_lsn, lsn_t &end_lsn);
109
110 byte *parse_mlog_rec(byte *begin, byte *end);
111
112 const Pages &flushed() const;
113
114 const Pages &recovered() const;
115
116 void sync_point(const std::string &sync_point_name);
117
119 const std::string &sync_point_name,
120 std::unique_ptr<Sync_point> &&sync_point_handler);
121
122 bool enabled(Options option) const;
123
124 void set_enabled(Options option, bool enabled);
125
126 int flush_every() const;
127
129
130 int verbosity() const;
131
132 void set_verbosity(int level);
133
134 private:
135 void recovered_reset(Key key, lsn_t oldest_modification,
136 lsn_t newest_modification);
137
138 void recovered_add(Key key, Value value, lsn_t oldest_modification,
139 lsn_t newest_modification);
140
141 mutable std::mutex m_mutex;
142 mutable std::mutex m_purge_mutex;
143 std::map<lsn_t, Page> m_buf;
148 uint64_t m_options_enabled = 0;
149 int m_verbosity = 0;
151};
152
153/** Represents currently running test of redo log, nullptr otherwise. */
154extern std::unique_ptr<Log_test> log_test;
155
156/** This function is responsible for three actions:
157
1581. Defines a conditional sync point with name = sync_point_name
159 (@see CONDITIONAL_SYNC_POINT).
160
1612. Crashes MySQL if debug variable with name = "crash_" + sync_poit_name is
162 defined. You could use following approach to crash it:
163 SET GLOBAL DEBUG = '+d,crash_foo' (if sync_point_name='foo')
164
1653. Notifies log_test (unless it's nullptr) about the sync point.
166
167@param[in] sync_point_name name of syncpoint, must be a string literal */
168template <size_t N>
169static void log_sync_point(const char (&sync_point_name)[N]) {
170#ifdef UNIV_DEBUG
171 CONDITIONAL_SYNC_POINT(sync_point_name);
172 const std::string crash_var_name = std::string{"crash_"} + sync_point_name;
173 DBUG_EXECUTE_IF(crash_var_name.c_str(), DBUG_SUICIDE(););
174#endif /* UNIV_DEBUG */
175 if (log_test != nullptr) {
176 log_test->sync_point(sync_point_name);
177 }
178}
179
180#endif /* !UNIV_HOTBACKUP */
181
182#endif /* log0test_h */
uint32_t page_no_t
Page number.
Definition: api0api.h:49
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:241
std::mutex m_purge_mutex
Definition: log0test.h:142
std::map< Key, Page > Pages
Definition: log0test.h:75
int verbosity() const
Definition: log0test.cc:277
void sync_point(const std::string &sync_point_name)
Definition: log0test.cc:245
void set_verbosity(int level)
Definition: log0test.cc:279
void recovered_add(Key key, Value value, lsn_t oldest_modification, lsn_t newest_modification)
Definition: log0test.cc:229
uint64_t m_options_enabled
Definition: log0test.h:148
Pages m_flushed
Definition: log0test.h:145
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:255
Pages m_recovered
Definition: log0test.h:146
void set_enabled(Options option, bool enabled)
Definition: log0test.cc:265
Pages m_written
Definition: log0test.h:144
int flush_every() const
Definition: log0test.cc:273
std::mutex m_mutex
Definition: log0test.h:141
void recovered_reset(Key key, lsn_t oldest_modification, lsn_t newest_modification)
Definition: log0test.cc:218
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:143
int m_flush_every
Definition: log0test.h:150
bool enabled(Options option) const
Definition: log0test.cc:261
const Pages & recovered() const
Definition: log0test.cc:243
Sync_points m_sync_points
Definition: log0test.h:147
int64_t Value
Definition: log0test.h:66
static byte * parse_mlog_rec(byte *begin, byte *end, Key &key, Value &value, lsn_t &start_lsn, lsn_t &end_lsn)
Definition: log0test.cc:162
int m_verbosity
Definition: log0test.h:149
void add_dirty_page(const Page &page)
Definition: log0test.cc:55
void set_flush_every(int flush_every)
Definition: log0test.cc:275
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
a nullable SQL value.
Definition: sql_value.h:40
int page
Definition: ctype-mb.cc:1236
Declarations for the Debug Sync Facility.
#define CONDITIONAL_SYNC_POINT(NAME)
Definition: debug_sync.h:128
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:169
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
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
required string key
Definition: replication_asynchronous_connection_failover.proto:60
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