List:Commits« Previous MessageNext Message »
From:Davi Arnaut Date:September 27 2007 7:56pm
Subject:bk commit into 5.2 tree (davi:1.2608) BUG#28870
View as plain text  
Below is the list of changes that have just been committed into a local
5.2 repository of davi. When davi does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2007-09-27 16:56:27-03:00, davi@stripped +4 -0
  Bug#28870 check that table locks are released/reset
    
  The problem is that some mysql_lock_tables error paths are not resetting the
  tables lock type back to TL_UNLOCK. If the lock types are not reset properly,
  a table might be returned to the table cache with wrong lock_type.
      
  The proposed fix is to ensure that the tables lock type is always properly
  reset when mysql_lock_tables fails. This is a incompatible change with respect
  to the process state information.

  sql/lock.cc@stripped, 2007-09-27 16:56:21-03:00, davi@stripped +60 -73
    Merge mysql_lock_tables cleanup sequence and the reset_lock_data
    function into a single function and take steps to ensure it is
    always called for each error exit path. Also remove references
    to the redundant THD::locked variable which was almost exclusively
    used by this function and the same information is already on proc_info.

  sql/sql_class.cc@stripped, 2007-09-27 16:56:21-03:00, davi@stripped +1 -1
    Remove references to the THD::locked variable.

  sql/sql_class.h@stripped, 2007-09-27 16:56:22-03:00, davi@stripped +1 -1
    Remove the THD::locked variable.

  sql/sql_show.cc@stripped, 2007-09-27 16:56:22-03:00, davi@stripped +2 -4
    Remove references to THD:locked, state_info will now default to proc_info.

diff -Nrup a/sql/lock.cc b/sql/lock.cc
--- a/sql/lock.cc	2007-08-29 01:58:51 -03:00
+++ b/sql/lock.cc	2007-09-27 16:56:21 -03:00
@@ -87,7 +87,6 @@ extern HASH open_cache;
 
 static MYSQL_LOCK *get_lock_data(THD *thd, TABLE **table,uint count,
 				 uint flags, TABLE **write_locked);
-static void reset_lock_data(MYSQL_LOCK *sql_lock);
 static int lock_external(THD *thd, TABLE **table,uint count);
 static int unlock_external(THD *thd, TABLE **table,uint count);
 static void print_lock_error(int error, const char *);
@@ -191,6 +190,45 @@ int mysql_lock_tables_check(THD *thd, TA
   DBUG_RETURN(0);
 }
 
+
+/**
+  Reset lock type in lock data and free.
+
+  @param mysql_lock Lock structures to reset.
+
+  @note After a locking error we want to quit the locking of the table(s).
+        The test case in the bug report for Bug #18544 has the following
+        cases: 1. Locking error in lock_external() due to InnoDB timeout.
+        2. Locking error in get_lock_data() due to missing write permission.
+        3. Locking error in wait_if_global_read_lock() due to lock conflict.
+
+  @note In all these cases we have already set the lock type into the lock
+        data of the open table(s). If the table(s) are in the open table
+        cache, they could be reused with the non-zero lock type set. This
+        could lead to ignoring a different lock type with the next lock.
+
+  @note Clear the lock type of all lock data. This ensures that the next
+        lock request will set its lock type properly.
+*/
+
+static void reset_lock_data_and_free(MYSQL_LOCK **mysql_lock)
+{
+  MYSQL_LOCK *sql_lock= *mysql_lock;
+  THR_LOCK_DATA **ldata, **ldata_end;
+
+  /* Clear the lock type of all lock data to avoid reusage. */
+  for (ldata= sql_lock->locks, ldata_end= ldata + sql_lock->lock_count;
+       ldata < ldata_end;
+       ldata++)
+  {
+    /* Reset lock type. */
+    (*ldata)->type= TL_UNLOCK;
+  }
+  my_free((uchar*) sql_lock, MYF(0));
+  *mysql_lock= 0;
+}
+
+
 MYSQL_LOCK *mysql_lock_tables(THD *thd, TABLE **tables, uint count,
                               uint flags, bool *need_reopen)
 {
@@ -221,16 +259,13 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, 
       if (wait_if_global_read_lock(thd, 1, 1))
       {
         /* Clear the lock type of all lock data to avoid reusage. */
-        reset_lock_data(sql_lock);
-	my_free((uchar*) sql_lock,MYF(0));
-	sql_lock=0;
+        reset_lock_data_and_free(&sql_lock);
 	break;
       }
       if (thd->version != refresh_version)
       {
         /* Clear the lock type of all lock data to avoid reusage. */
-        reset_lock_data(sql_lock);
-	my_free((uchar*) sql_lock,MYF(0));
+        reset_lock_data_and_free(&sql_lock);
 	goto retry;
       }
     }
@@ -245,9 +280,7 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, 
 	Someone has issued SET GLOBAL READ_ONLY=1 and we want a write lock.
         We do not wait for READ_ONLY=0, and fail.
       */
-      reset_lock_data(sql_lock);
-      my_free((uchar*) sql_lock, MYF(0));
-      sql_lock=0;
+      reset_lock_data_and_free(&sql_lock);
       my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
       break;
     }
@@ -258,14 +291,11 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, 
                                                sql_lock->table_count))
     {
       /* Clear the lock type of all lock data to avoid reusage. */
-      reset_lock_data(sql_lock);
-      my_free((uchar*) sql_lock,MYF(0));
-      sql_lock=0;
+      reset_lock_data_and_free(&sql_lock);
       break;
     }
-    THD_SET_PROC_INFO(thd, "Table lock");
+    THD_SET_PROC_INFO(thd, "Locked");
     DBUG_PRINT("info", ("thd->proc_info %s", thd->proc_info));
-    thd->locked=1;
     /* Copy the lock data array. thr_multi_lock() reorders its contens. */
     memcpy(sql_lock->locks + sql_lock->lock_count, sql_lock->locks,
            sql_lock->lock_count * sizeof(*sql_lock->locks));
@@ -278,22 +308,15 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, 
     {
       if (sql_lock->table_count)
         VOID(unlock_external(thd, sql_lock->table, sql_lock->table_count));
+      reset_lock_data_and_free(&sql_lock);
       my_error(rc, MYF(0));
-      my_free((uchar*) sql_lock,MYF(0));
-      sql_lock= 0;
       break;
     }
     else if (rc == 1)                           /* aborted */
     {
-      /*
-        reset_lock_data is required here. If thr_multi_lock fails it
-        resets lock type for tables, which were locked before (and
-        including) one that caused error. Lock type for other tables
-        preserved.
-      */
-      reset_lock_data(sql_lock);
       thd->some_tables_deleted=1;		// Try again
       sql_lock->lock_count= 0;                  // Locks are already freed
+      // Fall through: unlock, reset lock data, free and retry
     }
     else if (!thd->some_tables_deleted || (flags & MYSQL_LOCK_IGNORE_FLUSH))
     {
@@ -301,23 +324,30 @@ MYSQL_LOCK *mysql_lock_tables(THD *thd, 
         Thread was killed or lock aborted. Let upper level close all
         used tables and retry or give error.
       */
-      thd->locked=0;
       break;
     }
     else if (!thd->open_tables)
     {
       // Only using temporary tables, no need to unlock
       thd->some_tables_deleted=0;
-      thd->locked=0;
       break;
     }
     THD_SET_PROC_INFO(thd, 0);
 
-    /* some table was altered or deleted. reopen tables marked deleted */
-    mysql_unlock_tables(thd,sql_lock);
-    thd->locked=0;
+    /* going to retry, unlock all tables */
+    if (sql_lock->lock_count)
+        thr_multi_unlock(sql_lock->locks, sql_lock->lock_count);
+
+    if (sql_lock->table_count)
+      VOID(unlock_external(thd, sql_lock->table, sql_lock->table_count));
+
+    /*
+      If thr_multi_lock fails it resets lock type for tables, which
+      were locked before (and including) one that caused error. Lock
+      type for other tables preserved.
+    */
+    reset_lock_data_and_free(&sql_lock);
 retry:
-    sql_lock=0;
     if (flags & MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN)
     {
       *need_reopen= TRUE;
@@ -868,8 +898,7 @@ static MYSQL_LOCK *get_lock_data(THD *th
 	my_error(ER_OPEN_AS_READONLY,MYF(0),table->alias);
         /* Clear the lock type of the lock data that are stored already. */
         sql_lock->lock_count= locks - sql_lock->locks;
-        reset_lock_data(sql_lock);
-	my_free((uchar*) sql_lock,MYF(0));
+        reset_lock_data_and_free(&sql_lock);
 	DBUG_RETURN(0);
       }
     }
@@ -890,48 +919,6 @@ static MYSQL_LOCK *get_lock_data(THD *th
 	(*org_locks)->debug_print_param= (void *) table;
   }
   DBUG_RETURN(sql_lock);
-}
-
-
-/*
-  Reset lock type in lock data.
-
-  SYNOPSIS
-    reset_lock_data()
-      sql_lock                  The MySQL lock.
-
-  DESCRIPTION
-
-    After a locking error we want to quit the locking of the table(s).
-    The test case in the bug report for Bug #18544 has the following
-    cases: 1. Locking error in lock_external() due to InnoDB timeout.
-    2. Locking error in get_lock_data() due to missing write permission.
-    3. Locking error in wait_if_global_read_lock() due to lock conflict.
-
-    In all these cases we have already set the lock type into the lock
-    data of the open table(s). If the table(s) are in the open table
-    cache, they could be reused with the non-zero lock type set. This
-    could lead to ignoring a different lock type with the next lock.
-
-    Clear the lock type of all lock data. This ensures that the next
-    lock request will set its lock type properly.
-
-  RETURN
-    void
-*/
-
-static void reset_lock_data(MYSQL_LOCK *sql_lock)
-{
-  THR_LOCK_DATA **ldata;
-  THR_LOCK_DATA **ldata_end;
-
-  for (ldata= sql_lock->locks, ldata_end= ldata + sql_lock->lock_count;
-       ldata < ldata_end;
-       ldata++)
-  {
-    /* Reset lock type. */
-    (*ldata)->type= TL_UNLOCK;
-  }
 }
 
 
diff -Nrup a/sql/sql_class.cc b/sql/sql_class.cc
--- a/sql/sql_class.cc	2007-09-04 11:52:01 -03:00
+++ b/sql/sql_class.cc	2007-09-27 16:56:21 -03:00
@@ -390,7 +390,7 @@ THD::THD()
   catalog= (char*)"std"; // the only catalog we have for now
   main_security_ctx.init();
   security_ctx= &main_security_ctx;
-  locked=some_tables_deleted=no_errors=password= 0;
+  some_tables_deleted=no_errors=password= 0;
   query_start_used= 0;
   count_cuted_fields= CHECK_FIELD_IGNORE;
   killed= NOT_KILLED;
diff -Nrup a/sql/sql_class.h b/sql/sql_class.h
--- a/sql/sql_class.h	2007-09-04 11:52:01 -03:00
+++ b/sql/sql_class.h	2007-09-27 16:56:22 -03:00
@@ -1457,7 +1457,7 @@ public:
   bool       slave_thread, one_shot_set;
   /* tells if current statement should binlog row-based(1) or stmt-based(0) */
   bool       current_stmt_binlog_row_based;
-  bool	     locked, some_tables_deleted;
+  bool	     some_tables_deleted;
   bool       last_cuted_field;
   bool	     no_errors, password;
   /**
diff -Nrup a/sql/sql_show.cc b/sql/sql_show.cc
--- a/sql/sql_show.cc	2007-08-30 19:49:11 -03:00
+++ b/sql/sql_show.cc	2007-09-27 16:56:22 -03:00
@@ -1699,8 +1699,7 @@ void mysqld_list_processes(THD *thd,cons
           pthread_mutex_lock(&mysys_var->mutex);
         thd_info->proc_info= (char*) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0);
 #ifndef EMBEDDED_LIBRARY
-        thd_info->state_info= (char*) (tmp->locked ? "Locked" :
-                                       tmp->net.reading_or_writing ?
+        thd_info->state_info= (char*) (tmp->net.reading_or_writing ?
                                        (tmp->net.reading_or_writing == 2 ?
                                         "Writing to net" :
                                         thd_info->command == COM_SLEEP ? "" :
@@ -1826,8 +1825,7 @@ int fill_schema_processlist(THD* thd, TA
                                       now - tmp->start_time : 0), TRUE);
       /* STATE */
 #ifndef EMBEDDED_LIBRARY
-      val= (char*) (tmp->locked ? "Locked" :
-                    tmp->net.reading_or_writing ?
+      val= (char*) (tmp->net.reading_or_writing ?
                     (tmp->net.reading_or_writing == 2 ?
                      "Writing to net" :
                      tmp->command == COM_SLEEP ? "" :
Thread
bk commit into 5.2 tree (davi:1.2608) BUG#28870Davi Arnaut27 Sep