Debian Codegraph @

storage/innobase/include/log0ddl.h

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 include/log0ddl.h
35 DDL log
36
37 Created 12/1/2016 Shaohua Wang
38 *******************************************************/
39
40 #ifndef log0ddl_h
41 #define log0ddl_h
42
43 /** DDL log types defined as uint32_t because it costs 4 bytes in
44 mysql.innodb_ddl_log. */
45 enum class Log_Type : uint32_t {
46 /** Smallest log type */
47 SMALLEST_LOG = 1,
48
49 /** Drop an index tree */
50 FREE_TREE_LOG = 1,
51
52 /** Delete a file */
53 DELETE_SPACE_LOG,
54
55 /** Rename a file */
56 RENAME_SPACE_LOG,
57
58 /** Drop the entry in innodb_table_metadata */
59 DROP_LOG,
60
61 /** Rename table in dict cache. */
62 RENAME_TABLE_LOG,
63
64 /** Remove a table from dict cache */
65 REMOVE_CACHE_LOG,
66
67 /** Alter Encrypt a tablespace */
68 ALTER_ENCRYPT_TABLESPACE_LOG,
69
70 /** Alter Unencrypt a tablespace */
71 ALTER_UNENCRYPT_TABLESPACE_LOG,
72
73 /** Biggest log type */
74 BIGGEST_LOG = ALTER_UNENCRYPT_TABLESPACE_LOG
75 };
76
77 /** DDL log record */
78 class DDL_Record {
79 public:
80 /** Constructor. */
81 DDL_Record();
82
83 /** Destructor. */
84 ~DDL_Record();
85
86 /** Get the id of the DDL log record.
87 @return id of the record. */
88 ulint get_id() const { return (m_id); }
89
90 /** Set the id for the DDL log record.
91 @param[in] id id of the record. */
92 void set_id(ulint id) { m_id = id; }
93
94 /** Get the type of operation to perform
95 for the DDL log record.
96 @return type of the record. */
97 Log_Type get_type() const { return (m_type); }
98
99 /** Set the type for the DDL log record.
100 @param[in] type set the record type.*/
101 void set_type(Log_Type type) { m_type = type; }
102
103 /** Get the thread id for the DDL log record.
104 @return thread id of the DDL log record. */
105 ulint get_thread_id() const { return (m_thread_id); }
106
107 /** Set the thread id for the DDL log record.
108 @param[in] thread_id thread id. */
109 void set_thread_id(ulint thread_id) { m_thread_id = thread_id; }
110
111 /** Get the space_id present in the DDL log record.
112 @return space_id in the DDL log record. */
113 space_id_t get_space_id() const { return (m_space_id); }
114
115 /** Set the space id for the DDL log record.
116 @param[in] space space id. */
117 void set_space_id(space_id_t space) { m_space_id = space; }
118
119 /** Get the page no present in the DDL log record.
120 @return page_no */
121 page_no_t get_page_no() const { return (m_page_no); }
122
123 /** Set the page number for the DDL log record.
124 @param[in] page_no page number. */
125 void set_page_no(page_no_t page_no) { m_page_no = page_no; }
126
127 /** Get the index id present in the DDL log record.
128 @return index id. */
129 ulint get_index_id() const { return (m_index_id); }
130
131 /** Set the index id for the DDL log record.
132 @param[in] index_id index id. */
133 void set_index_id(ulint index_id) { m_index_id = index_id; }
134
135 /** Get the table id present in the DDL log record.
136 @return table id from the record. */
137 table_id_t get_table_id() const { return (m_table_id); }
138
139 /** Set the table if for the DDL log record.
140 @param[in] table_id table id. */
141 void set_table_id(table_id_t table_id) { m_table_id = table_id; }
142
143 /** Set deletability of this record.
144 @param[in] deletable deletability. */
145 void set_deletable(bool deletable) { m_deletable = deletable; }
146
147 /** If this record can be deleted.
148 @return true if record is deletable. */
149 bool get_deletable() const { return (m_deletable); }
150
151 /** Get encryption operation type */
152 Encryption::Progress get_encryption_type() const {
153 auto type = get_type();
154
155 if (type == Log_Type::ALTER_ENCRYPT_TABLESPACE_LOG) {
156 return Encryption::Progress::ENCRYPTION;
157 }
158 ut_ad(type == Log_Type::ALTER_UNENCRYPT_TABLESPACE_LOG);
159 return Encryption::Progress::DECRYPTION;
160 }
161
162 /** Get the old file path/name present in the DDL log record.
163 @return old file path/name. */
164 const char *get_old_file_path() const { return (m_old_file_path); }
165
166 /** Set the old file path from the name for the DDL log record.
167 @param[in] name old file name. */
168 void set_old_file_path(const char *name);
169
170 /** Copy the data and set it in old file path
171 @param[in] data data to be set
172 @param[in] len length of the data. */
173 void set_old_file_path(const byte *data, ulint len);
174
175 /** Get the new file path/name present in the DDL log record.
176 @return new file path/name. */
177 const char *get_new_file_path() const { return (m_new_file_path); }
178
179 /** Set the new file path/name for the DDL log record.
180 @param[in] name name to be set. */
181 void set_new_file_path(const char *name);
182
183 /** Copy the data and set it in new file path.
184 @param[in] data data to be set
185 @param[in] len length of the data. */
186 void set_new_file_path(const byte *data, ulint len);
187
188 /** Print the DDL record to specified output stream
189 @param[in,out] out output stream
190 @return output stream */
191 std::ostream &print(std::ostream &out) const;
192
193 private:
194 /** Log id */
195 ulint m_id;
196
197 /** Log type */
198 Log_Type m_type;
199
200 /** Thread id */
201 ulint m_thread_id;
202
203 /** Tablespace id */
204 space_id_t m_space_id;
205
206 /** Index root page */
207 page_no_t m_page_no;
208
209 /** Index id */
210 ulint m_index_id;
211
212 /** Table id */
213 table_id_t m_table_id;
214
215 /** Tablespace file path for DELETE, Old tablespace file path
216 for RENAME */
217 char *m_old_file_path;
218
219 /** New tablespace file name for RENAME */
220 char *m_new_file_path;
221
222 /** memory heap object used for storing file name. */
223 mem_heap_t *m_heap;
224
225 /** If this record can be deleted */
226 bool m_deletable;
227 };
228
229 /** Forward declaration */
230 class THD;
231 struct que_thr_t;
232 struct dtuple_t;
233
234 /** Array of DDL records */
235 using DDL_Records = std::vector<DDL_Record *>;
236
237 /** Wrapper of mysql.innodb_ddl_log table. Accessing to this table doesn't
238 require row lock because thread could only access/modify its own ddl records. */
239 class DDL_Log_Table {
240 public:
241 /** Constructor. */
242 DDL_Log_Table();
243
244 /** Constructor and it initializes transaction and query thread.
245 Once trx is passed in, make sure destructor is called before the
246 trx commits.
247 @param[in,out] trx Transaction */
248 explicit DDL_Log_Table(trx_t *trx);
249
250 /** Destructor. */
251 ~DDL_Log_Table();
252
253 /** Insert the DDL log record into the innodb_ddl_log table.
254 This is thread safe.
255 @param[in] record Record to be inserted.
256 @return DB_SUCCESS or error. */
257 dberr_t insert(const DDL_Record &record);
258
259 /** Search for all records of specified thread_id. The records
260 are kept in reverse order.
261 This is thread safe. Because different threads have different thread
262 ids, there should not be any conflict with update.
263 @param[in] thread_id thread id to search
264 @param[out] records DDL_Records of the specified thread id
265 @return DB_SUCCESS or error. */
266 dberr_t search(ulint thread_id, DDL_Records &records);
267
268 /** Do a reverse scan on the table to fetch all the record.
269 This is only called during recovery
270 @param[out] records DDL_Records of the whole table
271 @return DB_SUCCESS or error. */
272 dberr_t search_all(DDL_Records &records);
273
274 /** Delete the innodb_ddl_log record of specified ID.
275 This is thread safe. One thread will only remove its ddl record.
276 @param[in] id ID of the DDL_Record
277 @return DB_SUCCESS or error. */
278 dberr_t remove(ulint id);
279
280 /** Delete specified DDL_Records from innodb_ddl_log.
281 This is thread safe. Different threads have their own ddl records
282 to delete. And this could be called during recovery.
283 @param[in] records DDL_Record(s) to be deleted
284 @return DB_SUCCESS or error. */
285 dberr_t remove(const DDL_Records &records);
286
287 private:
288 /** Set the query thread using graph. */
289 void start_query_thread();
290
291 /** Stop the query thread. */
292 void stop_query_thread();
293
294 /** Create tuple for the innodb_ddl_log table.
295 It is used for insert operation.
296 @param[in] record DDL log record. */
297 void create_tuple(const DDL_Record &record);
298
299 /** Create tuple for the given index. Used for search by id
300 (and following delete)
301 @param[in] id Thread id/ id of the record
302 @param[in] index Clustered index or secondary index. */
303 void create_tuple(ulint id, const dict_index_t *index);
304
305 /** Convert the innodb_ddl_log index record to DDL_Record.
306 @param[in] is_clustered true if this is clustered index record,
307 otherwise the secondary index record
308 @param[in] rec index record
309 @param[in] offsets index record offset
310 @param[in,out] record to store the innodb_ddl_log record. */
311 void convert_to_ddl_record(bool is_clustered, rec_t *rec,
312 const ulint *offsets, DDL_Record &record);
313
314 /** Parse the index record and get 'ID'.
315 @param[in] index index where the record resides
316 @param[in] rec index rec
317 @param[in] offsets offsets of the index.
318 @return id of the record. */
319 ulint parse_id(const dict_index_t *index, rec_t *rec, const ulint *offsets);
320
321 /** Set the given field of the innodb_ddl_log record from given data.
322 @param[in] data data to be set
323 @param[in] offset column of the ddl record
324 @param[in] len length of the data
325 @param[in,out] record DDL_Record to set */
326 void set_field(const byte *data, ulint offset, ulint len, DDL_Record &record);
327
328 /** Fetch the value from given offset.
329 @param[in] data value to be retrieved from data
330 @param[in] offset offset of the column
331 @return value of the given offset. */
332 ulint fetch_value(const byte *data, ulint offset);
333
334 /** Search specified index by specified ID
335 @param[in] id ID to search
336 @param[in] index index to search
337 @param[in,out] records DDL_Record(s) got by the search
338 @return DB_SUCCESS or error */
339 dberr_t search_by_id(ulint id, dict_index_t *index, DDL_Records &records);
340
341 private:
342 /** Column number of mysql.innodb_ddl_log.id. */
343 static constexpr unsigned s_id_col_no = 0;
344
345 /** Column length of mysql.innodb_ddl_log.id. */
346 static constexpr unsigned s_id_col_len = 8;
347
348 /** Column number of mysql.innodb_ddl_log.thread_id. */
349 static constexpr unsigned s_thread_id_col_no = 1;
350
351 /** Column length of mysql.innodb_ddl_log.thread_id. */
352 static constexpr unsigned s_thread_id_col_len = 8;
353
354 /** Column number of mysql.innodb_ddl_log.type. */
355 static constexpr unsigned s_type_col_no = 2;
356
357 /** Column length of mysql.innodb_ddl_log.type. */
358 static constexpr unsigned s_type_col_len = 4;
359
360 /** Column number of mysql.innodb_ddl_log.space_id. */
361 static constexpr unsigned s_space_id_col_no = 3;
362
363 /** Column length of mysql.innodb_ddl_log.space_id. */
364 static constexpr unsigned s_space_id_col_len = 4;
365
366 /** Column number of mysql.innodb_ddl_log.page_no. */
367 static constexpr unsigned s_page_no_col_no = 4;
368
369 /** Column length of mysql.innodb_ddl_log.page_no. */
370 static constexpr unsigned s_page_no_col_len = 4;
371
372 /** Column number of mysql.innodb_ddl_log.index_id. */
373 static constexpr unsigned s_index_id_col_no = 5;
374
375 /** Column length of mysql.innodb_ddl_log.index_id. */
376 static constexpr unsigned s_index_id_col_len = 8;
377
378 /** Column number of mysql.innodb_ddl_log.table_id. */
379 static constexpr unsigned s_table_id_col_no = 6;
380
381 /** Column length of mysql.innodb_ddl_log.table_id. */
382 static constexpr unsigned s_table_id_col_len = 8;
383
384 /** Column number of mysql.innodb_ddl_log.old_file_path. */
385 static constexpr unsigned s_old_file_path_col_no = 7;
386
387 /** Column number of mysql.innodb_ddl_log.new_file_path. */
388 static constexpr unsigned s_new_file_path_col_no = 8;
389
390 /** innodb_ddl_log table. */
391 dict_table_t *m_table;
392
393 /** Tuple used for insert, search, delete operation. */
394 dtuple_t *m_tuple;
395
396 /** Transaction used for insert, delete operation. */
397 trx_t *m_trx;
398
399 /** Dummy query thread. */
400 que_thr_t *m_thr;
401
402 /** Heap to store the m_tuple, m_thr and all
403 operation on mysql.innodb_ddl_log table. */
404 mem_heap_t *m_heap;
405 };
406
407 /** Class to write and replay ddl logs */
408 class Log_DDL {
409 public:
410 /** Constructor */
411 Log_DDL();
412
413 /** Deconstructor */
414 ~Log_DDL() = default;
415
416 /** Write DDL log for freeing B-tree
417 @param[in,out] trx transaction
418 @param[in] index dict index
419 @param[in] is_drop_table true if this is drop table
420 @return DB_SUCCESS or error */
421 dberr_t write_free_tree_log(trx_t *trx, const dict_index_t *index,
422 bool is_drop_table);
423
424 /** Write DDL log for deleting tablespace file
425 @param[in,out] trx transaction
426 @param[in] table dict table
427 @param[in] space_id tablespace id
428 @param[in] file_path file path
429 @param[in] is_drop flag whether dropping tablespace
430 @param[in] dict_locked true if dict_sys mutex is held
431 @return DB_SUCCESS or error */
432 dberr_t write_delete_space_log(trx_t *trx, const dict_table_t *table,
433 space_id_t space_id, const char *file_path,
434 bool is_drop, bool dict_locked);
435
436 /** Write a RENAME log record
437 @param[in] space_id tablespace id
438 @param[in] old_file_path file path after rename
439 @param[in] new_file_path file path before rename
440 @return DB_SUCCESS or error */
441 dberr_t write_rename_space_log(space_id_t space_id, const char *old_file_path,
442 const char *new_file_path);
443
444 /** Find alter encrypt record for the tablespace.
445 @param[in] space_id space_id
446 return log record if exists, null otherwise */
447 DDL_Record *find_alter_encrypt_record(space_id_t space_id);
448
449 /** Write an ALTER ENCRYPT Tablespace DDL log record
450 @param[in] space_id tablespace id
451 @param[in] type encryption operation type
452 @param[out] existing_rec alter_encrypt ddl record, nullptr if none
453 @return DB_SUCCESS or error */
454 dberr_t write_alter_encrypt_space_log(space_id_t space_id,
455 Encryption::Progress type,
456 DDL_Record *existing_rec);
457
458 /** Write a DROP log to indicate the entry in innodb_table_metadata
459 should be removed for specified table
460 @param[in,out] trx transaction
461 @param[in] table_id table ID
462 @return DB_SUCCESS or error */
463 dberr_t write_drop_log(trx_t *trx, const table_id_t table_id);
464
465 /** Write a RENAME table log record
466 @param[in] table dict table
467 @param[in] old_name table name after rename
468 @param[in] new_name table name before rename
469 @return DB_SUCCESS or error */
470 dberr_t write_rename_table_log(dict_table_t *table, const char *old_name,
471 const char *new_name);
472
473 /** Write a REMOVE cache log record
474 @param[in,out] trx transaction
475 @param[in] table dict table
476 @return DB_SUCCESS or error */
477 dberr_t write_remove_cache_log(trx_t *trx, dict_table_t *table);
478
479 /** Replay DDL log record
480 @param[in,out] record DDL log record
481 return DB_SUCCESS or error */
482 dberr_t replay(DDL_Record &record);
483
484 /** Replay and clean DDL logs after DDL transaction
485 commints or rollbacks.
486 @param[in] thd mysql thread
487 @return DB_SUCCESS or error */
488 dberr_t post_ddl(THD *thd);
489
490 /** Recover in server startup.
491 Scan innodb_ddl_log table, and replay all log entries.
492 Note: redo log should be applied, and DD transactions
493 should be recovered before calling this function.
494 @return DB_SUCCESS or error */
495 dberr_t recover();
496
497 /** Is it in ddl recovery in server startup.
498 @return true if it's in ddl recover */
499 static bool is_in_recovery() { return (s_in_recovery); }
500
501 private:
502 /** Insert a FREE log record
503 @param[in,out] trx transaction
504 @param[in] index dict index
505 @param[in] id log id
506 @param[in] thread_id thread id
507 @return DB_SUCCESS or error */
508 dberr_t insert_free_tree_log(trx_t *trx, const dict_index_t *index,
509 uint64_t id, ulint thread_id);
510
511 /** Replay FREE log(free B-tree if exist)
512 @param[in] space_id tablespace id
513 @param[in] page_no root page no
514 @param[in] index_id index id */
515 void replay_free_tree_log(space_id_t space_id, page_no_t page_no,
516 ulint index_id);
517
518 /** Insert a DELETE log record
519 @param[in,out] trx transaction
520 @param[in] id log id
521 @param[in] thread_id thread id
522 @param[in] space_id tablespace id
523 @param[in] file_path file path
524 @param[in] dict_locked true if dict_sys mutex is held
525 @return DB_SUCCESS or error */
526 dberr_t insert_delete_space_log(trx_t *trx, uint64_t id, ulint thread_id,
527 space_id_t space_id, const char *file_path,
528 bool dict_locked);
529
530 /** Replay DELETE log(delete file if exist)
531 @param[in] space_id tablespace id
532 @param[in] file_path file path */
533 void replay_delete_space_log(space_id_t space_id, const char *file_path);
534
535 /** Insert a RENAME log record
536 @param[in] id log id
537 @param[in] thread_id thread id
538 @param[in] space_id tablespace id
539 @param[in] old_file_path file path after rename
540 @param[in] new_file_path file path before rename
541 @return DB_SUCCESS or error */
542 dberr_t insert_rename_space_log(uint64_t id, ulint thread_id,
543 space_id_t space_id,
544 const char *old_file_path,
545 const char *new_file_path);
546
547 /** Replay RENAME log
548 @param[in] space_id tablespace id
549 @param[in] old_file_path old file path
550 @param[in] new_file_path new file path */
551 void replay_rename_space_log(space_id_t space_id, const char *old_file_path,
552 const char *new_file_path);
553
554 /** Insert an ALTER ENCRYPT TABLESPACE log record
555 @param[in] id log id
556 @param[in] thread_id thread id
557 @param[in] space_id tablespace id
558 @param[in] type encryption operation type
559 @param[out] existing_rec alter_encrypt ddl record, nullptr if none
560 @return DB_SUCCESS or error */
561 dberr_t insert_alter_encrypt_space_log(uint64_t id, ulint thread_id,
562 space_id_t space_id,
563 Encryption::Progress type,
564 DDL_Record *existing_rec);
565
566 /** Replay an ALTER ENCRYPT TABLESPACE log record
567 @param[in] record DDL Record
568 @return DB_SUCCESS or error */
569 dberr_t replay_alter_encrypt_space_log(DDL_Record &record);
570
571 /** Insert a DROP log record
572 @param[in,out] trx transaction
573 @param[in] id log id
574 @param[in] thread_id thread id
575 @param[in] table_id table id
576 @return DB_SUCCESS or error */
577 dberr_t insert_drop_log(trx_t *trx, uint64_t id, ulint thread_id,
578 const table_id_t table_id);
579
580 /** Replay DROP log
581 @param[in] table_id table id */
582 void replay_drop_log(const table_id_t table_id);
583
584 /** Insert a RENAME TABLE log record
585 @param[in] id log id
586 @param[in] thread_id thread id
587 @param[in] table_id table id
588 @param[in] old_name table name after rename
589 @param[in] new_name table name before rename
590 @return DB_SUCCESS or error */
591 dberr_t insert_rename_table_log(uint64_t id, ulint thread_id,
592 table_id_t table_id, const char *old_name,
593 const char *new_name);
594
595 /** Relay RENAME TABLE log
596 @param[in] old_name old name
597 @param[in] new_name new name */
598 void replay_rename_table_log(const char *old_name, const char *new_name);
599
600 /** Insert a REMOVE cache log record
601 @param[in] id log id
602 @param[in] thread_id thread id
603 @param[in] table_id table id
604 @param[in] table_name table name
605 @return DB_SUCCESS or error */
606 dberr_t insert_remove_cache_log(uint64_t id, ulint thread_id,
607 table_id_t table_id, const char *table_name);
608
609 /** Relay remove cache log
610 @param[in] table_id table id
611 @param[in] table_name table name */
612 void replay_remove_cache_log(table_id_t table_id, const char *table_name);
613
614 /** Delete log record by id
615 @param[in] trx transaction instance
616 @param[in] id log id
617 @param[in] dict_locked true if dict_sys mutex is held,
618 otherwise false
619 @return DB_SUCCESS or error */
620 dberr_t delete_by_id(trx_t *trx, uint64_t id, bool dict_locked);
621
622 /** Scan, replay and delete log records by thread id
623 @param[in] thread_id thread id
624 @return DB_SUCCESS or error */
625 dberr_t replay_by_thread_id(ulint thread_id);
626
627 /** Delete the log records present in the list.
628 @param[in] records DDL_Records where the IDs are got
629 @return DB_SUCCESS or error. */
630 dberr_t delete_by_ids(DDL_Records &records);
631
632 /** Scan, replay and delete all log records
633 @return DB_SUCCESS or error */
634 dberr_t replay_all();
635
636 /** Get next autoinc counter by increasing 1 for innodb_ddl_log
637 @return new next counter */
638 inline uint64_t next_id();
639
640 /** Check if we need to skip ddl log for a table.
641 @param[in] table dict table
642 @param[in] thd mysql thread
643 @return true if should skip, otherwise false */
644 inline bool skip(const dict_table_t *table, THD *thd);
645
646 private:
647 /** Whether in recover(replay) ddl log in startup. */
648 static bool s_in_recovery;
649 };
650
651 /** Object to handle Log_DDL */
652 extern Log_DDL *log_ddl;
653
654 /** Close the DDL log system */
655 inline void ddl_log_close() { ut::delete_(log_ddl); }
656
657 #ifdef UNIV_DEBUG
658 struct SYS_VAR;
659
660 /** Used by SET GLOBAL innodb_ddl_log_crash_counter_reset_debug = 1; */
661 extern bool innodb_ddl_log_crash_reset_debug;
662
663 /** Reset all crash injection counters. It's used by:
664 SET GLOBAL innodb_ddl_log_crash_reset_debug = 1 (0).
665 @param[in] thd thread handle
666 @param[in] var pointer to system variable
667 @param[in] var_ptr where the formal string goes
668 @param[in] save immediate result from check function */
669 void ddl_log_crash_reset(THD *thd, SYS_VAR *var, void *var_ptr,
670 const void *save);
671 #endif /* UNIV_DEBUG */
672
673 #endif /* log0ddl_h */