#############################################################
# Author:  Serge Kozlov <skozlov@mysql.com>
# Date:    03/17/2008
# Purpose: Testing cluster circular replication based on two
# independent channels between two clusters
#############################################################
--source include/have_ndb.inc
--source suite/ndb_rpl/ndb_master-slave_2ch.inc
--source include/have_binlog_format_mixed_or_row.inc
--echo

# Check server_id and set auto_increment_* variables
--echo *** Check server_id of mysqld servers ***
--connection master
SHOW VARIABLES LIKE "server_id";
SET auto_increment_offset = 1;
SET auto_increment_increment = 2;
--connection master1
SHOW VARIABLES LIKE "server_id";
SET auto_increment_offset = 1;
SET auto_increment_increment = 2;
--connection slave
SHOW VARIABLES LIKE "server_id";
SET auto_increment_offset = 2;
SET auto_increment_increment = 2;
--connection slave1
SHOW VARIABLES LIKE "server_id";
SET auto_increment_offset = 2;
SET auto_increment_increment = 2;
--echo

# Preparing data.
--echo *** Preparing data ***
--connection master
CREATE TABLE t1 (a INT NOT NULL AUTO_INCREMENT, b VARCHAR(100), c INT NOT NULL, PRIMARY KEY(a)) ENGINE=ndb;

let $wait_binlog_event= CREATE TABLE t1;
--source include/wait_for_binlog_event.inc
--connection master1
--source include/wait_for_binlog_event.inc
--connection slave
--source include/wait_for_binlog_event.inc
--connection slave1
--source include/wait_for_binlog_event.inc
--echo

#
# Testing
#

--echo *** Basic testing  ***
# insert data via all hosts
--echo Insert rows via all hosts
--disable_query_log
let $counter= 10;
while ($counter) {
 --connection master
 INSERT INTO t1(b,c) VALUES('master',1);
 --connection master1
 INSERT INTO t1(b,c) VALUES('master1',1);
 --connection slave
 INSERT INTO t1(b,c) VALUES('slave',1);
 --connection slave1
 INSERT INTO t1(b,c) VALUES('slave1',1);
 dec $counter;
}
--connection master
--enable_query_log

# Wait replication between clusters
let $wait_condition= SELECT COUNT(*)=40 FROM t1 WHERE c = 1;
--source include/wait_condition.inc
--connection slave
--source include/wait_condition.inc

# Check data
--echo Check data on both clusters 
let $diff_tables= master:t1, slave:t1;
source include/diff_tables.inc;

--echo *** Transaction testing ***
# Start transaction for one mysqld and do mass of inserts for other.
# Do it for for both clusters

--connection master
BEGIN;
--connection slave1
BEGIN;
let $counter= 100;
--connection master
--disable_query_log
while ($counter) {
 --connection master
 INSERT INTO t1(b,c) VALUES('master',2);
 --connection master1
 INSERT INTO t1(b,c) VALUES('master1',2);
 --connection slave
 INSERT INTO t1(b,c) VALUES('slave',2);
 --connection slave1
 INSERT INTO t1(b,c) VALUES('slave1',2);
 dec $counter;
}
--connection master
--enable_query_log
COMMIT;
--connection slave1
COMMIT;

# Wait replication between clusters
--connection master
let $wait_condition= SELECT COUNT(*)=400 FROM t1 WHERE c = 2;
--source include/wait_condition.inc
--connection slave
--source include/wait_condition.inc

--echo Check data on both clusters 
let $diff_tables= master:t1, slave:t1;
source include/diff_tables.inc;

# Start transaction and then roll back

--connection master
BEGIN;
--connection slave1
BEGIN;
let $counter= 100;
--connection master
--disable_query_log
while ($counter) {
 --connection master
 INSERT INTO t1(b,c) VALUES('master',3);
 --connection master1
 INSERT INTO t1(b,c) VALUES('master1',3);
 --connection slave
 INSERT INTO t1(b,c) VALUES('slave',3);
 --connection slave1
 INSERT INTO t1(b,c) VALUES('slave1',3);
 dec $counter;
}
--connection master
--enable_query_log
ROLLBACK;
--connection slave1
ROLLBACK;

# Wait replication between clusters
--connection master
let $wait_condition= SELECT COUNT(*)=200 FROM t1 WHERE c = 3;
--source include/wait_condition.inc
--connection slave
--source include/wait_condition.inc

--echo Check data on both clusters 
let $diff_tables= master:t1, slave:t1;
source include/diff_tables.inc;

# Clean up
--connection master
DROP TABLE IF EXISTS t1;
--connection slave
# Wait until table is dropped on slave.
--let $query= SELECT COUNT(*) FROM t1
--source include/wait_for_query_to_fail.inc
--echo

--connection master

# End of test 5.1
--source suite/ndb_rpl/ndb_master-slave_2ch_end.inc

