--source include/big_test.inc
--source include/not_gtid_enabled.inc
--source include/master-slave.inc
--source include/have_binlog_format_mixed_or_row.inc

let $max_workers= 4;

--connection slave
SET @save_slave_parallel_workers= @@slave_parallel_workers;
SET @save_default_storage_engine=@@global.default_storage_engine;
SET @@global.default_storage_engine='innodb';

eval SET GLOBAL slave_parallel_workers= $max_workers;
# As the test case is not going to do any synchronization with the master
# before issuing STOP SLAVE in the non-deterministic activity, there is a
# chance that the slave applier be in MTS mode and in a condition that it
# will throw the following error message:
call mtr.add_suppression("The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state");
call mtr.add_suppression("Request to stop slave SQL Thread received while applying a group that has non-transactional changes");

--source include/stop_slave.inc
--source include/start_slave.inc

--connection master
SET @save_default_storage_engine=@@global.default_storage_engine;
SET @@global.default_storage_engine='innodb';
SET @@session.default_storage_engine='innodb';

call mtr.add_suppression('.*Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.*');

let $i= 10;
let $slave_status= 0;
while ($i) {
  eval CREATE DATABASE test_$i;
  eval CREATE TABLE test_$i.t1 (a INT NOT NULL PRIMARY KEY, b VARCHAR(20), c BLOB, d INT NOT NULL DEFAULT 0);
  dec $i;
}
--sync_slave_with_master
--connection master


--disable_query_log
--disable_warnings

let $slave_status= 0;
let $trx= 0;
let $alter= 0;
let $i= 200; # with bigger value test times out on some PB hosts 
if ($VALGRIND_TEST)
{
  # test may be slow on Valgrind
  let $i=50;
}
if (`select @@binlog_format like "STATEMENT"`) {
   # relax mtr to scan unsafe warnings
   let $i=50;
}
while ($i) {
  if (`SELECT 10*RAND() > 8`) {
    if ($trx) {
      COMMIT;
      let $trx= 0;
    }
  }  
  if (`SELECT 10*RAND() > 8`) {
    if (!$trx) {
      BEGIN;
      let $trx= 1;
    }
  }  
  let $db= `SELECT FLOOR(10*RAND())+1`;
  let $k= 10;
  while ($k) {
    eval INSERT INTO test_$k.t1 VALUES ($i, 'test', NULL, 0);
    dec $k;
  }
  let $k= 10;
  while ($k) {
    eval UPDATE test_$k.t1 SET d=a WHERE a = FLOOR(1000*RAND());
    dec $k;
  }
  eval DELETE FROM test_$db.t1 WHERE a = FLOOR(1000*RAND());
  if (`SELECT 10*RAND() > 5`) {
    eval UPDATE test_$db.t1 SET c=REPEAT('a', 1000) WHERE a < $i;
  }  
  if ($trx) {
    if (`SELECT 10*RAND() > 8`) {
      let $k= 10;
      while ($k) {

	# Can't be CREATE .. SELECT here because of BUG#11756034 that allows STOP SLAVE
	# in the middle of CREATE-SELECT caused group.
	# TODO: fix the bug and create a use case.

        eval CREATE TABLE test_$k.t0  (a INT NOT NULL PRIMARY KEY, b VARCHAR(20), c BLOB, d INT NOT NULL DEFAULT 0);
	eval INSERT INTO test_$k.t0 SELECT * FROM test_$k.t1;
        dec $k;
      }
      let $k= 10;
      while ($k) {
        eval DROP TABLE test_$k.t1;
        dec $k;
      }
      let $k= 10;
      while ($k) {
        eval RENAME TABLE test_$k.t0 TO test_$k.t1 /* i= $i */;
        dec $k;
      }
    }
 }
  if (`SELECT 10*RAND() > 9`) {
    --connection slave
    FLUSH LOGS;
    --connection master 
  }
  if (`SELECT 100*RAND() > 95`) {
    if ($slave_status) {
      --connection slave
      let $workers= $max_workers;
      if (`SELECT 10*RAND() > 5`) {
        let $workers= 0;
      }
      eval SET GLOBAL slave_parallel_workers= $workers;
      START SLAVE;
      --connection master
      let $slave_status= 0;
    }
  }
  if (`SELECT 100*RAND() > 95`) {
    if (!$slave_status)
    {
      --connection slave
      STOP SLAVE;
      --connection master
      let $slave_status= 1;
    }
  }
  dec $i;
}
--enable_warnings

--enable_query_log
--connection slave
--disable_warnings
--source include/start_slave.inc
--enable_warnings

#cleanup

--connection master
let $i= 10;
while ($i) {
  eval DROP DATABASE test_$i;
  dec $i;
}

SET @@global.default_storage_engine= @save_default_storage_engine;

--connection slave
SET @@global.default_storage_engine= @save_default_storage_engine;
SET GLOBAL slave_parallel_workers= @save_slave_parallel_workers;

# It may generate a big delay between master and slave, that will need more time
# to sync data

--let slave-timeout= 3600
--source include/rpl_end.inc
