From: Jon Olav Hauglid Date: March 4 2011 3:54pm Subject: bzr commit into mysql-5.5 branch (jon.hauglid:3372) Bug#11764779 List-Archive: http://lists.mysql.com/commits/132489 X-Bug: 11764779 Message-Id: <201103041554.p24Ep2BC025153@rcsinet13.oracle.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============8502992753704826646==" --===============8502992753704826646== MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline #At file:///export/home/x/mysql-5.5-bug57649/ based on revid:jorgen.loland@stripped 3372 Jon Olav Hauglid 2011-03-04 Bug #11764779 (former 57649) FLUSH TABLES under FLUSH TABLES WITH READ LOCK leads to assert failure. This assert was triggered if a statement tried up upgrade a metadata lock with an active FLUSH TABLE WITH READ LOCK. The assert checks that the connection already holds a global intention exclusive metadata lock. However, FLUSH TABLE WITH READ LOCK does not acquire this lock in order to be compatible with FLUSH TABLES WITH READ LOCK. Therefore any metadata lock upgrade caused the assert to be triggered. This patch fixes the problem by preventing metadata lock upgrade if the connection has an active FLUSH TABLE WITH READ LOCK. ER_TABLE_NOT_LOCKED_FOR_WRITE will instead be reported to the client. Test case added to flush.test. modified: mysql-test/r/flush.result mysql-test/t/flush.test sql/sql_base.cc sql/sql_base.h sql/sql_reload.cc sql/sql_table.cc sql/sql_trigger.cc sql/sql_truncate.cc === modified file 'mysql-test/r/flush.result' --- a/mysql-test/r/flush.result 2010-11-11 17:11:05 +0000 +++ b/mysql-test/r/flush.result 2011-03-04 15:54:29 +0000 @@ -451,3 +451,18 @@ unlock tables; handler t1 close; # Cleanup. drop tables t1, t2; +# +# Bug#57649 FLUSH TABLES under FLUSH TABLES WITH READ LOCK leads +# to assert failure. +# +DROP TABLE IF EXISTS t1; +CREATE TABLE t1 (a INT); +FLUSH TABLES t1 WITH READ LOCK; +FLUSH TABLES; +ERROR HY000: Table 't1' was locked with a READ lock and can't be updated +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a= 1; +ERROR HY000: Table 't1' was locked with a READ lock and can't be updated +ALTER TABLE t1 COMMENT 'test'; +ERROR HY000: Table 't1' was locked with a READ lock and can't be updated +UNLOCK TABLES; +DROP TABLE t1; === modified file 'mysql-test/t/flush.test' --- a/mysql-test/t/flush.test 2010-11-11 17:11:05 +0000 +++ b/mysql-test/t/flush.test 2011-03-04 15:54:29 +0000 @@ -644,3 +644,27 @@ disconnect con2; --source include/wait_until_disconnected.inc connection default; drop tables t1, t2; + + +--echo # +--echo # Bug#57649 FLUSH TABLES under FLUSH TABLES WITH READ LOCK leads +--echo # to assert failure. +--echo # + +--disable_warnings +DROP TABLE IF EXISTS t1; +--enable_warnings + +CREATE TABLE t1 (a INT); +FLUSH TABLES t1 WITH READ LOCK; + +# All these triggered the assertion +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +FLUSH TABLES; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a= 1; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +ALTER TABLE t1 COMMENT 'test'; + +UNLOCK TABLES; +DROP TABLE t1; === modified file 'sql/sql_base.cc' --- a/sql/sql_base.cc 2011-02-08 15:47:33 +0000 +++ b/sql/sql_base.cc 2011-03-04 15:54:29 +0000 @@ -1026,7 +1026,7 @@ bool close_cached_tables(THD *thd, TABLE table_list= table_list->next_global) { /* A check that the table was locked for write is done by the caller. */ - TABLE *table= find_table_for_mdl_upgrade(thd->open_tables, table_list->db, + TABLE *table= find_table_for_mdl_upgrade(thd, table_list->db, table_list->table_name, TRUE); /* May return NULL if this table has already been closed via an alias. */ @@ -3120,7 +3120,7 @@ TABLE *find_locked_table(TABLE *list, co lock from the list of open tables, emit error if no such table found. - @param list List of TABLE objects to be searched + @param thd Thread context @param db Database name. @param table_name Name of table. @param no_error Don't emit error if no suitable TABLE @@ -3131,11 +3131,10 @@ TABLE *find_locked_table(TABLE *list, co lock, NULL otherwise. */ -TABLE *find_table_for_mdl_upgrade(TABLE *list, const char *db, - const char *table_name, - bool no_error) +TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db, + const char *table_name, bool no_error) { - TABLE *tab= find_locked_table(list, db, table_name); + TABLE *tab= find_locked_table(thd->open_tables, db, table_name); if (!tab) { @@ -3143,19 +3142,28 @@ TABLE *find_table_for_mdl_upgrade(TABLE my_error(ER_TABLE_NOT_LOCKED, MYF(0), table_name); return NULL; } - else + + /* + It is not safe to upgrade the metadata lock without GLOBAL IX lock. + This can happen with FLUSH TABLES WITH READ LOCK as we in these + cases don't take a GLOBAL IX lock in order to be compatible with + global read lock. + */ + if (!thd->mdl_context.is_lock_owner(MDL_key::GLOBAL, "", "", + MDL_INTENTION_EXCLUSIVE)) { - while (tab->mdl_ticket != NULL && - !tab->mdl_ticket->is_upgradable_or_exclusive() && - (tab= find_locked_table(tab->next, db, table_name))) - continue; - if (!tab) - { - if (!no_error) - my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_name); - return 0; - } + my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_name); + return NULL; } + + while (tab->mdl_ticket != NULL && + !tab->mdl_ticket->is_upgradable_or_exclusive() && + (tab= find_locked_table(tab->next, db, table_name))) + continue; + + if (!tab && !no_error) + my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), table_name); + return tab; } @@ -4653,8 +4661,7 @@ open_tables_check_upgradable_mdl(THD *th Note that find_table_for_mdl_upgrade() will report an error if no suitable ticket is found. */ - if (!find_table_for_mdl_upgrade(thd->open_tables, table->db, - table->table_name, FALSE)) + if (!find_table_for_mdl_upgrade(thd, table->db, table->table_name, false)) return TRUE; } } === modified file 'sql/sql_base.h' --- a/sql/sql_base.h 2010-11-11 17:11:05 +0000 +++ b/sql/sql_base.h 2011-03-04 15:54:29 +0000 @@ -1,4 +1,4 @@ -/* Copyright 2006-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc. +/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 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 @@ -290,7 +290,7 @@ bool tdc_open_view(THD *thd, TABLE_LIST char *cache_key, uint cache_key_length, MEM_ROOT *mem_root, uint flags); void tdc_flush_unused_tables(); -TABLE *find_table_for_mdl_upgrade(TABLE *list, const char *db, +TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db, const char *table_name, bool no_error); void mark_tmp_table_for_reuse(TABLE *table); === modified file 'sql/sql_reload.cc' --- a/sql/sql_reload.cc 2010-12-07 16:11:13 +0000 +++ b/sql/sql_reload.cc 2011-03-04 15:54:29 +0000 @@ -1,4 +1,4 @@ -/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 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 @@ -220,12 +220,26 @@ bool reload_acl_and_cache(THD *thd, unsi if (tables) { for (TABLE_LIST *t= tables; t; t= t->next_local) - if (!find_table_for_mdl_upgrade(thd->open_tables, t->db, - t->table_name, FALSE)) + if (!find_table_for_mdl_upgrade(thd, t->db, t->table_name, false)) return 1; } else { + /* + It is not safe to upgrade the metadata lock without GLOBAL IX lock. + This can happen with FLUSH TABLES WITH READ LOCK as we in these + cases don't take a GLOBAL IX lock in order to be compatible with + global read lock. + */ + if (thd->open_tables && + !thd->mdl_context.is_lock_owner(MDL_key::GLOBAL, "", "", + MDL_INTENTION_EXCLUSIVE)) + { + my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), + thd->open_tables->s->table_name.str); + return true; + } + for (TABLE *tab= thd->open_tables; tab; tab= tab->next) { if (! tab->mdl_ticket->is_upgradable_or_exclusive()) === modified file 'sql/sql_table.cc' --- a/sql/sql_table.cc 2011-01-26 13:23:29 +0000 +++ b/sql/sql_table.cc 2011-03-04 15:54:29 +0000 @@ -1917,7 +1917,7 @@ bool mysql_rm_table(THD *thd,TABLE_LIST by parser) it is safe to cache pointer to the TABLE instances in its elements. */ - table->table= find_table_for_mdl_upgrade(thd->open_tables, table->db, + table->table= find_table_for_mdl_upgrade(thd, table->db, table->table_name, false); if (!table->table) DBUG_RETURN(true); === modified file 'sql/sql_trigger.cc' --- a/sql/sql_trigger.cc 2010-11-11 17:11:05 +0000 +++ b/sql/sql_trigger.cc 2011-03-04 15:54:29 +0000 @@ -1,4 +1,4 @@ -/* Copyright (C) 2004-2005 MySQL AB, 2008-2009 Sun Microsystems, Inc +/* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 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 @@ -467,8 +467,7 @@ bool mysql_create_or_drop_trigger(THD *t if (thd->locked_tables_mode) { /* Under LOCK TABLES we must only accept write locked tables. */ - if (!(tables->table= find_table_for_mdl_upgrade(thd->open_tables, - tables->db, + if (!(tables->table= find_table_for_mdl_upgrade(thd, tables->db, tables->table_name, FALSE))) goto end; === modified file 'sql/sql_truncate.cc' --- a/sql/sql_truncate.cc 2010-10-29 14:10:53 +0000 +++ b/sql/sql_truncate.cc 2011-03-04 15:54:29 +0000 @@ -1,4 +1,4 @@ -/* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 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 @@ -327,7 +327,7 @@ bool Truncate_statement::lock_table(THD */ if (thd->locked_tables_mode) { - if (!(table= find_table_for_mdl_upgrade(thd->open_tables, table_ref->db, + if (!(table= find_table_for_mdl_upgrade(thd, table_ref->db, table_ref->table_name, FALSE))) DBUG_RETURN(TRUE); --===============8502992753704826646== MIME-Version: 1.0 Content-Type: text/bzr-bundle; charset="us-ascii"; name="bzr/jon.hauglid@stripped" Content-Transfer-Encoding: 7bit Content-Disposition: inline # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: jon.hauglid@stripped # target_branch: file:///export/home/x/mysql-5.5-bug57649/ # testament_sha1: 3697d0011b112dd7090d66804960d3f5d1324560 # timestamp: 2011-03-04 16:54:32 +0100 # base_revision_id: jorgen.loland@stripped\ # jlybxl8ukw6uj8ib # # Begin bundle IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWVGeDPsACC9/gHfQIAF59/// f//f4L////pgD/0+ZVoWmAAAAAGazQRAKoAaGgoAUNUaAkkJpMhDanpT9FN6mhMMmmoYEPU9CaYg 0yaGhoaBzTEZGTTJoBkNGQyZAAADI0yNAwhkHNMRkZNMmgGQ0ZDJkAAAMjTI0DCGQJT1RBBNJ6R5 T1M0PVPUekaeUB6j0mhkNABoaGg0BzTEZGTTJoBkNGQyZAAADI0yNAwhkCSIEAIAAJoINGkxCgPU PUekPFPU0D1M1PapdDiEjYBCDa/25lVwX5HLtRLctZZdikLJO3bl+J21hB7SC7trvoxfyTb6irQL sMejip66O3PHWLrzES3MkJYzxnnj/oz1BCW85rLYLclcNNns986/MwrjFbL1duCfVc6q/Ne3pLc9 mOMUZMVTf+3G2wLO3WAktTJFuY1VOMc1mz3SaoNMZDRi0ZPHnqpHlClXSM8BAZbHBATBZ2A2DBEx VVCnZ2QowhycWO79NWPechGAvB0ZU0ah+ywHzk94DdDGDAosQLCwZfsEGSwsv1wc6CLmUHkUOUEf cQWsN4Vdq+6ovq7SjemvaLD3uQk5S2mmglU4KJTbcySya2TbKcnGYmE2DFlDq2hMDzD/YyX4Gvoe 8im2lR6ziVr9sMwSPEOyReunXUM8MYiVMfUqcu0smcOHgusaB0LPNLdApLxUtx3tzjwB2j+qii8b 0TSZ13cFr/lMiozZMKRBikiyaiTA3Ctst/fdWn2l5JVn8y46hiuS/UzXg9jM8E3yvtPiBdNYq6IE JzmNAIoIBeV6DgH/RvHGOhERMqAK9uRrluwJ5jGEsyqIEJuTvUoLX+zWnxU0xfosFkadTLqIsM3Q 2Xe1d/hbiH8lGnCHL6JqTKtpjStCHhmBt99lf1L+LCGQM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