Bug #40302 error on tablespace creation is not handled cleanly
Submitted: 24 Oct 2008 12:37 Modified: 13 Dec 2008 10:05
Reporter: Daniel Fischer Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Falcon storage engine Severity:S2 (Serious)
Version: OS:Other (UNIX)
Assigned to: Vladislav Vaintroub CPU Architecture:Any
Tags: F_TABLESPACE

[24 Oct 2008 12:37] Daniel Fischer
Description:
A falcon tablespace name can not be used for a new tablespace in some cases where previously, creation of a tablespace with the same name failed. The table space name initially is not listed in information_schema.falcon_tablespace_io, but appears there after restarting the server.

Found on Mac OS X, but no reason to believe that no other UNIX are affected. 

How to repeat:

mysql> select * from information_schema.falcon_tablespace_io;
+------------------+-----------+---------+----------------+--------+---------------+-------+
| TABLESPACE       | PAGE_SIZE | BUFFERS | PHYSICAL_READS | WRITES | LOGICAL_READS | FAKES |
+------------------+-----------+---------+----------------+--------+---------------+-------+
| FALCON_MASTER    |      4096 |    1024 |             50 |      0 |           916 |     1 |
| FALCON_TEMPORARY |      4096 |    1024 |              1 |      0 |             0 |     1 |
| FALCON_USER      |      4096 |    1024 |              1 |      0 |             0 |     1 |
+------------------+-----------+---------+----------------+--------+---------------+-------+

molly:foo df$ pwd
/tmp/foo

molly:foo df$ ls -la
total 8
drwxr-xr-x   3 df    wheel  102 Oct 24 14:24 .
drwxrwxrwt  10 root  wheel  340 Oct 24 14:22 ..
lrwxr-xr-x   1 df    wheel    3 Oct 24 14:22 bar -> foo

mysql> create tablespace test_if_it_works_at_all add datafile "/tmp/foo/test_if_it_works_at_all" engine=falcon;
Query OK, 0 rows affected (0.00 sec)

mysql> create tablespace test_if_it_exhibits_this_bug add datafile "/tmp/foo/bar" engine=falcon;
ERROR 303 (HY000): Unknown error 303

mysql> create tablespace test_if_it_exhibits_this_bug add datafile "/tmp/foo/test_if_it_exhibits_this_bug" engine=falcon;
ERROR 303 (HY000): Unknown error 303

mysql> create tablespace test_if_it_still_works_for_other_names add datafile "/tmp/foo/test_if_it_still_works_for_other_names" engine=falcon;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from information_schema.falcon_tablespace_io;
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
| TABLESPACE                             | PAGE_SIZE | BUFFERS | PHYSICAL_READS | WRITES | LOGICAL_READS | FAKES |
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
| FALCON_MASTER                          |      4096 |    1024 |             56 |      0 |           994 |     1 |
| test_if_it_still_works_for_other_names |      4096 |    1024 |              0 |      0 |             0 |     4 |
| test_if_it_works_at_all                |      4096 |    1024 |              0 |      0 |             0 |     4 |
| FALCON_TEMPORARY                       |      4096 |    1024 |              1 |      0 |             0 |     1 |
| FALCON_USER                            |      4096 |    1024 |              1 |      0 |             0 |     1 |
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
5 rows in set (0.00 sec)

molly:60 df$ bin/mysqladmin -u root shutdown
molly:60 df$ libexec/mysqld --no-defaults --basedir=. --datadir=data/

mysql> select * from information_schema.falcon_tablespace_io;
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
| TABLESPACE                             | PAGE_SIZE | BUFFERS | PHYSICAL_READS | WRITES | LOGICAL_READS | FAKES |
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
| FALCON_MASTER                          |      4096 |    1024 |             49 |      0 |           888 |     0 |
| test_if_it_still_works_for_other_names |      4096 |    1024 |              1 |      0 |             0 |     1 |
| test_if_it_exhibits_this_bug           |      4096 |    1024 |              0 |      0 |             0 |     0 |
| test_if_it_works_at_all                |      4096 |    1024 |              1 |      0 |             0 |     1 |
| FALCON_TEMPORARY                       |      4096 |    1024 |              1 |      0 |             0 |     1 |
| FALCON_USER                            |      4096 |    1024 |              1 |      0 |             0 |     1 |
+----------------------------------------+-----------+---------+----------------+--------+---------------+-------+
6 rows in set (0.00 sec)

Suggested fix:
I didn't dig into the source too much, but it seems like Falcon treats *any* error from stat(2) as "file does not exist", which could be potentially dangerous in cases like the described, where the test is for non-existance, although I have no idea whether this is related to this bug.
[24 Oct 2008 14:26] Vladislav Vaintroub
Daniel,
can you please specify falcon version that you're using. Falcon creates files 
with O_CREAT|O_EXCL now, it was different in the past. so there should be no way we open an existing file.stat() is obsolete actually.
[24 Oct 2008 16:00] Daniel Fischer
molly:falcon df$ grep "CREAT.*EXCL" *cpp
IO.cpp: fileId = ::open (fileName.getString(),O_CREAT | O_RDWR | O_RANDOM | O_EXCL | O_BINARY,
SerialLogFile.cpp:              handle = ::open(filename, O_RDWR | O_BINARY | O_CREAT|O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
[24 Oct 2008 16:19] Vladislav Vaintroub
got it after reading the bug description once again.
Tablespace not created, but present in falcon system tables.
[5 Nov 2008 16:32] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/57916

2899 Vladislav Vaintroub	2008-11-05
       Bug #40302 error on tablespace creation is not handled cleanly 
      
      Problem: If tablespace.create() fails, system transaction that updates tablespace
      info is not rolled back. It can be possibly commited with the next system transaction,
      that will add a tablespace to system tables even if datafile does not exist.
      
      Additionally, createTableSpace log record (indicating creation of datafile)  that logically 
      belongs to the same transaction as  system tables and has the same transaction
      id was wrongly written after "commit".
      
      With this patch, 
      -  there is a  rollback in case of error and appropriate cleanup in the filesystem
      -  createTableSpace now belongs to the same transaction that changes system tables.
[5 Nov 2008 17:59] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/57926

2904 Vladislav Vaintroub	2008-11-05
       Bug #40302 error on tablespace creation is not handled cleanly 
            
      Problem: If tablespace.create() fails, system transaction that updates tablespace
      info is not rolled back. It can be possibly commited with the next system transaction,
      that will add a tablespace to system tables even if datafile does not exist.
            
      Additionally, createTableSpace log record (indicating creation of datafile)  that logically 
      belongs to the same transaction as  system tables and has the same transaction
      id was wrongly written after "commit".
            
      With this patch, 
       -  there is a  rollback in case of error and appropriate cleanup in the filesystem
       -  createTableSpace now belongs to the same transaction that changes system tables.
[20 Nov 2008 12:55] Bugs System
Pushed into 6.0.9-alpha  (revid:vvaintroub@mysql.com-20081105175843-z3kinij0dao9jxwo) (version source revid:vvaintroub@mysql.com-20081105175843-z3kinij0dao9jxwo) (pib:5)
[13 Dec 2008 10:05] MC Brown
A note has been added to the 6.0.9 changelog: 

Creation of a tablespace file within FALCON could create a tablespace entry in the INFORMATION_SCHEMA.FALCON_TABLESPACE_IO even the underlying data file had not been created.