MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Jon Olav Hauglid Date:September 22 2009 8:31am
Subject:bzr commit into mysql-6.0-bugfixing branch (jon.hauglid:2830) Bug#46654
View as plain text  
#At file:///export/home/z/mysql-6.0-codebase-bugfixing-bug46654/ based on revid:tor.didriksen@stripped

 2830 Jon Olav Hauglid	2009-09-22
      Bug #46654 False deadlock on concurrent DML/DDL with partitions, 
                 inconsistent behavior
      
      The problem was that if one connection is running a multi-statement 
      transaction which involves a single partitioned table, and another 
      connection attempts to alter the table, the first connection gets 
      ER_LOCK_DEADLOCK and cannot proceed anymore, even when the ALTER TABLE 
      statement in another connection has timed out or failed.
      
      The reason for this was that the prepare phase for ALTER TABLE for 
      partitioned tables removed all instances of the table from the table 
      definition cache before it started waiting on the lock. The transaction 
      running in the first connection would notice this and report ER_LOCK_DEADLOCK. 
      
      This patch changes the prep_alter_part_table() ALTER TABLE code so that 
      tdc_remove_table() is no longer called. Instead, only the TABLE instance
      changed by prep_alter_part_table() is marked as needing reopen.
      
      The patch also removes an unnecessary call to tdc_remove_table() from 
      mysql_unpack_partition() as the changed TABLE object is destroyed by the 
      caller at a later point.
      
      Test case added in partition_sync.test.

    added:
      mysql-test/r/partition_sync.result
      mysql-test/t/partition_sync.test
    modified:
      sql/sql_base.cc
      sql/sql_handler.cc
      sql/sql_insert.cc
      sql/sql_partition.cc
      sql/sql_select.cc
      sql/sql_table.cc
      sql/table.cc
      sql/table.h
=== added file 'mysql-test/r/partition_sync.result'
--- a/mysql-test/r/partition_sync.result	1970-01-01 00:00:00 +0000
+++ b/mysql-test/r/partition_sync.result	2009-09-22 08:31:10 +0000
@@ -0,0 +1,32 @@
+#
+# Bug #46654 False deadlock on concurrent DML/DDL 
+#            with partitions, inconsistent behavior
+#
+DROP TABLE IF EXISTS tbl_with_partitions;
+CREATE TABLE tbl_with_partitions ( i INT ) 
+PARTITION BY HASH(i);
+INSERT INTO tbl_with_partitions VALUES (1);
+# Connection 3
+LOCK TABLE tbl_with_partitions READ;
+# Connection 1
+# Access table with disabled autocommit
+SET AUTOCOMMIT = 0;
+SELECT * FROM tbl_with_partitions;
+i
+1
+# Connection 2
+# Alter table, abort after prepare
+set session debug="+d,abort_copy_table";
+ALTER TABLE tbl_with_partitions ADD COLUMN f INT;
+ERROR HY000: Lock wait timeout exceeded; try restarting transaction
+# Connection 1
+# Try accessing the table after Alter aborted.
+# This used to give ER_LOCK_DEADLOCK.
+SELECT * FROM tbl_with_partitions;
+i
+1
+# Connection 3
+UNLOCK TABLES;
+# Connection 1
+# Cleanup
+DROP TABLE tbl_with_partitions;

=== added file 'mysql-test/t/partition_sync.test'
--- a/mysql-test/t/partition_sync.test	1970-01-01 00:00:00 +0000
+++ b/mysql-test/t/partition_sync.test	2009-09-22 08:31:10 +0000
@@ -0,0 +1,57 @@
+--source include/have_partition.inc
+# Save the initial number of concurrent sessions.
+--source include/count_sessions.inc
+
+--echo #
+--echo # Bug #46654 False deadlock on concurrent DML/DDL 
+--echo #            with partitions, inconsistent behavior
+--echo #
+
+--disable_warnings
+DROP TABLE IF EXISTS tbl_with_partitions;
+--enable_warnings
+
+CREATE TABLE tbl_with_partitions ( i INT ) 
+	PARTITION BY HASH(i);
+INSERT INTO tbl_with_partitions VALUES (1);
+
+connect(con2,localhost,root);
+connect(con3,localhost,root);
+
+--echo # Connection 3
+connection con3;
+LOCK TABLE tbl_with_partitions READ;
+
+--echo # Connection 1
+--echo # Access table with disabled autocommit
+connection default;
+SET AUTOCOMMIT = 0;
+SELECT * FROM tbl_with_partitions;
+
+--echo # Connection 2
+--echo # Alter table, abort after prepare
+connection con2;
+set session debug="+d,abort_copy_table";
+--error ER_LOCK_WAIT_TIMEOUT
+ALTER TABLE tbl_with_partitions ADD COLUMN f INT;
+
+--echo # Connection 1
+--echo # Try accessing the table after Alter aborted.
+--echo # This used to give ER_LOCK_DEADLOCK.
+connection default;
+SELECT * FROM tbl_with_partitions;
+
+--echo # Connection 3
+connection con3;
+UNLOCK TABLES;
+
+--echo # Connection 1
+--echo # Cleanup
+connection default;
+disconnect con2;
+disconnect con3;
+DROP TABLE tbl_with_partitions;
+
+# Check that all connections opened by test cases in this file are really
+# gone so execution of other tests won't be affected by their presence.
+--source include/wait_until_count_sessions.inc

=== modified file 'sql/sql_base.cc'
--- a/sql/sql_base.cc	2009-09-16 14:26:50 +0000
+++ b/sql/sql_base.cc	2009-09-22 08:31:10 +0000
@@ -163,7 +163,7 @@ static void check_unused(void)
     while ((entry= it++))
     {
       /* We must not have TABLEs in the free list that have their file closed. */
-      DBUG_ASSERT(entry->db_stat && entry->file);
+      DBUG_ASSERT(!entry->needs_reopen() && entry->file);
       /* Merge children should be detached from a merge parent */
       DBUG_ASSERT(! entry->file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
 
@@ -399,7 +399,7 @@ static void table_def_use_table(THD *thd
   table->s->used_tables.push_front(table);
   table->in_use= thd;
   /* The ex-unused table must be fully functional. */
-  DBUG_ASSERT(table->db_stat && table->file);
+  DBUG_ASSERT(!table->needs_reopen() && table->file);
   /* The children must be detached from the table. */
   DBUG_ASSERT(! table->file->extra(HA_EXTRA_IS_ATTACHED_CHILDREN));
 }
@@ -1311,7 +1311,7 @@ close_all_tables_for_name(THD *thd, TABL
         !memcmp(table->s->table_cache_key.str, key, key_length))
     {
       /* Inform handler that table will be dropped after close */
-      if (table->db_stat)
+      if (!table->needs_reopen())
         table->file->extra(HA_EXTRA_PREPARE_FOR_DROP);
 
       /*
@@ -1526,8 +1526,8 @@ bool close_thread_table(THD *thd, TABLE 
   *table_ptr=table->next;
 
   table->mdl_ticket= NULL;
-  if (table->needs_reopen() ||
-      thd->version != refresh_version || !table->db_stat ||
+  if (table->s->needs_reopen() ||
+      thd->version != refresh_version || table->needs_reopen() ||
       table_def_shutdown_in_progress)
   {
     free_cache_entry(table);
@@ -8181,13 +8181,13 @@ bool mysql_notify_thread_having_shared_l
        thd_table= thd_table->next)
   {
     /*
-      Check for TABLE::db_stat is needed since in some places we call
+      Check for TABLE::needs_reopen() is needed since in some places we call
       handler::close() for table instance (and set TABLE::db_stat to 0)
       and do not remove such instances from the THD::open_tables
       for some time, during which other thread can see those instances
       (e.g. see partitioning code).
     */
-    if (thd_table->db_stat)
+    if (!thd_table->needs_reopen())
       signalled|= mysql_lock_abort_for_thread(thd, thd_table);
   }
   pthread_mutex_unlock(&LOCK_open);

=== modified file 'sql/sql_handler.cc'
--- a/sql/sql_handler.cc	2009-09-18 15:22:02 +0000
+++ b/sql/sql_handler.cc	2009-09-22 08:31:10 +0000
@@ -820,7 +820,7 @@ void mysql_ha_flush(THD *thd)
     if (hash_tables->table &&
         ((hash_tables->table->mdl_ticket &&
          hash_tables->table->mdl_ticket->has_pending_conflicting_lock()) ||
-         hash_tables->table->needs_reopen()))
+         hash_tables->table->s->needs_reopen()))
       mysql_ha_close_table(thd, hash_tables);
   }
 

=== modified file 'sql/sql_insert.cc'
--- a/sql/sql_insert.cc	2009-09-16 08:23:16 +0000
+++ b/sql/sql_insert.cc	2009-09-22 08:31:10 +0000
@@ -2698,7 +2698,7 @@ bool Delayed_insert::handle_inserts(void
 
   thd_proc_info(&thd, "insert");
   max_rows= delayed_insert_limit;
-  if (thd.killed || table->needs_reopen())
+  if (thd.killed || table->s->needs_reopen())
   {
     thd.killed= THD::KILL_CONNECTION;
     max_rows= ULONG_MAX;                     // Do as much as possible

=== modified file 'sql/sql_partition.cc'
--- a/sql/sql_partition.cc	2009-09-16 06:41:28 +0000
+++ b/sql/sql_partition.cc	2009-09-22 08:31:10 +0000
@@ -3932,15 +3932,13 @@ bool mysql_unpack_partition(THD *thd,
       We need to free any memory objects allocated on item_free_list
       by the parser since we are keeping the old info from the first
       parser call in CREATE TABLE.
-      We'll ensure that this object isn't put into table cache also
-      just to ensure we don't get into strange situations with the
-      item objects.
+
+      This table object can not be used any more. However, since
+      this is CREATE TABLE, we know that it will be destroyed by the
+      caller, and rely on that.
     */
     thd->free_items();
     part_info= thd->work_part_info;
-    tdc_remove_table(thd, TDC_RT_REMOVE_UNUSED,
-                      table->s->db.str,
-                      table->s->table_name.str);
     *work_part_info_used= true;
   }
   table->part_info= part_info;
@@ -4233,17 +4231,11 @@ uint prep_alter_part_table(THD *thd, TAB
 
   /*
     We are going to manipulate the partition info on the table object
-    so we need to ensure that the table instances cached and all other
-    instances are properly closed.
+    so we need to ensure that the table instance is removed from the
+    table cache.
   */
   if (table->part_info)
-  {
-    pthread_mutex_lock(&LOCK_open);
-    tdc_remove_table(thd, TDC_RT_REMOVE_UNUSED,
-                     table->s->db.str,
-                     table->s->table_name.str);
-    pthread_mutex_unlock(&LOCK_open);
-  }
+    table->m_needs_reopen= TRUE;
 
   thd->work_part_info= thd->lex->part_info;
   if (thd->work_part_info &&

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2009-09-21 09:09:24 +0000
+++ b/sql/sql_select.cc	2009-09-22 08:31:10 +0000
@@ -15625,7 +15625,7 @@ free_tmp_table(THD *thd, TABLE *entry)
 
   if (entry->file)
   {
-    if (entry->db_stat)
+    if (!entry->needs_reopen())
       entry->file->ha_drop_table(entry->s->table_name.str);
     else
       entry->file->ha_delete_table(entry->s->table_name.str);

=== modified file 'sql/sql_table.cc'
--- a/sql/sql_table.cc	2009-09-18 15:22:02 +0000
+++ b/sql/sql_table.cc	2009-09-22 08:31:10 +0000
@@ -7424,6 +7424,10 @@ view_err:
     new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
     new_table->next_number_field=new_table->found_next_number_field;
     thd_proc_info(thd, "copy to tmp table");
+    DBUG_EXECUTE_IF("abort_copy_table", {
+        my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
+        goto err_new_table_cleanup;
+      });
     error= copy_data_between_tables(table, new_table,
                                     alter_info->create_list, ignore,
                                    order_num, order, &copied, &deleted,

=== modified file 'sql/table.cc'
--- a/sql/table.cc	2009-09-16 15:43:00 +0000
+++ b/sql/table.cc	2009-09-22 08:31:10 +0000
@@ -1792,6 +1792,7 @@ int open_table_from_share(THD *thd, TABL
   outparam->s= share;
   outparam->db_stat= db_stat;
   outparam->write_row_record= NULL;
+  outparam->m_needs_reopen= FALSE;
 
   init_sql_alloc(&outparam->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 

=== modified file 'sql/table.h'
--- a/sql/table.h	2009-09-16 14:26:50 +0000
+++ b/sql/table.h	2009-09-22 08:31:10 +0000
@@ -294,6 +294,8 @@ TABLE_CATEGORY get_table_category(const 
 
 struct TABLE_share;
 
+extern ulong refresh_version;
+
 /*
   This structure is shared between different table objects. There is one
   instance of table share per one table in the database.
@@ -503,6 +505,14 @@ struct TABLE_SHARE
     return table_map_id;
   }
 
+
+  /*
+    Must all TABLEs be reopened?
+  */
+  inline bool needs_reopen()
+  {
+    return version != refresh_version;
+  }
   /**
     Convert unrelated members of TABLE_SHARE to one enum
     representing its type.
@@ -605,8 +615,6 @@ struct TABLE_SHARE
 };
 
 
-extern ulong refresh_version;
-
 /* Information for one open table */
 enum index_hint_type
 {
@@ -788,6 +796,7 @@ public:
   my_bool auto_increment_field_not_null;
   my_bool alias_name_used;		/* true if table_name is alias */
   my_bool get_fields_in_item_tree;      /* Signal to fix_field */
+  my_bool m_needs_reopen;
 
   REGINFO reginfo;			/* field connections */
   MEM_ROOT mem_root;
@@ -837,7 +846,7 @@ public:
     Is this instance of the table should be reopen?
   */
   inline bool needs_reopen()
-  { return s->version != refresh_version; }
+  { return !db_stat || m_needs_reopen; }
 };
 
 


Attachment: [text/bzr-bundle] bzr/jon.hauglid@sun.com-20090922083110-f62eiyw5ayw70uhr.bundle
Thread
bzr commit into mysql-6.0-bugfixing branch (jon.hauglid:2830) Bug#46654Jon Olav Hauglid22 Sep
  • Re: bzr commit into mysql-6.0-bugfixing branch (jon.hauglid:2830)Bug#46654Konstantin Osipov22 Sep