Debian Codegraph @

storage/innobase/include/ut0test.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 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, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27 /*
28
29 ABOUT:
30
31 If you want to execute some arbitrary code on the server side, for testing
32 purposes (mtr tests), then this module is for that purpose. There is no
33 need to introduce more debug variables to execute code in InnoDB. Many such
34 uses can be accomplished using this module.
35
36 OVERVIEW:
37
38 The idea of this module is to be able to write mtr test case like:
39
40 Example Code:
41
42 SET SESSION innodb_interpreter = 'init';
43 SET SESSION innodb_interpreter = 'open_table test/t1';
44
45 --echo # Identify the space_id of the given table.
46 SET SESSION innodb_interpreter = 'find_space_id test/t1';
47 SELECT @@session.innodb_interpreter_output INTO @space_id;
48
49 --echo # Find the root page number of the given table.
50 SET SESSION innodb_interpreter = 'find_root_page_no test/t1';
51 SELECT @@session.innodb_interpreter_output INTO @page_no;
52
53 --echo # Find the on-disk page type of the given page.
54 SET @cmd = CONCAT('find_ondisk_page_type ', @space_id, ' ', @page_no);
55 SET SESSION innodb_interpreter = @cmd;
56 SELECT @@session.innodb_interpreter_output INTO @page_type;
57 SELECT @page_type;
58
59 SET SESSION innodb_interpreter = 'corrupt_ondisk_root_page test/t1';
60 SET SESSION innodb_interpreter = 'destroy';
61
62 INTRODUCTION:
63
64 There are two system variables innodb_interpreter and
65 innodb_interpreter_output. We issue commands via innodb_interpreter variable
66 and we access its output via innodb_interpreter_output variable. Refer to the
67 above example.
68
69 USAGE:
70
71 The basic way to use this infrastructure is as follows:
72
73 SET SESSION innodb_interpreter = 'command arg1 arg2 ... argN';
74 SELECT @@session.innodb_interpreter_output;
75
76 We send some command and arguments to the innodb_interpreter variable. And we
77 look at the results in the innodb_interpreter_output variable.
78
79 ADDING NEW COMMANDS:
80
81 If you want to add a new command, here are the list of things you need to do in
82 this module:
83
84 1. Decide the command name. Let 'hello_world' be the command name and let is
85 take 3 arguments - arg1, arg2 and arg3. So the command will be invoked as
86 follows:
87
88 SET SESSION innodb_interpreter = 'hello_world arg1 arg2 arg3';
89
90 2. Add member function with same name as the command to the Tester class. Its
91 signature would be as follows:
92
93 [[nodiscard]] Ret_t hello_world(std::vector<std::string> &tokens) noexcept;
94
95 The command has 4 tokens - the command name followed by 3 arguments. The
96 vector will be of size 4.
97
98 3. In the constructor Tester::Tester() add the line:
99
100 DISPATCH(hello_world);
101
102 4. While implementing the member function hello_world(), populate the member
103 Tester::m_log with the output you want to give the user for the given
104 command.
105
106 5. Execute the command as follows:
107
108 SET SESSION innodb_interpreter = 'hello_world one two three';
109
110 Check its output as follows:
111
112 SELECT @@session.innodb_interpreter_output;
113
114 NOTE: You can make the command either stateful or stateless. It is up to you.
115
116 */
117
118 #ifndef ut0test_h
119 #define ut0test_h
120 #ifdef UNIV_DEBUG
121
122 #include <string>
123 #include <vector>
124 #include "current_thd.h"
125 #include "fil0fil.h"
126 #include "mysql/plugin.h"
127
128 /** This is the prefix used for the log messages that will be updated in the
129 innodb_interpreter_output system variable. */
130 #define TPREFIX "[ib::Tester] "
131
132 #define TLOG(x) \
133 { \
134 std::cout << "[TLOG] thread=" << std::this_thread::get_id() << ": " << x \
135 << std::endl; \
136 }
137
138 /** A macro to log output in both the server error log (via std::cout, the
139 standard output) and the server system variable innodb_interpreter_output.
140 To use this macro, there must be a std::ostringstream object "sout" available
141 in the scope in which it is used. */
142 #define XLOG(x) \
143 { \
144 TLOG(x) \
145 sout << x; \
146 }
147
148 namespace ib {
149
150 /** This class contains implementations of the commands that can be executed
151 at server side by passing them via the innodb_interpreter system variable. */
152 struct Tester {
153 public:
154 /** The return status code used by the various commands in this module. */
155 enum Ret_t {
156 RET_PASS = 0,
157 RET_FAIL = 1,
158
159 /* The command is yet to handled. */
160 RET_CMD_TBD = 2
161 };
162
163 /** Default constructor. */
164 Tester() noexcept;
165
166 /** Run the given command.
167 @param[in] cmd the command to run.
168 @return 0 on success.
169 @return error code on failure. */
170 [[nodiscard]] Ret_t run(ulong cmd) noexcept;
171
172 /** Run the given command.
173 @param[in] cmd the command to run.
174 @return 0 on success.
175 @return error code on failure. */
176 [[nodiscard]] Ret_t run(const std::string &cmd) noexcept;
177
178 /** Get the last generated output.
179 @return the last generated output. */
180 [[nodiscard]] const char *get_last_log() const noexcept {
181 return m_log.c_str();
182 }
183
184 /** Let the thread-variable innodb_interpreter_output point to the current
185 output. */
186 void update_thd_variable() noexcept;
187
188 private:
189 /** Initialize the internal state of the tester. */
190 void init() noexcept;
191
192 /** Open the specified table.
193 @param[in] tokens the given command line
194 @return RET_PASS on success, or the error code. */
195 [[nodiscard]] Ret_t open_table(std::vector<std::string> &tokens) noexcept;
196
197 /** Close the given table.
198 @param[in] table the table object */
199 void close_table(dict_table_t *table) noexcept;
200
201 /** Find the space_id of the given table.
202 @param[in] tokens the given command line
203 @return RET_PASS on success, or the error code. */
204 [[nodiscard]] Ret_t find_space_id(std::vector<std::string> &tokens) noexcept;
205
206 /** Find the root page of the given table.
207 @param[in] tokens the given command line
208 @return RET_PASS on success, or the error code. */
209 [[nodiscard]] Ret_t find_root_page_no(
210 std::vector<std::string> &tokens) noexcept;
211
212 /** Find the on-disk page type of the given page.
213 @param[in] tokens the given command line
214 @return RET_PASS on success, or the error code. */
215 [[nodiscard]] Ret_t find_ondisk_page_type(
216 std::vector<std::string> &tokens) noexcept;
217
218 /** Find the FIL_PAGE_LSN of the given page.
219 @param[in] tokens the given command line
220 @return RET_PASS on success, or the error code. */
221 [[nodiscard]] Ret_t find_fil_page_lsn(
222 std::vector<std::string> &tokens) noexcept;
223
224 /** Find the flush sync lsn from the buffer pool module.
225 @param[in] tokens the given command line
226 @return RET_PASS on success, or the error code. */
227 [[nodiscard]] Ret_t find_flush_sync_lsn(
228 std::vector<std::string> &tokens) noexcept;
229
230 /** Print the page type of pages in dblwr file to server error log.
231 @param[in] tokens the given command line
232 @return RET_PASS on success, or the error code. */
233 [[nodiscard]] Ret_t print_dblwr_has_encrypted_pages(
234 std::vector<std::string> &tokens) noexcept;
235
236 /** Obtain the file name of the given tablespace
237 @param[in] tokens the given command line
238 @return RET_PASS on success, or the error code. */
239 [[nodiscard]] Ret_t find_tablespace_file_name(
240 std::vector<std::string> &tokens) noexcept;
241
242 /** A macro to declare a dispatch function or a command function. They all
243 have the same signature.
244 @param[in] func_ the function that is being declared. */
245 #define DISPATCH_FUNCTION(func_) \
246 /* @param[in] tokens the command line */ \
247 /* @return RET_PASS on success, or the error code. */ \
248 [[nodiscard]] Ret_t func_(std::vector<std::string> &tokens) noexcept
249
250 /** Count various page_types for given tablespace. */
251 DISPATCH_FUNCTION(count_page_type);
252
253 /** Count various page_types for given tablespace. */
254 DISPATCH_FUNCTION(count_used_and_free);
255
256 /** Obtain the page size of the given tablespace. */
257 DISPATCH_FUNCTION(find_tablespace_physical_page_size);
258
259 /** Fill the root page of the given table with zeroes. */
260 DISPATCH_FUNCTION(make_ondisk_root_page_zeroes);
261
262 /** Make the page dirty. It takes two arguments.
263 make_page_dirty space_id page_no */
264 DISPATCH_FUNCTION(make_page_dirty);
265
266 /** Corrupt the root page of the given table.
267 @param[in] tokens the given command line
268 @return RET_PASS on success, or the error code. */
269 [[nodiscard]] Ret_t corrupt_ondisk_root_page(
270 std::vector<std::string> &tokens) noexcept;
271
272 /** Corrupt the first page of the given tablespace.
273 @param[in] tokens the given command line
274 @return RET_PASS on success, or the error code. */
275 [[nodiscard]] Ret_t corrupt_ondisk_page0(
276 std::vector<std::string> &tokens) noexcept;
277
278 /** Set the dblwr::Force_crash to the desired page. This will
279 crash the server after flushing the page to dblwr.
280 @return RET_PASS on success, or the error code. */
281 DISPATCH_FUNCTION(dblwr_force_crash);
282
283 /** Destroy the tester object. */
284 void destroy() noexcept;
285
286 private:
287 /** Check if the given table is already opened.
288 @param[in] table_name name of the table to open.
289 @return if table is already open return its pointer.
290 @return if table is NOT already open return nullptr. */
291 [[nodiscard]] dict_table_t *is_table_open(
292 const std::string &table_name) const noexcept;
293
294 /** Set the output value of the interpreter.
295 @param[in] sout the output string stream containing the output string. */
296 void set_output(const std::ostringstream &sout) noexcept;
297
298 /** Set the output value of the interpreter to the given value.
299 @param[in] log the output string */
300 void set_output(const std::string &log) noexcept;
301
302 /** Append the given string to the output value of the interpreter.
303 @param[in] log the output string to be appended*/
304 void append_output(const std::string &log) noexcept;
305
306 /** Make the output empty. */
307 void clear_output() noexcept;
308
309 /** Make the first prefix_length bytes of the given page as zeroes.
310 @param[in] space_id the tablespace identifier of the page to be zeroed.
311 @param[in] page_no page number within the given tablespace.
312 @param[in] prefix_length the length of the page, from beginning, to be
313 zeroed.
314 @return RET_PASS on success, or error code on failure. */
315 Ret_t clear_page_prefix(const space_id_t space_id, page_no_t page_no,
316 const size_t prefix_length);
317
318 private:
319 /** List of open tables. */
320 std::list<dict_table_t *, ut::allocator<dict_table_t *>> m_open_tables{};
321
322 /** Current thread object. */
323 THD *m_thd{};
324
325 /** The actual log data that is shared with the client via the thread-variable
326 innodb_interpreter_output. (Search for the variable interpreter_output in
327 the file ha_innodb.cc) */
328 std::string m_log{};
329
330 /** The latest command executed. */
331 std::string m_command{};
332
333 using Function_executor = Ret_t (Tester::*)(std::vector<std::string> &);
334 using Pair = std::pair<const std::string, Function_executor>;
335 using Allocator = ut::allocator<Pair>;
336
337 /** Mapping b/w the command name and the function to execute. */
338 std::map<std::string, Function_executor, std::less<std::string>, Allocator>
339 m_dispatch;
340 };
341
342 /** The main function to execute the commands in the tester.
343 @param[in] command the command to execute.
344 @return the error code. */
345 [[nodiscard]] int interpreter_run(const char *command) noexcept;
346
347 } // namespace ib
348
349 /** Update the innodb_interpreter_output system variable to let the user
350 access the output generated by the tester. Refer to mysql_var_update_func() for
351 details of the function signature.
352 @param[in] thd thread handle
353 @param[in] var dynamic variable being altered
354 @param[in] var_ptr pointer to dynamic variable
355 @param[in] save pointer to temporary storage. */
356 void ib_interpreter_update(MYSQL_THD thd, SYS_VAR *var, void *var_ptr,
357 const void *save);
358
359 /**Check whether given command is valid for the InnoDB interpreter
360 Refer to mysql_var_check_func() for more details.
361 @param[in] thd thread handle
362 @param[in] var pointer to system variable
363 @param[out] save immediate result for update function
364 @param[in] value incoming string
365 @return 0 for valid command. */
366 int ib_interpreter_check(THD *thd, SYS_VAR *var, void *save,
367 struct st_mysql_value *value);
368
369 #endif /* UNIV_DEBUG */
370 #endif // ut0test_h