1
/*****************************************************************************
3
Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
4
Copyright (C) 2008, Google Inc.
5
Copyright (C) 2009, Percona Inc.
7
Portions of this file contain modifications contributed and copyrighted by
8
Google, Inc. Those modifications are gratefully acknowledged and are described
9
briefly in the InnoDB documentation. The contributions by Google are
10
incorporated with their permission, and subject to the conditions contained in
11
the file COPYING.Google.
13
Portions of this file contain modifications contributed and copyrighted
14
by Percona Inc.. Those modifications are
15
gratefully acknowledged and are described briefly in the InnoDB
16
documentation. The contributions by Percona Inc. are incorporated with
17
their permission, and subject to the conditions contained in the file
20
This program is free software; you can redistribute it and/or modify it under
21
the terms of the GNU General Public License as published by the Free Software
22
Foundation; version 2 of the License.
24
This program is distributed in the hope that it will be useful, but WITHOUT
25
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
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., 51 Franklin
30
St, Fifth Floor, Boston, MA 02110-1301 USA
32
*****************************************************************************/
34
/********************************************************************//**
36
Starts the InnoDB database server
38
Created 2/16/1996 Heikki Tuuri
39
*************************************************************************/
43
#include "data0data.h"
44
#include "data0type.h"
45
#include "dict0dict.h"
48
#include "os0thread.h"
55
#include "page0page.h"
62
#include "ibuf0ibuf.h"
63
#include "srv0start.h"
65
#ifndef UNIV_HOTBACKUP
67
# include "sync0sync.h"
70
# include "dict0boot.h"
71
# include "dict0load.h"
73
# include "usr0sess.h"
74
# include "lock0lock.h"
75
# include "trx0roll.h"
76
# include "trx0purge.h"
77
# include "lock0lock.h"
78
# include "pars0pars.h"
81
# include "dict0crea.h"
86
# include "row0mysql.h"
87
# include "btr0pcur.h"
89
# include "os0sync.h" /* for INNODB_RW_LOCKS_USE_ATOMICS */
90
# include "zlib.h" /* for ZLIB_VERSION */
95
/** Log sequence number immediately after startup */
96
UNIV_INTERN ib_uint64_t srv_start_lsn;
97
/** Log sequence number at shutdown */
98
UNIV_INTERN ib_uint64_t srv_shutdown_lsn;
100
#ifdef HAVE_DARWIN_THREADS
101
# include <sys/utsname.h>
102
/** TRUE if the F_FULLFSYNC option is available */
103
UNIV_INTERN ibool srv_have_fullfsync = FALSE;
106
/** TRUE if a raw partition is in use */
107
UNIV_INTERN ibool srv_start_raw_disk_in_use = FALSE;
109
/** TRUE if the server is being started, before rolling back any
110
incomplete transactions */
111
UNIV_INTERN ibool srv_startup_is_before_trx_rollback_phase = FALSE;
112
/** TRUE if the server is being started */
113
UNIV_INTERN ibool srv_is_being_started = FALSE;
114
/** TRUE if the server was successfully started */
115
UNIV_INTERN ibool srv_was_started = FALSE;
116
/** TRUE if innobase_start_or_create_for_mysql() has been called */
117
static ibool srv_start_has_been_called = FALSE;
119
/** At a shutdown this value climbs from SRV_SHUTDOWN_NONE to
120
SRV_SHUTDOWN_CLEANUP and then to SRV_SHUTDOWN_LAST_PHASE, and so on */
121
UNIV_INTERN enum srv_shutdown_state srv_shutdown_state = SRV_SHUTDOWN_NONE;
123
/** Files comprising the system tablespace */
124
static os_file_t files[1000];
126
/** Mutex protecting the ios count */
127
static mutex_t ios_mutex;
128
/** Count of I/O operations in io_handler_thread() */
131
/** io_handler_thread parameters for thread identification */
132
static ulint n[SRV_MAX_N_IO_THREADS + 6];
133
/** io_handler_thread identifiers */
134
static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 6];
136
/** We use this mutex to test the return value of pthread_mutex_trylock
137
on successful locking. HP-UX does NOT return 0, though Linux et al do. */
138
static os_fast_mutex_t srv_os_test_mutex;
140
/** Name of srv_monitor_file */
141
static char* srv_monitor_file_name;
142
#endif /* !UNIV_HOTBACKUP */
145
#define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD
146
#define SRV_MAX_N_PENDING_SYNC_IOS 100
148
#ifdef UNIV_PFS_THREAD
149
/* Keys to register InnoDB threads with performance schema */
150
UNIV_INTERN mysql_pfs_key_t io_handler_thread_key;
151
UNIV_INTERN mysql_pfs_key_t srv_lock_timeout_thread_key;
152
UNIV_INTERN mysql_pfs_key_t srv_error_monitor_thread_key;
153
UNIV_INTERN mysql_pfs_key_t srv_monitor_thread_key;
154
UNIV_INTERN mysql_pfs_key_t srv_master_thread_key;
155
#endif /* UNIV_PFS_THREAD */
157
#ifdef UNIV_PFS_MUTEX
158
/* Key to register ios_mutex_key with performance schema */
159
UNIV_INTERN mysql_pfs_key_t ios_mutex_key;
160
#endif /* UNIV_PFS_MUTEX */
162
/*********************************************************************//**
163
Convert a numeric string that optionally ends in G or M, to a number
164
containing megabytes.
165
@return next character in string */
170
char* str, /*!< in: string containing a quantity in bytes */
171
ulint* megs) /*!< out: the number in megabytes */
176
size = strtoul(str, &endp, 10);
196
/*********************************************************************//**
197
Reads the data files and their sizes from a character string given in
199
@return TRUE if ok, FALSE on parse error */
202
srv_parse_data_file_paths_and_sizes(
203
/*================================*/
204
char* str) /*!< in/out: the data file path string */
211
srv_auto_extend_last_data_file = FALSE;
212
srv_last_file_size_max = 0;
213
srv_data_file_names = NULL;
214
srv_data_file_sizes = NULL;
215
srv_data_file_is_raw_partition = NULL;
219
/* First calculate the number of data files and check syntax:
220
path:size[M | G];path:size[M | G]... . Note that a Windows path may
221
contain a drive name and a ':'. */
223
while (*str != '\0') {
226
while ((*str != ':' && *str != '\0')
228
&& (*(str + 1) == '\\' || *(str + 1) == '/'
229
|| *(str + 1) == ':'))) {
239
str = srv_parse_megabytes(str, &size);
241
if (0 == strncmp(str, ":autoextend",
242
(sizeof ":autoextend") - 1)) {
244
str += (sizeof ":autoextend") - 1;
246
if (0 == strncmp(str, ":max:",
247
(sizeof ":max:") - 1)) {
249
str += (sizeof ":max:") - 1;
251
str = srv_parse_megabytes(str, &size);
263
&& *(str + 2) == 'w') {
267
if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
279
} else if (*str != '\0') {
286
/* If innodb_data_file_path was defined it must contain
287
at least one data file definition */
292
srv_data_file_names = malloc(i * sizeof *srv_data_file_names);
293
srv_data_file_sizes = malloc(i * sizeof *srv_data_file_sizes);
294
srv_data_file_is_raw_partition = malloc(
295
i * sizeof *srv_data_file_is_raw_partition);
297
srv_n_data_files = i;
299
/* Then store the actual values to our arrays */
304
while (*str != '\0') {
307
/* Note that we must step over the ':' in a Windows path;
308
a Windows path normally looks like C:\ibdata\ibdata1:1G, but
309
a Windows raw partition may have a specification like
310
\\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */
312
while ((*str != ':' && *str != '\0')
314
&& (*(str + 1) == '\\' || *(str + 1) == '/'
315
|| *(str + 1) == ':'))) {
320
/* Make path a null-terminated string */
325
str = srv_parse_megabytes(str, &size);
327
srv_data_file_names[i] = path;
328
srv_data_file_sizes[i] = size;
330
if (0 == strncmp(str, ":autoextend",
331
(sizeof ":autoextend") - 1)) {
333
srv_auto_extend_last_data_file = TRUE;
335
str += (sizeof ":autoextend") - 1;
337
if (0 == strncmp(str, ":max:",
338
(sizeof ":max:") - 1)) {
340
str += (sizeof ":max:") - 1;
342
str = srv_parse_megabytes(
343
str, &srv_last_file_size_max);
352
(srv_data_file_is_raw_partition)[i] = 0;
357
&& *(str + 2) == 'w') {
359
(srv_data_file_is_raw_partition)[i] = SRV_NEW_RAW;
362
if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
365
if ((srv_data_file_is_raw_partition)[i] == 0) {
366
(srv_data_file_is_raw_partition)[i] = SRV_OLD_RAW;
380
/*********************************************************************//**
381
Reads log group home directories from a character string given in
383
@return TRUE if ok, FALSE on parse error */
386
srv_parse_log_group_home_dirs(
387
/*==========================*/
388
char* str) /*!< in/out: character string */
394
srv_log_group_home_dirs = NULL;
398
/* First calculate the number of directories and check syntax:
401
while (*str != '\0') {
404
while (*str != ';' && *str != '\0') {
412
} else if (*str != '\0') {
419
/* If innodb_log_group_home_dir was defined it must
420
contain exactly one path definition under current MySQL */
425
srv_log_group_home_dirs = malloc(i * sizeof *srv_log_group_home_dirs);
427
/* Then store the actual values to our array */
432
while (*str != '\0') {
435
while (*str != ';' && *str != '\0') {
444
srv_log_group_home_dirs[i] = path;
452
/*********************************************************************//**
453
Frees the memory allocated by srv_parse_data_file_paths_and_sizes()
454
and srv_parse_log_group_home_dirs(). */
457
srv_free_paths_and_sizes(void)
458
/*==========================*/
460
free(srv_data_file_names);
461
srv_data_file_names = NULL;
462
free(srv_data_file_sizes);
463
srv_data_file_sizes = NULL;
464
free(srv_data_file_is_raw_partition);
465
srv_data_file_is_raw_partition = NULL;
466
free(srv_log_group_home_dirs);
467
srv_log_group_home_dirs = NULL;
470
#ifndef UNIV_HOTBACKUP
471
/********************************************************************//**
472
I/o-handler thread function.
473
@return OS_THREAD_DUMMY_RETURN */
478
void* arg) /*!< in: pointer to the number of the segment in
483
segment = *((ulint*)arg);
485
#ifdef UNIV_DEBUG_THREAD_CREATION
486
fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
487
os_thread_pf(os_thread_get_curr_id()));
490
#ifdef UNIV_PFS_THREAD
491
pfs_register_thread(io_handler_thread_key);
492
#endif /* UNIV_PFS_THREAD */
494
while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
495
fil_aio_wait(segment);
497
mutex_enter(&ios_mutex);
499
mutex_exit(&ios_mutex);
502
thr_local_free(os_thread_get_curr_id());
504
/* We count the number of threads in os_thread_exit(). A created
505
thread should always use that to exit and not use return() to exit.
506
The thread actually never comes here because it is exited in an
510
#endif /* !UNIV_HOTBACKUP */
513
#define SRV_PATH_SEPARATOR '\\'
515
#define SRV_PATH_SEPARATOR '/'
518
/*********************************************************************//**
519
Normalizes a directory path for Windows: converts slashes to backslashes. */
522
srv_normalize_path_for_win(
523
/*=======================*/
524
char* str __attribute__((unused))) /*!< in/out: null-terminated
528
for (; *str; str++) {
537
#ifndef UNIV_HOTBACKUP
538
/*********************************************************************//**
539
Calculates the low 32 bits when a file size which is given as a number
540
database pages is converted to the number of bytes.
541
@return low 32 bytes of file size when expressed in bytes */
546
ulint file_size) /*!< in: file size in database pages */
548
return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT));
551
/*********************************************************************//**
552
Calculates the high 32 bits when a file size which is given as a number
553
database pages is converted to the number of bytes.
554
@return high 32 bytes of file size when expressed in bytes */
559
ulint file_size) /*!< in: file size in database pages */
561
return(file_size >> (32 - UNIV_PAGE_SIZE_SHIFT));
564
/*********************************************************************//**
565
Creates or opens the log files and closes them.
566
@return DB_SUCCESS or error code */
569
open_or_create_log_file(
570
/*====================*/
571
ibool create_new_db, /*!< in: TRUE if we should create a
573
ibool* log_file_created, /*!< out: TRUE if new log file
575
ibool log_file_has_been_opened,/*!< in: TRUE if a log file has been
576
opened before: then it is an error
577
to try to create another log file */
578
ulint k, /*!< in: log group number */
579
ulint i) /*!< in: log file number in group */
587
UT_NOT_USED(create_new_db);
589
*log_file_created = FALSE;
591
srv_normalize_path_for_win(srv_log_group_home_dirs[k]);
593
dirnamelen = strlen(srv_log_group_home_dirs[k]);
594
ut_a(dirnamelen < (sizeof name) - 10 - sizeof "ib_logfile");
595
memcpy(name, srv_log_group_home_dirs[k], dirnamelen);
597
/* Add a path separator if needed. */
598
if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
599
name[dirnamelen++] = SRV_PATH_SEPARATOR;
602
sprintf(name + dirnamelen, "%s%lu", "ib_logfile", (ulong) i);
604
files[i] = os_file_create(innodb_file_log_key, name,
605
OS_FILE_CREATE, OS_FILE_NORMAL,
608
if (os_file_get_last_error(FALSE) != OS_FILE_ALREADY_EXISTS
610
/* AIX 5.1 after security patch ML7 may have errno set
611
to 0 here, which causes our function to return 100;
612
work around that AIX problem */
613
&& os_file_get_last_error(FALSE) != 100
617
"InnoDB: Error in creating"
618
" or opening %s\n", name);
623
files[i] = os_file_create(innodb_file_log_key, name,
624
OS_FILE_OPEN, OS_FILE_AIO,
628
"InnoDB: Error in opening %s\n", name);
633
ret = os_file_get_size(files[i], &size, &size_high);
636
if (size != srv_calc_low32(srv_log_file_size)
637
|| size_high != srv_calc_high32(srv_log_file_size)) {
640
"InnoDB: Error: log file %s is"
641
" of different size %lu %lu bytes\n"
642
"InnoDB: than specified in the .cnf"
643
" file %lu %lu bytes!\n",
644
name, (ulong) size_high, (ulong) size,
645
(ulong) srv_calc_high32(srv_log_file_size),
646
(ulong) srv_calc_low32(srv_log_file_size));
651
*log_file_created = TRUE;
653
ut_print_timestamp(stderr);
656
" InnoDB: Log file %s did not exist:"
657
" new to be created\n",
659
if (log_file_has_been_opened) {
664
fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n",
665
name, (ulong) srv_log_file_size
666
>> (20 - UNIV_PAGE_SIZE_SHIFT));
669
"InnoDB: Database physically writes the file"
672
ret = os_file_set_size(name, files[i],
673
srv_calc_low32(srv_log_file_size),
674
srv_calc_high32(srv_log_file_size));
677
"InnoDB: Error in creating %s:"
678
" probably out of disk space\n",
685
ret = os_file_close(files[i]);
689
/* Create in memory the file space object
690
which is for this log group */
692
fil_space_create(name,
693
2 * k + SRV_LOG_SPACE_FIRST_ID, 0, FIL_LOG);
696
ut_a(fil_validate());
698
fil_node_create(name, srv_log_file_size,
699
2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE);
700
#ifdef UNIV_LOG_ARCHIVE
701
/* If this is the first log group, create the file space object
703
Under MySQL, no archiving ever done. */
705
if (k == 0 && i == 0) {
706
arch_space_id = 2 * k + 1 + SRV_LOG_SPACE_FIRST_ID;
708
fil_space_create("arch_log_space", arch_space_id, 0, FIL_LOG);
710
arch_space_id = ULINT_UNDEFINED;
712
#endif /* UNIV_LOG_ARCHIVE */
714
log_group_init(k, srv_n_log_files,
715
srv_log_file_size * UNIV_PAGE_SIZE,
716
2 * k + SRV_LOG_SPACE_FIRST_ID,
717
SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch
724
/*********************************************************************//**
725
Creates or opens database data files and closes them.
726
@return DB_SUCCESS or error code */
729
open_or_create_data_files(
730
/*======================*/
731
ibool* create_new_db, /*!< out: TRUE if new database should be
733
#ifdef UNIV_LOG_ARCHIVE
734
ulint* min_arch_log_no,/*!< out: min of archived log
735
numbers in data files */
736
ulint* max_arch_log_no,/*!< out: max of archived log
737
numbers in data files */
738
#endif /* UNIV_LOG_ARCHIVE */
739
ib_uint64_t* min_flushed_lsn,/*!< out: min of flushed lsn
740
values in data files */
741
ib_uint64_t* max_flushed_lsn,/*!< out: max of flushed lsn
742
values in data files */
743
ulint* sum_of_new_sizes)/*!< out: sum of sizes of the
748
ibool one_opened = FALSE;
749
ibool one_created = FALSE;
752
ulint rounded_size_pages;
755
if (srv_n_data_files >= 1000) {
756
fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
757
"InnoDB: you have defined %lu\n",
758
(ulong) srv_n_data_files);
762
*sum_of_new_sizes = 0;
764
*create_new_db = FALSE;
766
srv_normalize_path_for_win(srv_data_home);
768
for (i = 0; i < srv_n_data_files; i++) {
771
srv_normalize_path_for_win(srv_data_file_names[i]);
772
dirnamelen = strlen(srv_data_home);
774
ut_a(dirnamelen + strlen(srv_data_file_names[i])
775
< (sizeof name) - 1);
776
memcpy(name, srv_data_home, dirnamelen);
777
/* Add a path separator if needed. */
778
if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
779
name[dirnamelen++] = SRV_PATH_SEPARATOR;
782
strcpy(name + dirnamelen, srv_data_file_names[i]);
784
if (srv_data_file_is_raw_partition[i] == 0) {
786
/* First we try to create the file: if it already
787
exists, ret will get value FALSE */
789
files[i] = os_file_create(innodb_file_data_key,
790
name, OS_FILE_CREATE,
794
if (ret == FALSE && os_file_get_last_error(FALSE)
795
!= OS_FILE_ALREADY_EXISTS
797
/* AIX 5.1 after security patch ML7 may have
798
errno set to 0 here, which causes our function
799
to return 100; work around that AIX problem */
800
&& os_file_get_last_error(FALSE) != 100
804
"InnoDB: Error in creating"
810
} else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
811
/* The partition is opened, not created; then it is
814
srv_start_raw_disk_in_use = TRUE;
815
srv_created_new_raw = TRUE;
817
files[i] = os_file_create(innodb_file_data_key,
818
name, OS_FILE_OPEN_RAW,
823
"InnoDB: Error in opening %s\n", name);
827
} else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
828
srv_start_raw_disk_in_use = TRUE;
836
/* We open the data file */
840
"InnoDB: Error: data files can only"
841
" be added at the end\n");
843
"InnoDB: of a tablespace, but"
844
" data file %s existed beforehand.\n",
849
if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
850
files[i] = os_file_create(
851
innodb_file_data_key,
852
name, OS_FILE_OPEN_RAW,
853
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
855
files[i] = os_file_create(
856
innodb_file_data_key,
857
name, OS_FILE_OPEN_RETRY,
858
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
860
files[i] = os_file_create(
861
innodb_file_data_key,
862
name, OS_FILE_OPEN, OS_FILE_NORMAL,
868
"InnoDB: Error in opening %s\n", name);
869
os_file_get_last_error(TRUE);
874
if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
876
goto skip_size_check;
879
ret = os_file_get_size(files[i], &size, &size_high);
881
/* Round size downward to megabytes */
884
= (size / (1024 * 1024) + 4096 * size_high)
885
<< (20 - UNIV_PAGE_SIZE_SHIFT);
887
if (i == srv_n_data_files - 1
888
&& srv_auto_extend_last_data_file) {
890
if (srv_data_file_sizes[i] > rounded_size_pages
891
|| (srv_last_file_size_max > 0
892
&& srv_last_file_size_max
893
< rounded_size_pages)) {
896
"InnoDB: Error: auto-extending"
898
" of a different size\n"
899
"InnoDB: %lu pages (rounded"
900
" down to MB) than specified"
901
" in the .cnf file:\n"
902
"InnoDB: initial %lu pages,"
903
" max %lu (relevant if"
904
" non-zero) pages!\n",
906
(ulong) rounded_size_pages,
907
(ulong) srv_data_file_sizes[i],
909
srv_last_file_size_max);
914
srv_data_file_sizes[i] = rounded_size_pages;
917
if (rounded_size_pages != srv_data_file_sizes[i]) {
920
"InnoDB: Error: data file %s"
921
" is of a different size\n"
923
" (rounded down to MB)\n"
924
"InnoDB: than specified"
925
" in the .cnf file %lu pages!\n",
927
(ulong) rounded_size_pages,
928
(ulong) srv_data_file_sizes[i]);
933
fil_read_flushed_lsn_and_arch_log_no(
934
files[i], one_opened,
935
#ifdef UNIV_LOG_ARCHIVE
936
min_arch_log_no, max_arch_log_no,
937
#endif /* UNIV_LOG_ARCHIVE */
938
min_flushed_lsn, max_flushed_lsn);
941
/* We created the data file and now write it full of
947
ut_print_timestamp(stderr);
949
" InnoDB: Data file %s did not"
950
" exist: new to be created\n",
954
"InnoDB: The first specified"
955
" data file %s did not exist:\n"
956
"InnoDB: a new database"
957
" to be created!\n", name);
958
*create_new_db = TRUE;
961
ut_print_timestamp(stderr);
963
" InnoDB: Setting file %s size to %lu MB\n",
965
(ulong) (srv_data_file_sizes[i]
966
>> (20 - UNIV_PAGE_SIZE_SHIFT)));
969
"InnoDB: Database physically writes the"
970
" file full: wait...\n");
972
ret = os_file_set_size(
974
srv_calc_low32(srv_data_file_sizes[i]),
975
srv_calc_high32(srv_data_file_sizes[i]));
979
"InnoDB: Error in creating %s:"
980
" probably out of disk space\n", name);
985
*sum_of_new_sizes = *sum_of_new_sizes
986
+ srv_data_file_sizes[i];
989
ret = os_file_close(files[i]);
993
fil_space_create(name, 0, 0, FIL_TABLESPACE);
996
ut_a(fil_validate());
998
fil_node_create(name, srv_data_file_sizes[i], 0,
999
srv_data_file_is_raw_partition[i] != 0);
1004
mutex_create(ios_mutex_key, &ios_mutex, SYNC_NO_ORDER_CHECK);
1009
/********************************************************************
1010
Starts InnoDB and creates a new database if database files
1011
are not found and the user wants.
1012
@return DB_SUCCESS or error code */
1015
innobase_start_or_create_for_mysql(void)
1016
/*====================================*/
1018
ibool create_new_db;
1019
ibool log_file_created;
1020
ibool log_created = FALSE;
1021
ibool log_opened = FALSE;
1022
ib_uint64_t min_flushed_lsn;
1023
ib_uint64_t max_flushed_lsn;
1024
#ifdef UNIV_LOG_ARCHIVE
1025
ulint min_arch_log_no;
1026
ulint max_arch_log_no;
1027
#endif /* UNIV_LOG_ARCHIVE */
1028
ulint sum_of_new_sizes;
1029
ulint sum_of_data_file_sizes;
1030
ulint tablespace_size_in_header;
1034
my_bool srv_file_per_table_original_value
1035
= srv_file_per_table;
1037
#ifdef HAVE_DARWIN_THREADS
1039
/* This executable has been compiled on Mac OS X 10.3 or later.
1040
Assume that F_FULLFSYNC is available at run-time. */
1041
srv_have_fullfsync = TRUE;
1042
# else /* F_FULLFSYNC */
1043
/* This executable has been compiled on Mac OS X 10.2
1044
or earlier. Determine if the executable is running
1045
on Mac OS X 10.3 or later. */
1046
struct utsname utsname;
1047
if (uname(&utsname)) {
1048
fputs("InnoDB: cannot determine Mac OS X version!\n", stderr);
1050
srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0;
1052
if (!srv_have_fullfsync) {
1053
fputs("InnoDB: On Mac OS X, fsync() may be"
1054
" broken on internal drives,\n"
1055
"InnoDB: making transactions unsafe!\n", stderr);
1057
# endif /* F_FULLFSYNC */
1058
#endif /* HAVE_DARWIN_THREADS */
1060
if (sizeof(ulint) != sizeof(void*)) {
1062
"InnoDB: Error: size of InnoDB's ulint is %lu,"
1063
" but size of void* is %lu.\n"
1064
"InnoDB: The sizes should be the same"
1065
" so that on a 64-bit platform you can\n"
1066
"InnoDB: allocate more than 4 GB of memory.",
1067
(ulong)sizeof(ulint), (ulong)sizeof(void*));
1070
/* System tables are created in tablespace 0. Thus, we must
1071
temporarily clear srv_file_per_table. This is ok, because the
1072
server will not accept connections (which could modify
1073
innodb_file_per_table) until this function has returned. */
1074
srv_file_per_table = FALSE;
1077
"InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!\n");
1080
#ifdef UNIV_IBUF_DEBUG
1082
"InnoDB: !!!!!!!! UNIV_IBUF_DEBUG switched on !!!!!!!!!\n"
1083
# ifdef UNIV_IBUF_COUNT_DEBUG
1084
"InnoDB: !!!!!!!! UNIV_IBUF_COUNT_DEBUG switched on !!!!!!!!!\n"
1085
"InnoDB: Crash recovery will fail with UNIV_IBUF_COUNT_DEBUG\n"
1090
#ifdef UNIV_SYNC_DEBUG
1092
"InnoDB: !!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!\n");
1095
#ifdef UNIV_SEARCH_DEBUG
1097
"InnoDB: !!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!\n");
1100
#ifdef UNIV_LOG_LSN_DEBUG
1102
"InnoDB: !!!!!!!! UNIV_LOG_LSN_DEBUG switched on !!!!!!!!!\n");
1103
#endif /* UNIV_LOG_LSN_DEBUG */
1104
#ifdef UNIV_MEM_DEBUG
1106
"InnoDB: !!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!\n");
1109
if (UNIV_LIKELY(srv_use_sys_malloc)) {
1111
"InnoDB: The InnoDB memory heap is disabled\n");
1114
fputs("InnoDB: " IB_ATOMICS_STARTUP_MSG
1115
"\nInnoDB: Compressed tables use zlib " ZLIB_VERSION
1116
#ifdef UNIV_ZIP_DEBUG
1118
#endif /* UNIV_ZIP_DEBUG */
1119
#ifdef UNIV_ZIP_COPY
1120
" and extra copying"
1121
#endif /* UNIV_ZIP_COPY */
1125
/* Since InnoDB does not currently clean up all its internal data
1126
structures in MySQL Embedded Server Library server_end(), we
1127
print an error message if someone tries to start up InnoDB a
1128
second time during the process lifetime. */
1130
if (srv_start_has_been_called) {
1132
"InnoDB: Error: startup called second time"
1133
" during the process lifetime.\n"
1134
"InnoDB: In the MySQL Embedded Server Library"
1135
" you cannot call server_init()\n"
1136
"InnoDB: more than once during"
1137
" the process lifetime.\n");
1140
srv_start_has_been_called = TRUE;
1143
log_do_write = TRUE;
1144
#endif /* UNIV_DEBUG */
1145
/* yydebug = TRUE; */
1147
srv_is_being_started = TRUE;
1148
srv_startup_is_before_trx_rollback_phase = TRUE;
1151
switch (os_get_os_version()) {
1155
/* On Win 95, 98, ME, Win32 subsystem for Windows 3.1,
1156
and NT use simulated aio. In NT Windows provides async i/o,
1157
but when run in conjunction with InnoDB Hot Backup, it seemed
1158
to corrupt the data files. */
1160
srv_use_native_aio = FALSE;
1165
/* On 2000 and XP, async IO is available. */
1166
srv_use_native_aio = TRUE;
1170
/* Vista and later have both async IO and condition variables */
1171
srv_use_native_aio = TRUE;
1172
srv_use_native_conditions = TRUE;
1176
#elif defined(LINUX_NATIVE_AIO)
1178
if (srv_use_native_aio) {
1179
ut_print_timestamp(stderr);
1181
" InnoDB: Using Linux native AIO\n");
1184
/* Currently native AIO is supported only on windows and linux
1185
and that also when the support is compiled in. In all other
1186
cases, we ignore the setting of innodb_use_native_aio. */
1187
srv_use_native_aio = FALSE;
1191
if (srv_file_flush_method_str == NULL) {
1192
/* These are the default options */
1194
srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1196
srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1198
} else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
1199
srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1201
} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
1202
srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;
1204
} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
1205
srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;
1207
} else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
1208
srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;
1210
} else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
1211
srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
1213
} else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
1214
srv_win_file_flush_method = SRV_WIN_IO_NORMAL;
1215
srv_use_native_aio = FALSE;
1217
} else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) {
1218
srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1219
srv_use_native_aio = FALSE;
1221
} else if (0 == ut_strcmp(srv_file_flush_method_str,
1222
"async_unbuffered")) {
1223
srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1227
"InnoDB: Unrecognized value %s for"
1228
" innodb_flush_method\n",
1229
srv_file_flush_method_str);
1233
/* Note that the call srv_boot() also changes the values of
1234
some variables to the units used by InnoDB internally */
1236
/* Set the maximum number of threads which can wait for a semaphore
1237
inside InnoDB: this is the 'sync wait array' size, as well as the
1238
maximum number of threads that can wait in the 'srv_conc array' for
1239
their time to enter InnoDB. */
1241
if (srv_buf_pool_size >= 1000 * 1024 * 1024) {
1242
/* If buffer pool is less than 1000 MB,
1243
assume fewer threads. Also use only one
1244
buffer pool instance */
1245
srv_max_n_threads = 50000;
1247
} else if (srv_buf_pool_size >= 8 * 1024 * 1024) {
1249
srv_buf_pool_instances = 1;
1250
srv_max_n_threads = 10000;
1252
srv_buf_pool_instances = 1;
1253
srv_max_n_threads = 1000; /* saves several MB of memory,
1254
especially in 64-bit
1260
if (err != DB_SUCCESS) {
1265
mutex_create(srv_monitor_file_mutex_key,
1266
&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
1268
if (srv_innodb_status) {
1269
srv_monitor_file_name = mem_alloc(
1270
strlen(fil_path_to_mysql_datadir)
1271
+ 20 + sizeof "/innodb_status.");
1272
sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
1273
fil_path_to_mysql_datadir, os_proc_get_number());
1274
srv_monitor_file = fopen(srv_monitor_file_name, "w+");
1275
if (!srv_monitor_file) {
1276
fprintf(stderr, "InnoDB: unable to create %s: %s\n",
1277
srv_monitor_file_name, strerror(errno));
1281
srv_monitor_file_name = NULL;
1282
srv_monitor_file = os_file_create_tmpfile();
1283
if (!srv_monitor_file) {
1288
mutex_create(srv_dict_tmpfile_mutex_key,
1289
&srv_dict_tmpfile_mutex, SYNC_DICT_OPERATION);
1291
srv_dict_tmpfile = os_file_create_tmpfile();
1292
if (!srv_dict_tmpfile) {
1296
mutex_create(srv_misc_tmpfile_mutex_key,
1297
&srv_misc_tmpfile_mutex, SYNC_ANY_LATCH);
1299
srv_misc_tmpfile = os_file_create_tmpfile();
1300
if (!srv_misc_tmpfile) {
1304
/* innodb_file_io_threads used to be user settable.
1305
It is now just a combination of read_io_threads and
1306
write_io_threads that is set by innodb internally. */
1308
/* Now overwrite the value on srv_n_file_io_threads */
1309
srv_n_file_io_threads = 2 + srv_n_read_io_threads
1310
+ srv_n_write_io_threads;
1312
ut_a(srv_n_file_io_threads <= SRV_MAX_N_IO_THREADS);
1314
/* TODO: Investigate if SRV_N_PENDING_IOS_PER_THREAD (32) limit
1315
still applies to windows. */
1316
if (!srv_use_native_aio) {
1317
io_limit = 8 * SRV_N_PENDING_IOS_PER_THREAD;
1319
io_limit = SRV_N_PENDING_IOS_PER_THREAD;
1322
os_aio_init(io_limit,
1323
srv_n_read_io_threads,
1324
srv_n_write_io_threads,
1325
SRV_MAX_N_PENDING_SYNC_IOS);
1327
fil_init(srv_file_per_table ? 50000 : 5000,
1328
srv_max_n_open_files);
1330
/* Print time to initialize the buffer pool */
1331
ut_print_timestamp(stderr);
1333
" InnoDB: Initializing buffer pool, size =");
1335
if (srv_buf_pool_size >= 1024 * 1024 * 1024) {
1338
((double) srv_buf_pool_size) / (1024 * 1024 * 1024));
1342
((double) srv_buf_pool_size) / (1024 * 1024));
1345
err = buf_pool_init(srv_buf_pool_size, srv_buf_pool_instances);
1347
ut_print_timestamp(stderr);
1349
" InnoDB: Completed initialization of buffer pool\n");
1351
if (err != DB_SUCCESS) {
1353
"InnoDB: Fatal error: cannot allocate the memory"
1354
" for the buffer pool\n");
1360
/* We have observed deadlocks with a 5MB buffer pool but
1361
the actual lower limit could very well be a little higher. */
1363
if (srv_buf_pool_size <= 5 * 1024 * 1024) {
1365
fprintf(stderr, "InnoDB: Warning: Small buffer pool size "
1366
"(%luM), the flst_validate() debug function "
1367
"can cause a deadlock if the buffer pool fills up.\n",
1368
srv_buf_pool_size / 1024 / 1024);
1375
lock_sys_create(srv_lock_table_size);
1377
/* Create i/o-handler threads: */
1379
for (i = 0; i < srv_n_file_io_threads; i++) {
1382
os_thread_create(io_handler_thread, n + i, thread_ids + i);
1385
#ifdef UNIV_LOG_ARCHIVE
1386
if (0 != ut_strcmp(srv_log_group_home_dirs[0], srv_arch_dir)) {
1388
"InnoDB: Error: you must set the log group"
1389
" home dir in my.cnf the\n"
1390
"InnoDB: same as log arch dir.\n");
1394
#endif /* UNIV_LOG_ARCHIVE */
1396
if (srv_n_log_files * srv_log_file_size >= 262144) {
1398
"InnoDB: Error: combined size of log files"
1399
" must be < 4 GB\n");
1404
sum_of_new_sizes = 0;
1406
for (i = 0; i < srv_n_data_files; i++) {
1408
if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) {
1410
"InnoDB: Error: file size must be < 4 GB"
1411
" with this MySQL binary\n"
1412
"InnoDB: and operating system combination,"
1413
" in some OS's < 2 GB\n");
1418
sum_of_new_sizes += srv_data_file_sizes[i];
1421
if (sum_of_new_sizes < 10485760 / UNIV_PAGE_SIZE) {
1423
"InnoDB: Error: tablespace size must be"
1424
" at least 10 MB\n");
1429
err = open_or_create_data_files(&create_new_db,
1430
#ifdef UNIV_LOG_ARCHIVE
1431
&min_arch_log_no, &max_arch_log_no,
1432
#endif /* UNIV_LOG_ARCHIVE */
1433
&min_flushed_lsn, &max_flushed_lsn,
1435
if (err != DB_SUCCESS) {
1437
"InnoDB: Could not open or create data files.\n"
1438
"InnoDB: If you tried to add new data files,"
1439
" and it failed here,\n"
1440
"InnoDB: you should now edit innodb_data_file_path"
1442
"InnoDB: to what it was, and remove the"
1443
" new ibdata files InnoDB created\n"
1444
"InnoDB: in this failed attempt. InnoDB only wrote"
1445
" those files full of\n"
1446
"InnoDB: zeros, but did not yet use them in any way."
1447
" But be careful: do not\n"
1448
"InnoDB: remove old data files"
1449
" which contain your precious data!\n");
1454
#ifdef UNIV_LOG_ARCHIVE
1455
srv_normalize_path_for_win(srv_arch_dir);
1456
srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
1457
#endif /* UNIV_LOG_ARCHIVE */
1459
for (i = 0; i < srv_n_log_files; i++) {
1460
err = open_or_create_log_file(create_new_db, &log_file_created,
1462
if (err != DB_SUCCESS) {
1467
if (log_file_created) {
1472
if ((log_opened && create_new_db)
1473
|| (log_opened && log_created)) {
1475
"InnoDB: Error: all log files must be"
1476
" created at the same time.\n"
1477
"InnoDB: All log files must be"
1478
" created also in database creation.\n"
1479
"InnoDB: If you want bigger or smaller"
1480
" log files, shut down the\n"
1481
"InnoDB: database and make sure there"
1482
" were no errors in shutdown.\n"
1483
"InnoDB: Then delete the existing log files."
1484
" Edit the .cnf file\n"
1485
"InnoDB: and start the database again.\n");
1491
/* Open all log files and data files in the system tablespace: we
1492
keep them open until database shutdown */
1494
fil_open_log_and_system_tablespace_files();
1496
if (log_created && !create_new_db
1497
#ifdef UNIV_LOG_ARCHIVE
1498
&& !srv_archive_recovery
1499
#endif /* UNIV_LOG_ARCHIVE */
1501
if (max_flushed_lsn != min_flushed_lsn
1502
#ifdef UNIV_LOG_ARCHIVE
1503
|| max_arch_log_no != min_arch_log_no
1504
#endif /* UNIV_LOG_ARCHIVE */
1507
"InnoDB: Cannot initialize created"
1508
" log files because\n"
1509
"InnoDB: data files were not in sync"
1510
" with each other\n"
1511
"InnoDB: or the data files are corrupt.\n");
1516
if (max_flushed_lsn < (ib_uint64_t) 1000) {
1518
"InnoDB: Cannot initialize created"
1519
" log files because\n"
1520
"InnoDB: data files are corrupt,"
1521
" or new data files were\n"
1522
"InnoDB: created when the database"
1523
" was started previous\n"
1524
"InnoDB: time but the database"
1525
" was not shut down\n"
1526
"InnoDB: normally after that.\n");
1531
mutex_enter(&(log_sys->mutex));
1533
#ifdef UNIV_LOG_ARCHIVE
1534
/* Do not + 1 arch_log_no because we do not use log
1536
recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE);
1538
recv_reset_logs(max_flushed_lsn, TRUE);
1539
#endif /* UNIV_LOG_ARCHIVE */
1541
mutex_exit(&(log_sys->mutex));
1544
trx_sys_file_format_init();
1546
if (create_new_db) {
1549
fsp_header_init(0, sum_of_new_sizes, &mtr);
1553
/* To maintain backward compatibility we create only
1554
the first rollback segment before the double write buffer.
1555
All the remaining rollback segments will be created later,
1556
after the double write buffer has been created. */
1561
srv_startup_is_before_trx_rollback_phase = FALSE;
1563
#ifdef UNIV_LOG_ARCHIVE
1564
} else if (srv_archive_recovery) {
1566
"InnoDB: Starting archive"
1567
" recovery from a backup...\n");
1568
err = recv_recovery_from_archive_start(
1569
min_flushed_lsn, srv_archive_recovery_limit_lsn,
1571
if (err != DB_SUCCESS) {
1575
/* Since ibuf init is in dict_boot, and ibuf is needed
1576
in any disk i/o, first call dict_boot */
1580
trx_sys_init_at_db_start();
1582
srv_startup_is_before_trx_rollback_phase = FALSE;
1584
/* Initialize the fsp free limit global variable in the log
1586
fsp_header_get_free_limit();
1588
recv_recovery_from_archive_finish();
1589
#endif /* UNIV_LOG_ARCHIVE */
1592
/* Check if we support the max format that is stamped
1593
on the system tablespace.
1594
Note: We are NOT allowed to make any modifications to
1595
the TRX_SYS_PAGE_NO page before recovery because this
1596
page also contains the max_trx_id etc. important system
1597
variables that are required for recovery. We need to
1598
ensure that we return the system to a state where normal
1599
recovery is guaranteed to work. We do this by
1600
invalidating the buffer cache, this will force the
1601
reread of the page and restoration to its last known
1602
consistent state, this is REQUIRED for the recovery
1604
err = trx_sys_file_format_max_check(
1605
srv_max_file_format_at_startup);
1607
if (err != DB_SUCCESS) {
1611
/* Invalidate the buffer pool to ensure that we reread
1612
the page that we read above, during recovery.
1613
Note that this is not as heavy weight as it seems. At
1614
this point there will be only ONE page in the buf_LRU
1615
and there must be no page in the buf_flush list. */
1616
buf_pool_invalidate();
1618
/* We always try to do a recovery, even if the database had
1619
been shut down normally: this is the normal startup path */
1621
err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
1625
if (err != DB_SUCCESS) {
1630
/* Since the insert buffer init is in dict_boot, and the
1631
insert buffer is needed in any disk i/o, first we call
1632
dict_boot(). Note that trx_sys_init_at_db_start() only needs
1633
to access space 0, and the insert buffer at this stage already
1634
works for space 0. */
1637
trx_sys_init_at_db_start();
1639
/* Initialize the fsp free limit global variable in the log
1641
fsp_header_get_free_limit();
1643
/* recv_recovery_from_checkpoint_finish needs trx lists which
1644
are initialized in trx_sys_init_at_db_start(). */
1646
recv_recovery_from_checkpoint_finish();
1647
if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) {
1648
/* The following call is necessary for the insert
1649
buffer to work with multiple tablespaces. We must
1650
know the mapping between space id's and .ibd file
1653
In a crash recovery, we check that the info in data
1654
dictionary is consistent with what we already know
1655
about space id's from the call of
1656
fil_load_single_table_tablespaces().
1658
In a normal startup, we create the space objects for
1659
every table in the InnoDB data dictionary that has
1662
We also determine the maximum tablespace id used. */
1664
dict_check_tablespaces_and_store_max_id(
1665
recv_needed_recovery);
1668
srv_startup_is_before_trx_rollback_phase = FALSE;
1669
recv_recovery_rollback_active();
1671
/* It is possible that file_format tag has never
1672
been set. In this case we initialize it to minimum
1673
value. Important to note that we can do it ONLY after
1674
we have finished the recovery process so that the
1675
image of TRX_SYS_PAGE_NO is not stale. */
1676
trx_sys_file_format_tag_init();
1679
if (!create_new_db && sum_of_new_sizes > 0) {
1680
/* New data file(s) were added */
1683
fsp_header_inc_size(0, sum_of_new_sizes, &mtr);
1687
/* Immediately write the log record about increased tablespace
1688
size to disk, so that it is durable even if mysqld would crash
1691
log_buffer_flush_to_disk();
1694
#ifdef UNIV_LOG_ARCHIVE
1695
/* Archiving is always off under MySQL */
1696
if (!srv_log_archive_on) {
1697
ut_a(DB_SUCCESS == log_archive_noarchivelog());
1699
mutex_enter(&(log_sys->mutex));
1701
start_archive = FALSE;
1703
if (log_sys->archiving_state == LOG_ARCH_OFF) {
1704
start_archive = TRUE;
1707
mutex_exit(&(log_sys->mutex));
1709
if (start_archive) {
1710
ut_a(DB_SUCCESS == log_archive_archivelog());
1713
#endif /* UNIV_LOG_ARCHIVE */
1715
/* fprintf(stderr, "Max allowed record size %lu\n",
1716
page_get_free_space_of_empty() / 2); */
1718
if (trx_doublewrite == NULL) {
1719
/* Create the doublewrite buffer to a new tablespace */
1721
trx_sys_create_doublewrite_buf();
1724
/* Here the double write buffer has already been created and so
1725
any new rollback segments will be allocated after the double
1726
write buffer. The default segment should already exist.
1727
We create the new segments only if it's a new database or
1728
the database was shutdown cleanly. */
1730
/* Note: When creating the extra rollback segments during an upgrade
1731
we violate the latching order, even if the change buffer is empty.
1732
We make an exception in sync0sync.c and check srv_is_being_started
1733
for that violation. It cannot create a deadlock because we are still
1734
running in single threaded mode essentially. Only the IO threads
1735
should be running at this stage. */
1737
trx_sys_create_rsegs(TRX_SYS_N_RSEGS - 1);
1739
/* Create the thread which watches the timeouts for lock waits */
1740
os_thread_create(&srv_lock_timeout_thread, NULL,
1741
thread_ids + 2 + SRV_MAX_N_IO_THREADS);
1743
/* Create the thread which warns of long semaphore waits */
1744
os_thread_create(&srv_error_monitor_thread, NULL,
1745
thread_ids + 3 + SRV_MAX_N_IO_THREADS);
1747
/* Create the thread which prints InnoDB monitor info */
1748
os_thread_create(&srv_monitor_thread, NULL,
1749
thread_ids + 4 + SRV_MAX_N_IO_THREADS);
1751
srv_is_being_started = FALSE;
1753
err = dict_create_or_check_foreign_constraint_tables();
1755
if (err != DB_SUCCESS) {
1756
return((int)DB_ERROR);
1759
/* Create the master thread which does purge and other utility
1762
os_thread_create(&srv_master_thread, NULL, thread_ids
1763
+ (1 + SRV_MAX_N_IO_THREADS));
1765
/* Currently we allow only a single purge thread. */
1766
ut_a(srv_n_purge_threads == 0 || srv_n_purge_threads == 1);
1768
/* If the user has requested a separate purge thread then
1769
start the purge thread. */
1770
if (srv_n_purge_threads == 1) {
1771
os_thread_create(&srv_purge_thread, NULL, NULL);
1775
/* buf_debug_prints = TRUE; */
1776
#endif /* UNIV_DEBUG */
1777
sum_of_data_file_sizes = 0;
1779
for (i = 0; i < srv_n_data_files; i++) {
1780
sum_of_data_file_sizes += srv_data_file_sizes[i];
1783
tablespace_size_in_header = fsp_header_get_tablespace_size();
1785
if (!srv_auto_extend_last_data_file
1786
&& sum_of_data_file_sizes != tablespace_size_in_header) {
1789
"InnoDB: Error: tablespace size"
1790
" stored in header is %lu pages, but\n"
1791
"InnoDB: the sum of data file sizes is %lu pages\n",
1792
(ulong) tablespace_size_in_header,
1793
(ulong) sum_of_data_file_sizes);
1795
if (srv_force_recovery == 0
1796
&& sum_of_data_file_sizes < tablespace_size_in_header) {
1797
/* This is a fatal error, the tail of a tablespace is
1801
"InnoDB: Cannot start InnoDB."
1802
" The tail of the system tablespace is\n"
1803
"InnoDB: missing. Have you edited"
1804
" innodb_data_file_path in my.cnf in an\n"
1805
"InnoDB: inappropriate way, removing"
1806
" ibdata files from there?\n"
1807
"InnoDB: You can set innodb_force_recovery=1"
1808
" in my.cnf to force\n"
1809
"InnoDB: a startup if you are trying"
1810
" to recover a badly corrupt database.\n");
1816
if (srv_auto_extend_last_data_file
1817
&& sum_of_data_file_sizes < tablespace_size_in_header) {
1820
"InnoDB: Error: tablespace size stored in header"
1821
" is %lu pages, but\n"
1822
"InnoDB: the sum of data file sizes"
1823
" is only %lu pages\n",
1824
(ulong) tablespace_size_in_header,
1825
(ulong) sum_of_data_file_sizes);
1827
if (srv_force_recovery == 0) {
1830
"InnoDB: Cannot start InnoDB. The tail of"
1831
" the system tablespace is\n"
1832
"InnoDB: missing. Have you edited"
1833
" innodb_data_file_path in my.cnf in an\n"
1834
"InnoDB: inappropriate way, removing"
1835
" ibdata files from there?\n"
1836
"InnoDB: You can set innodb_force_recovery=1"
1837
" in my.cnf to force\n"
1838
"InnoDB: a startup if you are trying to"
1839
" recover a badly corrupt database.\n");
1845
/* Check that os_fast_mutexes work as expected */
1846
os_fast_mutex_init(&srv_os_test_mutex);
1848
if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) {
1850
"InnoDB: Error: pthread_mutex_trylock returns"
1851
" an unexpected value on\n"
1852
"InnoDB: success! Cannot continue.\n");
1856
os_fast_mutex_unlock(&srv_os_test_mutex);
1858
os_fast_mutex_lock(&srv_os_test_mutex);
1860
os_fast_mutex_unlock(&srv_os_test_mutex);
1862
os_fast_mutex_free(&srv_os_test_mutex);
1864
if (srv_print_verbose_log) {
1865
ut_print_timestamp(stderr);
1867
" InnoDB %s started; "
1868
"log sequence number %"PRIu64"\n",
1869
INNODB_VERSION_STR, srv_start_lsn);
1872
if (srv_force_recovery > 0) {
1874
"InnoDB: !!! innodb_force_recovery"
1875
" is set to %lu !!!\n",
1876
(ulong) srv_force_recovery);
1881
if (trx_doublewrite_must_reset_space_ids) {
1882
/* Actually, we did not change the undo log format between
1883
4.0 and 4.1.1, and we would not need to run purge to
1884
completion. Note also that the purge algorithm in 4.1.1
1885
can process the the history list again even after a full
1886
purge, because our algorithm does not cut the end of the
1887
history list in all cases so that it would become empty
1888
after a full purge. That mean that we may purge 4.0 type
1889
undo log even after this phase.
1891
The insert buffer record format changed between 4.0 and
1892
4.1.1. It is essential that the insert buffer is emptied
1896
"InnoDB: You are upgrading to an"
1897
" InnoDB version which allows multiple\n"
1898
"InnoDB: tablespaces. Wait that purge"
1899
" and insert buffer merge run to\n"
1900
"InnoDB: completion...\n");
1902
os_thread_sleep(1000000);
1904
if (0 == strcmp(srv_main_thread_op_info,
1905
"waiting for server activity")) {
1907
ut_a(ibuf_is_empty());
1913
"InnoDB: Full purge and insert buffer merge"
1916
trx_sys_mark_upgraded_to_multiple_tablespaces();
1919
"InnoDB: You have now successfully upgraded"
1920
" to the multiple tablespaces\n"
1921
"InnoDB: format. You should NOT DOWNGRADE"
1922
" to an earlier version of\n"
1923
"InnoDB: InnoDB! But if you absolutely need to"
1925
"InnoDB: " REFMAN "multiple-tablespaces.html\n"
1926
"InnoDB: for instructions.\n");
1929
if (srv_force_recovery == 0) {
1930
/* In the insert buffer we may have even bigger tablespace
1931
id's, because we may have dropped those tablespaces, but
1932
insert buffer merge has not had time to clean the records from
1935
ibuf_update_max_tablespace_id();
1938
srv_file_per_table = srv_file_per_table_original_value;
1940
srv_was_started = TRUE;
1942
return((int) DB_SUCCESS);
1945
/****************************************************************//**
1946
Shuts down the InnoDB database.
1947
@return DB_SUCCESS or error code */
1950
innobase_shutdown_for_mysql(void)
1951
/*=============================*/
1954
if (!srv_was_started) {
1955
if (srv_is_being_started) {
1956
ut_print_timestamp(stderr);
1958
" InnoDB: Warning: shutting down"
1959
" a not properly started\n"
1960
"InnoDB: or created database!\n");
1966
/* 1. Flush the buffer pool to disk, write the current lsn to
1967
the tablespace header(s), and copy all log data to archive.
1968
The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
1969
just free data structures after the shutdown. */
1972
if (srv_fast_shutdown == 2) {
1973
ut_print_timestamp(stderr);
1975
" InnoDB: MySQL has requested a very fast shutdown"
1976
" without flushing "
1977
"the InnoDB buffer pool to data files."
1978
" At the next mysqld startup "
1979
"InnoDB will do a crash recovery!\n");
1982
logs_empty_and_mark_files_at_shutdown();
1984
if (srv_conc_n_threads != 0) {
1986
"InnoDB: Warning: query counter shows %ld queries"
1988
"InnoDB: inside InnoDB at shutdown\n",
1989
srv_conc_n_threads);
1992
/* 2. Make all threads created by InnoDB to exit */
1994
srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS;
1996
/* In a 'very fast' shutdown, we do not need to wait for these threads
1997
to die; all which counts is that we flushed the log; a 'very fast'
1998
shutdown is essentially a crash. */
2000
if (srv_fast_shutdown == 2) {
2004
/* All threads end up waiting for certain events. Put those events
2005
to the signaled state. Then the threads will exit themselves in
2006
os_thread_event_wait(). */
2008
for (i = 0; i < 1000; i++) {
2009
/* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM
2012
/* a. Let the lock timeout thread exit */
2013
os_event_set(srv_lock_timeout_thread_event);
2015
/* b. srv error monitor thread exits automatically, no need
2016
to do anything here */
2018
/* c. We wake the master thread so that it exits */
2019
srv_wake_master_thread();
2021
/* d. We wake the purge thread so that it exits */
2022
srv_wake_purge_thread();
2024
/* e. Exit the i/o threads */
2026
os_aio_wake_all_threads_at_shutdown();
2028
os_mutex_enter(os_sync_mutex);
2030
if (os_thread_count == 0) {
2031
/* All the threads have exited or are just exiting;
2032
NOTE that the threads may not have completed their
2033
exit yet. Should we use pthread_join() to make sure
2034
they have exited? If we did, we would have to
2035
remove the pthread_detach() from
2036
os_thread_exit(). Now we just sleep 0.1
2037
seconds and hope that is enough! */
2039
os_mutex_exit(os_sync_mutex);
2041
os_thread_sleep(100000);
2046
os_mutex_exit(os_sync_mutex);
2048
os_thread_sleep(100000);
2053
"InnoDB: Warning: %lu threads created by InnoDB"
2054
" had not exited at shutdown!\n",
2055
(ulong) os_thread_count);
2058
if (srv_monitor_file) {
2059
fclose(srv_monitor_file);
2060
srv_monitor_file = 0;
2061
if (srv_monitor_file_name) {
2062
unlink(srv_monitor_file_name);
2063
mem_free(srv_monitor_file_name);
2066
if (srv_dict_tmpfile) {
2067
fclose(srv_dict_tmpfile);
2068
srv_dict_tmpfile = 0;
2071
if (srv_misc_tmpfile) {
2072
fclose(srv_misc_tmpfile);
2073
srv_misc_tmpfile = 0;
2076
/* This must be disabled before closing the buffer pool
2077
and closing the data dictionary. */
2078
btr_search_disable();
2084
trx_sys_file_format_close();
2087
mutex_free(&srv_monitor_file_mutex);
2088
mutex_free(&srv_dict_tmpfile_mutex);
2089
mutex_free(&srv_misc_tmpfile_mutex);
2091
btr_search_sys_free();
2093
/* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
2100
/* 4. Free the os_conc_mutex and all os_events and os_mutexes */
2104
/* 5. Free all allocated memory */
2108
buf_pool_free(srv_buf_pool_instances);
2111
/* ut_free_all_mem() frees all allocated memory not freed yet
2112
in shutdown, and it will also free the ut_list_mutex, so it
2113
should be the last one for all operation */
2116
if (os_thread_count != 0
2117
|| os_event_count != 0
2118
|| os_mutex_count != 0
2119
|| os_fast_mutex_count != 0) {
2121
"InnoDB: Warning: some resources were not"
2122
" cleaned up in shutdown:\n"
2123
"InnoDB: threads %lu, events %lu,"
2124
" os_mutexes %lu, os_fast_mutexes %lu\n",
2125
(ulong) os_thread_count, (ulong) os_event_count,
2126
(ulong) os_mutex_count, (ulong) os_fast_mutex_count);
2129
if (dict_foreign_err_file) {
2130
fclose(dict_foreign_err_file);
2132
if (lock_latest_err_file) {
2133
fclose(lock_latest_err_file);
2136
if (srv_print_verbose_log) {
2137
ut_print_timestamp(stderr);
2139
" InnoDB: Shutdown completed;"
2140
" log sequence number %"PRIu64"\n",
2144
srv_was_started = FALSE;
2145
srv_start_has_been_called = FALSE;
2147
return((int) DB_SUCCESS);
2149
#endif /* !UNIV_HOTBACKUP */