-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsrv0start.cc
3083 lines (2435 loc) · 96.6 KB
/
srv0start.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1996, 2025, Oracle and/or its affiliates.
Copyright (c) 2008, Google Inc.
Copyright (c) 2009, Percona Inc.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
Portions of this file contain modifications contributed and copyrighted
by Percona Inc.. Those modifications are
gratefully acknowledged and are described briefly in the InnoDB
documentation. The contributions by Percona Inc. are incorporated with
their permission, and subject to the conditions contained in the file
COPYING.Percona.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/** @file srv/srv0start.cc
Starts the InnoDB database server
Created 2/16/1996 Heikki Tuuri
*************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <zlib.h>
#include "my_dbug.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "buf0buf.h"
#include "buf0dump.h"
#include "current_thd.h"
#include "data0data.h"
#include "data0type.h"
#include "dict0dd.h"
#include "dict0dict.h"
#include "fil0fil.h"
#include "fsp0fsp.h"
#include "fsp0sysspace.h"
#include "ha_prototypes.h"
#include "ibuf0ibuf.h"
#include "log0buf.h"
#include "log0chkp.h"
#include "log0recv.h"
#include "log0write.h"
#include "mem0mem.h"
#include "mtr0mtr.h"
#include "my_dbug.h"
#include "my_psi_config.h"
#include "mysql/psi/mysql_stage.h"
#include "mysqld.h"
#include "ddl0fts.h"
#include "os0file.h"
#include "os0thread-create.h"
#include "os0thread.h"
#include "page0cur.h"
#include "page0page.h"
#include "rem0rec.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "trx0sys.h"
#include "trx0trx.h"
#include "ut0mem.h"
#include <zlib.h>
#include "arch0arch.h"
#include "arch0recv.h"
#include "btr0pcur.h"
#include "btr0sea.h"
#include "buf0flu.h"
#include "buf0rea.h"
#include "clone0api.h"
#include "clone0clone.h"
#include "dict0boot.h"
#include "dict0crea.h"
#include "dict0load.h"
#include "dict0stats_bg.h"
#include "lock0lock.h"
#include "os0event.h"
#include "os0proc.h"
#include "pars0pars.h"
#include "que0que.h"
#include "rem0cmp.h"
#include "row0ins.h"
#include "row0mysql.h"
#include "row0row.h"
#include "row0sel.h"
#include "row0upd.h"
#include "srv0tmp.h"
#include "trx0purge.h"
#include "trx0roll.h"
#include "trx0rseg.h"
#include "usr0sess.h"
#include "ut0crc32.h"
#include "ut0new.h"
/** fil_space_t::flags for hard-coded tablespaces */
extern uint32_t predefined_flags;
/** true if a raw partition is in use */
bool srv_start_raw_disk_in_use = false;
/** true if the server is being started */
bool srv_is_being_started = false;
/** true if SYS_TABLESPACES is available for lookups */
bool srv_sys_tablespaces_open = false;
/** true if the server is being started, before rolling back any
incomplete transactions */
bool srv_startup_is_before_trx_rollback_phase = false;
/** true if srv_start() has been called */
static bool srv_start_has_been_called = false;
/** Bit flags for tracking background thread creation. They are used to
determine which threads need to be stopped if we need to abort during
the initialisation step. */
enum srv_start_state_t {
/** No thread started */
SRV_START_STATE_NONE = 0,
/** Started IO threads */
SRV_START_STATE_IO = 1,
/** Started purge thread(s) */
SRV_START_STATE_PURGE = 2,
/** Started bufdump + dict stat and FTS optimize thread. */
SRV_START_STATE_STAT = 4
};
/** Track server thrd starting phases */
static uint64_t srv_start_state = SRV_START_STATE_NONE;
std::atomic<enum srv_shutdown_t> srv_shutdown_state{SRV_SHUTDOWN_NONE};
/** Name of srv_monitor_file */
static char *srv_monitor_file_name;
/** */
#define SRV_MAX_N_PENDING_SYNC_IOS 100
/* Keys to register InnoDB threads with performance schema */
#ifdef UNIV_PFS_THREAD
mysql_pfs_key_t log_archiver_thread_key;
mysql_pfs_key_t page_archiver_thread_key;
mysql_pfs_key_t buf_dump_thread_key;
mysql_pfs_key_t buf_resize_thread_key;
mysql_pfs_key_t clone_ddl_thread_key;
mysql_pfs_key_t clone_gtid_thread_key;
mysql_pfs_key_t ddl_thread_key;
mysql_pfs_key_t dict_stats_thread_key;
mysql_pfs_key_t fts_optimize_thread_key;
mysql_pfs_key_t fts_parallel_merge_thread_key;
mysql_pfs_key_t fts_parallel_tokenization_thread_key;
mysql_pfs_key_t srv_error_monitor_thread_key;
mysql_pfs_key_t srv_lock_timeout_thread_key;
mysql_pfs_key_t srv_master_thread_key;
mysql_pfs_key_t srv_monitor_thread_key;
mysql_pfs_key_t srv_purge_thread_key;
mysql_pfs_key_t srv_worker_thread_key;
mysql_pfs_key_t trx_recovery_rollback_thread_key;
mysql_pfs_key_t srv_ts_alter_encrypt_thread_key;
mysql_pfs_key_t parallel_rseg_init_thread_key;
mysql_pfs_key_t bulk_flusher_thread_key;
mysql_pfs_key_t bulk_alloc_thread_key;
#endif /* UNIV_PFS_THREAD */
#ifdef HAVE_PSI_STAGE_INTERFACE
/** Array of all InnoDB stage events for monitoring activities via
performance schema. */
static PSI_stage_info *srv_stages[] = {
&srv_stage_alter_table_end,
&srv_stage_alter_table_flush,
&srv_stage_alter_table_insert,
&srv_stage_alter_table_log_index,
&srv_stage_alter_table_log_table,
&srv_stage_alter_table_merge_sort,
&srv_stage_alter_table_read_pk_internal_sort,
&srv_stage_alter_tablespace_encryption,
&srv_stage_buffer_pool_load,
&srv_stage_clone_file_copy,
&srv_stage_clone_redo_copy,
&srv_stage_clone_page_copy,
};
#endif /* HAVE_PSI_STAGE_INTERFACE */
/** Sleep time in loops which wait for pending tasks during shutdown. */
static constexpr uint32_t SHUTDOWN_SLEEP_TIME_US = 100;
/** Number of wait rounds during shutdown, after which error is produced,
or other policy for timed out wait is applied. */
static constexpr uint32_t SHUTDOWN_SLEEP_ROUNDS =
60 * 1000 * 1000 / SHUTDOWN_SLEEP_TIME_US;
/** Create undo tablespace.
@param[in] undo_space Undo Tablespace
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespace_create(undo::Tablespace &undo_space) {
pfs_os_file_t fh;
bool ret;
dberr_t err = DB_SUCCESS;
char *file_name = undo_space.file_name();
space_id_t space_id = undo_space.id();
ut_a(!srv_read_only_mode);
ut_a(!srv_force_recovery);
os_file_create_subdirs_if_needed(file_name);
/* Until this undo tablespace can become active, keep a truncate log
file around so that if a crash happens it can be rebuilt at startup. */
err = undo::start_logging(&undo_space);
if (err != DB_SUCCESS) {
ib::error(ER_IB_MSG_1070, undo_space.log_file_name(),
undo_space.space_name());
}
ut_ad(err == DB_SUCCESS);
fh = os_file_create(innodb_data_file_key, file_name,
(srv_read_only_mode ? OS_FILE_OPEN : OS_FILE_CREATE) |
OS_FILE_ON_ERROR_NO_EXIT,
OS_DATA_FILE, srv_read_only_mode, &ret);
if (ret == false) {
std::ostringstream stmt;
if (os_file_get_last_error(false) == OS_FILE_ALREADY_EXISTS) {
stmt << " since '" << file_name << "' already exists.";
} else {
stmt << ". os_file_create() returned " << ret << ".";
}
ib::error(ER_IB_MSG_1214, undo_space.space_name(), stmt.str().c_str());
err = DB_ERROR;
} else {
ut_a(!srv_read_only_mode);
/* We created the data file and now write it full of zeros */
undo_space.set_new();
ib::info(ER_IB_MSG_1071, file_name);
ulint size_mb = UNDO_INITIAL_SIZE >> 20;
ib::info(ER_IB_MSG_1072, file_name, ulonglong{size_mb});
ib::info(ER_IB_MSG_1073);
ret = os_file_set_size(file_name, fh, 0, UNDO_INITIAL_SIZE, true);
DBUG_EXECUTE_IF("ib_undo_tablespace_create_fail", ret = false;);
if (!ret) {
ib::info(ER_IB_MSG_1074, file_name);
err = DB_OUT_OF_FILE_SPACE;
}
os_file_close(fh);
/* Add this space to the list of undo tablespaces to
construct by creating header pages. If an old undo
tablespace needed fixup before it is upgraded,
there is no need to construct it.*/
if (undo::is_reserved(space_id)) {
undo::add_space_to_construction_list(space_id);
}
}
return (err);
}
/** Try to enable encryption of an undo log tablespace.
@param[in] space_id undo tablespace id
@return DB_SUCCESS if success */
static dberr_t srv_undo_tablespace_enable_encryption(space_id_t space_id) {
dberr_t err;
ut_ad(Encryption::check_keyring());
/* Set the space flag. The encryption metadata
will be generated in fsp_header_init later. */
fil_space_t *space = fil_space_get(space_id);
if (!FSP_FLAGS_GET_ENCRYPTION(space->flags)) {
fsp_flags_set_encryption(space->flags);
err = fil_set_encryption(space_id, Encryption::AES, nullptr, nullptr);
if (err != DB_SUCCESS) {
ib::error(ER_IB_MSG_1075, space->name);
return (err);
}
}
return (DB_SUCCESS);
}
/** Try to read encryption metadata from an undo tablespace.
@param[in] fh file handle of undo log file
@param[in] file_name file name
@param[in] space undo tablespace
@return DB_SUCCESS if success */
static dberr_t srv_undo_tablespace_read_encryption(pfs_os_file_t fh,
const char *file_name,
fil_space_t *space) {
IORequest request;
ulint n_read = 0;
size_t page_size = UNIV_PAGE_SIZE_MAX;
dberr_t err = DB_ERROR;
/* Align the memory for a possible read from a raw device */
byte *first_page = static_cast<byte *>(
ut::aligned_alloc(UNIV_PAGE_SIZE_MAX, UNIV_PAGE_SIZE));
/* Don't want unnecessary complaints about partial reads. */
request.disable_partial_io_warnings();
err = os_file_read_no_error_handling(request, file_name, fh, first_page, 0,
page_size, &n_read);
if (err != DB_SUCCESS) {
ib::info(ER_IB_MSG_1076, space->name, ut_strerr(err));
ut::aligned_free(first_page);
return (err);
}
ulint offset;
const page_size_t space_page_size(space->flags);
offset = fsp_header_get_encryption_offset(space_page_size);
ut_ad(offset);
/* Return if the encryption metadata is empty. */
if (!Encryption::is_encrypted_with_v3(first_page + offset)) {
ut::aligned_free(first_page);
return (DB_SUCCESS);
}
byte key[Encryption::KEY_LEN];
byte iv[Encryption::KEY_LEN];
Encryption_key e_key{key, iv};
if (fsp_header_get_encryption_key(space->flags, e_key, first_page)) {
fsp_flags_set_encryption(space->flags);
err = fil_set_encryption(space->id, Encryption::AES, key, iv);
ut_ad(err == DB_SUCCESS);
} else {
ut::aligned_free(first_page);
return (DB_FAIL);
}
ut::aligned_free(first_page);
ib::info(ER_IB_MSG_UNDO_ENCRYPTION_INFO_LOADED, space->name);
return (DB_SUCCESS);
}
/** Fix up a v5.7 type undo tablespace that was being truncated.
The space_id is not a reserved undo space_id. We will just delete
the file since it will be replaced.
@param[in] space_id Tablespace ID
@return error code */
static dberr_t srv_undo_tablespace_fixup_57(space_id_t space_id) {
space_id_t space_num = undo::id2num(space_id);
ut_ad(space_num == space_id);
if (undo::is_active_truncate_log_present(space_num)) {
ib::info(ER_IB_MSG_1077, ulong{space_num});
if (srv_read_only_mode) {
ib::error(ER_IB_MSG_1078);
return (DB_READ_ONLY);
}
undo::Tablespace undo_space(space_id);
/* Flush any changes recovered in REDO */
fil_flush(space_id);
fil_space_close(space_id);
os_file_delete_if_exists(innodb_data_file_key, undo_space.file_name(),
nullptr);
return (DB_TABLESPACE_DELETED);
}
return (DB_SUCCESS);
}
/** Start the fix-up process on an undo tablespace if it was in the process
of being truncated when the server crashed. At this point, just delete the
old file if it exists.
We could do the whole reconstruction here for implicit undo spaces since we
know the space_id, space_name, and file_name implicitly. But for explicit
undo spaces, we must wait for the DD to be scanned in boot_tablespaces()
in order to know the space_id, space_name, and file_name.
@param[in] space_num undo tablespace number
@return error code */
static dberr_t srv_undo_tablespace_fixup_num(space_id_t space_num) {
if (!undo::is_active_truncate_log_present(space_num)) {
return (DB_SUCCESS);
}
ib::info(ER_IB_MSG_1077, ulong{space_num});
if (srv_read_only_mode) {
ib::error(ER_IB_MSG_1078);
return (DB_READ_ONLY);
}
/*
Search for a file that is using any of the space IDs assigned to this
undo number. The directory scan assured that there are no duplicate files
with the same space_id or with the same undo space number.
*/
space_id_t space_id = SPACE_UNKNOWN;
std::string scanned_name;
fil_system_get_file_by_space_num(space_num, space_id, scanned_name);
/* If the previous file still exists, delete it. */
if (scanned_name.length() > 0) {
/* Flush any changes recovered in REDO */
fil_flush(space_id);
fil_space_close(space_id);
os_file_delete_if_exists(innodb_data_file_key, scanned_name.c_str(),
nullptr);
} else if (space_num < FSP_IMPLICIT_UNDO_TABLESPACES) {
/* If there is any file with the implicit file name, delete it. */
undo::Tablespace undo_space(undo::num2id(space_num, 0));
os_file_delete_if_exists(innodb_data_file_key, undo_space.file_name(),
nullptr);
}
return (DB_SUCCESS);
}
/** Fix up an undo tablespace if it was in the process of being truncated
when the server crashed. This is the second call and is done after the DD
is available so now we know the space_name, file_name and previous space_id.
@param[in] space_name undo tablespace name
@param[in] file_name undo tablespace file name
@param[in] space_id undo tablespace ID
@return error code */
dberr_t srv_undo_tablespace_fixup(const char *space_name, const char *file_name,
space_id_t space_id) {
ut_ad(fsp_is_undo_tablespace(space_id));
space_id_t space_num = undo::id2num(space_id);
if (!undo::is_active_truncate_log_present(space_num)) {
return (DB_SUCCESS);
}
if (srv_read_only_mode) {
return (DB_READ_ONLY);
}
ib::info(ER_IB_MSG_1079, ulong{space_num});
/* It is possible for an explicit undo tablespace to have been truncated and
recreated but not yet written with a header page when a crash occurred. In
this case, the empty file would not have been scanned at startup and the
first call to fixup did not know the filename. Now that we know it, just
delete any file with that name if it exists. The dictionary claims it is
an undo tablespace and there is a truncate log file present. */
os_file_delete_if_exists(innodb_data_file_key, file_name, nullptr);
/* Mark the space_id for this undo tablespace number as in-use. */
undo::spaces->x_lock();
undo::unuse_space_id(space_id);
space_id_t new_space_id = undo::next_space_id(space_id);
undo::use_space_id(new_space_id);
undo::spaces->x_unlock();
dberr_t err = srv_undo_tablespace_create(space_name, file_name, new_space_id);
if (err != DB_SUCCESS) {
return (err);
}
/* Update the DD with the new space ID and state. */
undo::spaces->s_lock();
undo::Tablespace *undo_space = undo::spaces->find(space_num);
dd_space_states to_state;
if (undo_space->is_inactive_explicit()) {
to_state = DD_SPACE_STATE_EMPTY;
undo_space->set_empty();
} else {
to_state = DD_SPACE_STATE_ACTIVE;
undo_space->set_active();
}
undo::spaces->s_unlock();
bool dd_result = dd_tablespace_get_mdl(space_name);
if (dd_result == DD_SUCCESS) {
dd_result =
dd_tablespace_set_id_and_state(space_name, new_space_id, to_state);
}
if (dd_result != DD_SUCCESS) {
err = DB_ERROR;
}
return (err);
}
/** Open an undo tablespace.
@param[in] undo_space Undo tablespace
@return DB_SUCCESS or error code */
dberr_t srv_undo_tablespace_open(undo::Tablespace &undo_space) {
DBUG_EXECUTE_IF("ib_undo_tablespace_open_fail",
return (DB_CANNOT_OPEN_FILE););
pfs_os_file_t fh;
bool success;
uint32_t flags;
dberr_t err = DB_ERROR;
space_id_t space_id = undo_space.id();
char *undo_name = undo_space.space_name();
char *file_name = undo_space.file_name();
/* Check if it was already opened during redo recovery. */
fil_space_t *space = fil_space_get(space_id);
/* Flush and close any current file handle so we can open
a local one below. */
if (space != nullptr) {
fil_flush(space_id);
fil_space_close(space_id);
}
if (!os_file_check_mode(file_name, srv_read_only_mode)) {
ib::error(ER_IB_MSG_1081, file_name,
srv_read_only_mode ? "readable!" : "writable!");
return (DB_READ_ONLY);
}
/* Open a local handle. */
fh = os_file_create(
innodb_data_file_key, file_name,
OS_FILE_OPEN_RETRY | OS_FILE_ON_ERROR_NO_EXIT | OS_FILE_ON_ERROR_SILENT,
OS_DATA_FILE, srv_read_only_mode, &success);
if (!success) {
return (DB_CANNOT_OPEN_FILE);
}
if (space == nullptr) {
/* Load the tablespace into InnoDB's internal data structures.
Set the compressed page size to 0 (non-compressed) */
flags = fsp_flags_init(univ_page_size, false, false, false, false);
space = fil_space_create(undo_name, space_id, flags, FIL_TYPE_TABLESPACE);
ut_a(space != nullptr);
ut_ad(fil_validate());
os_offset_t size = os_file_get_size(fh);
ut_a(size != (os_offset_t)-1);
page_no_t n_pages = static_cast<page_no_t>(size / UNIV_PAGE_SIZE);
if (fil_node_create(file_name, n_pages, space, false) == nullptr) {
os_file_close(fh);
ib::error(ER_IB_MSG_1082, undo_name);
return (DB_ERROR);
}
}
/* Read the encryption metadata in this undo tablespace.
If the encryption info in the first page cannot be decrypted
by the master key, this table cannot be opened. */
err = srv_undo_tablespace_read_encryption(fh, file_name, space);
/* The file handle will no longer be needed. */
success = os_file_close(fh);
ut_ad(success);
if (err != DB_SUCCESS) {
ib::error(ER_IB_MSG_1083, undo_name);
return (err);
}
/* Now that space and node exist, make sure this undo tablespace
is open so that it stays open until shutdown.
But if it is under construction, we cannot open it until the
header page has been written. */
if (!undo::is_under_construction(space_id)) {
bool success = fil_space_open(space_id);
ut_a(success);
}
if (undo::is_reserved(space_id)) {
undo::spaces->add(undo_space);
}
return (DB_SUCCESS);
}
/** Open an undo tablespace with a specified space_id.
@param[in] space_id tablespace ID
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespace_open_by_id(space_id_t space_id) {
undo::Tablespace undo_space(space_id);
std::string scanned_name;
/* If an undo tablespace with this space_id already exists,
check if the name found in the file map for this undo space_id
is the standard name. The directory scan assured that there are
no duplicates. The filename found must match the standard name
if this is an implicit undo tablespace. In other words, implicit
undo tablespaces must be found in srv_undo_dir. */
bool found = fil_system_get_file_by_space_id(space_id, scanned_name);
if (found &&
!Fil_path::is_same_as(undo_space.file_name(), scanned_name.c_str())) {
ib::error(ER_IB_MSG_FOUND_WRONG_UNDO_SPACE, undo_space.file_name(),
ulong{space_id}, scanned_name.c_str());
return (DB_WRONG_FILE_NAME);
}
dberr_t err = srv_undo_tablespace_open(undo_space);
if (err == DB_SUCCESS) {
fil_space_set_undo_size(space_id, false);
}
return (err);
}
/** Open an undo tablespace with a specified undo number.
@param[in] space_num undo tablespace number
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespace_open_by_num(space_id_t space_num) {
space_id_t space_id = SPACE_UNKNOWN;
std::string scanned_name;
/* Search for a file that is using any of the space IDs assigned to this
undo number. The directory scan assured that there are no duplicate files
with the same space_id or with the same undo space number. */
if (!fil_system_get_file_by_space_num(space_num, space_id, scanned_name)) {
return (DB_CANNOT_OPEN_FILE);
}
/* The first 2 undo space numbers must be implicit. */
bool is_default = (space_num <= FSP_IMPLICIT_UNDO_TABLESPACES);
undo::Tablespace undo_space(space_id);
if (!Fil_path::is_same_as(undo_space.file_name(), scanned_name.c_str())) {
if (is_default) {
ib::info(ER_IB_MSG_1080, undo_space.file_name(), scanned_name.c_str(),
ulong{space_id});
return (DB_WRONG_FILE_NAME);
}
/* Explicit undo tablespaces must end with the suffix '.ibu'. */
if (!Fil_path::has_suffix(IBU, scanned_name)) {
ib::info(ER_IB_MSG_NOT_END_WITH_IBU, scanned_name.c_str());
return (DB_WRONG_FILE_NAME);
}
/* Use the file name found in the scan. */
undo_space.set_file_name(scanned_name.c_str());
}
/* Mark the space_id for this undo tablespace number as in-use. */
undo::use_space_id(space_id);
ib::info(ER_IB_MSG_USING_UNDO_SPACE, scanned_name.c_str());
dberr_t err = srv_undo_tablespace_open(undo_space);
if (err == DB_SUCCESS) {
fil_space_set_undo_size(space_id, false);
}
return (err);
}
/* Open existing undo tablespaces up to the number in target_undo_tablespace.
If we are making a new database, these have been created.
If doing recovery, these should exist and may be needed for recovery.
If we fail to open any of these it is a fatal error.
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespaces_open() {
dberr_t err;
/* If upgrading from 5.7, build a list of existing undo tablespaces
from the references in the TRX_SYS page. (not including the system
tablespace) */
trx_rseg_get_n_undo_tablespaces(trx_sys_undo_spaces);
/* If undo tablespaces are being tracked in trx_sys then these
will need to be replaced by independent undo tablespaces with
reserved space_ids and RSEG_ARRAY pages. */
if (trx_sys_undo_spaces->size() > 0) {
/* Open each undo tablespace tracked in TRX_SYS. */
for (const auto space_id : *trx_sys_undo_spaces) {
fil_set_max_space_id_if_bigger(space_id);
/* Check if this undo tablespace was in the process of being truncated.
If so, just delete the file since it will be replaced. */
if (DB_TABLESPACE_DELETED == srv_undo_tablespace_fixup_57(space_id)) {
continue;
}
err = srv_undo_tablespace_open_by_id(space_id);
if (err != DB_SUCCESS) {
ib::error(ER_IB_MSG_CANNOT_OPEN_57_UNDO, ulong{space_id});
return (err);
}
}
}
/* Open all existing implicit and explicit undo tablespaces.
The tablespace scan has completed and the undo::space_id_bank has been
filled with the space Ids that were found. */
undo::spaces->x_lock();
ut_ad(undo::spaces->size() == 0);
for (space_id_t num = 1; num <= FSP_MAX_UNDO_TABLESPACES; ++num) {
/* Check if this undo tablespace was in the process of being truncated.
If so, recreate it and add it to the construction list. */
dberr_t err = srv_undo_tablespace_fixup_num(num);
if (err != DB_SUCCESS) {
undo::spaces->x_unlock();
return (err);
}
err = srv_undo_tablespace_open_by_num(num);
switch (err) {
case DB_WRONG_FILE_NAME:
/* An Undo tablespace was found where the mapping
file said it was. Now we have a different filename
for it. The undo directory must have changed and
the the files were not moved. Cannot startup. */
case DB_READ_ONLY:
/* The undo tablespace was found where it should be
but it cannot be opened in read/write mode. */
default:
/* The undo tablespace was found where it should be
but it cannot be used. */
undo::spaces->x_unlock();
return (err);
case DB_SUCCESS:
case DB_CANNOT_OPEN_FILE:
/* Doesn't exist, keep looking */
break;
}
}
ulint n_found_new = undo::spaces->size();
ulint n_found_old = trx_sys_undo_spaces->size();
undo::spaces->x_unlock();
if (n_found_old != 0 || n_found_new < FSP_IMPLICIT_UNDO_TABLESPACES) {
std::ostringstream msg;
if (n_found_old != 0) {
msg << "Found " << n_found_old << " undo tablespaces that"
<< " need to be upgraded. ";
}
if (n_found_new < FSP_IMPLICIT_UNDO_TABLESPACES) {
msg << "Will create " << (FSP_IMPLICIT_UNDO_TABLESPACES - n_found_new)
<< " new undo tablespaces.";
}
ib::info(ER_IB_MSG_1215) << msg.str();
}
if (n_found_new + n_found_old) {
ib::info(ER_IB_MSG_1085, ulonglong{n_found_new + n_found_old});
}
return (DB_SUCCESS);
}
/** Create the implicit undo tablespaces if we are creating a new instance
or if there was not enough implicit undo tablespaces previously existing.
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespaces_create() {
dberr_t err = DB_SUCCESS;
undo::spaces->x_lock();
ulint initial_implicit_undo_spaces = 0;
for (auto undo_space : undo::spaces->m_spaces) {
if (undo_space->num() <= FSP_IMPLICIT_UNDO_TABLESPACES) {
initial_implicit_undo_spaces++;
}
}
if (initial_implicit_undo_spaces >= FSP_IMPLICIT_UNDO_TABLESPACES) {
undo::spaces->x_unlock();
return (DB_SUCCESS);
}
if (srv_read_only_mode || srv_force_recovery > 0) {
const char *mode;
mode = srv_read_only_mode ? "read_only" : "force_recovery",
ib::warn(ER_IB_MSG_1086, mode, ulonglong{initial_implicit_undo_spaces});
if (initial_implicit_undo_spaces == 0) {
ib::error(ER_IB_MSG_1087, mode);
undo::spaces->x_unlock();
return (DB_ERROR);
}
undo::spaces->x_unlock();
return (DB_SUCCESS);
}
/* Create all implicit undo tablespaces that are needed. */
for (space_id_t num = 1; num <= FSP_IMPLICIT_UNDO_TABLESPACES; ++num) {
/* If the trunc log file is present, the fixup process will be
finished later. */
if (undo::is_active_truncate_log_present(num)) {
continue;
}
/* Check if an independent undo space for this space_id
has already been found. */
if (undo::spaces->contains(num)) {
continue;
}
/* Mark this implicit undo space number as used and return the next
available space_id. */
space_id_t space_id = undo::use_next_space_id(num);
/* Since it is not found, create it. */
undo::Tablespace undo_space(space_id);
undo_space.set_new();
err = srv_undo_tablespace_create(undo_space);
if (err != DB_SUCCESS) {
ib::info(ER_IB_MSG_1088, undo_space.space_name());
break;
}
/* Open this new undo tablespace. */
err = srv_undo_tablespace_open(undo_space);
if (err != DB_SUCCESS) {
ib::info(ER_IB_MSG_1089, int{err}, ut_strerr(err),
undo_space.space_name());
break;
}
}
undo::spaces->x_unlock();
ulint new_spaces =
FSP_IMPLICIT_UNDO_TABLESPACES - initial_implicit_undo_spaces;
ib::info(ER_IB_MSG_1090, ulonglong{new_spaces});
return (err);
}
/** Finish building an undo tablespace. So far these tablespace files in
the construction list should be created and filled with zeros.
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespaces_construct() {
mtr_t mtr;
if (undo::s_under_construction.empty()) {
return (DB_SUCCESS);
}
ut_a(!srv_read_only_mode);
ut_a(!srv_force_recovery);
if (srv_undo_log_encrypt && Encryption::check_keyring() == false) {
my_error(ER_CANNOT_FIND_KEY_IN_KEYRING, MYF(0));
return (DB_ERROR);
}
for (auto space_id : undo::s_under_construction) {
/* Enable undo log encryption if it's ON. */
if (srv_undo_log_encrypt) {
dberr_t err = srv_undo_tablespace_enable_encryption(space_id);
if (err != DB_SUCCESS) {
ib::error(ER_IB_MSG_1091, ulong{undo::id2num(space_id)});
return (err);
}
}
log_free_check();
mtr_start(&mtr);
mtr_x_lock(fil_space_get_latch(space_id), &mtr, UT_LOCATION_HERE);
if (!fsp_header_init(space_id, UNDO_INITIAL_SIZE_IN_PAGES, &mtr)) {
ib::error(ER_IB_MSG_1093, ulong{undo::id2num(space_id)});
mtr_commit(&mtr);
return (DB_ERROR);
}
/* Add the RSEG_ARRAY page. */
trx_rseg_array_create(space_id, &mtr);
mtr_commit(&mtr);
/* The rollback segments will get created later in
trx_rseg_add_rollback_segments(). */
}
if (srv_undo_log_encrypt) {
ut_d(bool ret =) srv_enable_undo_encryption();
ut_ad(!ret);
}
return (DB_SUCCESS);
}
/** Mark the point in which the undo tablespaces in the construction list
are fully constructed and ready to use. */
static void srv_undo_tablespaces_mark_construction_done() {
/* Remove the truncate log files if they exist. */
for (auto space_id : undo::s_under_construction) {
/* Flush these pages to disk since they were not redo logged. */
auto flush_observer = ut::new_withkey<Flush_observer>(
UT_NEW_THIS_FILE_PSI_KEY, space_id, nullptr, nullptr);
flush_observer->flush();
ut::delete_(flush_observer);
space_id_t space_num = undo::id2num(space_id);
if (undo::is_active_truncate_log_present(space_num)) {
undo::done_logging(space_num);
}
}
undo::clear_construction_list();
}
/** Upgrade undo tablespaces by deleting the old undo tablespaces
referenced by the TRX_SYS page.
@return error code */
dberr_t srv_undo_tablespaces_upgrade() {
if (trx_sys_undo_spaces->empty()) {
goto cleanup;
}
/* Recovered transactions in the prepared state prevent the old
rsegs and undo tablespaces they are in from being deleted.
These transactions must be either committed or rolled back by
the mysql server.*/
if (trx_sys->n_prepared_trx > 0) {
ib::warn(ER_IB_MSG_1094);
return (DB_SUCCESS);
}
ib::info(ER_IB_MSG_1095, trx_sys_undo_spaces->size(),
ulong{FSP_IMPLICIT_UNDO_TABLESPACES});
/* All Undo Tablespaces found in the TRX_SYS page need to be
deleted. The new independent undo tablespaces were created in
in srv_undo_tablespaces_create() */
for (const auto space_id : *trx_sys_undo_spaces) {
undo::Tablespace undo_space(space_id);
fil_space_close(undo_space.id());
auto err = fil_delete_tablespace(undo_space.id(), BUF_REMOVE_ALL_NO_WRITE);