Showing posts with label fun. Show all posts
Showing posts with label fun. Show all posts

Saturday, February 15, 2014

Magic deadlock: what locks are really set by InnoDB?

Megabytes of text had been written already on InnoDB locking and deadlocks. Still, even very simple cases of deadlocks while working with a table having only one row sometimes make people wonder what happened and why.

Today I want to check if this topic is explained well in the manual and existing blog posts and understood properly. So, it's an exercise for my dear readers and those who like to report bugs as much as I do.

Let's consider a very simple example. In session #1 with default transaction isolation level execute the following:

CREATE TABLE `tt` (
  `id` int(11) NOT NULL,
  `c1` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into tt values(1,1); -- insert a row there
select * from tt; -- check that we have row (1,1)
begin work;
select * from tt where c1=1 for update; -- we see a row (1,1) and now it's locked for us
update tt set id=id+1 where c1=1; -- and we safely increment the id


So, just a table with one row and two columns. One of columns is a PRIMARY KEY, other is UNIQUE. Why one may need a table like this? Some people (not me) may think this is a good way to create sequences and get globally unique ids generated for a set of rows, with sequence identifier being stored in column I've named c1. This happens, that's why...

Now, in session #2 execute the following:

select * from tt; -- we have row (1,1) there
select * from tt where c1=1 for update; -- let's try to use the sequence...

At this moment session #2 hangs waiting for a lock. It's expected - we actually updated the same row in an yet uncommitted transaction in session #1. This is clear.

Now, in session #1, try to do the following:

select * from tt where c1=1 for update; -- you'll see a row (2,1), as you've already incremented counter

But interesting thing happens in session #2 in the meantime:

ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

If you do not trust me, just try this with the following isolation level for both sessions:

mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)


At this moment you can take a break in reading, and check the output of SHOW ENGINE INNODB STATUS from session #2. On MySQL 5.6.16 here I see the following (details of report may depend on server version and settings):

------------------------
LATEST DETECTED DEADLOCK
------------------------
2014-02-15 18:09:20 16a8
*** (1) TRANSACTION:
TRANSACTION 36657, ACTIVE 4 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 3, OS thread handle 0x1884, query id 55 localhost ::1 root statistics
select * from tt where c1=1 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 242 page no 4 n bits 72 index `c1` of table `test`.`tt` trx id 36657 lock_mode X waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 80000001; asc     ;;
 1: len 4; hex 80000001; asc     ;;

*** (2) TRANSACTION:
TRANSACTION 36656, ACTIVE 10 sec starting index read, thread declared inside InnoDB 5000
mysql tables in use 1, locked 1
6 lock struct(s), heap size 1184, 6 row lock(s), undo log entries 2
MySQL thread id 5, OS thread handle 0x16a8, query id 56 localhost ::1 root statistics
select * from tt where c1=1 for update
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 242 page no 4 n bits 72 index `c1` of table `test`.`tt` trx id 36656 lock_mode X locks rec but not gap
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 80000001; asc     ;;
 1: len 4; hex 80000001; asc     ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 242 page no 4 n bits 72 index `c1` of table `test`.`tt` trx id 36656 lock_mode X waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 80000001; asc     ;;
 1: len 4; hex 80000001; asc     ;;

*** WE ROLL BACK TRANSACTION (1)



This is your problem statement: can you explain the deadlock above based on the output of INNODB STATUS or some other sources of information? Do you know what exact locks are really set by each session?

Take your time to write down your exact explanation, in a comment here or in separate blog post. I wonder if your explanation mentions "intention" locks of any kind, "gap" locks, "next key" locks and so on. I've read very creative texts on similar topic, and would not mind to read some more in the comments... I also wonder to know how you are going to prove your points.

We see one record lock that session holds and two lock waits in the above. All of them are obviously related to poor record with values (1,1) in the unique index named c1... You surely know that primary key value is a part of any secondary key in InnoDB, so this is all clear. But why session that already got X lock on this record has to wait to get X lock on the same(?) record? Note also 6 row locks for one of transactions in the report above...

You can surely try to rely on the manual in explaining this. But there I fail to see even clear explanation of what exact locks are set by SELECT ... FOR UPDATE in cases like this. All it says is the following:
"For SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODE, locks are acquired for scanned rows, and expected to be released for rows that do not qualify for inclusion in the result set (for example, if they do not meet the criteria given in the WHERE clause). However, in some cases, rows might not be unlocked immediately because the relationship between a result row and its original source is lost during query execution."
and later:
"For index records the search encounters, SELECT ... FROM ... FOR UPDATE blocks other sessions from doing SELECT ... FROM ... LOCK IN SHARE MODE or from reading in certain transaction isolation levels."

I've already reported some documentation requests while trying to explain this deadlock based on the manual. I'll list them all eventually. I hope you'll report several more bugs while trying to solve and discussing this exercise.

Now, why should you care? Because I am going to buy a beer (or any drink you prefer) for everybody who will post good (even if not entirely correct) explanation of this deadlock and make me aware of it, and/or report new valid bug based on this test case! I may be able to do this during PLMCE 2014 (still not sure if I go there, waiting for the decisions on 2 BOF sessions I've submitted) or during some conference later.

So, let's fun begin! You can expect summary post about this exercise (with a list of winners, my own explanation of this case, if still needed, and some ways to study the details) before April 1, 2014 :)

Thursday, July 18, 2013

Fun with Bugs #17 - Oldies but Goldies

I've just noted the date... 8 years ago I had my first official working day as a support engineer in bugs verification team of MySQL Support at MySQL AB. Why not to celebrate this anniversary with a blog post about bugs?

So, here they are, 12 oldest bugs in MySQL software that are still just "Verified" (it should mean they are accepted, but not yet fixed):
  1. Bug #2 - MySQL Connector/J doesn't make toast. I knew that Connector/J must be the most broken MySQL software (as I hate Hibernate). Now you can see how much it is broken, and nobody cares to fix it since 2002! This is a real shame...
  2. Bug #199 - Innodb autoincrement counter is lost on restart. This great report from Peter Zaitsev is still "Verified", since 2003. It became a documented "feature" in the process, but still "Verified" status means that over these years we all agree - it's broken by design! Let's hope MySQL 5.7 will fix it.
  3. Bug #363  - SHOW SESSION VARIABLES LIKE 'x' != SELECT @@SESSION.x. I spent some time verifying the original test case, and it seems in 5.6.12 it is no longer repeatable (but it seems more general things were discussed there:

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.6.12    |
    +-----------+
    1 row in set (0.01 sec)

    mysql> show session variables like 'sql_log_bin';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | sql_log_bin   | ON    |
    +---------------+-------+
    1 row in set (0.15 sec)

    mysql> select @@session.sql_log_bin;
    +-----------------------+
    | @@session.sql_log_bin |
    +-----------------------+
    |                     1 |
    +-----------------------+
    1 row in set (0.00 sec)
  4. Bug #1382 (and it's later duplicate, Bug #23385, reported by my friend Tonci) - Server returns only VARCHAR as type for any metadata sent to the client. Metadata server returns had always been a problem for all MySQL connectors. Still "Verified". Now you have "Affects Me" button at least...
  5. Bug #1396 - "Lost Data with delayed insert / mysqlhotcopy or lock and flush". It was declared a feature request to implement a workaround to make this more safe. Do not use INSERT DELAYED and/or mysqlhotcopy, what else?
  6. Bug #1939 - "Wrong case sensitivity for table names when in "ANSI" mode". We all know setting lower_case_table_names=1 is the ultimate solution, and hardly this is going to change while we still have .frm files, but the bug is still "Verified".
  7. Bug #1956 - "Parser allows naming of PRIMARY KEY". Still happens with 5.6.12, and surely in SHOW CREATE TABLE you will not see the name. "One day at a time, Penny, one day at a time".
  8. Bug #2122  - "changing hostname confuses master or slave". We all know since day one with MySQL replication that binary logs should get explicit names. Let me quote one comment form the bug report though:

    [13 Sep 2007 11:54] Lars Thalmann
    We are analyzing this and will fix it after 5.1 is released. It is, unfortunately, not likely that this will be fixed in 5.1, but we prioritize it for the next version.

    Still just "Verified".
  9. Bug #2261 - "Can't use @user_variable as FETCH target in stored procedure". So, what? Just scroll down and read last comment from the best MySQL architect of all times, famous writer and bug reporter of the past, Peter Gulutzan. I was drinking wine with him near the hotel entrance last time we met (2008 AFAIR)!
  10. Bug #2742 - "fields-escaped-by only allows single character separator!". Not sure what is the status and the real problem, but look at the bug reporter's name: "Are you mortal Then prepare to die." It's the last still active bug report from this famous personality who was well known during good old times of MySQL AB.
  11. Bug #2812. When executing SHOW VARIABLES in a prepared statement stmt->field_count is 0 instead of 2. Still "Verified" and you know what these mean in the bug report: "Triaged: D2 (Serious) / R4 (High) / E4 (High)". It means that bug was considered serious, but efforts to fix and risk to introduce regressions or other problems was estimated as high. Poor MySQL connectors' developers, really...
  12. Bug #3052 - "Rollback in stored procedure doesn't close cursor". Now read the final comment:

    [22 Aug 2006 15:02] Konstantin Osipov
    This is too big to be fixed in 5.0, will be fixed in 5.1

    You know what, it's still in 5.6.12:

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.6.12    |
    +-----------+
    1 row in set (0.00 sec)

    mysql> delimiter //
    mysql> create table t (s1 int) engine=InnoDB//
    Query OK, 0 rows affected (0.73 sec)

    mysql> insert into t values (55)//
    Query OK, 1 row affected (0.06 sec)

    mysql> create procedure p2 () begin
        ->   declare v int;
        ->   declare x cursor for select s1 from t;
        ->   open x;
        ->   rollback;
        ->   fetch x into v;
        ->   set @x = v;
        ->   end;//
    Query OK, 0 rows affected (0.24 sec)

    mysql> call p2()//
    Query OK, 0 rows affected (0.07 sec)
I can continue like that for days and 20 more posts... Reading (and processing) MySQL bug reports is a real fun! That's why I do it (with small breaks) for 8 years already and not going to stop. It's great reading no matter what are looking for - technical insights, workarounds, patches, hints, nice stories or just to something to laugh at.