Interactive Debugging of Transaction Conflicts with TokuDB

I am developing a concurrent application that uses TokuDB to store its database. Sometimes, one of my SQL statements returns with a ‘lock wait timeout exceeded’ error. How do I identify the cause of this error? First, I need to understand a little bit about how TokuDB transactions use locks. Then, I need to understand how to use the MySQL information schema to look at the current state of the locks.

Transactions and Locks

TokuDB uses key range locks to implement serializable transactions. These locks are acquired as the transaction progresses. The locks are released when the transaction commits or aborts.

TokuDB stores these locks in a data structure called the lock tree. The lock tree stores the set of range locks granted to each transaction. In addition, the lock tree stores the set of locks that are not granted due to a conflict with locks granted to some other transaction.

TokuDB 7.1 provides access to the set of locks taken and the set of locks that are blocked via tables in the MySQL information schema. In addition, TokuDB stores the last lock conflict information in a session variable that can be retrieved by the application.


Here is an example debug session.

I create a table with a single column that is the primary key.

I have 2 MySQL clients with ID’s 1 and 2 respectively. MySQL client 1 inserts some values into the table ‘t’. TokuDB transaction 51 is created for the insert statement. Since autocommit is off, transaction 51 is still live after the insert statement completes, and I can query the ‘tokudb_locks’ information schema table to see the locks that are held by the transaction.

The keys are currently hex dumped. Someday we will pretty print the keys.
Now we switch to the other MySQL client with ID 2.

The insert gets blocked since there is a conflict on the primary key with value 100.
The granted TokuDB locks are:

The locks that are pending due to a conflict are:

Eventually, the lock for client 2 times out, and we can retrieve a JSON coded document that describes the conflict.

The JSON document encodes the database, table and index where the conflict occurred in the ‘dname’ key. The ‘key’ encodes the conflicting key. The ‘blocking_txnid’ encodes the transaction identifier that is blocking my transaction. I can find the MySQL client that created the blocking transaction via joins of information schema tables.

Now I have information about the two client operations that are conflicting and can hopefully change my application to avoid this conflict.

Share this post

Comments (4)

  • yam Reply


    I am using mysql-5.5.30-tokudb-7.0.4

    I found some insert queries are slow in slow query log.
    It seems that unique key has locked until finish insert transaction in tables include unique key.
    I think, because of unique key lock, other insert queries are not finish.

    Is it the feature of tokudb engine? or some bug?

    Here is my test to find this problem.

    Thank you.

    (tested in mysql-5.5.30-tokudb-7.0.4-linux-x86_64 and mysql-5.5.30-tokudb-7.1.0-linux-x86_64)


    ## 1. Session 1 – table create
    mysql> create table uniqueTest (id int, unique key(id));

    ## 2. Session 1 – insert transaction start
    mysql> set session autocommit=off;
    mysql> begin;
    mysql> insert into uniqueTest values (1);

    ## 3. Session 2 – insert other row
    mysql> set session autocommit=on;
    mysql> insert into uniqueTest values (12345);
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    mysql> select @@tokudb_last_lock_timeout;
    *************************** 1. row ***************************
    @@tokudb_last_lock_timeout: {“mysql_thread_id”:4, “dbname”:”./test/uniqueTest-key-id”, “requesting_txnid”:41469663, “blocking_txnid”:41469660, “key_left”:”ff0139300000″, “key_right”:”+infinity”}
    1 row in set (0.00 sec)

    No query specified

    October 17, 2013 at 6:28 am
  • Rich Prohaska Reply

    IMO, this is a bug.

    When inserting into the table, TokuDB uses a range lock (id >= 1) for session 1 to implement the uniqueness property on the id index. This is correct but causes serialization of other sessions since their range locks will conflict. A lock on the range (id == 1) for session 1 is also correct and allows concurrent insertions into the table since the range locks are unlikely to conflict. I will create an issue in our github repository for this problem.

    October 17, 2013 at 11:14 am
  • Rich Prohaska Reply

    I created github issue for this problem.

    October 17, 2013 at 2:28 pm
  • yam Reply

    Thank you. ^^

    October 18, 2013 at 12:28 am

Leave a Reply