From ec8779e95a46b3dfff492196ad004cb2716df3c3 Mon Sep 17 00:00:00 2001
From: unknown <monty@mysql.com>
Date: Wed, 20 Oct 2004 16:04:28 +0300
Subject: [PATCH] Fix test case for innodb-lock

mysql-test/r/innodb-lock.result:
  Fix test case (old one didn't test things correctly)
mysql-test/t/innodb-lock.test:
  Fix test case (old one didn't test things correctly)
mysys/thr_lock.c:
  More debugging information
sql/mysqld.cc:
  Enable innodb_table_locks as default, as otherwise there is a possibility for deadlocks
sql/sql_base.cc:
  More debug information
---
 mysql-test/r/innodb-lock.result     | 30 +++++++++++-------
 mysql-test/t/innodb-lock-master.opt |  1 +
 mysql-test/t/innodb-lock.test       | 47 ++++++++++++++++++-----------
 mysys/thr_lock.c                    | 12 ++++++--
 sql/mysqld.cc                       |  2 +-
 sql/sql_base.cc                     |  2 ++
 6 files changed, 62 insertions(+), 32 deletions(-)
 create mode 100644 mysql-test/t/innodb-lock-master.opt

diff --git a/mysql-test/r/innodb-lock.result b/mysql-test/r/innodb-lock.result
index f87f221ea66..407a85ed038 100644
--- a/mysql-test/r/innodb-lock.result
+++ b/mysql-test/r/innodb-lock.result
@@ -1,7 +1,7 @@
-drop table if exists t1;
 select @@innodb_table_locks;
 @@innodb_table_locks
-0
+1
+drop table if exists t1;
 set @@innodb_table_locks=1;
 create table t1 (id integer, x integer) engine=INNODB;
 insert into t1 values(0, 0);
@@ -25,24 +25,32 @@ id	x
 commit;
 drop table t1;
 set @@innodb_table_locks=0;
-create table t1 (id integer, x integer) engine=INNODB;
-insert into t1 values(0, 0);
-set autocommit=0;
+create table t1 (id integer primary key, x integer) engine=INNODB;
+insert into t1 values(0, 0),(1,1),(2,2);
+commit;
 SELECT * from t1 where id = 0 FOR UPDATE;
 id	x
 0	0
 set autocommit=0;
+set @@innodb_table_locks=0;
 lock table t1 write;
-update t1 set x=1 where id = 0;
-select * from t1;
+update t1 set x=10 where id = 2;
+SELECT * from t1 where id = 2;
 id	x
-0	1
+2	2
+UPDATE t1 set x=3 where id = 2;
 commit;
-update t1 set x=2 where id = 0;
+SELECT * from t1;
+id	x
+0	0
+1	1
+2	3
 commit;
 unlock tables;
+commit;
 select * from t1;
 id	x
-0	2
-commit;
+0	0
+1	1
+2	10
 drop table t1;
diff --git a/mysql-test/t/innodb-lock-master.opt b/mysql-test/t/innodb-lock-master.opt
new file mode 100644
index 00000000000..403fcde87ed
--- /dev/null
+++ b/mysql-test/t/innodb-lock-master.opt
@@ -0,0 +1 @@
+--innodb-table-lock=1
diff --git a/mysql-test/t/innodb-lock.test b/mysql-test/t/innodb-lock.test
index 11395b301c4..430369f4fda 100644
--- a/mysql-test/t/innodb-lock.test
+++ b/mysql-test/t/innodb-lock.test
@@ -1,9 +1,5 @@
 -- source include/have_innodb.inc
 
-connect (con1,localhost,root,,);
-connect (con2,localhost,root,,);
-drop table if exists t1;
-
 #
 # Check and select innodb lock type
 #
@@ -14,6 +10,14 @@ select @@innodb_table_locks;
 # Testing of explicit table locks with enforced table locks
 #
 
+connect (con1,localhost,root,,);
+connect (con2,localhost,root,,);
+drop table if exists t1;
+
+#
+# Testing of explicit table locks with enforced table locks
+#
+
 set @@innodb_table_locks=1;
 
 connection con1;
@@ -48,37 +52,44 @@ commit;
 drop table t1;
 
 #
-# Try with old lock method (where LOCK TABLE is ignored)
+# Try with old lock method (where LOCK TABLE is ignored by InnoDB)
 #
 
 set @@innodb_table_locks=0;
 
-create table t1 (id integer, x integer) engine=INNODB;
-insert into t1 values(0, 0);
-set autocommit=0;
+create table t1 (id integer primary key, x integer) engine=INNODB;
+insert into t1 values(0, 0),(1,1),(2,2);
+commit;
 SELECT * from t1 where id = 0 FOR UPDATE;
 
 connection con2;
 set autocommit=0;
+set @@innodb_table_locks=0;
 
-# The following statement should hang because con1 is locking the page
---send
+# The following statement should work becase innodb doesn't check table locks
 lock table t1 write;
---sleep 2;
 
 connection con1;
-update t1 set x=1 where id = 0;
-select * from t1;
-commit;
+
+# This will be locked by MySQL
+--send
+update t1 set x=10 where id = 2;
+--sleep 2
 
 connection con2;
-reap;
-update t1 set x=2 where id = 0;
+
+# Note that we will get a deadlock if we try to select any rows marked
+# for update by con1 !
+
+SELECT * from t1 where id = 2;
+UPDATE t1 set x=3 where id = 2;
+commit;
+SELECT * from t1;
 commit;
 unlock tables;
 
 connection con1;
-select * from t1;
+reap;
 commit;
-
+select * from t1;
 drop table t1;
diff --git a/mysys/thr_lock.c b/mysys/thr_lock.c
index d5236cb1ef9..935ed4ea282 100644
--- a/mysys/thr_lock.c
+++ b/mysys/thr_lock.c
@@ -552,8 +552,14 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type)
 	   !lock->write_wait.data &&
 	   lock->write.data->type == TL_WRITE_ALLOW_WRITE))
       {
-	/* We have already got a write lock or all locks are
-	   TL_WRITE_ALLOW_WRITE */
+	/*
+          We have already got a write lock or all locks are
+          TL_WRITE_ALLOW_WRITE
+        */
+        DBUG_PRINT("info", ("write_wait.data: 0x%lx  old_type: %d",
+                            (ulong) lock->write_wait.data,
+                            lock->write.data->type));
+
 	(*lock->write.last)=data;	/* Add to running fifo */
 	data->prev=lock->write.last;
 	lock->write.last= &data->next;
@@ -568,6 +574,8 @@ int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type)
     }
     else
     {
+      DBUG_PRINT("info", ("write_wait.data: 0x%lx",
+                          (ulong) lock->write_wait.data));
       if (!lock->write_wait.data)
       {						/* no scheduled write locks */
 	if (lock_type == TL_WRITE_CONCURRENT_INSERT &&
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index b7eafcbcc14..5838bd909dd 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -3704,7 +3704,7 @@ struct my_option my_long_options[] =
    "If Innodb should enforce LOCK TABLE",
    (gptr*) &global_system_variables.innodb_table_locks,
    (gptr*) &global_system_variables.innodb_table_locks,
-   0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
+   0, GET_BOOL, OPT_ARG, 1, 0, 0, 0, 0, 0},
 #endif /* End HAVE_INNOBASE_DB */
   {"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
    0, 0, 0, 0, 0},
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 9810ec6c3d6..72400bf0abb 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -826,6 +826,7 @@ TABLE *open_table(THD *thd,const char *db,const char *table_name,
 	DBUG_RETURN(0);
       }
       table->query_id=thd->query_id;
+      DBUG_PRINT("info",("Using temporary table"));
       goto reset;
     }
   }
@@ -840,6 +841,7 @@ TABLE *open_table(THD *thd,const char *db,const char *table_name,
 	  table->query_id != thd->query_id)
       {
 	table->query_id=thd->query_id;
+        DBUG_PRINT("info",("Using locked table"));
 	goto reset;
       }
     }