Debian Codegraph @

storage/innobase/log/log0ddl.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
4
5 Portions of this file contain modifications contributed and copyrighted by
6 Google, Inc. Those modifications are gratefully acknowledged and are described
7 briefly in the InnoDB documentation. The contributions by Google are
8 incorporated with their permission, and subject to the conditions contained in
9 the file COPYING.Google.
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License, version 2.0, as published by the
13 Free Software Foundation.
14
15 This program is designed to work with certain software (including
16 but not limited to OpenSSL) that is licensed under separate terms,
17 as designated in a particular file or component or in included license
18 documentation. The authors of MySQL hereby grant you an additional
19 permission to link the program and your derivative works with the
20 separately licensed software that they have either included with
21 the program or referenced in the documentation.
22
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32 *****************************************************************************/
33
34 /** @file log/log0ddl.cc
35 DDL log
36
37 Created 12/1/2016 Shaohua Wang
38 *******************************************************/
39
40 #include <debug_sync.h>
41 #include "ha_prototypes.h"
42
43 #include <current_thd.h>
44 #include <sql_thd_internal_api.h>
45
46 #include "btr0sea.h"
47 #include "dict0dd.h"
48 #include "dict0mem.h"
49 #include "dict0stats.h"
50 #include "ha_innodb.h"
51 #include "log0chkp.h"
52 #include "log0ddl.h"
53 #include "mysql/plugin.h"
54 #include "pars0pars.h"
55 #include "que0que.h"
56 #include "row0ins.h"
57 #include "row0row.h"
58 #include "row0sel.h"
59 #include "trx0trx.h"
60
61 /** Object to handle Log_DDL */
62 Log_DDL *log_ddl = nullptr;
63
64 /** Whether replaying DDL log
65 Note: we should not write DDL log when replaying DDL log. */
66 thread_local bool thread_local_ddl_log_replay = false;
67
68 /** Whether in recover(replay) DDL log in startup. */
69 bool Log_DDL::s_in_recovery = false;
70
71 #ifdef UNIV_DEBUG
72
73 /** Used by SET GLOBAL innodb_ddl_log_crash_counter_reset_debug = 1; */
74 bool innodb_ddl_log_crash_reset_debug;
75
76 /** Below counters are only used for four types of DDL log:
77 1. FREE TREE
78 2. DELETE SPACE
79 3. RENAME SPACE
80 4. DROP
81 Other RENAME_TABLE and REMOVE CACHE doesn't touch the data files at all,
82 so would be skipped */
83
84 /** Crash injection counter used before writing FREE TREE log */
85 static uint32_t crash_before_free_tree_log_counter = 1;
86
87 /** Crash injection counter used after writing FREE TREE log */
88 static uint32_t crash_after_free_tree_log_counter = 1;
89
90 /** Crash injection counter used after deleting FREE TREE log */
91 static uint32_t crash_after_free_tree_delete_counter = 1;
92
93 /** Crash injection counter used before writing DELETE SPACE log */
94 static uint32_t crash_before_delete_space_log_counter = 1;
95
96 /** Crash injection counter used after writing DELETE SPACE log */
97 static uint32_t crash_after_delete_space_log_counter = 1;
98
99 /** Crash injection counter used after deleting DELETE SPACE log */
100 static uint32_t crash_after_delete_space_delete_counter = 1;
101
102 /** Crash injection counter used before writing RENAME SPACE log */
103 static uint32_t crash_before_rename_space_log_counter = 1;
104
105 /** Crash injection counter used after writing RENAME SPACE log */
106 static uint32_t crash_after_rename_space_log_counter = 1;
107
108 /** Crash injection counter used after deleting RENAME SPACE log */
109 static uint32_t crash_after_rename_space_delete_counter = 1;
110
111 /** Crash injection counter used before writing DROP log */
112 static uint32_t crash_before_drop_log_counter = 1;
113
114 /** Crash injection counter used after writing DROP log */
115 static uint32_t crash_after_drop_log_counter = 1;
116
117 /** Crash injection counter used after any replay */
118 static uint32_t crash_after_replay_counter = 1;
119
120 /** Crash injection counter used before writing ALTER ENCRYPT TABLESPACE log */
121 static uint32_t crash_before_alter_encrypt_space_log_counter = 1;
122
123 /** Crash injection counter used after writing ALTER ENCRYPT TABLESPACE log */
124 static uint32_t crash_after_alter_encrypt_space_log_counter = 1;
125
126 void ddl_log_crash_reset(THD *, SYS_VAR *, void *, const void *save) {
127 const bool reset = *static_cast<const bool *>(save);
128
129 innodb_ddl_log_crash_reset_debug = reset;
130
131 if (reset) {
132 crash_before_free_tree_log_counter = 1;
133 crash_after_free_tree_log_counter = 1;
134 crash_after_free_tree_delete_counter = 1;
135 crash_before_delete_space_log_counter = 1;
136 crash_after_delete_space_log_counter = 1;
137 crash_after_delete_space_delete_counter = 1;
138 crash_before_rename_space_log_counter = 1;
139 crash_after_rename_space_log_counter = 1;
140 crash_after_rename_space_delete_counter = 1;
141 crash_before_drop_log_counter = 1;
142 crash_after_drop_log_counter = 1;
143 crash_after_replay_counter = 1;
144 }
145 }
146
147 #endif /* UNIV_DEBUG */
148
149 DDL_Record::DDL_Record()
150 : m_id(ULINT_UNDEFINED),
151 m_thread_id(ULINT_UNDEFINED),
152 m_space_id(SPACE_UNKNOWN),
153 m_page_no(FIL_NULL),
154 m_index_id(ULINT_UNDEFINED),
155 m_table_id(ULINT_UNDEFINED),
156 m_old_file_path(nullptr),
157 m_new_file_path(nullptr),
158 m_heap(nullptr),
159 m_deletable(true) {}
160
161 DDL_Record::~DDL_Record() {
162 if (m_heap != nullptr) {
163 mem_heap_free(m_heap);
164 }
165 }
166
167 void DDL_Record::set_old_file_path(const char *name) {
168 ulint len = strlen(name);
169
170 if (m_heap == nullptr) {
171 m_heap = mem_heap_create(FN_REFLEN + 1, UT_LOCATION_HERE);
172 }
173
174 m_old_file_path = mem_heap_strdupl(m_heap, name, len);
175 }
176
177 void DDL_Record::set_old_file_path(const byte *data, ulint len) {
178 if (m_heap == nullptr) {
179 m_heap = mem_heap_create(FN_REFLEN + 1, UT_LOCATION_HERE);
180 }
181
182 m_old_file_path = static_cast<char *>(mem_heap_dup(m_heap, data, len + 1));
183 m_old_file_path[len] = '\0';
184 }
185
186 void DDL_Record::set_new_file_path(const char *name) {
187 ulint len = strlen(name);
188
189 if (m_heap == nullptr) {
190 m_heap = mem_heap_create(FN_REFLEN + 1, UT_LOCATION_HERE);
191 }
192
193 m_new_file_path = mem_heap_strdupl(m_heap, name, len);
194 }
195
196 void DDL_Record::set_new_file_path(const byte *data, ulint len) {
197 if (m_heap == nullptr) {
198 m_heap = mem_heap_create(FN_REFLEN + 1, UT_LOCATION_HERE);
199 }
200
201 m_new_file_path = static_cast<char *>(mem_heap_dup(m_heap, data, len + 1));
202 m_new_file_path[len] = '\0';
203 }
204
205 std::ostream &DDL_Record::print(std::ostream &out) const {
206 ut_ad(m_type >= Log_Type::SMALLEST_LOG);
207 ut_ad(m_type <= Log_Type::BIGGEST_LOG);
208
209 bool printed = false;
210
211 out << "[DDL record: ";
212
213 switch (m_type) {
214 case Log_Type::FREE_TREE_LOG:
215 out << "FREE";
216 break;
217 case Log_Type::DELETE_SPACE_LOG:
218 out << "DELETE SPACE";
219 break;
220 case Log_Type::RENAME_SPACE_LOG:
221 out << "RENAME SPACE";
222 break;
223 case Log_Type::DROP_LOG:
224 out << "DROP";
225 break;
226 case Log_Type::RENAME_TABLE_LOG:
227 out << "RENAME TABLE";
228 break;
229 case Log_Type::REMOVE_CACHE_LOG:
230 out << "REMOVE CACHE";
231 break;
232 case Log_Type::ALTER_ENCRYPT_TABLESPACE_LOG:
233 out << "ALTER ENCRYPT TABLESPACE";
234 break;
235 case Log_Type::ALTER_UNENCRYPT_TABLESPACE_LOG:
236 out << "ALTER UNENCRYPT TABLESPACE";
237 break;
238 default:
239 ut_d(ut_error);
240 }
241
242 out << ",";
243
244 if (m_id != ULINT_UNDEFINED) {
245 out << " id=" << m_id;
246 printed = true;
247 }
248
249 if (m_thread_id != ULINT_UNDEFINED) {
250 if (printed) {
251 out << ",";
252 }
253 out << " thread_id=" << m_thread_id;
254 printed = true;
255 }
256
257 if (m_space_id != SPACE_UNKNOWN) {
258 if (printed) {
259 out << ",";
260 }
261 out << " space_id=" << m_space_id;
262 printed = true;
263 }
264
265 if (m_table_id != ULINT_UNDEFINED) {
266 if (printed) {
267 out << ",";
268 }
269 out << " table_id=" << m_table_id;
270 printed = true;
271 }
272
273 if (m_index_id != ULINT_UNDEFINED) {
274 if (printed) {
275 out << ",";
276 }
277 out << " index_id=" << m_index_id;
278 printed = true;
279 }
280
281 if (m_page_no != FIL_NULL) {
282 if (printed) {
283 out << ",";
284 }
285 out << " page_no=" << m_page_no;
286 printed = true;
287 }
288
289 if (m_old_file_path != nullptr) {
290 if (printed) {
291 out << ",";
292 }
293 out << " old_file_path=" << m_old_file_path;
294 printed = true;
295 }
296
297 if (m_new_file_path != nullptr) {
298 if (printed) {
299 out << ",";
300 }
301 out << " new_file_path=" << m_new_file_path;
302 }
303
304 out << "]";
305
306 return (out);
307 }
308
309 /** Display a DDL record
310 @param[in,out] o output stream
311 @param[in] record DDL record to display
312 @return the output stream */
313 std::ostream &operator<<(std::ostream &o, const DDL_Record &record) {
314 return (record.print(o));
315 }
316
317 DDL_Log_Table::DDL_Log_Table() : DDL_Log_Table(nullptr) {}
318
319 DDL_Log_Table::DDL_Log_Table(trx_t *trx)
320 : m_table(dict_sys->ddl_log), m_tuple(nullptr), m_trx(trx), m_thr(nullptr) {
321 ut_ad(m_trx == nullptr || m_trx->ddl_operation);
322 m_heap = mem_heap_create(100, UT_LOCATION_HERE);
323 if (m_trx != nullptr) {
324 start_query_thread();
325 }
326 }
327
328 DDL_Log_Table::~DDL_Log_Table() {
329 stop_query_thread();
330 mem_heap_free(m_heap);
331 }
332
333 void DDL_Log_Table::start_query_thread() {
334 que_t *graph = static_cast<que_fork_t *>(que_node_get_parent(
335 pars_complete_graph_for_exec(nullptr, m_trx, m_heap, nullptr)));
336 m_thr = que_fork_start_command(graph);
337 ut_ad(m_trx->lock.n_active_thrs == 1);
338 }
339
340 void DDL_Log_Table::stop_query_thread() {
341 if (m_thr != nullptr) {
342 que_thr_stop_for_mysql_no_error(m_thr, m_trx);
343 }
344 }
345
346 void DDL_Log_Table::create_tuple(const DDL_Record &record) {
347 const dict_col_t *col;
348 dfield_t *dfield;
349 byte *buf;
350
351 m_tuple = dtuple_create(m_heap, m_table->get_n_cols());
352 dict_table_copy_types(m_tuple, m_table);
353 buf = static_cast<byte *>(mem_heap_alloc(m_heap, 8));
354 memset(buf, 0xFF, 8);
355
356 col = m_table->get_sys_col(DATA_ROW_ID);
357 dfield = dtuple_get_nth_field(m_tuple, dict_col_get_no(col));
358 dfield_set_data(dfield, buf, DATA_ROW_ID_LEN);
359
360 col = m_table->get_sys_col(DATA_ROLL_PTR);
361 dfield = dtuple_get_nth_field(m_tuple, dict_col_get_no(col));
362 dfield_set_data(dfield, buf, DATA_ROLL_PTR_LEN);
363
364 buf = static_cast<byte *>(mem_heap_alloc(m_heap, DATA_TRX_ID_LEN));
365 mach_write_to_6(buf, m_trx->id);
366 col = m_table->get_sys_col(DATA_TRX_ID);
367 dfield = dtuple_get_nth_field(m_tuple, dict_col_get_no(col));
368 dfield_set_data(dfield, buf, DATA_TRX_ID_LEN);
369
370 const ulint rec_id = record.get_id();
371
372 if (rec_id != ULINT_UNDEFINED) {
373 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_id_col_len));
374 mach_write_to_8(buf, rec_id);
375 dfield = dtuple_get_nth_field(m_tuple, s_id_col_no);
376 dfield_set_data(dfield, buf, s_id_col_len);
377 }
378
379 if (record.get_thread_id() != ULINT_UNDEFINED) {
380 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_thread_id_col_len));
381 mach_write_to_8(buf, record.get_thread_id());
382 dfield = dtuple_get_nth_field(m_tuple, s_thread_id_col_no);
383 dfield_set_data(dfield, buf, s_thread_id_col_len);
384 }
385
386 ut_ad(record.get_type() >= Log_Type::SMALLEST_LOG);
387 ut_ad(record.get_type() <= Log_Type::BIGGEST_LOG);
388 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_type_col_len));
389 mach_write_to_4(buf,
390 static_cast<typename std::underlying_type<Log_Type>::type>(
391 record.get_type()));
392 dfield = dtuple_get_nth_field(m_tuple, s_type_col_no);
393 dfield_set_data(dfield, buf, s_type_col_len);
394
395 if (record.get_space_id() != SPACE_UNKNOWN) {
396 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_space_id_col_len));
397 mach_write_to_4(buf, record.get_space_id());
398 dfield = dtuple_get_nth_field(m_tuple, s_space_id_col_no);
399 dfield_set_data(dfield, buf, s_space_id_col_len);
400 }
401
402 if (record.get_page_no() != FIL_NULL) {
403 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_page_no_col_len));
404 mach_write_to_4(buf, record.get_page_no());
405 dfield = dtuple_get_nth_field(m_tuple, s_page_no_col_no);
406 dfield_set_data(dfield, buf, s_page_no_col_len);
407 }
408
409 if (record.get_index_id() != ULINT_UNDEFINED) {
410 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_index_id_col_len));
411 mach_write_to_8(buf, record.get_index_id());
412 dfield = dtuple_get_nth_field(m_tuple, s_index_id_col_no);
413 dfield_set_data(dfield, buf, s_index_id_col_len);
414 }
415
416 if (record.get_table_id() != ULINT_UNDEFINED) {
417 buf = static_cast<byte *>(mem_heap_alloc(m_heap, s_table_id_col_len));
418 mach_write_to_8(buf, record.get_table_id());
419 dfield = dtuple_get_nth_field(m_tuple, s_table_id_col_no);
420 dfield_set_data(dfield, buf, s_table_id_col_len);
421 }
422
423 if (record.get_old_file_path() != nullptr) {
424 ulint m_len = strlen(record.get_old_file_path()) + 1;
425 dfield = dtuple_get_nth_field(m_tuple, s_old_file_path_col_no);
426 dfield_set_data(dfield, record.get_old_file_path(), m_len);
427 }
428
429 if (record.get_new_file_path() != nullptr) {
430 ulint m_len = strlen(record.get_new_file_path()) + 1;
431 dfield = dtuple_get_nth_field(m_tuple, s_new_file_path_col_no);
432 dfield_set_data(dfield, record.get_new_file_path(), m_len);
433 }
434 }
435
436 void DDL_Log_Table::create_tuple(ulint id, const dict_index_t *index) {
437 ut_ad(id != ULINT_UNDEFINED);
438
439 dfield_t *dfield;
440 ulint len;
441 ulint table_col_offset;
442 ulint index_col_offset;
443
444 m_tuple = dtuple_create(m_heap, 1);
445 dict_index_copy_types(m_tuple, index, 1);
446
447 if (index->is_clustered()) {
448 len = s_id_col_len;
449 table_col_offset = s_id_col_no;
450 } else {
451 len = s_thread_id_col_len;
452 table_col_offset = s_thread_id_col_no;
453 }
454
455 index_col_offset = index->get_col_pos(table_col_offset);
456 byte *buf = static_cast<byte *>(mem_heap_alloc(m_heap, len));
457 mach_write_to_8(buf, id);
458 dfield = dtuple_get_nth_field(m_tuple, index_col_offset);
459 dfield_set_data(dfield, buf, len);
460 }
461
462 dberr_t DDL_Log_Table::insert(const DDL_Record &record) {
463 dberr_t error;
464 dict_index_t *index = m_table->first_index();
465 dtuple_t *entry;
466 uint32_t flags = BTR_NO_LOCKING_FLAG;
467 mem_heap_t *offsets_heap = mem_heap_create(100, UT_LOCATION_HERE);
468 static std::atomic<uint64_t> count(0);
469
470 if (count++ % 64 == 0) {
471 log_free_check();
472 }
473
474 create_tuple(record);
475 entry = row_build_index_entry(m_tuple, nullptr, index, m_heap);
476
477 #ifdef UNIV_DEBUG
478 bool insert = true;
479 DBUG_EXECUTE_IF("ddl_log_return_error_from_insert", insert = false;);
480
481 if (insert) {
482 #endif
483 error = row_ins_clust_index_entry_low(flags, BTR_MODIFY_LEAF, index,
484 index->n_uniq, entry, m_thr, false);
485 #ifdef UNIV_DEBUG
486 } else {
487 error = DB_ERROR;
488 }
489 #endif
490
491 if (error == DB_FAIL) {
492 error = row_ins_clust_index_entry_low(flags, BTR_MODIFY_TREE, index,
493 index->n_uniq, entry, m_thr, false);
494 ut_ad(error == DB_SUCCESS);
495 }
496
497 if (error != DB_SUCCESS) {
498 ib::error(ER_IB_ERR_DDL_LOG_INSERT_FAILURE);
499 mem_heap_free(offsets_heap);
500 return error;
501 }
502
503 index = index->next();
504
505 entry = row_build_index_entry(m_tuple, nullptr, index, m_heap);
506
507 error =
508 row_ins_sec_index_entry_low(flags, BTR_MODIFY_LEAF, index, offsets_heap,
509 m_heap, entry, m_trx->id, m_thr, false);
510
511 if (error == DB_FAIL) {
512 error =
513 row_ins_sec_index_entry_low(flags, BTR_MODIFY_TREE, index, offsets_heap,
514 m_heap, entry, m_trx->id, m_thr, false);
515 }
516
517 mem_heap_free(offsets_heap);
518 ut_ad(error == DB_SUCCESS);
519 return (error);
520 }
521
522 void DDL_Log_Table::convert_to_ddl_record(bool is_clustered, rec_t *rec,
523 const ulint *offsets,
524 DDL_Record &record) {
525 if (is_clustered) {
526 for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) {
527 const byte *data;
528 ulint len;
529
530 if (i == DATA_ROLL_PTR || i == DATA_TRX_ID) {
531 continue;
532 }
533
534 const dict_index_t *clust_index = m_table->first_index();
535 data = rec_get_nth_field(clust_index, rec, offsets, i, &len);
536
537 if (len != UNIV_SQL_NULL) {
538 set_field(data, i, len, record);
539 }
540 }
541 } else {
542 /* For secondary index, only the ID would be stored */
543 record.set_id(parse_id(m_table->first_index()->next(), rec, offsets));
544 }
545 }
546
547 ulint DDL_Log_Table::parse_id(const dict_index_t *index, rec_t *rec,
548 const ulint *offsets) {
549 ulint len;
550 ulint index_offset = index->get_col_pos(s_id_col_no);
551
552 const byte *data = rec_get_nth_field(index, rec, offsets, index_offset, &len);
553 ut_ad(len == s_id_col_len);
554
555 return (mach_read_from_8(data));
556 }
557
558 void DDL_Log_Table::set_field(const byte *data, ulint index_offset, ulint len,
559 DDL_Record &record) {
560 dict_index_t *index = dict_sys->ddl_log->first_index();
561 ulint col_offset = index->get_col_no(index_offset);
562
563 if (col_offset == s_new_file_path_col_no) {
564 record.set_new_file_path(data, len);
565 return;
566 }
567
568 if (col_offset == s_old_file_path_col_no) {
569 record.set_old_file_path(data, len);
570 return;
571 }
572
573 ulint value = fetch_value(data, col_offset);
574 switch (col_offset) {
575 case s_id_col_no:
576 record.set_id(value);
577 break;
578 case s_thread_id_col_no:
579 record.set_thread_id(value);
580 break;
581 case s_type_col_no:
582 record.set_type(static_cast<Log_Type>(value));
583 break;
584 case s_space_id_col_no:
585 record.set_space_id(static_cast<space_id_t>(value));
586 break;
587 case s_page_no_col_no:
588 record.set_page_no(static_cast<page_no_t>(value));
589 break;
590 case s_index_id_col_no:
591 record.set_index_id(value);
592 break;
593 case s_table_id_col_no:
594 record.set_table_id(value);
595 break;
596 case s_old_file_path_col_no:
597 case s_new_file_path_col_no:
598 default:
599 ut_d(ut_error);
600 }
601 }
602
603 ulint DDL_Log_Table::fetch_value(const byte *data, ulint offset) {
604 ulint value = 0;
605 switch (offset) {
606 case s_id_col_no:
607 case s_thread_id_col_no:
608 case s_index_id_col_no:
609 case s_table_id_col_no:
610 value = mach_read_from_8(data);
611 return (value);
612 case s_type_col_no:
613 case s_space_id_col_no:
614 case s_page_no_col_no:
615 value = mach_read_from_4(data);
616 return (value);
617 case s_new_file_path_col_no:
618 case s_old_file_path_col_no:
619 default:
620 ut_d(ut_error);
621 ut_o(break);
622 }
623
624 ut_o(return (value));
625 }
626
627 dberr_t DDL_Log_Table::search_all(DDL_Records &records) {
628 mtr_t mtr;
629 btr_pcur_t pcur;
630 rec_t *rec;
631 bool move = true;
632 ulint *offsets;
633 dict_index_t *index = m_table->first_index();
634 dberr_t error = DB_SUCCESS;
635
636 mtr_start(&mtr);
637
638 /** Scan the index in decreasing order. */
639 pcur.open_at_side(false, index, BTR_SEARCH_LEAF, true, 0, &mtr);
640
641 for (; move == true; move = pcur.move_to_prev(&mtr)) {
642 rec = pcur.get_rec();
643
644 if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec)) {
645 continue;
646 }
647
648 offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
649 UT_LOCATION_HERE, &m_heap);
650
651 if (rec_get_deleted_flag(rec, dict_table_is_comp(m_table))) {
652 continue;
653 }
654
655 DDL_Record *record = ut::new_withkey<DDL_Record>(UT_NEW_THIS_FILE_PSI_KEY);
656 convert_to_ddl_record(index->is_clustered(), rec, offsets, *record);
657 records.push_back(record);
658 }
659
660 pcur.close();
661 mtr_commit(&mtr);
662
663 return (error);
664 }
665
666 dberr_t DDL_Log_Table::search(ulint thread_id, DDL_Records &records) {
667 dberr_t error;
668 DDL_Records records_of_thread_id;
669
670 error = search_by_id(thread_id, m_table->first_index()->next(),
671 records_of_thread_id);
672 ut_ad(error == DB_SUCCESS);
673
674 for (auto it = records_of_thread_id.rbegin();
675 it != records_of_thread_id.rend(); ++it) {
676 error = search_by_id((*it)->get_id(), m_table->first_index(), records);
677 ut_ad(error == DB_SUCCESS);
678 }
679
680 for (auto record : records_of_thread_id) {
681 ut::delete_(record);
682 }
683
684 return (error);
685 }
686
687 dberr_t DDL_Log_Table::search_by_id(ulint id, dict_index_t *index,
688 DDL_Records &records) {
689 mtr_t mtr;
690 btr_pcur_t pcur;
691 rec_t *rec;
692 bool move = true;
693 ulint *offsets;
694 dberr_t error = DB_SUCCESS;
695
696 mtr_start(&mtr);
697
698 create_tuple(id, index);
699 pcur.open_no_init(index, m_tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, 0, &mtr,
700 UT_LOCATION_HERE);
701
702 for (; move == true; move = pcur.move_to_next(&mtr)) {
703 rec = pcur.get_rec();
704
705 if (page_rec_is_infimum(rec) || page_rec_is_supremum(rec)) {
706 continue;
707 }
708
709 offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
710 UT_LOCATION_HERE, &m_heap);
711
712 if (cmp_dtuple_rec(m_tuple, rec, index, offsets) != 0) {
713 break;
714 }
715
716 if (rec_get_deleted_flag(rec, dict_table_is_comp(m_table))) {
717 continue;
718 }
719
720 DDL_Record *record = ut::new_withkey<DDL_Record>(UT_NEW_THIS_FILE_PSI_KEY);
721 convert_to_ddl_record(index->is_clustered(), rec, offsets, *record);
722 records.push_back(record);
723 }
724
725 mtr_commit(&mtr);
726
727 return (error);
728 }
729
730 dberr_t DDL_Log_Table::remove(ulint id) {
731 mtr_t mtr;
732 dict_index_t *clust_index = m_table->first_index();
733 btr_pcur_t pcur;
734 ulint *offsets;
735 rec_t *rec;
736 dict_index_t *index;
737 dtuple_t *row;
738 btr_cur_t *btr_cur;
739 dtuple_t *entry;
740 dberr_t err = DB_SUCCESS;
741 enum row_search_result search_result;
742 ulint flags = BTR_NO_LOCKING_FLAG;
743 static uint64_t count = 0;
744
745 if (count++ % 64 == 0) {
746 log_free_check();
747 }
748
749 create_tuple(id, clust_index);
750
751 mtr_start(&mtr);
752
753 pcur.open(clust_index, 0, m_tuple, PAGE_CUR_LE,
754 BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE, &mtr, UT_LOCATION_HERE);
755
756 btr_cur = pcur.get_btr_cur();
757
758 if (page_rec_is_infimum(pcur.get_rec()) ||
759 pcur.get_low_match() < clust_index->n_uniq) {
760 pcur.close();
761 mtr_commit(&mtr);
762 return (DB_SUCCESS);
763 }
764
765 offsets = rec_get_offsets(pcur.get_rec(), clust_index, nullptr,
766 ULINT_UNDEFINED, UT_LOCATION_HERE, &m_heap);
767
768 row = row_build(ROW_COPY_DATA, clust_index, pcur.get_rec(), offsets, nullptr,
769 nullptr, nullptr, nullptr, m_heap);
770
771 rec = btr_cur_get_rec(btr_cur);
772
773 if (!rec_get_deleted_flag(rec, dict_table_is_comp(m_table))) {
774 err = btr_cur_del_mark_set_clust_rec(flags, btr_cur_get_block(btr_cur), rec,
775 clust_index, offsets, m_thr, m_tuple,
776 &mtr);
777 }
778
779 pcur.close();
780 mtr_commit(&mtr);
781
782 if (err != DB_SUCCESS) {
783 return (err);
784 }
785
786 mtr_start(&mtr);
787
788 index = clust_index->next();
789 entry = row_build_index_entry(row, nullptr, index, m_heap);
790 search_result = row_search_index_entry(
791 index, entry, BTR_MODIFY_LEAF | BTR_DELETE_MARK, &pcur, &mtr);
792 btr_cur = pcur.get_btr_cur();
793
794 if (search_result == ROW_NOT_FOUND) {
795 pcur.close();
796 mtr_commit(&mtr);
797 ut_d(ut_error);
798 ut_o(return (DB_CORRUPTION));
799 }
800
801 rec = btr_cur_get_rec(btr_cur);
802
803 if (!rec_get_deleted_flag(rec, dict_table_is_comp(m_table))) {
804 err = btr_cur_del_mark_set_sec_rec(flags, btr_cur, true, m_thr, &mtr);
805 }
806
807 pcur.close();
808 mtr_commit(&mtr);
809
810 return (err);
811 }
812
813 dberr_t DDL_Log_Table::remove(const DDL_Records &records) {
814 dberr_t ret = DB_SUCCESS;
815
816 for (auto record : records) {
817 if (record->get_deletable()) {
818 dberr_t err = remove(record->get_id());
819
820 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
821 if (err != DB_SUCCESS) {
822 ret = err;
823 }
824 }
825 }
826
827 return (ret);
828 }
829
830 Log_DDL::Log_DDL() {
831 ut_ad(dict_sys->ddl_log != nullptr);
832 ut_ad(dict_table_has_autoinc_col(dict_sys->ddl_log));
833 }
834
835 inline uint64_t Log_DDL::next_id() {
836 uint64_t autoinc;
837
838 dict_table_autoinc_lock(dict_sys->ddl_log);
839 autoinc = dict_table_autoinc_read(dict_sys->ddl_log);
840 ++autoinc;
841 dict_table_autoinc_update_if_greater(dict_sys->ddl_log, autoinc);
842 dict_table_autoinc_unlock(dict_sys->ddl_log);
843
844 return (autoinc);
845 }
846
847 inline bool Log_DDL::skip(const dict_table_t *table, THD *thd) {
848 return (recv_recovery_on || thread_local_ddl_log_replay ||
849 (table != nullptr && table->is_temporary()) ||
850 thd_is_bootstrap_thread(thd));
851 }
852
853 dberr_t Log_DDL::write_free_tree_log(trx_t *trx, const dict_index_t *index,
854 bool is_drop_table) {
855 ut_ad(trx == thd_to_trx(current_thd));
856
857 if (skip(index->table, trx->mysql_thd)) {
858 return (DB_SUCCESS);
859 }
860
861 if (index->type & DICT_FTS) {
862 ut_ad(index->page == FIL_NULL);
863 return (DB_SUCCESS);
864 }
865
866 if (dict_index_get_online_status(index) != ONLINE_INDEX_COMPLETE) {
867 /* To skip any previously aborted index. This is because this kind
868 of index should be already freed in previous post_ddl. It's improper
869 to log it and may free it again later, which may trigger some
870 double free page problem. */
871 return (DB_SUCCESS);
872 }
873
874 uint64_t id = next_id();
875 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
876 dberr_t err;
877
878 trx->ddl_operation = true;
879
880 DBUG_INJECT_CRASH("ddl_log_crash_before_free_tree_log",
881 crash_before_free_tree_log_counter++);
882
883 if (is_drop_table) {
884 /* Drop index case, if committed, will be redo only */
885 err = insert_free_tree_log(trx, index, id, thread_id);
886 if (err != DB_SUCCESS) {
887 return err;
888 }
889
890 DBUG_INJECT_CRASH("ddl_log_crash_after_free_tree_log",
891 crash_after_free_tree_log_counter++);
892 } else {
893 /* This is the case of building index during create table
894 scenario. The index will be dropped if ddl is rolled back */
895 err = insert_free_tree_log(nullptr, index, id, thread_id);
896 if (err != DB_SUCCESS) {
897 return err;
898 }
899
900 DBUG_INJECT_CRASH("ddl_log_crash_after_free_tree_log",
901 crash_after_free_tree_log_counter++);
902
903 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_1",
904 srv_inject_too_many_concurrent_trxs = true;);
905
906 /* Delete this operation if the create trx is committed */
907 err = delete_by_id(trx, id, false);
908 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
909
910 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_1",
911 srv_inject_too_many_concurrent_trxs = false;);
912
913 DBUG_INJECT_CRASH("ddl_log_crash_after_free_tree_delete",
914 crash_after_free_tree_delete_counter++);
915 }
916
917 return (err);
918 }
919
920 dberr_t Log_DDL::insert_free_tree_log(trx_t *trx, const dict_index_t *index,
921 uint64_t id, ulint thread_id) {
922 ut_ad(index->page != FIL_NULL);
923
924 dberr_t error;
925 bool has_dd_trx = (trx != nullptr);
926 if (!has_dd_trx) {
927 trx = trx_allocate_for_background();
928 trx_start_internal(trx, UT_LOCATION_HERE);
929 trx->ddl_operation = true;
930 } else {
931 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
932 }
933
934 ut_ad(trx->ddl_operation);
935
936 DDL_Record record;
937 record.set_id(id);
938 record.set_thread_id(thread_id);
939 record.set_type(Log_Type::FREE_TREE_LOG);
940 record.set_space_id(index->space);
941 record.set_page_no(index->page);
942 record.set_index_id(index->id);
943
944 {
945 DDL_Log_Table ddl_log(trx);
946 error = ddl_log.insert(record);
947 }
948
949 if (!has_dd_trx) {
950 trx_commit_for_mysql(trx);
951 trx_free_for_background(trx);
952 }
953
954 if (error == DB_SUCCESS && srv_print_ddl_logs) {
955 ib::info(ER_IB_MSG_647) << "DDL log insert : " << record;
956 }
957
958 return (error);
959 }
960
961 dberr_t Log_DDL::write_delete_space_log(trx_t *trx, const dict_table_t *table,
962 space_id_t space_id,
963 const char *file_path, bool is_drop,
964 bool dict_locked) {
965 ut_ad(trx == thd_to_trx(current_thd));
966 ut_ad(table == nullptr || dict_table_is_file_per_table(table));
967
968 if (skip(table, trx->mysql_thd)) {
969 return (DB_SUCCESS);
970 }
971
972 uint64_t id = next_id();
973 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
974 dberr_t err;
975
976 trx->ddl_operation = true;
977
978 DBUG_INJECT_CRASH("ddl_log_crash_before_delete_space_log",
979 crash_before_delete_space_log_counter++);
980
981 if (is_drop) {
982 err = insert_delete_space_log(trx, id, thread_id, space_id, file_path,
983 dict_locked);
984 if (err != DB_SUCCESS) {
985 return err;
986 }
987
988 DBUG_INJECT_CRASH("ddl_log_crash_after_delete_space_log",
989 crash_after_delete_space_log_counter++);
990 } else {
991 err = insert_delete_space_log(nullptr, id, thread_id, space_id, file_path,
992 dict_locked);
993 if (err != DB_SUCCESS) {
994 return err;
995 }
996
997 DBUG_INJECT_CRASH("ddl_log_crash_after_delete_space_log",
998 crash_after_delete_space_log_counter++);
999
1000 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_2",
1001 srv_inject_too_many_concurrent_trxs = true;);
1002
1003 err = delete_by_id(trx, id, dict_locked);
1004 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1005
1006 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_2",
1007 srv_inject_too_many_concurrent_trxs = false;);
1008
1009 DBUG_INJECT_CRASH("ddl_log_crash_after_delete_space_delete",
1010 crash_after_delete_space_delete_counter++);
1011 }
1012
1013 return (err);
1014 }
1015
1016 dberr_t Log_DDL::insert_delete_space_log(trx_t *trx, uint64_t id,
1017 ulint thread_id, space_id_t space_id,
1018 const char *file_path,
1019 bool dict_locked) {
1020 dberr_t error;
1021 bool has_dd_trx = (trx != nullptr);
1022
1023 if (!has_dd_trx) {
1024 trx = trx_allocate_for_background();
1025 trx_start_internal(trx, UT_LOCATION_HERE);
1026 trx->ddl_operation = true;
1027 } else {
1028 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
1029 }
1030
1031 ut_ad(trx->ddl_operation);
1032
1033 if (dict_locked) {
1034 dict_sys_mutex_exit();
1035 }
1036
1037 DDL_Record record;
1038 record.set_id(id);
1039 record.set_thread_id(thread_id);
1040 record.set_type(Log_Type::DELETE_SPACE_LOG);
1041 record.set_space_id(space_id);
1042 record.set_old_file_path(file_path);
1043
1044 {
1045 DDL_Log_Table ddl_log(trx);
1046 error = ddl_log.insert(record);
1047 }
1048
1049 if (dict_locked) {
1050 dict_sys_mutex_enter();
1051 }
1052
1053 if (!has_dd_trx) {
1054 trx_commit_for_mysql(trx);
1055 trx_free_for_background(trx);
1056 }
1057
1058 if (error == DB_SUCCESS && srv_print_ddl_logs) {
1059 ib::info(ER_IB_MSG_648) << "DDL log insert : " << record;
1060 }
1061
1062 return (error);
1063 }
1064
1065 dberr_t Log_DDL::write_rename_space_log(space_id_t space_id,
1066 const char *old_file_path,
1067 const char *new_file_path) {
1068 /* Missing current_thd, it happens during crash recovery */
1069 if (!current_thd) {
1070 return (DB_SUCCESS);
1071 }
1072
1073 trx_t *trx = thd_to_trx(current_thd);
1074
1075 /* This is special case for fil_rename_tablespace during recovery */
1076 if (trx == nullptr) {
1077 return (DB_SUCCESS);
1078 }
1079
1080 if (skip(nullptr, trx->mysql_thd)) {
1081 return (DB_SUCCESS);
1082 }
1083
1084 uint64_t id = next_id();
1085 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
1086
1087 trx->ddl_operation = true;
1088
1089 DBUG_INJECT_CRASH("ddl_log_crash_before_rename_space_log",
1090 crash_before_rename_space_log_counter++);
1091
1092 dberr_t err = insert_rename_space_log(id, thread_id, space_id, old_file_path,
1093 new_file_path);
1094 if (err != DB_SUCCESS) {
1095 return err;
1096 }
1097
1098 DBUG_INJECT_CRASH("ddl_log_crash_after_rename_space_log",
1099 crash_after_rename_space_log_counter++);
1100
1101 DBUG_EXECUTE_IF("ddl_log_crash_after_rename_space_log_insert",
1102 DBUG_SUICIDE(););
1103
1104 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_4",
1105 srv_inject_too_many_concurrent_trxs = true;);
1106
1107 err = delete_by_id(trx, id, true);
1108 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1109
1110 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_4",
1111 srv_inject_too_many_concurrent_trxs = false;);
1112
1113 DBUG_INJECT_CRASH("ddl_log_crash_after_rename_space_delete",
1114 crash_after_rename_space_delete_counter++);
1115
1116 return (err);
1117 }
1118
1119 dberr_t Log_DDL::insert_rename_space_log(uint64_t id, ulint thread_id,
1120 space_id_t space_id,
1121 const char *old_file_path,
1122 const char *new_file_path) {
1123 dberr_t error;
1124 trx_t *trx = trx_allocate_for_background();
1125 trx_start_internal(trx, UT_LOCATION_HERE);
1126 trx->ddl_operation = true;
1127
1128 ut_ad(dict_sys_mutex_own());
1129 dict_sys_mutex_exit();
1130
1131 DDL_Record record;
1132 record.set_id(id);
1133 record.set_thread_id(thread_id);
1134 record.set_type(Log_Type::RENAME_SPACE_LOG);
1135 record.set_space_id(space_id);
1136 record.set_old_file_path(old_file_path);
1137 record.set_new_file_path(new_file_path);
1138
1139 {
1140 DDL_Log_Table ddl_log(trx);
1141 error = ddl_log.insert(record);
1142 }
1143
1144 dict_sys_mutex_enter();
1145
1146 trx_commit_for_mysql(trx);
1147 trx_free_for_background(trx);
1148
1149 if (error == DB_SUCCESS && srv_print_ddl_logs) {
1150 ib::info(ER_IB_MSG_649) << "DDL log insert : " << record;
1151 }
1152
1153 return (error);
1154 }
1155
1156 DDL_Record *Log_DDL::find_alter_encrypt_record(space_id_t space_id) {
1157 if (!ts_encrypt_ddl_records.empty()) {
1158 for (const auto it : ts_encrypt_ddl_records) {
1159 if (it->get_space_id() == space_id) {
1160 return it;
1161 }
1162 }
1163 }
1164 return nullptr;
1165 }
1166
1167 dberr_t Log_DDL::write_alter_encrypt_space_log(space_id_t space_id,
1168 Encryption::Progress type,
1169 DDL_Record *existing_rec) {
1170 /* Missing current_thd, it happens during crash recovery */
1171 if (!current_thd) {
1172 return (DB_SUCCESS);
1173 }
1174
1175 trx_t *trx = thd_to_trx(current_thd);
1176
1177 if (skip(nullptr, trx->mysql_thd)) {
1178 return (DB_SUCCESS);
1179 }
1180
1181 uint64_t id = next_id();
1182 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
1183
1184 trx->ddl_operation = true;
1185
1186 DBUG_INJECT_CRASH("ddl_log_crash_before_alter_encrypt_space_log",
1187 crash_before_alter_encrypt_space_log_counter++);
1188
1189 dberr_t err = insert_alter_encrypt_space_log(id, thread_id, space_id, type,
1190 existing_rec);
1191 if (err != DB_SUCCESS) {
1192 return err;
1193 }
1194
1195 DBUG_INJECT_CRASH("ddl_log_crash_after_alter_encrypt_space_log",
1196 crash_after_alter_encrypt_space_log_counter++);
1197
1198 DBUG_EXECUTE_IF("DDL_Log_remove_inject_startup_error_1",
1199 srv_inject_too_many_concurrent_trxs = true;);
1200
1201 /* This record to be removed with main transaction commit */
1202 err = delete_by_id(trx, id, false);
1203 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1204
1205 DBUG_EXECUTE_IF("DDL_Log_remove_inject_startup_error_1",
1206 srv_inject_too_many_concurrent_trxs = false;);
1207
1208 return (err);
1209 }
1210
1211 dberr_t Log_DDL::insert_alter_encrypt_space_log(uint64_t id, ulint thread_id,
1212 space_id_t space_id,
1213 Encryption::Progress type,
1214 DDL_Record *existing_rec) {
1215 dberr_t err = DB_SUCCESS;
1216 trx_t *trx = trx_allocate_for_background();
1217 trx_start_internal(trx, UT_LOCATION_HERE);
1218 trx->ddl_operation = true;
1219
1220 DDL_Record record;
1221 record.set_id(id);
1222 record.set_thread_id(thread_id);
1223
1224 if (type == Encryption::Progress::ENCRYPTION) {
1225 record.set_type(Log_Type::ALTER_ENCRYPT_TABLESPACE_LOG);
1226 } else {
1227 ut_ad(type == Encryption::Progress::DECRYPTION);
1228 record.set_type(Log_Type::ALTER_UNENCRYPT_TABLESPACE_LOG);
1229 }
1230 record.set_space_id(space_id);
1231
1232 {
1233 DDL_Log_Table ddl_log(trx);
1234 if (existing_rec != nullptr) {
1235 err = ddl_log.remove(existing_rec->get_id());
1236 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1237
1238 if (err == DB_TOO_MANY_CONCURRENT_TRXS) {
1239 ib::error(ER_IB_MSG_DDL_LOG_DELETE_BY_ID_TMCT);
1240 }
1241 }
1242
1243 if (err == DB_SUCCESS) {
1244 err = ddl_log.insert(record);
1245 }
1246 }
1247
1248 trx_commit_for_mysql(trx);
1249 trx_free_for_background(trx);
1250
1251 if (err == DB_SUCCESS && srv_print_ddl_logs) {
1252 ib::info(ER_IB_MSG_1284) << "DDL log insert : " << record;
1253 }
1254
1255 return (err);
1256 }
1257
1258 dberr_t Log_DDL::write_drop_log(trx_t *trx, const table_id_t table_id) {
1259 if (skip(nullptr, trx->mysql_thd)) {
1260 return (DB_SUCCESS);
1261 }
1262
1263 trx->ddl_operation = true;
1264
1265 uint64_t id = next_id();
1266 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
1267
1268 DBUG_INJECT_CRASH("ddl_log_crash_before_drop_log",
1269 crash_before_drop_log_counter++);
1270
1271 dberr_t err;
1272 err = insert_drop_log(trx, id, thread_id, table_id);
1273 if (err != DB_SUCCESS) {
1274 return err;
1275 }
1276
1277 DBUG_INJECT_CRASH("ddl_log_crash_after_drop_log",
1278 crash_after_drop_log_counter++);
1279
1280 return (err);
1281 }
1282
1283 dberr_t Log_DDL::insert_drop_log(trx_t *trx, uint64_t id, ulint thread_id,
1284 const table_id_t table_id) {
1285 ut_ad(trx->ddl_operation);
1286 ut_ad(dict_sys_mutex_own());
1287
1288 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
1289
1290 dict_sys_mutex_exit();
1291
1292 dberr_t error;
1293 DDL_Record record;
1294 record.set_id(id);
1295 record.set_thread_id(thread_id);
1296 record.set_type(Log_Type::DROP_LOG);
1297 record.set_table_id(table_id);
1298
1299 {
1300 DDL_Log_Table ddl_log(trx);
1301 error = ddl_log.insert(record);
1302 }
1303
1304 dict_sys_mutex_enter();
1305
1306 if (error == DB_SUCCESS && srv_print_ddl_logs) {
1307 ib::info(ER_IB_MSG_650) << "DDL log insert : " << record;
1308 }
1309
1310 return (error);
1311 }
1312
1313 dberr_t Log_DDL::write_rename_table_log(dict_table_t *table,
1314 const char *old_name,
1315 const char *new_name) {
1316 trx_t *trx = thd_to_trx(current_thd);
1317
1318 if (skip(table, trx->mysql_thd)) {
1319 return (DB_SUCCESS);
1320 }
1321
1322 uint64_t id = next_id();
1323 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
1324
1325 trx->ddl_operation = true;
1326
1327 dberr_t err =
1328 insert_rename_table_log(id, thread_id, table->id, old_name, new_name);
1329 if (err != DB_SUCCESS) {
1330 return err;
1331 }
1332
1333 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_5",
1334 srv_inject_too_many_concurrent_trxs = true;);
1335
1336 err = delete_by_id(trx, id, true);
1337 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1338
1339 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_5",
1340 srv_inject_too_many_concurrent_trxs = false;);
1341
1342 return (err);
1343 }
1344
1345 dberr_t Log_DDL::insert_rename_table_log(uint64_t id, ulint thread_id,
1346 table_id_t table_id,
1347 const char *old_name,
1348 const char *new_name) {
1349 dberr_t error;
1350 trx_t *trx = trx_allocate_for_background();
1351 trx_start_internal(trx, UT_LOCATION_HERE);
1352 trx->ddl_operation = true;
1353
1354 ut_ad(dict_sys_mutex_own());
1355 dict_sys_mutex_exit();
1356
1357 DDL_Record record;
1358 record.set_id(id);
1359 record.set_thread_id(thread_id);
1360 record.set_type(Log_Type::RENAME_TABLE_LOG);
1361 record.set_table_id(table_id);
1362 record.set_old_file_path(old_name);
1363 record.set_new_file_path(new_name);
1364
1365 {
1366 DDL_Log_Table ddl_log(trx);
1367 error = ddl_log.insert(record);
1368 }
1369
1370 dict_sys_mutex_enter();
1371
1372 trx_commit_for_mysql(trx);
1373 trx_free_for_background(trx);
1374
1375 if (error == DB_SUCCESS && srv_print_ddl_logs) {
1376 ib::info(ER_IB_MSG_651) << "DDL log insert : " << record;
1377 }
1378
1379 return (error);
1380 }
1381
1382 dberr_t Log_DDL::write_remove_cache_log(trx_t *trx, dict_table_t *table) {
1383 ut_ad(trx == thd_to_trx(current_thd));
1384
1385 if (skip(table, trx->mysql_thd)) {
1386 return (DB_SUCCESS);
1387 }
1388
1389 uint64_t id = next_id();
1390 ulint thread_id = thd_get_thread_id(trx->mysql_thd);
1391
1392 trx->ddl_operation = true;
1393
1394 dberr_t err =
1395 insert_remove_cache_log(id, thread_id, table->id, table->name.m_name);
1396 if (err != DB_SUCCESS) {
1397 return err;
1398 }
1399
1400 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_3",
1401 srv_inject_too_many_concurrent_trxs = true;);
1402
1403 err = delete_by_id(trx, id, false);
1404 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1405
1406 DBUG_EXECUTE_IF("DDL_Log_remove_inject_error_3",
1407 srv_inject_too_many_concurrent_trxs = false;);
1408
1409 return (err);
1410 }
1411
1412 dberr_t Log_DDL::insert_remove_cache_log(uint64_t id, ulint thread_id,
1413 table_id_t table_id,
1414 const char *table_name) {
1415 dberr_t error;
1416 trx_t *trx = trx_allocate_for_background();
1417 trx_start_internal(trx, UT_LOCATION_HERE);
1418 trx->ddl_operation = true;
1419
1420 DDL_Record record;
1421 record.set_id(id);
1422 record.set_thread_id(thread_id);
1423 record.set_type(Log_Type::REMOVE_CACHE_LOG);
1424 record.set_table_id(table_id);
1425 record.set_new_file_path(table_name);
1426
1427 {
1428 DDL_Log_Table ddl_log(trx);
1429 error = ddl_log.insert(record);
1430 }
1431
1432 trx_commit_for_mysql(trx);
1433 trx_free_for_background(trx);
1434
1435 if (error == DB_SUCCESS && srv_print_ddl_logs) {
1436 ib::info(ER_IB_MSG_652) << "DDL log insert : " << record;
1437 }
1438
1439 return (error);
1440 }
1441
1442 dberr_t Log_DDL::delete_by_id(trx_t *trx, uint64_t id, bool dict_locked) {
1443 dberr_t err;
1444
1445 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
1446
1447 ut_ad(trx->ddl_operation);
1448
1449 if (dict_locked) {
1450 dict_sys_mutex_exit();
1451 }
1452
1453 {
1454 DDL_Log_Table ddl_log(trx);
1455 err = ddl_log.remove(id);
1456 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1457
1458 if (err == DB_TOO_MANY_CONCURRENT_TRXS) {
1459 ib::error(ER_IB_MSG_DDL_LOG_DELETE_BY_ID_TMCT);
1460 }
1461 }
1462
1463 if (dict_locked) {
1464 dict_sys_mutex_enter();
1465 }
1466
1467 if (srv_print_ddl_logs && err == DB_SUCCESS) {
1468 ib::info(ER_IB_MSG_DDL_LOG_DELETE_BY_ID_OK) << "DDL log delete : " << id;
1469 }
1470
1471 return (err);
1472 }
1473
1474 dberr_t Log_DDL::replay_all() {
1475 ut_ad(is_in_recovery());
1476
1477 DDL_Log_Table ddl_log;
1478 DDL_Records records;
1479
1480 dberr_t err = ddl_log.search_all(records);
1481 ut_ad(err == DB_SUCCESS);
1482
1483 for (auto record : records) {
1484 err = log_ddl->replay(*record);
1485 if (err != DB_SUCCESS) {
1486 break;
1487 }
1488 }
1489
1490 if (err != DB_SUCCESS) {
1491 return err;
1492 }
1493
1494 err = delete_by_ids(records);
1495 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1496
1497 for (auto record : records) {
1498 if (record->get_deletable()) {
1499 ut::delete_(record);
1500 }
1501 }
1502
1503 return (err);
1504 }
1505
1506 dberr_t Log_DDL::replay_by_thread_id(ulint thread_id) {
1507 DDL_Log_Table ddl_log;
1508 DDL_Records records;
1509
1510 dberr_t err = ddl_log.search(thread_id, records);
1511 ut_ad(err == DB_SUCCESS);
1512
1513 for (auto record : records) {
1514 if (record->get_type() == Log_Type::ALTER_ENCRYPT_TABLESPACE_LOG ||
1515 record->get_type() == Log_Type::ALTER_UNENCRYPT_TABLESPACE_LOG) {
1516 DDL_Record *rec = find_alter_encrypt_record(record->get_space_id());
1517 if (rec != nullptr) {
1518 ut_ad(record->get_id() != rec->get_id());
1519 }
1520 } else {
1521 log_ddl->replay(*record);
1522 }
1523 }
1524
1525 err = delete_by_ids(records);
1526 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1527
1528 for (auto record : records) {
1529 if (record->get_deletable()) {
1530 ut::delete_(record);
1531 }
1532 }
1533
1534 return (err);
1535 }
1536
1537 constexpr uint32_t DELETE_IDS_RETRIES_MAX = 10;
1538
1539 dberr_t Log_DDL::delete_by_ids(DDL_Records &records) {
1540 dberr_t err = DB_SUCCESS;
1541
1542 if (records.empty()) {
1543 return (err);
1544 }
1545
1546 int t;
1547 for (t = DELETE_IDS_RETRIES_MAX; t > 0; t--) {
1548 trx_t *trx;
1549 trx = trx_allocate_for_background();
1550 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
1551 trx->ddl_operation = true;
1552
1553 {
1554 DDL_Log_Table ddl_log(trx);
1555 err = ddl_log.remove(records);
1556 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1557 }
1558
1559 trx_commit_for_mysql(trx);
1560 trx_free_for_background(trx);
1561
1562 #ifdef UNIV_DEBUG
1563 if (srv_inject_too_many_concurrent_trxs) {
1564 srv_inject_too_many_concurrent_trxs = false;
1565 }
1566 #endif /* UNIV_DEBUG */
1567
1568 if (err != DB_TOO_MANY_CONCURRENT_TRXS) {
1569 break;
1570 }
1571 }
1572
1573 return (err);
1574 }
1575
1576 dberr_t Log_DDL::replay(DDL_Record &record) {
1577 dberr_t err = DB_SUCCESS;
1578
1579 if (srv_print_ddl_logs) {
1580 ib::info(ER_IB_MSG_654) << "DDL log replay : " << record;
1581 }
1582
1583 switch (record.get_type()) {
1584 case Log_Type::FREE_TREE_LOG:
1585 replay_free_tree_log(record.get_space_id(), record.get_page_no(),
1586 record.get_index_id());
1587 break;
1588
1589 case Log_Type::DELETE_SPACE_LOG:
1590 replay_delete_space_log(record.get_space_id(),
1591 record.get_old_file_path());
1592 break;
1593
1594 case Log_Type::RENAME_SPACE_LOG:
1595 replay_rename_space_log(record.get_space_id(), record.get_old_file_path(),
1596 record.get_new_file_path());
1597 break;
1598
1599 case Log_Type::DROP_LOG:
1600 replay_drop_log(record.get_table_id());
1601 break;
1602
1603 case Log_Type::RENAME_TABLE_LOG:
1604 replay_rename_table_log(record.get_old_file_path(),
1605 record.get_new_file_path());
1606 break;
1607
1608 case Log_Type::REMOVE_CACHE_LOG:
1609 replay_remove_cache_log(record.get_table_id(),
1610 record.get_new_file_path());
1611 break;
1612
1613 case Log_Type::ALTER_ENCRYPT_TABLESPACE_LOG:
1614 case Log_Type::ALTER_UNENCRYPT_TABLESPACE_LOG:
1615 err = replay_alter_encrypt_space_log(record);
1616 break;
1617
1618 default:
1619 ut_error;
1620 }
1621
1622 return (err);
1623 }
1624
1625 void Log_DDL::replay_free_tree_log(space_id_t space_id, page_no_t page_no,
1626 ulint index_id) {
1627 ut_ad(space_id != SPACE_UNKNOWN);
1628 ut_ad(page_no != FIL_NULL);
1629
1630 bool found;
1631 const page_size_t page_size(fil_space_get_page_size(space_id, &found));
1632
1633 /* Skip if it is a single table tablespace and the .ibd
1634 file is missing */
1635 if (!found) {
1636 if (srv_print_ddl_logs) {
1637 ib::info(ER_IB_MSG_655)
1638 << "DDL log replay : FREE tablespace " << space_id << " is missing.";
1639 }
1640
1641 return;
1642 }
1643
1644 /* This is required by dropping hash index afterwards. */
1645 dict_sys_mutex_enter();
1646
1647 mtr_t mtr;
1648 mtr_start(&mtr);
1649
1650 btr_free_if_exists(page_id_t(space_id, page_no), page_size, index_id, &mtr);
1651
1652 mtr_commit(&mtr);
1653
1654 dict_sys_mutex_exit();
1655
1656 DBUG_INJECT_CRASH("ddl_log_crash_after_replay", crash_after_replay_counter++);
1657 }
1658
1659 void Log_DDL::replay_delete_space_log(space_id_t space_id,
1660 const char *file_path) {
1661 THD *thd = current_thd;
1662
1663 if (fsp_is_undo_tablespace(space_id)) {
1664 /* Serialize this delete with all undo tablespace DDLs. */
1665 mutex_enter(&undo::ddl_mutex);
1666
1667 /* If this is called during DROP UNDO TABLESPACE, then the undo_space
1668 is already gone. But if this is called at startup after a crash, that
1669 memory object might exist. If the crash occurred just before the file
1670 was deleted, then at startup it was opened in srv_undo_tablespaces_open().
1671 Then in trx_rsegs_init(), any explicit undo tablespace that did not
1672 contain any undo logs was set to empty. That prevented any new undo
1673 logs to be added during the startup process up till now. So whether
1674 we are at runtime or startup, we assert that the undo tablespace is
1675 empty and delete the undo::Tablespace object if it exists. */
1676 undo::spaces->x_lock();
1677 space_id_t space_num = undo::id2num(space_id);
1678 undo::Tablespace *undo_space = undo::spaces->find(space_num);
1679 if (undo_space != nullptr) {
1680 ut_a(undo_space->is_empty());
1681 undo::spaces->drop(undo_space);
1682 }
1683 undo::spaces->x_unlock();
1684 }
1685
1686 if (thd != nullptr) {
1687 /* For general tablespace, MDL on SDI tables is already
1688 acquired at innobase_drop_tablespace() and for file_per_table
1689 tablespace, MDL is acquired at row_drop_table_for_mysql() */
1690 dict_sys_mutex_enter();
1691 dict_sdi_remove_from_cache(space_id, nullptr, true);
1692 dict_sys_mutex_exit();
1693 }
1694
1695 /* A master key rotation blocks all DDLs using backup_lock, so it is assured
1696 that during CREATE/DROP TABLE, master key will not change. */
1697
1698 DBUG_EXECUTE_IF("ddl_log_replay_delete_space_crash_before_drop",
1699 DBUG_SUICIDE(););
1700
1701 /* Update filename with correct partition case, of needed. */
1702 std::string path_str(file_path);
1703 std::string space_name;
1704 fil_update_partition_name(space_id, 0, false, space_name, path_str);
1705 file_path = path_str.c_str();
1706
1707 row_drop_tablespace(space_id, file_path);
1708
1709 /* If this is an undo space_id, allow the undo number for it
1710 to be reused. */
1711 if (fsp_is_undo_tablespace(space_id)) {
1712 undo::spaces->x_lock();
1713 undo::unuse_space_id(space_id);
1714 undo::spaces->x_unlock();
1715
1716 mutex_exit(&undo::ddl_mutex);
1717 }
1718
1719 DBUG_INJECT_CRASH("ddl_log_crash_after_replay", crash_after_replay_counter++);
1720 }
1721
1722 void Log_DDL::replay_rename_space_log(space_id_t space_id,
1723 const char *old_file_path,
1724 const char *new_file_path) {
1725 bool ret;
1726 page_id_t page_id(space_id, 0);
1727
1728 std::string space_name;
1729
1730 /* Update old filename with correct partition case, of needed. */
1731 std::string old_path(old_file_path);
1732 fil_update_partition_name(space_id, 0, false, space_name, old_path);
1733 old_file_path = old_path.c_str();
1734
1735 /* Update new filename with correct partition case, of needed. */
1736 std::string new_path(new_file_path);
1737 fil_update_partition_name(space_id, 0, false, space_name, new_path);
1738 new_file_path = new_path.c_str();
1739
1740 ret = fil_op_replay_rename_for_ddl(page_id, old_file_path, new_file_path);
1741
1742 if (!ret && srv_print_ddl_logs) {
1743 ib::info(ER_IB_MSG_656) << "DDL log replay : RENAME from " << old_file_path
1744 << " to " << new_file_path << " failed";
1745 }
1746
1747 DBUG_INJECT_CRASH("ddl_log_crash_after_replay", crash_after_replay_counter++);
1748 }
1749
1750 static dberr_t replace_and_insert(DDL_Record *record) {
1751 dberr_t err = DB_SUCCESS;
1752 trx_t *trx = trx_allocate_for_background();
1753 trx_start_internal(trx, UT_LOCATION_HERE);
1754 trx->ddl_operation = true;
1755
1756 /* update the thread_id for the record */
1757 record->set_thread_id(ULINT_MAX);
1758
1759 {
1760 DDL_Log_Table ddl_log(trx);
1761 /* Remove old record and insert the new record */
1762 err = ddl_log.remove(record->get_id());
1763 ut_ad(err == DB_SUCCESS || err == DB_TOO_MANY_CONCURRENT_TRXS);
1764
1765 if (err == DB_SUCCESS) {
1766 /* Insert new record entry */
1767 err = ddl_log.insert(*record);
1768 }
1769 }
1770
1771 trx_commit_for_mysql(trx);
1772 trx_free_for_background(trx);
1773
1774 return err;
1775 }
1776
1777 dberr_t Log_DDL::replay_alter_encrypt_space_log(DDL_Record &record) {
1778 dberr_t error = DB_SUCCESS;
1779 /* Normal operation, we shouldn't come here during post_ddl */
1780 ut_ad(is_in_recovery());
1781
1782 error = replace_and_insert(&record);
1783 if (error != DB_SUCCESS) {
1784 return error;
1785 }
1786 ut_ad(record.get_thread_id() == ULINT_MAX);
1787
1788 /* We could have resume encryption execution one by one for each tablespace
1789 from here by calling SQL API to run the query. But then it would be blocking
1790 server bootstrap. We need to resume this encryption in BG thread so we need
1791 to just make a note of this space and operation here and don't do any real
1792 operation. */
1793 ts_encrypt_ddl_records.push_back(&record);
1794
1795 /* Make sure not to delete this record till resume operation finishes.
1796 This is to make sure that if there is a crash before that, we can resume
1797 encryption in the next restart. */
1798 record.set_deletable(false);
1799
1800 DBUG_INJECT_CRASH("ddl_log_crash_after_replay", crash_after_replay_counter++);
1801 return error;
1802 }
1803
1804 void Log_DDL::replay_drop_log(const table_id_t table_id) {
1805 mutex_enter(&dict_persist->mutex);
1806 ut_d(dberr_t error =) dict_persist->table_buffer->remove(table_id);
1807 ut_ad(error == DB_SUCCESS);
1808 mutex_exit(&dict_persist->mutex);
1809
1810 DBUG_INJECT_CRASH("ddl_log_crash_after_replay", crash_after_replay_counter++);
1811 }
1812
1813 void Log_DDL::replay_rename_table_log(const char *old_name,
1814 const char *new_name) {
1815 if (is_in_recovery()) {
1816 if (srv_print_ddl_logs) {
1817 ib::info(ER_IB_MSG_657) << "DDL log replay : in recovery,"
1818 << " skip RENAME TABLE";
1819 }
1820
1821 return;
1822 }
1823
1824 trx_t *trx;
1825 trx = trx_allocate_for_background();
1826 trx->mysql_thd = current_thd;
1827 trx_start_if_not_started(trx, true, UT_LOCATION_HERE);
1828
1829 row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE);
1830 trx_set_dict_operation(trx, TRX_DICT_OP_TABLE);
1831
1832 /* Convert partition table name DDL log, if needed. Required if
1833 upgrading a crashed database. */
1834 std::string old_table(old_name);
1835 dict_name::rebuild(old_table);
1836 old_name = old_table.c_str();
1837
1838 std::string new_table(new_name);
1839 dict_name::rebuild(new_table);
1840 new_name = new_table.c_str();
1841
1842 dberr_t err;
1843 err = row_rename_table_for_mysql(old_name, new_name, nullptr, trx, true);
1844
1845 dict_table_t *table;
1846 table = dd_table_open_on_name_in_mem(new_name, true);
1847 if (table != nullptr) {
1848 dict_table_ddl_release(table);
1849 dd_table_close(table, nullptr, nullptr, true);
1850 }
1851
1852 row_mysql_unlock_data_dictionary(trx);
1853
1854 trx_commit_for_mysql(trx);
1855 trx_free_for_background(trx);
1856
1857 if (err != DB_SUCCESS) {
1858 if (srv_print_ddl_logs) {
1859 ib::info(ER_IB_MSG_658)
1860 << "DDL log replay : rename table"
1861 << " in cache from " << old_name << " to " << new_name;
1862 }
1863 } else {
1864 /* TODO: Once we get rid of dict_operation_lock,
1865 we may consider to do this in row_rename_table_for_mysql,
1866 so no need to worry this rename here */
1867 char errstr[512];
1868
1869 dict_stats_rename_table(old_name, new_name, errstr, sizeof(errstr));
1870 }
1871 }
1872
1873 void Log_DDL::replay_remove_cache_log(table_id_t table_id,
1874 const char *table_name) {
1875 if (is_in_recovery()) {
1876 if (srv_print_ddl_logs) {
1877 ib::info(ER_IB_MSG_659) << "DDL log replay : in recovery,"
1878 << " skip REMOVE CACHE";
1879 }
1880
1881 return;
1882 }
1883
1884 dict_table_t *table;
1885
1886 table = dd_table_open_on_id_in_mem(table_id, false);
1887
1888 /* Convert partition table name DDL log, if needed. Required if
1889 upgrading a crashed database. */
1890 std::string table_str(table_name);
1891 dict_name::rebuild(table_str);
1892 table_name = table_str.c_str();
1893
1894 if (table != nullptr) {
1895 ut_ad(strcmp(table->name.m_name, table_name) == 0);
1896
1897 dict_sys_mutex_enter();
1898 dd_table_close(table, nullptr, nullptr, true);
1899 btr_drop_ahi_for_table(table);
1900 dict_table_remove_from_cache(table);
1901 dict_sys_mutex_exit();
1902 }
1903 }
1904
1905 dberr_t Log_DDL::post_ddl(THD *thd) {
1906 if (skip(nullptr, thd)) {
1907 return (DB_SUCCESS);
1908 }
1909
1910 if (srv_read_only_mode || srv_force_recovery >= SRV_FORCE_NO_UNDO_LOG_SCAN) {
1911 return (DB_SUCCESS);
1912 }
1913
1914 DEBUG_SYNC(thd, "innodb_ddl_log_before_enter");
1915
1916 DBUG_EXECUTE_IF("ddl_log_before_post_ddl", DBUG_SUICIDE(););
1917
1918 /* If srv_force_recovery > 0, DROP TABLE is allowed, and here only
1919 DELETE and DROP log can be replayed. */
1920
1921 ulint thread_id = thd_get_thread_id(thd);
1922
1923 if (srv_print_ddl_logs) {
1924 ib::info(ER_IB_MSG_660)
1925 << "DDL log post ddl : begin for thread id : " << thread_id;
1926 }
1927
1928 thread_local_ddl_log_replay = true;
1929
1930 dberr_t err = replay_by_thread_id(thread_id);
1931
1932 thread_local_ddl_log_replay = false;
1933
1934 if (srv_print_ddl_logs) {
1935 ib::info(ER_IB_MSG_661)
1936 << "DDL log post ddl : end for thread id : " << thread_id;
1937 }
1938
1939 return (err);
1940 }
1941
1942 dberr_t Log_DDL::recover() {
1943 if (srv_read_only_mode || srv_force_recovery > 0) {
1944 return (DB_SUCCESS);
1945 }
1946
1947 ib::info(ER_IB_MSG_662) << "DDL log recovery : begin";
1948
1949 thread_local_ddl_log_replay = true;
1950 s_in_recovery = true;
1951
1952 dberr_t err = replay_all();
1953
1954 thread_local_ddl_log_replay = false;
1955 s_in_recovery = false;
1956
1957 ib::info(ER_IB_MSG_663) << "DDL log recovery : end";
1958
1959 return (err);
1960 }