/
srv0start.cc
2150 lines (1784 loc) · 59.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, 2017, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2009, Percona Inc.
Copyright (c) 2013, 2022, MariaDB Corporation.
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 as published by the Free Software
Foundation; version 2 of the License.
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 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 Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/********************************************************************//**
@file srv/srv0start.cc
Starts the InnoDB database server
Created 2/16/1996 Heikki Tuuri
*************************************************************************/
#include "my_global.h"
#include "mysqld.h"
#include "mysql/psi/mysql_stage.h"
#include "mysql/psi/psi.h"
#include "row0ftsort.h"
#include "ut0mem.h"
#include "mem0mem.h"
#include "data0data.h"
#include "data0type.h"
#include "dict0dict.h"
#include "buf0buf.h"
#include "buf0dblwr.h"
#include "buf0dump.h"
#include "os0file.h"
#include "os0thread.h"
#include "fil0fil.h"
#include "fil0crypt.h"
#include "fsp0fsp.h"
#include "rem0rec.h"
#include "mtr0mtr.h"
#include "log0crypt.h"
#include "log0recv.h"
#include "page0page.h"
#include "page0cur.h"
#include "trx0trx.h"
#include "trx0sys.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "rem0rec.h"
#include "ibuf0ibuf.h"
#include "srv0start.h"
#include "srv0srv.h"
#include "btr0defragment.h"
#include "mysql/service_wsrep.h" /* wsrep_recovery */
#include "trx0rseg.h"
#include "buf0flu.h"
#include "buf0rea.h"
#include "dict0boot.h"
#include "dict0load.h"
#include "dict0stats_bg.h"
#include "que0que.h"
#include "lock0lock.h"
#include "trx0roll.h"
#include "trx0purge.h"
#include "lock0lock.h"
#include "pars0pars.h"
#include "btr0sea.h"
#include "rem0cmp.h"
#include "dict0crea.h"
#include "row0ins.h"
#include "row0sel.h"
#include "row0upd.h"
#include "row0row.h"
#include "row0mysql.h"
#include "btr0pcur.h"
#include "os0event.h"
#include "zlib.h"
#include "ut0crc32.h"
/** We are prepared for a situation that we have this many threads waiting for
a semaphore inside InnoDB. srv_start() sets the value. */
ulint srv_max_n_threads;
/** Log sequence number at shutdown */
lsn_t srv_shutdown_lsn;
/** TRUE if a raw partition is in use */
ibool srv_start_raw_disk_in_use;
/** UNDO tablespaces starts with space id. */
ulint srv_undo_space_id_start;
/** TRUE if the server is being started, before rolling back any
incomplete transactions */
bool srv_startup_is_before_trx_rollback_phase;
/** TRUE if the server is being started */
bool srv_is_being_started;
/** TRUE if SYS_TABLESPACES is available for lookups */
bool srv_sys_tablespaces_open;
/** TRUE if the server was successfully started */
bool srv_was_started;
/** The original value of srv_log_file_size (innodb_log_file_size) */
static ulonglong srv_log_file_size_requested;
/** whether srv_start() has been called */
static bool srv_start_has_been_called;
/** Whether any undo log records can be generated */
UNIV_INTERN bool srv_undo_sources;
#ifdef UNIV_DEBUG
/** InnoDB system tablespace to set during recovery */
UNIV_INTERN uint srv_sys_space_size_debug;
/** whether redo log file have been created at startup */
UNIV_INTERN bool srv_log_file_created;
#endif /* UNIV_DEBUG */
/** whether some background threads that create redo log have been started */
static bool srv_started_redo;
/** At a shutdown this value climbs from SRV_SHUTDOWN_NONE to
SRV_SHUTDOWN_CLEANUP and then to SRV_SHUTDOWN_LAST_PHASE, and so on */
enum srv_shutdown_t srv_shutdown_state = SRV_SHUTDOWN_NONE;
/** Name of srv_monitor_file */
static char* srv_monitor_file_name;
std::unique_ptr<tpool::timer> srv_master_timer;
/** */
#define SRV_MAX_N_PENDING_SYNC_IOS 100
#ifdef UNIV_PFS_THREAD
/* Keys to register InnoDB threads with performance schema */
mysql_pfs_key_t thread_pool_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_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_buffer_pool_load,
};
#endif /* HAVE_PSI_STAGE_INTERFACE */
/*********************************************************************//**
Check if a file can be opened in read-write mode.
@return true if it doesn't exist or can be opened in rw mode. */
static
bool
srv_file_check_mode(
/*================*/
const char* name) /*!< in: filename to check */
{
os_file_stat_t stat;
memset(&stat, 0x0, sizeof(stat));
dberr_t err = os_file_get_status(
name, &stat, true, srv_read_only_mode);
if (err == DB_FAIL) {
ib::error() << "os_file_get_status() failed on '" << name
<< "'. Can't determine file permissions.";
return(false);
} else if (err == DB_SUCCESS) {
/* Note: stat.rw_perm is only valid of files */
if (stat.type == OS_FILE_TYPE_FILE) {
if (!stat.rw_perm) {
const char* mode = srv_read_only_mode
? "read" : "read-write";
ib::error() << name << " can't be opened in "
<< mode << " mode.";
return(false);
}
} else {
/* Not a regular file, bail out. */
ib::error() << "'" << name << "' not a regular file.";
return(false);
}
} else {
/* This is OK. If the file create fails on RO media, there
is nothing we can do. */
ut_a(err == DB_NOT_FOUND);
}
return(true);
}
/** Initial number of the redo log file */
static const char INIT_LOG_FILE0[]= "101";
/** Creates log file.
@param[in] create_new_db whether the database is being initialized
@param[in] lsn FIL_PAGE_FILE_FLUSH_LSN value
@param[out] logfile0 name of the log file
@return DB_SUCCESS or error code */
static dberr_t create_log_file(bool create_new_db, lsn_t lsn,
std::string& logfile0)
{
if (srv_read_only_mode) {
ib::error() << "Cannot create log file in read-only mode";
return DB_READ_ONLY;
}
if (!log_set_capacity(srv_log_file_size_requested)) {
return(DB_ERROR);
}
/* Crashing after deleting the first file should be
recoverable. The buffer pool was clean, and we can simply
create log file from the scratch. */
DBUG_EXECUTE_IF("innodb_log_abort_6", delete_log_file("0");
return DB_ERROR;);
for (size_t i = 0; i < 102; i++) {
delete_log_file(std::to_string(i).c_str());
}
DBUG_PRINT("ib_log", ("After innodb_log_abort_6"));
DBUG_ASSERT(!buf_pool.any_io_pending());
DBUG_EXECUTE_IF("innodb_log_abort_7", return DB_ERROR;);
DBUG_PRINT("ib_log", ("After innodb_log_abort_7"));
logfile0 = get_log_file_path(LOG_FILE_NAME_PREFIX)
.append(INIT_LOG_FILE0);
bool ret;
pfs_os_file_t file = os_file_create(
innodb_log_file_key, logfile0.c_str(),
OS_FILE_CREATE|OS_FILE_ON_ERROR_NO_EXIT, OS_FILE_NORMAL,
OS_LOG_FILE, srv_read_only_mode, &ret);
if (!ret) {
ib::error() << "Cannot create " << logfile0;
return DB_ERROR;
}
ib::info() << "Setting log file " << logfile0 << " size to "
<< srv_log_file_size << " bytes";
ret = os_file_set_size(logfile0.c_str(), file, srv_log_file_size);
if (!ret) {
os_file_close(file);
ib::error() << "Cannot set log file " << logfile0
<< " size to " << srv_log_file_size << " bytes";
return DB_ERROR;
}
ret = os_file_close(file);
ut_a(ret);
DBUG_EXECUTE_IF("innodb_log_abort_8", return(DB_ERROR););
DBUG_PRINT("ib_log", ("After innodb_log_abort_8"));
/* We did not create the first log file initially as LOG_FILE_NAME, so
that crash recovery cannot find it until it has been completed and
renamed. */
log_sys.log.create();
log_sys.log.open_file(logfile0);
if (!fil_system.sys_space->open(create_new_db)) {
return DB_ERROR;
}
/* Create a log checkpoint. */
mysql_mutex_lock(&log_sys.mutex);
if (log_sys.is_encrypted() && !log_crypt_init()) {
return DB_ERROR;
}
ut_d(recv_no_log_write = false);
lsn = ut_uint64_align_up(lsn, OS_FILE_LOG_BLOCK_SIZE);
log_sys.set_lsn(lsn + LOG_BLOCK_HDR_SIZE);
log_sys.log.set_lsn(lsn);
log_sys.log.set_lsn_offset(LOG_FILE_HDR_SIZE);
log_sys.buf_next_to_write = 0;
log_sys.write_lsn = lsn;
log_sys.next_checkpoint_no = 0;
log_sys.last_checkpoint_lsn = 0;
memset(log_sys.buf, 0, srv_log_buffer_size);
log_block_init(log_sys.buf, lsn);
log_block_set_first_rec_group(log_sys.buf, LOG_BLOCK_HDR_SIZE);
memset(log_sys.flush_buf, 0, srv_log_buffer_size);
log_sys.buf_free = LOG_BLOCK_HDR_SIZE;
log_sys.log.write_header_durable(lsn);
ut_ad(srv_startup_is_before_trx_rollback_phase);
if (create_new_db) {
srv_startup_is_before_trx_rollback_phase = false;
}
/* Enable checkpoints in buf_flush_page_cleaner(). */
recv_sys.recovery_on = false;
mysql_mutex_unlock(&log_sys.mutex);
log_make_checkpoint();
log_buffer_flush_to_disk();
return DB_SUCCESS;
}
/** Rename the first redo log file.
@param[in] lsn FIL_PAGE_FILE_FLUSH_LSN value
@param[in,out] logfile0 name of the first log file
@return error code
@retval DB_SUCCESS on successful operation */
MY_ATTRIBUTE((warn_unused_result))
static dberr_t create_log_file_rename(lsn_t lsn, std::string &logfile0)
{
ut_ad(!srv_log_file_created);
ut_d(srv_log_file_created= true);
DBUG_EXECUTE_IF("innodb_log_abort_9", return (DB_ERROR););
DBUG_PRINT("ib_log", ("After innodb_log_abort_9"));
/* Rename the first log file, now that a log checkpoint has been created. */
auto new_name = get_log_file_path();
ib::info() << "Renaming log file " << logfile0 << " to " << new_name;
mysql_mutex_lock(&log_sys.mutex);
ut_ad(logfile0.size() == 2 + new_name.size());
logfile0= new_name;
dberr_t err= log_sys.log.rename(std::move(new_name));
mysql_mutex_unlock(&log_sys.mutex);
DBUG_EXECUTE_IF("innodb_log_abort_10", err= DB_ERROR;);
if (err == DB_SUCCESS)
ib::info() << "New log file created, LSN=" << lsn;
return err;
}
/** Create an undo tablespace file
@param[in] name file name
@return DB_SUCCESS or error code */
static dberr_t srv_undo_tablespace_create(const char* name)
{
pfs_os_file_t fh;
bool ret;
dberr_t err = DB_SUCCESS;
os_file_create_subdirs_if_needed(name);
fh = os_file_create(
innodb_data_file_key,
name,
srv_read_only_mode ? OS_FILE_OPEN : OS_FILE_CREATE,
OS_FILE_NORMAL, OS_DATA_FILE, srv_read_only_mode, &ret);
if (!ret) {
if (os_file_get_last_error(false) != OS_FILE_ALREADY_EXISTS
#ifdef _AIX
/* AIX 5.1 after security patch ML7 may have
errno set to 0 here, which causes our function
to return 100; work around that AIX problem */
&& os_file_get_last_error(false) != 100
#endif
) {
ib::error() << "Can't create UNDO tablespace "
<< name;
}
err = DB_ERROR;
} else if (srv_read_only_mode) {
ib::info() << name << " opened in read-only mode";
} else {
/* We created the data file and now write it full of zeros */
ib::info() << "Data file " << name << " did not exist: new to"
" be created";
ib::info() << "Setting file " << name << " size to "
<< (SRV_UNDO_TABLESPACE_SIZE_IN_PAGES >> (20 - srv_page_size_shift)) << " MB";
ib::info() << "Database physically writes the file full: "
<< "wait...";
if (!os_file_set_size(name, fh, os_offset_t
{SRV_UNDO_TABLESPACE_SIZE_IN_PAGES}
<< srv_page_size_shift)) {
ib::error() << "Unable to allocate " << name;
err = DB_ERROR;
}
os_file_close(fh);
}
return(err);
}
/* Validate the number of undo opened undo tablespace and user given
undo tablespace
@return DB_SUCCESS if it is valid */
static dberr_t srv_validate_undo_tablespaces()
{
/* If the user says that there are fewer than what we find we
tolerate that discrepancy but not the inverse. Because there could
be unused undo tablespaces for future use. */
if (srv_undo_tablespaces > srv_undo_tablespaces_open)
{
ib::error() << "Expected to open innodb_undo_tablespaces="
<< srv_undo_tablespaces
<< " but was able to find only "
<< srv_undo_tablespaces_open;
return DB_ERROR;
}
else if (srv_undo_tablespaces_open > 0)
{
ib::info() << "Opened " << srv_undo_tablespaces_open
<< " undo tablespaces";
if (srv_undo_tablespaces == 0)
ib::warn() << "innodb_undo_tablespaces=0 disables"
" dedicated undo log tablespaces";
}
return DB_SUCCESS;
}
/** @return the number of active undo tablespaces (except system tablespace) */
static ulint trx_rseg_get_n_undo_tablespaces()
{
std::set<uint32_t> space_ids;
mtr_t mtr;
mtr.start();
if (const buf_block_t *sys_header= trx_sysf_get(&mtr, false))
for (ulint rseg_id= 0; rseg_id < TRX_SYS_N_RSEGS; rseg_id++)
if (trx_sysf_rseg_get_page_no(sys_header, rseg_id) != FIL_NULL)
if (uint32_t space= trx_sysf_rseg_get_space(sys_header, rseg_id))
space_ids.insert(space);
mtr.commit();
return space_ids.size();
}
/** Open an undo tablespace.
@param[in] create whether undo tablespaces are being created
@param[in] name tablespace file name
@param[in] i undo tablespace count
@return undo tablespace identifier
@retval 0 on failure */
static ulint srv_undo_tablespace_open(bool create, const char* name, ulint i)
{
bool success;
char undo_name[sizeof "innodb_undo000"];
ulint space_id= 0;
ulint fsp_flags= 0;
if (create)
{
space_id= srv_undo_space_id_start + i;
snprintf(undo_name, sizeof(undo_name),
"innodb_undo%03u", static_cast<unsigned>(space_id));
switch (srv_checksum_algorithm) {
case SRV_CHECKSUM_ALGORITHM_FULL_CRC32:
case SRV_CHECKSUM_ALGORITHM_STRICT_FULL_CRC32:
fsp_flags= FSP_FLAGS_FCRC32_MASK_MARKER | FSP_FLAGS_FCRC32_PAGE_SSIZE();
break;
default:
fsp_flags= FSP_FLAGS_PAGE_SSIZE();
}
}
pfs_os_file_t fh= os_file_create(innodb_data_file_key, name, OS_FILE_OPEN |
OS_FILE_ON_ERROR_NO_EXIT |
OS_FILE_ON_ERROR_SILENT,
OS_FILE_AIO, OS_DATA_FILE,
srv_read_only_mode, &success);
if (!success)
return 0;
os_offset_t size= os_file_get_size(fh);
ut_a(size != os_offset_t(-1));
if (!create)
{
page_t *page= static_cast<byte*>(aligned_malloc(srv_page_size,
srv_page_size));
dberr_t err= os_file_read(IORequestRead, fh, page, 0, srv_page_size);
if (err != DB_SUCCESS)
{
err_exit:
ib::error() << "Unable to read first page of file " << name;
aligned_free(page);
return err;
}
uint32_t id= mach_read_from_4(FIL_PAGE_SPACE_ID + page);
if (id == 0 || id >= SRV_SPACE_ID_UPPER_BOUND ||
memcmp_aligned<2>(FIL_PAGE_SPACE_ID + page,
FSP_HEADER_OFFSET + FSP_SPACE_ID + page, 4))
{
ib::error() << "Inconsistent tablespace ID in file " << name;
err= DB_CORRUPTION;
goto err_exit;
}
fsp_flags= mach_read_from_4(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + page);
if (buf_page_is_corrupted(false, page, fsp_flags))
{
ib::error() << "Checksum mismatch in the first page of file " << name;
err= DB_CORRUPTION;
goto err_exit;
}
space_id= id;
snprintf(undo_name, sizeof undo_name, "innodb_undo%03u", id);
aligned_free(page);
}
/* Load the tablespace into InnoDB's internal data structures. */
/* We set the biggest space id to the undo tablespace
because InnoDB hasn't opened any other tablespace apart
from the system tablespace. */
fil_set_max_space_id_if_bigger(space_id);
fil_space_t *space= fil_space_t::create(undo_name, space_id, fsp_flags,
FIL_TYPE_TABLESPACE, NULL,
FIL_ENCRYPTION_DEFAULT, true);
ut_a(fil_validate());
ut_a(space);
fil_node_t *file= space->add(name, fh, 0, false, true);
mutex_enter(&fil_system.mutex);
if (create)
{
space->set_sizes(SRV_UNDO_TABLESPACE_SIZE_IN_PAGES);
space->size= file->size= uint32_t(size >> srv_page_size_shift);
}
else if (!file->read_page0())
{
os_file_close(file->handle);
file->handle= OS_FILE_CLOSED;
ut_a(fil_system.n_open > 0);
fil_system.n_open--;
}
mutex_exit(&fil_system.mutex);
return space_id;
}
/** Check if undo tablespaces and redo log files exist before creating a
new system tablespace
@retval DB_SUCCESS if all undo and redo logs are not found
@retval DB_ERROR if any undo and redo logs are found */
static
dberr_t
srv_check_undo_redo_logs_exists()
{
bool ret;
pfs_os_file_t fh;
char name[OS_FILE_MAX_PATH];
/* Check if any undo tablespaces exist */
for (ulint i = 1; i <= srv_undo_tablespaces; ++i) {
snprintf(
name, sizeof(name),
"%s%cundo%03zu",
srv_undo_dir, OS_PATH_SEPARATOR,
i);
fh = os_file_create(
innodb_data_file_key, name,
OS_FILE_OPEN_RETRY
| OS_FILE_ON_ERROR_NO_EXIT
| OS_FILE_ON_ERROR_SILENT,
OS_FILE_NORMAL,
OS_DATA_FILE,
srv_read_only_mode,
&ret);
if (ret) {
os_file_close(fh);
ib::error()
<< "undo tablespace '" << name << "' exists."
" Creating system tablespace with existing undo"
" tablespaces is not supported. Please delete"
" all undo tablespaces before creating new"
" system tablespace.";
return(DB_ERROR);
}
}
/* Check if redo log file exists */
auto logfilename = get_log_file_path();
fh = os_file_create(innodb_log_file_key, logfilename.c_str(),
OS_FILE_OPEN_RETRY | OS_FILE_ON_ERROR_NO_EXIT
| OS_FILE_ON_ERROR_SILENT,
OS_FILE_NORMAL, OS_LOG_FILE, srv_read_only_mode,
&ret);
if (ret) {
os_file_close(fh);
ib::error() << "redo log file '" << logfilename
<< "' exists. Creating system tablespace with"
" existing redo log file is not recommended."
" Please delete redo log file before"
" creating new system tablespace.";
return DB_ERROR;
}
return(DB_SUCCESS);
}
static dberr_t srv_all_undo_tablespaces_open(bool create_new_db, ulint n_undo)
{
/* Open all the undo tablespaces that are currently in use. If we
fail to open any of these it is a fatal error. The tablespace ids
should be contiguous. It is a fatal error because they are required
for recovery and are referenced by the UNDO logs (a.k.a RBS). */
ulint prev_id= create_new_db ? srv_undo_space_id_start - 1 : 0;
for (ulint i= 0; i < n_undo; ++i)
{
char name[OS_FILE_MAX_PATH];
snprintf(name, sizeof name, "%s%cundo%03zu", srv_undo_dir,
OS_PATH_SEPARATOR, i + 1);
ulint space_id= srv_undo_tablespace_open(create_new_db, name, i);
if (!space_id)
{
if (!create_new_db)
break;
ib::error() << "Unable to open create tablespace '" << name << "'.";
return DB_ERROR;
}
/* Should be no gaps in undo tablespace ids. */
ut_a(!i || prev_id + 1 == space_id);
prev_id= space_id;
/* Note the first undo tablespace id in case of
no active undo tablespace. */
if (0 == srv_undo_tablespaces_open++)
srv_undo_space_id_start= space_id;
}
/* Open any extra unused undo tablespaces. These must be contiguous.
We stop at the first failure. These are undo tablespaces that are
not in use and therefore not required by recovery. We only check
that there are no gaps. */
for (ulint i= prev_id + 1; i < srv_undo_space_id_start + TRX_SYS_N_RSEGS;
++i)
{
char name[OS_FILE_MAX_PATH];
snprintf(name, sizeof(name),
"%s%cundo%03zu", srv_undo_dir, OS_PATH_SEPARATOR, i);
if (!srv_undo_tablespace_open(create_new_db, name, i))
break;
++srv_undo_tablespaces_open;
}
return srv_validate_undo_tablespaces();
}
/** Open the configured number of dedicated undo tablespaces.
@param[in] create_new_db whether the database is being initialized
@return DB_SUCCESS or error code */
dberr_t
srv_undo_tablespaces_init(bool create_new_db)
{
srv_undo_tablespaces_open= 0;
ut_a(srv_undo_tablespaces <= TRX_SYS_N_RSEGS);
ut_a(!create_new_db || srv_operation == SRV_OPERATION_NORMAL);
if (srv_undo_tablespaces == 1)
srv_undo_tablespaces= 0;
/* Create the undo spaces only if we are creating a new
instance. We don't allow creating of new undo tablespaces
in an existing instance (yet). */
if (create_new_db)
{
srv_undo_space_id_start= 1;
DBUG_EXECUTE_IF("innodb_undo_upgrade", srv_undo_space_id_start= 3;);
for (ulint i= 0; i < srv_undo_tablespaces; ++i)
{
char name[OS_FILE_MAX_PATH];
snprintf(name, sizeof name, "%s%cundo%03zu",
srv_undo_dir, OS_PATH_SEPARATOR, i + 1);
if (dberr_t err= srv_undo_tablespace_create(name))
{
ib::error() << "Could not create undo tablespace '" << name << "'.";
return err;
}
}
}
/* Get the tablespace ids of all the undo segments excluding
the system tablespace (0). If we are creating a new instance then
we build the undo_tablespace_ids ourselves since they don't
already exist. */
srv_undo_tablespaces_active= srv_undo_tablespaces;
ulint n_undo= (create_new_db || srv_operation == SRV_OPERATION_BACKUP ||
srv_operation == SRV_OPERATION_RESTORE_DELTA)
? srv_undo_tablespaces : TRX_SYS_N_RSEGS;
if (dberr_t err= srv_all_undo_tablespaces_open(create_new_db, n_undo))
return err;
/* Initialize srv_undo_space_id_start=0 when there are no
dedicated undo tablespaces. */
if (srv_undo_tablespaces_open == 0)
srv_undo_space_id_start= 0;
if (create_new_db)
{
mtr_t mtr;
for (ulint i= 0; i < srv_undo_tablespaces; ++i)
{
mtr.start();
fsp_header_init(fil_space_get(srv_undo_space_id_start + i),
SRV_UNDO_TABLESPACE_SIZE_IN_PAGES, &mtr);
mtr.commit();
}
}
return DB_SUCCESS;
}
/** Create the temporary file tablespace.
@param[in] create_new_db whether we are creating a new database
@return DB_SUCCESS or error code. */
static
dberr_t
srv_open_tmp_tablespace(bool create_new_db)
{
ulint sum_of_new_sizes;
/* Will try to remove if there is existing file left-over by last
unclean shutdown */
srv_tmp_space.set_sanity_check_status(true);
srv_tmp_space.delete_files();
srv_tmp_space.set_ignore_read_only(true);
ib::info() << "Creating shared tablespace for temporary tables";
bool create_new_temp_space;
srv_tmp_space.set_space_id(SRV_TMP_SPACE_ID);
dberr_t err = srv_tmp_space.check_file_spec(
&create_new_temp_space, 12 * 1024 * 1024);
if (err == DB_FAIL) {
ib::error() << "The innodb_temporary"
" data file must be writable!";
err = DB_ERROR;
} else if (err != DB_SUCCESS) {
ib::error() << "Could not create the shared innodb_temporary.";
} else if ((err = srv_tmp_space.open_or_create(
true, create_new_db, &sum_of_new_sizes, NULL))
!= DB_SUCCESS) {
ib::error() << "Unable to create the shared innodb_temporary";
} else if (fil_system.temp_space->open(true)) {
/* Initialize the header page */
mtr_t mtr;
mtr.start();
mtr.set_log_mode(MTR_LOG_NO_REDO);
fsp_header_init(fil_system.temp_space,
srv_tmp_space.get_sum_of_sizes(),
&mtr);
mtr.commit();
} else {
/* This file was just opened in the code above! */
ib::error() << "The innodb_temporary"
" data file cannot be re-opened"
" after check_file_spec() succeeded!";
err = DB_ERROR;
}
return(err);
}
/** Shutdown background threads, except the page cleaner. */
static void srv_shutdown_threads()
{
ut_ad(!srv_undo_sources);
srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS;
lock_sys.timeout_timer.reset();
srv_master_timer.reset();
if (purge_sys.enabled()) {
srv_purge_shutdown();
}
if (srv_n_fil_crypt_threads) {
fil_crypt_set_thread_cnt(0);
}
}
/** Shut down background threads that can generate undo log. */
static void srv_shutdown_bg_undo_sources()
{
srv_shutdown_state= SRV_SHUTDOWN_INITIATED;
if (srv_undo_sources)
{
ut_ad(!srv_read_only_mode);
fts_optimize_shutdown();
dict_stats_shutdown();
while (row_get_background_drop_list_len_low())
{
srv_inc_activity_count();
os_thread_yield();
}
srv_undo_sources= false;
}
}
#ifdef UNIV_DEBUG
# define srv_init_abort(_db_err) \
srv_init_abort_low(create_new_db, __FILE__, __LINE__, _db_err)
#else
# define srv_init_abort(_db_err) \
srv_init_abort_low(create_new_db, _db_err)
#endif /* UNIV_DEBUG */
/** Innobase start-up aborted. Perform cleanup actions.
@param[in] create_new_db TRUE if new db is being created
@param[in] file File name
@param[in] line Line number
@param[in] err Reason for aborting InnoDB startup
@return DB_SUCCESS or error code. */
MY_ATTRIBUTE((warn_unused_result, nonnull))
static
dberr_t
srv_init_abort_low(
bool create_new_db,
#ifdef UNIV_DEBUG
const char* file,
unsigned line,
#endif /* UNIV_DEBUG */
dberr_t err)
{
ut_ad(srv_is_being_started);
if (create_new_db) {
ib::error() << "Database creation was aborted"
#ifdef UNIV_DEBUG
" at " << innobase_basename(file) << "[" << line << "]"
#endif /* UNIV_DEBUG */
" with error " << err << ". You may need"
" to delete the ibdata1 file before trying to start"
" up again.";
} else {
ib::error() << "Plugin initialization aborted"
#ifdef UNIV_DEBUG
" at " << innobase_basename(file) << "[" << line << "]"
#endif /* UNIV_DEBUG */
" with error " << err;
}
srv_shutdown_bg_undo_sources();
srv_shutdown_threads();
return(err);
}
/** Prepare to delete the redo log file. Flush the dirty pages from all the
buffer pools. Flush the redo log buffer to the redo log file.
@param[in] old_exists old redo log file exists
@return lsn upto which data pages have been flushed. */
static lsn_t srv_prepare_to_delete_redo_log_file(bool old_exists)
{
DBUG_ENTER("srv_prepare_to_delete_redo_log_file");
/* Disable checkpoints in the page cleaner. */
ut_ad(!recv_sys.recovery_on);
recv_sys.recovery_on= true;
/* Clean the buffer pool. */
buf_flush_sync();
if (log_sys.log.subformat != 2)
srv_log_file_size= 0;
DBUG_EXECUTE_IF("innodb_log_abort_1", DBUG_RETURN(0););
DBUG_PRINT("ib_log", ("After innodb_log_abort_1"));
mysql_mutex_lock(&log_sys.mutex);
const bool latest_format= (log_sys.log.format & ~log_t::FORMAT_ENCRYPTED) ==
log_t::FORMAT_10_5;
lsn_t flushed_lsn= log_sys.get_lsn();
if (latest_format)
{
fil_names_clear(flushed_lsn, false);
flushed_lsn= log_sys.get_lsn();
}
{
const char *msg;
if (!latest_format || srv_log_file_size == 0)
{
msg= "Upgrading redo log: ";
same_size:
ib::info() << msg << srv_log_file_size_requested << " bytes; LSN="
<< flushed_lsn;
}
else if (old_exists && srv_log_file_size == srv_log_file_size_requested)
{
msg= srv_encrypt_log
? "Encrypting redo log: " : "Removing redo log encryption: ";
goto same_size;
}
else
{
if (srv_encrypt_log == (my_bool)log_sys.is_encrypted())
msg= srv_encrypt_log ? "Resizing encrypted" : "Resizing";
else
msg= srv_encrypt_log
? "Encrypting and resizing"
: "Removing encryption and resizing";
ib::info() << msg << " redo log from " << srv_log_file_size << " to "
<< srv_log_file_size_requested
<< " bytes; LSN=" << flushed_lsn;
}
}
mysql_mutex_unlock(&log_sys.mutex);
if (flushed_lsn != log_sys.get_flushed_lsn())
{
log_write_up_to(flushed_lsn, false);
log_sys.log.flush();
}
ut_ad(flushed_lsn == log_sys.get_lsn());
ut_ad(!buf_pool.any_io_pending());
DBUG_RETURN(flushed_lsn);
}
/** Tries to locate LOG_FILE_NAME and check it's size, etc
@param[out] log_file_found returns true here if correct file was found
@return dberr_t with DB_SUCCESS or some error */
static dberr_t find_and_check_log_file(bool &log_file_found)
{
log_file_found= false;
auto logfile0= get_log_file_path();