diff options
Diffstat (limited to 'innobase')
-rw-r--r-- | innobase/buf/buf0rea.c | 2 | ||||
-rw-r--r-- | innobase/dict/dict0load.c | 30 | ||||
-rw-r--r-- | innobase/fil/fil0fil.c | 93 | ||||
-rw-r--r-- | innobase/include/dict0load.h | 10 | ||||
-rw-r--r-- | innobase/include/fil0fil.h | 28 | ||||
-rw-r--r-- | innobase/include/row0mysql.h | 20 | ||||
-rw-r--r-- | innobase/include/trx0trx.h | 13 | ||||
-rw-r--r-- | innobase/os/os0file.c | 14 | ||||
-rw-r--r-- | innobase/row/row0ins.c | 30 | ||||
-rw-r--r-- | innobase/row/row0mysql.c | 268 | ||||
-rw-r--r-- | innobase/row/row0sel.c | 18 | ||||
-rw-r--r-- | innobase/srv/srv0start.c | 70 | ||||
-rw-r--r-- | innobase/trx/trx0trx.c | 8 |
13 files changed, 467 insertions, 137 deletions
diff --git a/innobase/buf/buf0rea.c b/innobase/buf/buf0rea.c index f34920549fe..1ce52c8ee31 100644 --- a/innobase/buf/buf0rea.c +++ b/innobase/buf/buf0rea.c @@ -332,7 +332,7 @@ buf_read_page( if (err == DB_TABLESPACE_DELETED) { ut_print_timestamp(stderr); fprintf(stderr, -" InnoDB: error: trying to access tablespace %lu page no. %lu,\n" +" InnoDB: Error: trying to access tablespace %lu page no. %lu,\n" "InnoDB: but the tablespace does not exist or is just being dropped.\n", (ulong) space, (ulong) offset); } diff --git a/innobase/dict/dict0load.c b/innobase/dict/dict0load.c index 12ceba38815..f835d1b949a 100644 --- a/innobase/dict/dict0load.c +++ b/innobase/dict/dict0load.c @@ -207,12 +207,14 @@ loop: In a crash recovery we already have all the tablespace objects created. This function compares the space id information in the InnoDB data dictionary to what we already read with fil_load_single_table_tablespaces(). -In a normal startup we just scan the biggest space id, and store it to -fil_system. */ + +In a normal startup, we create the tablespace objects for every table in +InnoDB's data dictionary, if the corresponding .ibd file exists. +We also scan the biggest space id, and store it to fil_system. */ void -dict_check_tablespaces_or_store_max_id( -/*===================================*/ +dict_check_tablespaces_and_store_max_id( +/*====================================*/ ibool in_crash_recovery) /* in: are we doing a crash recovery */ { dict_table_t* sys_tables; @@ -283,6 +285,14 @@ loop: FALSE, TRUE, TRUE); } + if (space_id != 0 && !in_crash_recovery) { + /* It is a normal database startup: create the space + object and check that the .ibd file exists. */ + + fil_open_single_table_tablespace(FALSE, space_id, + name); + } + mem_free(name); if (space_id > max_space_id) { @@ -797,8 +807,18 @@ dict_load_table( /* Ok; (if we did a crash recovery then the tablespace can already be in the memory cache) */ } else { + /* In >= 4.1.9, InnoDB scans the data dictionary also + at a normal mysqld startup. It is an error if the + space object does not exist in memory. */ + + ut_print_timestamp(stderr); + fprintf(stderr, +" InnoDB: error: space object of table %s,\n" +"InnoDB: space id %lu did not exist in memory. Retrying an open.\n", + name, (ulong)space); /* Try to open the tablespace */ - if (!fil_open_single_table_tablespace(space, name)) { + if (!fil_open_single_table_tablespace(TRUE, + space, name)) { /* We failed to find a sensible tablespace file */ diff --git a/innobase/fil/fil0fil.c b/innobase/fil/fil0fil.c index f62f63da538..f2d0790892e 100644 --- a/innobase/fil/fil0fil.c +++ b/innobase/fil/fil0fil.c @@ -469,6 +469,10 @@ fil_node_open_file( ulint size_low; ulint size_high; ibool ret; + byte* buf2; + byte* page; + ibool success; + ulint space_id; #ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(system->mutex))); @@ -497,6 +501,8 @@ fil_node_open_file( system->n_open++; if (node->size == 0) { + ut_a(space->purpose != FIL_LOG); + os_file_get_size(node->handle, &size_low, &size_high); size_bytes = (((ib_longlong)size_high) << 32) @@ -510,6 +516,46 @@ fil_node_open_file( ut_a(space->id != 0); + if (size_bytes < FIL_IBD_FILE_INITIAL_SIZE * UNIV_PAGE_SIZE) { + fprintf(stderr, +"InnoDB: Error: the size of single-table tablespace file %s\n" +"InnoDB: is only %lu %lu, should be at least %lu!", node->name, + (ulong) size_high, + (ulong) size_low, (ulong) (4 * UNIV_PAGE_SIZE)); + + ut_a(0); + } + + /* Read the first page of the tablespace */ + + buf2 = ut_malloc(2 * UNIV_PAGE_SIZE); + /* Align the memory for file i/o if we might have O_DIRECT + set */ + page = ut_align(buf2, UNIV_PAGE_SIZE); + + success = os_file_read(node->handle, page, 0, 0, + UNIV_PAGE_SIZE); + space_id = fsp_header_get_space_id(page); + + ut_free(buf2); + + if (space_id == ULINT_UNDEFINED || space_id == 0) { + fprintf(stderr, +"InnoDB: Error: tablespace id %lu in file %s is not sensible\n", + (ulong) space_id, + node->name); + + ut_a(0); + } + + if (space_id != space->id) { + fprintf(stderr, +"InnoDB: Error: tablespace id is %lu in the data dictionary\n" +"InnoDB: but in file %s it is %lu!\n", space->id, node->name, space_id); + + ut_a(0); + } + if (size_bytes >= FSP_EXTENT_SIZE * UNIV_PAGE_SIZE) { node->size = (ulint) ((size_bytes / (1024 * 1024)) * ((1024 * 1024) / UNIV_PAGE_SIZE)); @@ -2498,21 +2544,29 @@ func_exit: } /************************************************************************ -Tries to open a single-table tablespace and checks the space id is right in -it. If does not succeed, prints an error message to the .err log. This -function is used to open the tablespace when we load a table definition -to the dictionary cache. NOTE that we assume this operation is used under the -protection of the dictionary mutex, so that two users cannot race here. This -operation does not leave the file associated with the tablespace open, but -closes it after we have looked at the space id in it. */ +Tries to open a single-table tablespace and optionally checks the space id is +right in it. If does not succeed, prints an error message to the .err log. This +function is used to open a tablespace when we start up mysqld, and also in +IMPORT TABLESPACE. +NOTE that we assume this operation is used either at the database startup +or under the protection of the dictionary mutex, so that two users cannot +race here. This operation does not leave the file associated with the +tablespace open, but closes it after we have looked at the space id in it. */ ibool fil_open_single_table_tablespace( /*=============================*/ - /* out: TRUE if success */ - ulint id, /* in: space id */ - const char* name) /* in: table name in the - databasename/tablename format */ + /* out: TRUE if success */ + ibool check_space_id, /* in: should we check that the space + id in the file is right; we assume + that this function runs much faster + if no check is made, since accessing + the file inode probably is much + faster (the OS caches them) than + accessing the first page of the file */ + ulint id, /* in: space id */ + const char* name) /* in: table name in the + databasename/tablename format */ { os_file_t file; char* filepath; @@ -2551,6 +2605,12 @@ fil_open_single_table_tablespace( return(FALSE); } + if (!check_space_id) { + space_id = id; + + goto skip_check; + } + /* Read the first page of the tablespace */ buf2 = ut_malloc(2 * UNIV_PAGE_SIZE); @@ -2563,6 +2623,8 @@ fil_open_single_table_tablespace( space_id = fsp_header_get_space_id(page); + ut_free(buf2); + if (space_id != id) { ut_print_timestamp(stderr); @@ -2583,6 +2645,7 @@ fil_open_single_table_tablespace( goto func_exit; } +skip_check: success = fil_space_create(filepath, space_id, FIL_TABLESPACE); if (!success) { @@ -2595,7 +2658,6 @@ fil_open_single_table_tablespace( fil_node_create(filepath, 0, space_id, FALSE); func_exit: os_file_close(file); - ut_free(buf2); mem_free(filepath); return(ret); @@ -2662,7 +2724,7 @@ fil_load_single_table_tablespace( fprintf(stderr, "InnoDB: Error: could not open single-table tablespace file\n" "InnoDB: %s!\n" -"InnoDB: We do not continue crash recovery, because the table will become\n" +"InnoDB: We do not continue the crash recovery, because the table may become\n" "InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.\n" "InnoDB: To fix the problem and start mysqld:\n" "InnoDB: 1) If there is a permission problem in the file and mysqld cannot\n" @@ -2833,8 +2895,9 @@ fil_load_single_table_tablespace( goto func_exit; } - /* We do not measure the size of the file, that is why we pass the 0 - below */ + /* We do not use the size information we have about the file, because + the rounding formula for extents and pages is somewhat complex; we + let fil_node_open() do that task. */ fil_node_create(filepath, 0, space_id, FALSE); func_exit: diff --git a/innobase/include/dict0load.h b/innobase/include/dict0load.h index d4dccb33373..1f0a5407140 100644 --- a/innobase/include/dict0load.h +++ b/innobase/include/dict0load.h @@ -18,12 +18,14 @@ Created 4/24/1996 Heikki Tuuri In a crash recovery we already have all the tablespace objects created. This function compares the space id information in the InnoDB data dictionary to what we already read with fil_load_single_table_tablespaces(). -In a normal startup we just scan the biggest space id, and store it to -fil_system. */ + +In a normal startup, we create the tablespace objects for every table in +InnoDB's data dictionary, if the corresponding .ibd file exists. +We also scan the biggest space id, and store it to fil_system. */ void -dict_check_tablespaces_or_store_max_id( -/*===================================*/ +dict_check_tablespaces_and_store_max_id( +/*====================================*/ ibool in_crash_recovery); /* in: are we doing a crash recovery */ /************************************************************************ Finds the first table name in the given database. */ diff --git a/innobase/include/fil0fil.h b/innobase/include/fil0fil.h index 75b32937e0b..aa1ec5c25a5 100644 --- a/innobase/include/fil0fil.h +++ b/innobase/include/fil0fil.h @@ -366,19 +366,29 @@ fil_create_new_single_table_tablespace( tablespace file in pages, must be >= FIL_IBD_FILE_INITIAL_SIZE */ /************************************************************************ -Tries to open a single-table tablespace and checks the space id is right in -it. If does not succeed, prints an error message to the .err log. This -function is used to open the tablespace when we load a table definition -to the dictionary cache. NOTE that we assume this operation is used under the -protection of the dictionary mutex, so that two users cannot race here. */ +Tries to open a single-table tablespace and optionally checks the space id is +right in it. If does not succeed, prints an error message to the .err log. This +function is used to open a tablespace when we start up mysqld, and also in +IMPORT TABLESPACE. +NOTE that we assume this operation is used either at the database startup +or under the protection of the dictionary mutex, so that two users cannot +race here. This operation does not leave the file associated with the +tablespace open, but closes it after we have looked at the space id in it. */ ibool fil_open_single_table_tablespace( /*=============================*/ - /* out: TRUE if success */ - ulint id, /* in: space id */ - const char* name); /* in: table name in the - databasename/tablename format */ + /* out: TRUE if success */ + ibool check_space_id, /* in: should we check that the space + id in the file is right; we assume + that this function runs much faster + if no check is made, since accessing + the file inode probably is much + faster (the OS caches them) than + accessing the first page of the file */ + ulint id, /* in: space id */ + const char* name); /* in: table name in the + databasename/tablename format */ /************************************************************************ It is possible, though very improbable, that the lsn's in the tablespace to be imported have risen above the current system lsn, if a lengthy purge, ibuf diff --git a/innobase/include/row0mysql.h b/innobase/include/row0mysql.h index c23a0e025ad..bd5ad3adba5 100644 --- a/innobase/include/row0mysql.h +++ b/innobase/include/row0mysql.h @@ -378,25 +378,7 @@ row_drop_table_for_mysql( /************************************************************************* Discards the tablespace of a table which stored in an .ibd file. Discarding means that this function deletes the .ibd file and assigns a new table id for -the table. Also the flag table->ibd_file_missing is set TRUE. - -How do we prevent crashes caused by ongoing operations on the table? Old -operations could try to access non-existent pages. - -1) SQL queries, INSERT, SELECT, ...: we must get an exclusive MySQL table lock -on the table before we can do DISCARD TABLESPACE. Then there are no running -queries on the table. -2) Purge and rollback: we assign a new table id for the table. Since purge and -rollback look for the table based on the table id, they see the table as -'dropped' and discard their operations. -3) Insert buffer: we remove all entries for the tablespace in the insert -buffer tree; as long as the tablespace mem object does not exist, ongoing -insert buffer page merges are discarded in buf0rea.c. If we recreate the -tablespace mem object with IMPORT TABLESPACE later, then the tablespace will -have the same id, but the tablespace_version field in the mem object is -different, and ongoing old insert buffer page merges get discarded. -4) Linear readahead and random readahead: we use the same method as in 3) to -discard ongoing operations. */ +the table. Also the flag table->ibd_file_missing is set TRUE. */ int row_discard_tablespace_for_mysql( diff --git a/innobase/include/trx0trx.h b/innobase/include/trx0trx.h index e305226f9e3..76b051105de 100644 --- a/innobase/include/trx0trx.h +++ b/innobase/include/trx0trx.h @@ -414,6 +414,19 @@ struct trx_struct{ replication slave, this is the position in the log file up to which replication has processed */ + /* A MySQL variable mysql_thd->synchronous_repl tells if we have + to use synchronous replication. See ha_innodb.cc. */ + char* repl_wait_binlog_name;/* NULL, or if synchronous MySQL + replication is used, the binlog name + up to which we must communicate the + binlog to the slave, before returning + from a commit; this is the same as + mysql_log_file_name, but we allocate + and copy the name to a separate buffer + here */ + ib_longlong repl_wait_binlog_pos;/* see above at + repl_wait_binlog_name */ + os_thread_id_t mysql_thread_id;/* id of the MySQL thread associated with this transaction object */ ulint mysql_process_no;/* since in Linux, 'top' reports diff --git a/innobase/os/os0file.c b/innobase/os/os0file.c index 434f9460bb6..e0d822b016f 100644 --- a/innobase/os/os0file.c +++ b/innobase/os/os0file.c @@ -1854,6 +1854,7 @@ os_file_pread( return(n_bytes); #else { + off_t ret_offset; ssize_t ret; ulint i; @@ -1862,12 +1863,12 @@ os_file_pread( os_mutex_enter(os_file_seek_mutexes[i]); - ret = lseek(file, offs, 0); + ret_offset = lseek(file, offs, SEEK_SET); - if (ret < 0) { + if (ret_offset < 0) { os_mutex_exit(os_file_seek_mutexes[i]); - return(ret); + return(-1); } ret = read(file, buf, (ssize_t)n); @@ -1940,6 +1941,7 @@ os_file_pwrite( return(ret); #else { + off_t ret_offset; ulint i; /* Protect the seek / write operation with a mutex */ @@ -1947,12 +1949,12 @@ os_file_pwrite( os_mutex_enter(os_file_seek_mutexes[i]); - ret = lseek(file, offs, 0); + ret_offset = lseek(file, offs, SEEK_SET); - if (ret < 0) { + if (ret_offset < 0) { os_mutex_exit(os_file_seek_mutexes[i]); - return(ret); + return(-1); } ret = write(file, buf, (ssize_t)n); diff --git a/innobase/row/row0ins.c b/innobase/row/row0ins.c index 8b19f9d9a11..3c190915732 100644 --- a/innobase/row/row0ins.c +++ b/innobase/row/row0ins.c @@ -1187,7 +1187,7 @@ run_again: check_index = foreign->foreign_index; } - if (check_table == NULL) { + if (check_table == NULL || check_table->ibd_file_missing) { if (check_ref) { FILE* ef = dict_foreign_err_file; mutex_enter(&dict_foreign_err_mutex); @@ -1206,7 +1206,7 @@ run_again: dtuple_print(ef, entry); fputs("\nBut the parent table ", ef); ut_print_name(ef, trx, foreign->referenced_table_name); - fputs(" does not currently exist!\n", ef); + fputs("\nor its .ibd file does not currently exist!\n", ef); mutex_exit(&dict_foreign_err_mutex); err = DB_NO_REFERENCED_ROW; @@ -1437,8 +1437,34 @@ row_ins_check_foreign_constraints( row_mysql_freeze_data_dictionary(trx); } + if (foreign->referenced_table) { + mutex_enter(&(dict_sys->mutex)); + + (foreign->referenced_table + ->n_foreign_key_checks_running)++; + + mutex_exit(&(dict_sys->mutex)); + } + + /* NOTE that if the thread ends up waiting for a lock + we will release dict_operation_lock temporarily! + But the counter on the table protects the referenced + table from being dropped while the check is running. */ + err = row_ins_check_foreign_constraint(TRUE, foreign, table, entry, thr); + + if (foreign->referenced_table) { + mutex_enter(&(dict_sys->mutex)); + + ut_a(foreign->referenced_table + ->n_foreign_key_checks_running > 0); + (foreign->referenced_table + ->n_foreign_key_checks_running)--; + + mutex_exit(&(dict_sys->mutex)); + } + if (got_s_lock) { row_mysql_unfreeze_data_dictionary(trx); } diff --git a/innobase/row/row0mysql.c b/innobase/row/row0mysql.c index 0a2e9e5eaad..380fcf236ed 100644 --- a/innobase/row/row0mysql.c +++ b/innobase/row/row0mysql.c @@ -875,7 +875,21 @@ row_insert_for_mysql( ut_ad(trx); ut_ad(trx->mysql_thread_id == os_thread_get_curr_id()); - + + if (prebuilt->table->ibd_file_missing) { + ut_print_timestamp(stderr); + fprintf(stderr, " InnoDB: Error:\n" +"InnoDB: MySQL is trying to use a table handle but the .ibd file for\n" +"InnoDB: table %s does not exist.\n" +"InnoDB: Have you deleted the .ibd file from the database directory under\n" +"InnoDB: the MySQL datadir, or have you used DISCARD TABLESPACE?\n" +"InnoDB: Look from\n" +"http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n" +"InnoDB: how you can resolve the problem.\n", + prebuilt->table->name); + return(DB_ERROR); + } + if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) { fprintf(stderr, "InnoDB: Error: trying to free a corrupt\n" @@ -1094,6 +1108,20 @@ row_update_for_mysql( ut_ad(trx->mysql_thread_id == os_thread_get_curr_id()); UT_NOT_USED(mysql_rec); + if (prebuilt->table->ibd_file_missing) { + ut_print_timestamp(stderr); + fprintf(stderr, " InnoDB: Error:\n" +"InnoDB: MySQL is trying to use a table handle but the .ibd file for\n" +"InnoDB: table %s does not exist.\n" +"InnoDB: Have you deleted the .ibd file from the database directory under\n" +"InnoDB: the MySQL datadir, or have you used DISCARD TABLESPACE?\n" +"InnoDB: Look from\n" +"http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n" +"InnoDB: how you can resolve the problem.\n", + prebuilt->table->name); + return(DB_ERROR); + } + if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) { fprintf(stderr, "InnoDB: Error: trying to free a corrupt\n" @@ -1865,20 +1893,19 @@ row_drop_table_for_mysql_in_background( trx = trx_allocate_for_background(); + /* If the original transaction was dropping a table referenced by + foreign keys, we must set the following to be able to drop the + table: */ + + trx->check_foreigns = FALSE; + /* fputs("InnoDB: Error: Dropping table ", stderr); ut_print_name(stderr, name); fputs(" in background drop list\n", stderr); */ - /* Drop the table in InnoDB */ + /* Try to drop the table in InnoDB */ error = row_drop_table_for_mysql(name, trx, FALSE); - - if (error != DB_SUCCESS) { - ut_print_timestamp(stderr); - fputs(" InnoDB: Error: Dropping table ", stderr); - ut_print_name(stderr, trx, name); - fputs(" in background drop list failed\n", stderr); - } /* Flush the log to reduce probability that the .frm files and the InnoDB data dictionary get out-of-sync if the user runs @@ -1890,7 +1917,7 @@ row_drop_table_for_mysql_in_background( trx_free_for_background(trx); - return(DB_SUCCESS); + return(error); } /************************************************************************* @@ -1924,6 +1951,7 @@ loop: mutex_exit(&kernel_mutex); if (drop == NULL) { + /* All tables dropped */ return(n_tables + n_tables_dropped); } @@ -1938,16 +1966,16 @@ loop: goto already_dropped; } - - if (table->n_mysql_handles_opened > 0 - || table->n_foreign_key_checks_running > 0) { + + if (DB_SUCCESS != row_drop_table_for_mysql_in_background( + drop->table_name)) { + /* If the DROP fails for some table, we return, and let the + main thread retry later */ return(n_tables + n_tables_dropped); } n_tables_dropped++; - - row_drop_table_for_mysql_in_background(drop->table_name); already_dropped: mutex_enter(&kernel_mutex); @@ -1991,21 +2019,21 @@ row_get_background_drop_list_len_low(void) } /************************************************************************* -Adds a table to the list of tables which the master thread drops in -background. We need this on Unix because in ALTER TABLE MySQL may call -drop table even if the table has running queries on it. */ +If a table is not yet in the drop list, adds the table to the list of tables +which the master thread drops in background. We need this on Unix because in +ALTER TABLE MySQL may call drop table even if the table has running queries on +it. Also, if there are running foreign key checks on the table, we drop the +table lazily. */ static -void +ibool row_add_table_to_background_drop_list( /*==================================*/ + /* out: TRUE if the table was not yet in the + drop list, and was added there */ dict_table_t* table) /* in: table */ { row_mysql_drop_t* drop; - drop = mem_alloc(sizeof(row_mysql_drop_t)); - - drop->table_name = mem_strdup(table->name); - mutex_enter(&kernel_mutex); if (!row_mysql_drop_list_inited) { @@ -2013,7 +2041,26 @@ row_add_table_to_background_drop_list( UT_LIST_INIT(row_mysql_drop_list); row_mysql_drop_list_inited = TRUE; } + + /* Look if the table already is in the drop list */ + drop = UT_LIST_GET_FIRST(row_mysql_drop_list); + + while (drop != NULL) { + if (strcmp(drop->table_name, table->name) == 0) { + /* Already in the list */ + + mutex_exit(&kernel_mutex); + + return(FALSE); + } + + drop = UT_LIST_GET_NEXT(row_mysql_drop_list, drop); + } + + drop = mem_alloc(sizeof(row_mysql_drop_t)); + drop->table_name = mem_strdup(table->name); + UT_LIST_ADD_LAST(row_mysql_drop_list, row_mysql_drop_list, drop); /* fputs("InnoDB: Adding table ", stderr); @@ -2021,14 +2068,32 @@ row_add_table_to_background_drop_list( fputs(" to background drop list\n", stderr); */ mutex_exit(&kernel_mutex); + + return(TRUE); } /************************************************************************* Discards the tablespace of a table which stored in an .ibd file. Discarding means that this function deletes the .ibd file and assigns a new table id for -the table. Also the flag table->ibd_file_missing is set TRUE. +the table. Also the flag table->ibd_file_missing is set TRUE. */ -How do we prevent crashes caused by ongoing operations on the table? Old +int +row_discard_tablespace_for_mysql( +/*=============================*/ + /* out: error code or DB_SUCCESS */ + const char* name, /* in: table name */ + trx_t* trx) /* in: transaction handle */ +{ + dict_foreign_t* foreign; + dulint new_id; + dict_table_t* table; + que_thr_t* thr; + que_t* graph = NULL; + ibool success; + ulint err; + char* buf; + +/* How do we prevent crashes caused by ongoing operations on the table? Old operations could try to access non-existent pages. 1) SQL queries, INSERT, SELECT, ...: we must get an exclusive MySQL table lock @@ -2044,22 +2109,9 @@ tablespace mem object with IMPORT TABLESPACE later, then the tablespace will have the same id, but the tablespace_version field in the mem object is different, and ongoing old insert buffer page merges get discarded. 4) Linear readahead and random readahead: we use the same method as in 3) to -discard ongoing operations. */ - -int -row_discard_tablespace_for_mysql( -/*=============================*/ - /* out: error code or DB_SUCCESS */ - const char* name, /* in: table name */ - trx_t* trx) /* in: transaction handle */ -{ - dulint new_id; - dict_table_t* table; - que_thr_t* thr; - que_t* graph = NULL; - ibool success; - ulint err; - char* buf; +discard ongoing operations. +5) FOREIGN KEY operations: if table->n_foreign_key_checks_running > 0, we +do not allow the discard. We also reserve the data dictionary latch. */ static const char discard_tablespace_proc1[] = "PROCEDURE DISCARD_TABLESPACE_PROC () IS\n" @@ -2120,6 +2172,54 @@ row_discard_tablespace_for_mysql( goto funct_exit; } + if (table->n_foreign_key_checks_running > 0) { + + ut_print_timestamp(stderr); + fputs(" InnoDB: You are trying to DISCARD table ", stderr); + ut_print_name(stderr, trx, table->name); + fputs("\n" + "InnoDB: though there is a foreign key check running on it.\n" + "InnoDB: Cannot discard the table.\n", + stderr); + + err = DB_ERROR; + + goto funct_exit; + } + + /* Check if the table is referenced by foreign key constraints from + some other table (not the table itself) */ + + foreign = UT_LIST_GET_FIRST(table->referenced_list); + + while (foreign && foreign->foreign_table == table) { + foreign = UT_LIST_GET_NEXT(referenced_list, foreign); + } + + if (foreign && trx->check_foreigns) { + + FILE* ef = dict_foreign_err_file; + + /* We only allow discarding a referenced table if + FOREIGN_KEY_CHECKS is set to 0 */ + + err = DB_CANNOT_DROP_CONSTRAINT; + + mutex_enter(&dict_foreign_err_mutex); + rewind(ef); + ut_print_timestamp(ef); + + fputs(" Cannot DISCARD table ", ef); + ut_print_name(ef, trx, name); + fputs("\n" + "because it is referenced by ", ef); + ut_print_name(ef, trx, foreign->foreign_table_name); + putc('\n', ef); + mutex_exit(&dict_foreign_err_mutex); + + goto funct_exit; + } + new_id = dict_hdr_get_new_id(DICT_HDR_TABLE_ID); buf = mem_alloc((sizeof discard_tablespace_proc1) + @@ -2137,6 +2237,10 @@ row_discard_tablespace_for_mysql( ut_a(graph); + /* Remove any locks there are on the table or its records */ + + lock_reset_all_on_table(table); + graph->trx = trx; trx->graph = NULL; @@ -2287,8 +2391,8 @@ row_import_tablespace_for_mysql( ibuf_delete_for_discarded_space(table->space); - success = fil_open_single_table_tablespace(table->space, table->name); - + success = fil_open_single_table_tablespace(TRUE, table->space, + table->name); if (success) { table->ibd_file_missing = FALSE; table->tablespace_discarded = FALSE; @@ -2296,7 +2400,7 @@ row_import_tablespace_for_mysql( if (table->ibd_file_missing) { ut_print_timestamp(stderr); fputs( -" InnoDB: cannot find of open in the database directory the .ibd file of\n" +" InnoDB: cannot find or open in the database directory the .ibd file of\n" "InnoDB: table ", stderr); ut_print_name(stderr, trx, name); fputs("\n" @@ -2318,7 +2422,7 @@ funct_exit: } /************************************************************************* -Drops a table for MySQL. If the name of the table to be dropped is equal +Drops a table for MySQL. If the name of the table to be dropped is equal with one of the predefined magic table names, then this also stops printing the corresponding monitor output by the master thread. */ @@ -2552,36 +2656,60 @@ row_drop_table_for_mysql( } if (table->n_mysql_handles_opened > 0) { + ibool added; - ut_print_timestamp(stderr); - fputs(" InnoDB: Warning: MySQL is trying to drop table ", - stderr); - ut_print_name(stderr, trx, table->name); - fputs("\n" - "InnoDB: though there are still open handles to it.\n" - "InnoDB: Adding the table to the background drop queue.\n", - stderr); + added = row_add_table_to_background_drop_list(table); - row_add_table_to_background_drop_list(table); + if (added) { + ut_print_timestamp(stderr); +fputs(" InnoDB: Warning: MySQL is trying to drop table ", stderr); + ut_print_name(stderr, trx, table->name); + fputs("\n" +"InnoDB: though there are still open handles to it.\n" +"InnoDB: Adding the table to the background drop queue.\n", + stderr); + + /* We return DB_SUCCESS to MySQL though the drop will + happen lazily later */ - err = DB_SUCCESS; + err = DB_SUCCESS; + } else { + /* The table is already in the background drop list */ + err = DB_ERROR; + } goto funct_exit; } + /* TODO: could we replace the counter n_foreign_key_checks_running + with lock checks on the table? Acquire here an exclusive lock on the + table, and rewrite lock0lock.c and the lock wait in srv0srv.c so that + they can cope with the table having been dropped here? Foreign key + checks take an IS or IX lock on the table. */ + if (table->n_foreign_key_checks_running > 0) { - ut_print_timestamp(stderr); - fputs(" InnoDB: You are trying to drop table ", stderr); - ut_print_name(stderr, trx, table->name); - fputs("\n" - "InnoDB: though there is a foreign key check running on it.\n" - "InnoDB: Adding the table to the background drop queue.\n", + ibool added; + + added = row_add_table_to_background_drop_list(table); + + if (added) { + ut_print_timestamp(stderr); +fputs(" InnoDB: You are trying to drop table ", stderr); + ut_print_name(stderr, trx, table->name); + fputs("\n" +"InnoDB: though there is a foreign key check running on it.\n" +"InnoDB: Adding the table to the background drop queue.\n", stderr); - row_add_table_to_background_drop_list(table); + /* We return DB_SUCCESS to MySQL though the drop will + happen lazily later */ - err = DB_SUCCESS; + err = DB_SUCCESS; + } else { + /* The table is already in the background drop list */ + err = DB_ERROR; + } goto funct_exit; } @@ -3349,6 +3477,20 @@ row_check_table_for_mysql( ulint ret = DB_SUCCESS; ulint old_isolation_level; + if (prebuilt->table->ibd_file_missing) { + ut_print_timestamp(stderr); + fprintf(stderr, " InnoDB: Error:\n" +"InnoDB: MySQL is trying to use a table handle but the .ibd file for\n" +"InnoDB: table %s does not exist.\n" +"InnoDB: Have you deleted the .ibd file from the database directory under\n" +"InnoDB: the MySQL datadir, or have you used DISCARD TABLESPACE?\n" +"InnoDB: Look from\n" +"http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n" +"InnoDB: how you can resolve the problem.\n", + prebuilt->table->name); + return(DB_ERROR); + } + prebuilt->trx->op_info = "checking table"; old_isolation_level = prebuilt->trx->isolation_level; diff --git a/innobase/row/row0sel.c b/innobase/row/row0sel.c index 9104d6e1f4f..6cd265f23e3 100644 --- a/innobase/row/row0sel.c +++ b/innobase/row/row0sel.c @@ -2897,7 +2897,7 @@ row_search_for_mysql( /* out: DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK, - DB_LOCK_TABLE_FULL, + DB_LOCK_TABLE_FULL, DB_CORRUPTION, or DB_TOO_BIG_RECORD */ byte* buf, /* in/out: buffer for the fetched row in the MySQL format */ @@ -2952,7 +2952,21 @@ row_search_for_mysql( ut_ad(index && pcur && search_tuple); ut_ad(trx->mysql_thread_id == os_thread_get_curr_id()); - + + if (prebuilt->table->ibd_file_missing) { + ut_print_timestamp(stderr); + fprintf(stderr, " InnoDB: Error:\n" +"InnoDB: MySQL is trying to use a table handle but the .ibd file for\n" +"InnoDB: table %s does not exist.\n" +"InnoDB: Have you deleted the .ibd file from the database directory under\n" +"InnoDB: the MySQL datadir, or have you used DISCARD TABLESPACE?\n" +"InnoDB: Look from\n" +"http://dev.mysql.com/doc/mysql/en/InnoDB_troubleshooting_datadict.html\n" +"InnoDB: how you can resolve the problem.\n", + prebuilt->table->name); + return(DB_ERROR); + } + if (prebuilt->magic_n != ROW_PREBUILT_ALLOCATED) { fprintf(stderr, "InnoDB: Error: trying to free a corrupt\n" diff --git a/innobase/srv/srv0start.c b/innobase/srv/srv0start.c index a0e763d7a44..061bdb82d3e 100644 --- a/innobase/srv/srv0start.c +++ b/innobase/srv/srv0start.c @@ -1378,14 +1378,39 @@ NetWare. */ return(DB_ERROR); } - /* Since ibuf init is in dict_boot, and ibuf is needed - in any disk i/o, first call dict_boot */ + /* Since the insert buffer init is in dict_boot, and the + insert buffer is needed in any disk i/o, first we call + dict_boot(). Note that trx_sys_init_at_db_start() only needs + to access space 0, and the insert buffer at this stage already + works for space 0. */ dict_boot(); trx_sys_init_at_db_start(); - /* The following needs trx lists which are initialized in - trx_sys_init_at_db_start */ + if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) { + /* The following call is necessary for the insert + buffer to work with multiple tablespaces. We must + know the mapping between space id's and .ibd file + names. + + In a crash recovery, we check that the info in data + dictionary is consistent with what we already know + about space id's from the call of + fil_load_single_table_tablespaces(). + + In a normal startup, we create the space objects for + every table in the InnoDB data dictionary that has + an .ibd file. + + We also determine the maximum tablespace id used. + + TODO: We may have incomplete transactions in the + data dictionary tables. Does that harm the scanning of + the data dictionary below? */ + + dict_check_tablespaces_and_store_max_id( + recv_needed_recovery); + } srv_startup_is_before_trx_rollback_phase = FALSE; @@ -1393,6 +1418,9 @@ NetWare. */ system */ fsp_header_get_free_limit(0); + /* recv_recovery_from_checkpoint_finish needs trx lists which + are initialized in trx_sys_init_at_db_start(). */ + recv_recovery_from_checkpoint_finish(); } @@ -1431,13 +1459,6 @@ NetWare. */ } } #endif /* UNIV_LOG_ARCHIVE */ - if (!create_new_db && srv_force_recovery == 0) { - /* After a crash recovery we only check that the info in data - dictionary is consistent with what we already know about space - id's from the call of fil_load_single_table_tablespaces(). */ - - dict_check_tablespaces_or_store_max_id(recv_needed_recovery); - } if (srv_measure_contention) { /* os_thread_create(&test_measure_cont, NULL, thread_ids + @@ -1505,6 +1526,21 @@ NetWare. */ "InnoDB: the sum of data file sizes is %lu pages\n", (ulong) tablespace_size_in_header, (ulong) sum_of_data_file_sizes); + + if (srv_force_recovery == 0 + && sum_of_data_file_sizes < tablespace_size_in_header) { + /* This is a fatal error, the tail of a tablespace is + missing */ + + fprintf(stderr, +"InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n" +"InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n" +"InnoDB: inappropriate way, removing ibdata files from there?\n" +"InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n" +"InnoDB: a startup if you are trying to recover a badly corrupt database.\n"); + + return(DB_ERROR); + } } if (srv_auto_extend_last_data_file @@ -1515,6 +1551,18 @@ NetWare. */ "InnoDB: the sum of data file sizes is only %lu pages\n", (ulong) tablespace_size_in_header, (ulong) sum_of_data_file_sizes); + + if (srv_force_recovery == 0) { + + fprintf(stderr, +"InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n" +"InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n" +"InnoDB: inappropriate way, removing ibdata files from there?\n" +"InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n" +"InnoDB: a startup if you are trying to recover a badly corrupt database.\n"); + + return(DB_ERROR); + } } /* Check that os_fast_mutexes work as expected */ diff --git a/innobase/trx/trx0trx.c b/innobase/trx/trx0trx.c index f676c21934b..6619286ee71 100644 --- a/innobase/trx/trx0trx.c +++ b/innobase/trx/trx0trx.c @@ -110,6 +110,9 @@ trx_create( trx->mysql_log_offset = 0; trx->mysql_master_log_file_name = ""; trx->mysql_master_log_pos = 0; + + trx->repl_wait_binlog_name = NULL; + trx->repl_wait_binlog_pos = 0; mutex_create(&(trx->undo_mutex)); mutex_set_level(&(trx->undo_mutex), SYNC_TRX_UNDO); @@ -277,6 +280,11 @@ trx_free( trx_undo_arr_free(trx->undo_no_arr); } + if (trx->repl_wait_binlog_name != NULL) { + + mem_free(trx->repl_wait_binlog_name); + } + ut_a(UT_LIST_GET_LEN(trx->signals) == 0); ut_a(UT_LIST_GET_LEN(trx->reply_signals) == 0); |