--source include/have_multi_ndb.inc
--source include/have_binlog_format_mixed_or_row.inc

# We need server 3 later on in this test.
--let $rpl_server_count= 3
--source include/master-slave.inc

--connection master

# note: server2 is another "master" connected to the master cluster

#
# Currently test only works with ndb since it retrieves "old"
# binlog positions with mysql.ndb_binlog_index and ndb_apply_status;
#

# create a table with one row, and make sure the other "master" gets it
CREATE TABLE t1 (c1 CHAR(15), c2 CHAR(15), c3 INT, PRIMARY KEY (c3)) ENGINE = NDB ;
connection server2;
reset master;
SHOW TABLES;
connection master;
INSERT INTO t1 VALUES ("row1","will go away",1);
SELECT * FROM t1 ORDER BY c3;
connection server2;
SELECT * FROM t1 ORDER BY c3;

# sync slave and retrieve epoch and stop the slave
connection master;
sync_slave_with_master;
--replace_column 1 <the_epoch>
SELECT @the_epoch:=MAX(epoch) FROM mysql.ndb_apply_status;
let $the_epoch= `select @the_epoch` ;
SELECT * FROM t1 ORDER BY c3;
stop slave;

# get the master binlog pos from the epoch, from the _other_ "master", server2
connection server2;
--replace_result $the_epoch <the_epoch>
eval SELECT @the_pos:=Position,@the_file:=SUBSTRING_INDEX(FILE, '/', -1)
   FROM mysql.ndb_binlog_index WHERE epoch = $the_epoch ;
let $the_pos= `SELECT @the_pos` ;
let $the_file= `SELECT @the_file` ;

# now connect the slave to the _other_ "master"
--let $rpl_topology= 3->2
--let $rpl_master_log_file= 2:$the_file
--let $rpl_master_log_pos= 2:$the_pos
--source include/rpl_change_topology.inc
--connection slave
--source include/start_slave.inc

# insert some more values on the first master
connection master;
INSERT INTO t1 VALUES ("row2","will go away",2),("row3","will change",3),("row4","D",4);
DELETE FROM t1 WHERE c3 = 1;
UPDATE t1 SET c2="should go away" WHERE c3 = 2;
UPDATE t1 SET c2="C" WHERE c3 = 3;
DELETE FROM t1 WHERE c3 = 2;

SELECT * FROM t1 ORDER BY c3;

# insert another row, and check that we have it on the slave
connection server2;
INSERT INTO t1 VALUES ("row5","E",5);
SELECT * FROM t1 ORDER BY c3;
sync_slave_with_master;
SELECT * FROM t1 ORDER BY c3;

--echo ==== clean up ====
connection server2;
DROP TABLE t1;
sync_slave_with_master;

--source include/rpl_end.inc
