Moved early check for table existance to mysql_execute_command()

MDEV-17772 - 3 way lock : ALTER, MDL, BACKUP STAGE BLOCK_DDL

While waiting for a (potentially long) RO transaction or SELECT, DDL and
LOCK TABLES ... WRITE hold protection against FTWRL and BACKUP STAGE.

This effectively makes FTWRL/BACKUP STAGE indirectly wait for this RO
transaction or SELECT to finish. Which is not great, as otherwise we
could do something useful meanwhile.

With this patch BACKUP lock is attempted to be acquired after TABLE/SCHEMA
locks. If this attempt fails, TABLE/SCHEMA locks gets released and we
start waiting for BACKUP lock. When wait finishes, BACKUP lock is released
(to avoid deadlocks) and we attempt to acquire all locks once again.

Other changes:
- Take MDL lock before testing if table exists as part of
  CREATE TABLE ... IF EXISTS.  This change was an effect of changes in
  lock_table_name and removes an inconsistency where one could get
  different error messages from CREATE TABLE .. IF EXISTS depending on
  active mdl locks.
  One effect of this change is that we don't binary log CREATE TABLE IF
  EXISTS if the table exists.  This was done because old code was sometimes
  behaving inconsistenly (it was logged some time and not other times)
  and sending the query to the slave could make the slave even more
  inconsistent as there is not guarantee that the new table will have
  the same definition as the old table on the master.
This commit is contained in:
Sergey Vojtovich 2018-12-06 19:23:24 +04:00 committed by Monty
commit 8cf7e3459d
13 changed files with 202 additions and 240 deletions

View file

@ -4168,91 +4168,6 @@ DROP TABLE t1;
disconnect con1;
--echo #
--echo # MDEV-5336 - Implement LOCK FOR BACKUP
--echo #
--echo # Make sure deadlock detector prefers FTWRL connection as a victim
--echo # and FTWRL retries lock attempt. This deadlock was present before
--echo # MDEV-5336.
CREATE TABLE t1(a INT) ENGINE=InnoDB;
CREATE TABLE t2(a INT) ENGINE=InnoDB;
BEGIN;
SELECT * FROM t2;
--echo #
connect(con1,localhost,root,,);
SET DEBUG_SYNC='mdl_acquire_lock_wait SIGNAL waiting';
send LOCK TABLES t2 WRITE;
--echo #
connect(con2,localhost,root,,);
SET DEBUG_SYNC='now WAIT_FOR waiting';
SET DEBUG_SYNC='mdl_acquire_lock_wait SIGNAL waiting';
send FLUSH TABLES WITH READ LOCK;
--echo #
connection default;
SET DEBUG_SYNC='now WAIT_FOR waiting';
INSERT INTO t1 VALUES(1);
COMMIT;
connection con1;
reap;
UNLOCK TABLES;
connection con2;
reap;
UNLOCK TABLES;
connection default;
DROP TABLE t1, t2;
SET DEBUG_SYNC='RESET';
disconnect con1;
disconnect con2;
--echo # Make sure deadlock detector prefers FTWRL connection as a victim
--echo # and FTWRL retries lock attempt. This deadlock was found during
--echo # MDEV-5336 review.
CREATE TABLE t1(a INT) ENGINE=InnoDB;
CREATE TABLE t2(a INT) ENGINE=InnoDB;
BEGIN;
INSERT INTO t2 VALUES(1);
SET DEBUG_SYNC='after_open_table_mdl_shared SIGNAL table_opened WAIT_FOR go';
send INSERT INTO t1 VALUES(1);
--echo #
connect(con1,localhost,root,,);
SET DEBUG_SYNC='now WAIT_FOR table_opened';
SET DEBUG_SYNC='mdl_acquire_lock_wait SIGNAL waiting';
send LOCK TABLES t1 WRITE;
--echo #
connect(con2,localhost,root,,);
SET DEBUG_SYNC='now WAIT_FOR waiting';
SET DEBUG_SYNC='mdl_acquire_lock_wait SIGNAL waiting';
send FLUSH TABLES WITH READ LOCK;
--echo #
connect(con3,localhost,root,,);
SET DEBUG_SYNC='now WAIT_FOR waiting';
SET DEBUG_SYNC='now SIGNAL go';
connection default;
reap;
COMMIT;
connection con1;
reap;
UNLOCK TABLES;
connection con2;
reap;
UNLOCK TABLES;
connection default;
DROP TABLE t1, t2;
SET DEBUG_SYNC='RESET';
disconnect con1;
disconnect con2;
disconnect con3;
# Check that all connections opened by test cases in this file are really
# gone so execution of other tests won't be affected by their presence.
--source include/wait_until_count_sessions.inc