Debian Codegraph @

storage/innobase/include/log0test.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, 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
62 replaces real buffer pool during the test. */
63 class Log_test {
64 public:
65 typedef page_no_t Key;
66 typedef int64_t Value;
67
68 struct Page {
69 Key key;
70 Value value;
71 lsn_t oldest_modification;
72 lsn_t newest_modification;
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 {
85 VALIDATE_RECENT_CLOSED = 1,
86 VALIDATE_RECENT_WRITTEN = 2
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 */
95 lsn_t oldest_modification_approx() const;
96
97 void add_dirty_page(const Page &page);
98
99 void fsync_written_pages();
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
118 void register_sync_point_handler(
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
128 void set_flush_every(int flush_every);
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;
144 Pages m_written;
145 Pages m_flushed;
146 Pages m_recovered;
147 Sync_points m_sync_points;
148 uint64_t m_options_enabled = 0;
149 int m_verbosity = 0;
150 int m_flush_every = 10;
151 };
152
153 /** Represents currently running test of redo log, nullptr otherwise. */
154 extern std::unique_ptr<Log_test> log_test;
155
156 /** This function is responsible for three actions:
157
158 1. Defines a conditional sync point with name = sync_point_name
159 (@see CONDITIONAL_SYNC_POINT).
160
161 2. 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
165 3. 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 */
168 template <size_t N>
169 static 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 */