Commit c0b4d74b authored by Nisha Gopalakrishnan's avatar Nisha Gopalakrishnan

BUG#27216817: INNODB: FAILING ASSERTION:

              PREBUILT->TABLE->N_MYSQL_HANDLES_OPENED == 1

ANALYSIS:
=========

Adding unique index to a InnoDB table which is locked as
mutliple instances may trigger an InnoDB assert.

When we add a primary key or an unique index, we need to
drop the original table and rebuild all indexes. InnoDB
expects that only the instance of the table that is being
rebuilt, is open during the process. In the current
scenario we have opened multiple instances of the table.
This triggers an assert during table rebuild.
'Locked_tables_list' encapsulates a list of all
instances of tables locked by LOCK TABLES statement.

FIX:
===
We are now temporarily closing all the instances of the
table except the one which is being altered and later
reopen them via Locked_tables_list::reopen_tables().
parent 873f8c25
/* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights /* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights
reserved. reserved.
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
...@@ -168,7 +168,7 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list, ...@@ -168,7 +168,7 @@ static int prepare_for_repair(THD *thd, TABLE_LIST *table_list,
*/ */
if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN)) if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
goto end; goto end;
close_all_tables_for_name(thd, table_list->table->s, FALSE); close_all_tables_for_name(thd, table_list->table->s, FALSE, NULL);
table_list->table= 0; table_list->table= 0;
} }
/* /*
......
/* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -1096,7 +1096,7 @@ bool close_cached_tables(THD *thd, TABLE_LIST *tables, ...@@ -1096,7 +1096,7 @@ bool close_cached_tables(THD *thd, TABLE_LIST *tables,
result= TRUE; result= TRUE;
goto err_with_reopen; goto err_with_reopen;
} }
close_all_tables_for_name(thd, table->s, FALSE); close_all_tables_for_name(thd, table->s, FALSE, NULL);
} }
} }
...@@ -1367,13 +1367,16 @@ static void close_open_tables(THD *thd) ...@@ -1367,13 +1367,16 @@ static void close_open_tables(THD *thd)
In that case the documented behaviour is to In that case the documented behaviour is to
implicitly remove the table from LOCK TABLES implicitly remove the table from LOCK TABLES
list. list.
@param[in] skip_table
TABLE instance that should be kept open.
@pre Must be called with an X MDL lock on the table. @pre Must be called with an X MDL lock on the table.
*/ */
void void
close_all_tables_for_name(THD *thd, TABLE_SHARE *share, close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
bool remove_from_locked_tables) bool remove_from_locked_tables,
TABLE *skip_table)
{ {
char key[MAX_DBKEY_LENGTH]; char key[MAX_DBKEY_LENGTH];
uint key_length= share->table_cache_key.length; uint key_length= share->table_cache_key.length;
...@@ -1388,7 +1391,8 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share, ...@@ -1388,7 +1391,8 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
TABLE *table= *prev; TABLE *table= *prev;
if (table->s->table_cache_key.length == key_length && if (table->s->table_cache_key.length == key_length &&
!memcmp(table->s->table_cache_key.str, key, key_length)) !memcmp(table->s->table_cache_key.str, key, key_length) &&
table != skip_table)
{ {
thd->locked_tables_list.unlink_from_list(thd, thd->locked_tables_list.unlink_from_list(thd,
table->pos_in_locked_tables, table->pos_in_locked_tables,
...@@ -1401,7 +1405,8 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share, ...@@ -1401,7 +1405,8 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
mysql_lock_remove(thd, thd->lock, table); mysql_lock_remove(thd, thd->lock, table);
/* Inform handler that table will be dropped after close */ /* Inform handler that table will be dropped after close */
if (table->db_stat) /* Not true for partitioned tables. */ if (table->db_stat && /* Not true for partitioned tables. */
skip_table == NULL)
table->file->extra(HA_EXTRA_PREPARE_FOR_DROP); table->file->extra(HA_EXTRA_PREPARE_FOR_DROP);
close_thread_table(thd, prev); close_thread_table(thd, prev);
} }
...@@ -1411,9 +1416,12 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share, ...@@ -1411,9 +1416,12 @@ close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
prev= &table->next; prev= &table->next;
} }
} }
if (skip_table == NULL) {
/* Remove the table share from the cache. */ /* Remove the table share from the cache. */
tdc_remove_table(thd, TDC_RT_REMOVE_ALL, db, table_name, tdc_remove_table(thd, TDC_RT_REMOVE_ALL, db, table_name,
FALSE); FALSE);
}
} }
......
/* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. /* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -306,7 +306,8 @@ bool close_cached_tables(THD *thd, TABLE_LIST *tables, ...@@ -306,7 +306,8 @@ bool close_cached_tables(THD *thd, TABLE_LIST *tables,
bool wait_for_refresh, ulong timeout); bool wait_for_refresh, ulong timeout);
bool close_cached_connection_tables(THD *thd, LEX_STRING *connect_string); bool close_cached_connection_tables(THD *thd, LEX_STRING *connect_string);
void close_all_tables_for_name(THD *thd, TABLE_SHARE *share, void close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
bool remove_from_locked_tables); bool remove_from_locked_tables,
TABLE *skip_table);
OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild); OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild);
void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type, void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
const char *db, const char *table_name, const char *db, const char *table_name,
......
/* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. /* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -6512,7 +6512,7 @@ static void alter_partition_lock_handling(ALTER_PARTITION_PARAM_TYPE *lpt) ...@@ -6512,7 +6512,7 @@ static void alter_partition_lock_handling(ALTER_PARTITION_PARAM_TYPE *lpt)
THD *thd= lpt->thd; THD *thd= lpt->thd;
if (lpt->old_table) if (lpt->old_table)
close_all_tables_for_name(thd, lpt->old_table->s, FALSE); close_all_tables_for_name(thd, lpt->old_table->s, FALSE, NULL);
if (lpt->table) if (lpt->table)
{ {
/* /*
...@@ -6549,7 +6549,7 @@ static int alter_close_tables(ALTER_PARTITION_PARAM_TYPE *lpt, bool close_old) ...@@ -6549,7 +6549,7 @@ static int alter_close_tables(ALTER_PARTITION_PARAM_TYPE *lpt, bool close_old)
} }
if (close_old && lpt->old_table) if (close_old && lpt->old_table)
{ {
close_all_tables_for_name(lpt->thd, lpt->old_table->s, FALSE); close_all_tables_for_name(lpt->thd, lpt->old_table->s, FALSE, NULL);
lpt->old_table= 0; lpt->old_table= 0;
} }
DBUG_RETURN(0); DBUG_RETURN(0);
......
/* /*
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -2165,7 +2165,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, ...@@ -2165,7 +2165,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
error= -1; error= -1;
goto err; goto err;
} }
close_all_tables_for_name(thd, table->table->s, TRUE); close_all_tables_for_name(thd, table->table->s, TRUE, NULL);
table->table= 0; table->table= 0;
} }
...@@ -6168,7 +6168,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -6168,7 +6168,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
*/ */
if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN)) if (wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN))
goto err; goto err;
close_all_tables_for_name(thd, table->s, TRUE); close_all_tables_for_name(thd, table->s, TRUE, NULL);
/* /*
Then, we want check once again that target table does not exist. Then, we want check once again that target table does not exist.
Actually the order of these two steps does not matter since Actually the order of these two steps does not matter since
...@@ -6305,6 +6305,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -6305,6 +6305,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
changes only" means also that the handler for the table does not changes only" means also that the handler for the table does not
change. The table is open and locked. The handler can be accessed. change. The table is open and locked. The handler can be accessed.
*/ */
if (need_copy_table == ALTER_TABLE_INDEX_CHANGED) if (need_copy_table == ALTER_TABLE_INDEX_CHANGED)
{ {
int pk_changed= 0; int pk_changed= 0;
...@@ -6606,6 +6607,19 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -6606,6 +6607,19 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
thd->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields thd->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
thd->cuted_fields=0L; thd->cuted_fields=0L;
copied=deleted=0; copied=deleted=0;
if (thd->locked_tables_mode == LTM_LOCK_TABLES ||
thd->locked_tables_mode == LTM_PRELOCKED_UNDER_LOCK_TABLES)
{
/*
Temporarily close the TABLE instances belonging to this
thread except the one to be used for ALTER TABLE.
This is mostly needed to satisfy InnoDB assumptions/asserts.
*/
close_all_tables_for_name(thd, table->s, false, table);
}
/* /*
We do not copy data for MERGE tables. Only the children have data. We do not copy data for MERGE tables. Only the children have data.
MERGE tables have HA_NO_COPY_ON_ALTER set. MERGE tables have HA_NO_COPY_ON_ALTER set.
...@@ -6877,7 +6891,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, ...@@ -6877,7 +6891,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
} }
close_all_tables_for_name(thd, table->s, close_all_tables_for_name(thd, table->s,
new_name != table_name || new_db != db); new_name != table_name || new_db != db, NULL);
error=0; error=0;
table_list->table= table= 0; /* Safety */ table_list->table= table= 0; /* Safety */
......
/* /*
Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -568,7 +568,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create) ...@@ -568,7 +568,7 @@ bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
if (result) if (result)
goto end; goto end;
close_all_tables_for_name(thd, table->s, FALSE); close_all_tables_for_name(thd, table->s, FALSE, NULL);
/* /*
Reopen the table if we were under LOCK TABLES. Reopen the table if we were under LOCK TABLES.
Ignore the return value for now. It's better to Ignore the return value for now. It's better to
......
/* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. /* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -394,7 +394,7 @@ bool Truncate_statement::lock_table(THD *thd, TABLE_LIST *table_ref, ...@@ -394,7 +394,7 @@ bool Truncate_statement::lock_table(THD *thd, TABLE_LIST *table_ref,
m_ticket_downgrade= table->mdl_ticket; m_ticket_downgrade= table->mdl_ticket;
/* Close if table is going to be recreated. */ /* Close if table is going to be recreated. */
if (*hton_can_recreate) if (*hton_can_recreate)
close_all_tables_for_name(thd, table->s, FALSE); close_all_tables_for_name(thd, table->s, FALSE, NULL);
} }
else else
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment