#
# This test is for the consistent snapshot online backup driver.
# The test is designed to show that a consistent snapshot
# backup can be taken while data is being inserted and deleted.
#
# The test is testing the driver to ensure it is entering a 
# consistent read state during the backup. There are several
# synchronization points in the code that can be used.
# The two most useful ones are:
#
#   after_backup_cs_locked - occurs after consistent read 
#     transaction has been started and before the open and
#     lock tables.
#
#   when_backup_cs_reading - occurs after the open and lock
#     tables during the read tables portion.
#
# The following tests test these conditions.
#

--source include/have_innodb.inc
--source include/have_debug_sync.inc
--source include/not_embedded.inc

#
# Preparative cleanup.
#
--disable_warnings
SET DEBUG_SYNC= 'reset';
DROP DATABASE IF EXISTS bup_snapshot;

let $MYSQLD_BACKUPDIR= `select @@backupdir`;
--error 0,1
remove_file $MYSQLD_BACKUPDIR/bup_snapshot.bak;
--enable_warnings

--echo #
--echo # Setup for tests.
--echo #

connect (con1,localhost,root,,);
connect (con2,localhost,root,,);

connection con1;

CREATE DATABASE bup_snapshot;

# Create a table and load it with lots of data.
CREATE TABLE bup_snapshot.t1 (word CHAR(20)) ENGINE=INNODB;

INSERT INTO bup_snapshot.t1 VALUES ("01 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("02 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("03 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("04 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("05 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("06 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("07 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("08 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("09 Some data to test");
INSERT INTO bup_snapshot.t1 VALUES ("10 Some data to test");

# Use a non-CS supported table to show driver can coexist with default driver
CREATE TABLE bup_snapshot.t2 (a int) ENGINE=MEMORY;
INSERT INTO bup_snapshot.t2 VALUES (1), (2), (3), (4), (5);

--echo con1: Show that the new data doesn't exist before backup.
SELECT * FROM bup_snapshot.t1 WHERE word LIKE '-%';
SELECT COUNT(*) FROM bup_snapshot.t1;
SELECT COUNT(*) FROM bup_snapshot.t2;

--echo #
--echo # Test 1: Check for consistent read prior to open and lock tables
--echo #
# While a consistent snapshot backup is executed,
# no external inserts should be visible to the transaction.

--echo con1: Activate sync points for the backup statement.
SET DEBUG_SYNC= 'after_backup_cs_locked SIGNAL locked WAIT_FOR inserting';

--echo con1: Backing up database. Spawn this and continue...
send BACKUP DATABASE bup_snapshot TO "bup_snapshot.bak";

connection con2;

--echo con2: Wait for backup to have locked the transaction.
SET DEBUG_SYNC= 'now WAIT_FOR locked';
 
--echo con2: Start an insert now that CS has a transaction 
# The commit blocker will block this until the read lock of 
# the backup is released.

--echo con2: Activate sync points for the insert statement.
SET DEBUG_SYNC= 'wait_if_global_read_lock SIGNAL inserting';

# Insert will wait in wait_if_global_read_lock() until backup is done.
INSERT INTO bup_snapshot.t1 VALUES("- Dave Mathews");

--echo con2: insert additional rows.
INSERT INTO bup_snapshot.t1 VALUES("- Yes");
INSERT INTO bup_snapshot.t1 VALUES("- Jethro Tull");
DELETE FROM bup_snapshot.t1 WHERE word LIKE '10%';

--echo con2: Showing the data after inserts.
SELECT * FROM bup_snapshot.t1 WHERE word LIKE '-%';
SELECT COUNT(*) FROM bup_snapshot.t1;

connection con1;

--echo con1: retrieve backup result.
--replace_column 1 #
reap;

--echo # Now restore the database and then check to make sure the new rows
--echo # were not backed up.

--echo con1: Dropping the database
DROP TABLE bup_snapshot.t1;

--echo con1: Restoring the database
--replace_column 1 #
RESTORE FROM "bup_snapshot.bak" OVERWRITE;

--echo con1: Showing the data (no new data should be here).
SELECT * FROM bup_snapshot.t1 WHERE word LIKE '-%';
SELECT COUNT(*) FROM bup_snapshot.t1;
SELECT COUNT(*) FROM bup_snapshot.t2;

remove_file $MYSQLD_BACKUPDIR/bup_snapshot.bak;

--echo #
--echo # Test 2: Check for consistent read after open and lock tables
--echo #
# While a consistent snapshot backup is executed,
# no external inserts should be visible to the transaction.

--echo con1: Activate sync points for the backup statement.
SET DEBUG_SYNC= 'when_backup_cs_reading SIGNAL reading WAIT_FOR inserting';

--echo con1: Backing up database. Spawn this and continue...
send BACKUP DATABASE bup_snapshot TO "bup_snapshot.bak";

connection con2;

--echo con2: Wait for backup to start reading.
SET DEBUG_SYNC= 'now WAIT_FOR reading';
 
--echo con2: Start an insert now that CS has a transaction 
# The commit blocker will block this until the read lock of 
# the backup is released.

--echo con2: Activate sync points for the insert statement.
SET DEBUG_SYNC= 'after_insert_locked_tables SIGNAL inserting';

# Insert will wait in wait_if_global_read_lock() until backup is done.
INSERT INTO bup_snapshot.t1 VALUES("- Dave Mathews");

--echo con2: insert additional rows.
INSERT INTO bup_snapshot.t1 VALUES("- Yes");
INSERT INTO bup_snapshot.t1 VALUES("- Jethro Tull");
DELETE FROM bup_snapshot.t1 WHERE word LIKE '10%';

--echo con2: Showing the data after inserts.
SELECT * FROM bup_snapshot.t1 WHERE word LIKE '-%';
SELECT COUNT(*) FROM bup_snapshot.t1;

connection con1;

--echo con1: retrieve backup result.
--replace_column 1 #
reap;

--echo # Now restore the database and then check to make sure the new rows
--echo # were not backed up.

--echo con1: Dropping the database
DROP TABLE bup_snapshot.t1;

--echo con1: Restoring the database
--replace_column 1 #
RESTORE FROM "bup_snapshot.bak" OVERWRITE;

--echo con1: Showing the data (no new data should be here).
SELECT * FROM bup_snapshot.t1 WHERE word LIKE '-%';
SELECT COUNT(*) FROM bup_snapshot.t1;
SELECT COUNT(*) FROM bup_snapshot.t2;

#
# Cleanup
#
SET DEBUG_SYNC= 'reset';
DROP DATABASE bup_snapshot;
remove_file $MYSQLD_BACKUPDIR/bup_snapshot.bak;

