# ==== Purpose ====
#
# Testing basic functionality of heartbeat.
#
# Description:
# * Testing different values for slave_heartbeat_period.
# * How to affect various statements to slave_heartbeat_period
# * Various states of slave and heartbeat
# * Various states of master and heartbeat
# * Circular replication
#
--source include/master-slave.inc
#
# The test runs long and does not have any specifics to 
# binlog_format. It is choosen therefore to run with MIXED mode
# in order to not slow down much `make test'.
#
--source include/have_binlog_format_mixed.inc

call mtr.add_suppression("Slave I/O: The slave I/O thread stops because a fatal error is encountered when it tried to SET @master_binlog_checksum");
call mtr.add_suppression("The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state");

--echo

# Set number of retries to connect to master
let $connect_retry= 20;

--echo *** Preparing ***
--source include/sync_slave_sql_with_master.inc

--disable_query_log
call mtr.add_suppression("The master's UUID has changed, although this should not happen unless you have changed it manually.");
--enable_query_log

--source include/stop_slave.inc
RESET SLAVE;
RESET MASTER;
SET @restore_slave_net_timeout=@@global.slave_net_timeout;
let $slave_heartbeat_timeout= query_get_value(SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period', Value, 1);
--disable_query_log
eval SET @restore_slave_heartbeat_timeout=$slave_heartbeat_timeout;
--enable_query_log

--connection master
RESET MASTER;
SET @restore_slave_net_timeout=@@global.slave_net_timeout;
SET @restore_event_scheduler=@@global.event_scheduler;
--echo

#
# Test slave_heartbeat_period
#

--connection slave

# Default value of slave_heartbeat_timeout = slave_net_timeout/2
--echo *** Default value ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root';
let $slave_net_timeout= query_get_value(SHOW VARIABLES LIKE 'slave_net_timeout', Value, 1);
let $slave_heartbeat_timeout= query_get_value(SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period', Value, 1);
let $result= query_get_value(SELECT $slave_net_timeout/$slave_heartbeat_timeout AS Result, Result, 1);
--echo slave_net_timeout/slave_heartbeat_timeout=$result
RESET SLAVE;
RESET MASTER;
--echo

# Reset slave set slave_heartbeat_timeout = slave_net_timeout/2
--echo *** Reset slave affect ***
--disable_warnings
SET @@global.slave_net_timeout=30;
--enable_warnings
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=5;
RESET SLAVE;
RESET MASTER;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
--echo

# Check default value of slave_heartbeat_timeout if slave_net_timeout is changed
--echo *** Default value if slave_net_timeout changed ***
--disable_warnings
SET @@global.slave_net_timeout=50;
--enable_warnings
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SET @@global.slave_net_timeout=@restore_slave_net_timeout;
RESET SLAVE;
RESET MASTER;
--echo

# Set slave_net_timeout less than current value of slave_heartbeat_period
--echo *** Warning if updated slave_net_timeout < slave_heartbeat_timeout ***
let $slave_heartbeat_timeout= query_get_value(SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period', Value, 1);
--replace_result $slave_heartbeat_timeout SLAVE_HEARTBEAT_TIMEOUT
eval SET @@global.slave_net_timeout=FLOOR($slave_heartbeat_timeout)-1;
SET @@global.slave_net_timeout=@restore_slave_net_timeout;
RESET SLAVE;
RESET MASTER;
--echo

# Set value of slave_heartbeat_period greater than slave_net_timeout
--echo *** Warning if updated slave_heartbeat_timeout > slave_net_timeout ***
let $slave_net_timeout= query_get_value(SHOW VARIABLES LIKE 'slave_net_timeout', Value, 1);
inc $slave_net_timeout;
--replace_result $MASTER_MYPORT MASTER_PORT $slave_net_timeout SLAVE_NET_TIMEOUT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=$slave_net_timeout;
RESET SLAVE;
RESET MASTER;
--echo 

# Changing of slave_net_timeout shouldn't affect to current value of slave_heartbeat_period
--echo *** CHANGE MASTER statement only updates slave_heartbeat_period ***
--disable_warnings
SET @@global.slave_net_timeout=20;
--enable_warnings
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=5;
SHOW VARIABLES LIKE 'slave_net_timeout';
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SET @@global.slave_net_timeout=2*@@global.slave_net_timeout;
SHOW VARIABLES LIKE 'slave_net_timeout';
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SET @@global.slave_net_timeout=@restore_slave_net_timeout;
RESET SLAVE;
RESET MASTER;
--echo

# Master value of slave_net_timeout shouldn't affect to slave's slave_heartbeat_period
--echo *** Update slave_net_timeout on master ***
--connection master
--disable_warnings
SET @@global.slave_net_timeout=500;
--enable_warnings
--connection slave
SET @@global.slave_net_timeout=200;
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry;
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc
SHOW VARIABLES LIKE 'slave_net_timeout';
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SET @@global.slave_net_timeout=@restore_slave_net_timeout;
--source include/stop_slave.inc
RESET SLAVE;
RESET MASTER;
--connection master
SET @@global.slave_net_timeout=@restore_slave_net_timeout;
--echo

# Start/stop slave shouldn't change slave_heartbeat_period
--echo *** Start/stop slave ***
--connection slave
--disable_warnings
SET @@global.slave_net_timeout=100;
--enable_warnings
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=20;
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
--source include/stop_slave.inc
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
--echo

# Reload slave shouldn't change slave_heartbeat_period
--echo *** Reload slave ***
--connection slave
--disable_warnings
SET @@global.slave_net_timeout=50;
--enable_warnings
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=30;
--let $rpl_server_number= 2
--source include/rpl_restart_server.inc
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SET @restore_slave_net_timeout=@@global.slave_net_timeout;
--echo

# Disable heartbeat
--echo *** Disable heartbeat ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SHOW STATUS LIKE 'slave_received_heartbeats';
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc
--sleep 2
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SHOW STATUS LIKE 'slave_received_heartbeats';
--source include/stop_slave.inc
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
SHOW STATUS LIKE 'slave_received_heartbeats';
RESET SLAVE;
RESET MASTER;
let $slave_heartbeat_timeout= query_get_value(SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period', Value, 1);
--replace_result $slave_heartbeat_timeout SLAVE_HEARTBEAT_TIMEOUT
--eval SELECT $slave_heartbeat_timeout = 0 AS Result
--echo 

#
# Check limits for slave_heartbeat_timeout
#

--echo *** Min slave_heartbeat_timeout ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.001;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.0009;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
RESET SLAVE;
RESET MASTER;
--echo

--echo *** Max slave_heartbeat_timeout ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=4294967;
SHOW GLOBAL STATUS LIKE 'slave_heartbeat_period';
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE 
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=4294968;
RESET SLAVE;
RESET MASTER;
# Check double size of max allowed value for master_heartbeat_period
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE 
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=8589935;
RESET SLAVE;
RESET MASTER;
# Check 2^32
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE 
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=4294967296;
RESET SLAVE;
RESET MASTER;
--echo

--echo *** Misc incorrect values ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_PARSE_ERROR
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD='-1';
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_PARSE_ERROR
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD='123abc';
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
--error ER_PARSE_ERROR
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD='';
RESET SLAVE;
RESET MASTER;
--echo

#
# Testing heartbeat 
#
# Check received heartbeat events for running slave.
# It must arrived not ealier than as specified by HEARTBEAT_PERIOD.
#
--let $hb_period= 3.0
--echo *** Running slave ***
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD= $hb_period;
SELECT unix_timestamp() into @time_0;
--source include/start_slave.inc

--connection master
--source include/sync_slave_sql_with_master.inc
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var= slave_received_heartbeats;
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
SELECT unix_timestamp() into @time_1;
if (`SELECT @time_1 - @time_0 < $hb_period`)
{
    --echo "Heartbeat is received ealier than specified."
    --die
}
--echo Heartbeat event received
--echo

# Check received heartbeat events for stopped slave
--echo *** Stopped slave ***
--source include/stop_slave.inc
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD= 0.1;

let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
sleep 2;
let $rcvd_heartbeats_after= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $result= query_get_value(SELECT ($rcvd_heartbeats_after - $rcvd_heartbeats_before) AS Result, Result, 1);
--echo Number of received heartbeat events while slave stopped: $result
--echo

# Check received heartbeat events for started slave
--echo *** Started slave ***
--source include/start_slave.inc
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
--source include/wait_for_status_var.inc
--echo Heartbeat event received
--echo

# Check received heartbeat events for stopped IO thread
--echo *** Stopped IO thread ***
--source include/stop_slave_io.inc
let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
sleep 2;
let $rcvd_heartbeats_after= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $result= query_get_value(SELECT ($rcvd_heartbeats_after - $rcvd_heartbeats_before) AS Result, Result, 1);
--echo Number of received heartbeat events while io thread stopped: $result
--echo

# Check received heartbeat events for started IO thread
--echo *** Started IO thread ***
START SLAVE IO_THREAD;
--source include/wait_for_slave_io_to_start.inc
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
--source include/wait_for_status_var.inc
--echo Heartbeat event received
--echo

# Check received heartbeat events for stopped SQL thread
--echo *** Stopped SQL thread ***
--source include/stop_slave_sql.inc
let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
sleep 2;
let $rcvd_heartbeats_after= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $result= query_get_value(SELECT ($rcvd_heartbeats_after - $rcvd_heartbeats_before) > 0 AS Result, Result, 1);
--echo Heartbeat events are received while sql thread stopped (1 means 'yes'): $result
--echo

# Check received heartbeat events for started SQL thread
--echo *** Started SQL thread ***
START SLAVE SQL_THREAD;
--source include/wait_for_slave_sql_to_start.inc
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
--source include/wait_for_status_var.inc
--echo Heartbeat event received
--echo

# Check received heartbeat event for stopped SQL thread by error
--echo *** Stopped SQL thread by error ***
--connection master
CREATE TABLE t1 (a INT PRIMARY KEY, b VARCHAR(10), c LONGTEXT);
--source include/sync_slave_sql_with_master.inc
INSERT INTO t1 VALUES (1, 'on slave', NULL);
--connection master
INSERT INTO t1 VALUES (1, 'on master', NULL);
--connection slave
call mtr.add_suppression("Slave SQL.*Duplicate entry .1. for key .PRIMARY.. on query.* Error_code: 1062");
call mtr.add_suppression("Slave SQL.*Request to stop slave SQL Thread received while applying a group that has non-transactional changes; waiting for completion of the group");
let $slave_errno= ER_DUP_ENTRY
--source include/wait_for_slave_sql_error.inc
let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
sleep 2;
let $rcvd_heartbeats_after= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $result= query_get_value(SELECT ($rcvd_heartbeats_after - $rcvd_heartbeats_before) > 0 AS Result, Result, 1);
--echo Heartbeat events are received while sql thread stopped (1 means 'yes'): $result
--source include/stop_slave.inc
DELETE FROM t1;
--source include/start_slave.inc
--connection master
--source include/sync_slave_sql_with_master.inc
--connection master
DROP TABLE t1;
--echo

# Check received heartbeat events while logs flushed on slave
--source include/sync_slave_sql_with_master.inc
--echo *** Flush logs on slave ***
--let $rpl_no_start_slave= 1
--source include/rpl_reset.inc
--let $rpl_no_start_slave= 0
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.5;
let $slave_param_comparison= =;
--source include/start_slave.inc
let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
# Flush logs every 0.1 second during 10 sec
--disable_query_log
let $i=100;
while ($i) {
  FLUSH LOGS;
  dec $i;
  sleep 0.1;
}
--enable_query_log
--let $assert_cond= [SHOW STATUS LIKE "slave_received_heartbeats", Value, 1] > $rcvd_heartbeats_before
--let $assert_text= Should receive at least one heartbeat event while running FLUSH LOGS for 10 seconds
--source include/assert.inc
--echo

# Use compressed protocol between master and slave
--echo *** Compressed protocol ***
--connection master
SET @@global.slave_compressed_protocol=1;
--connection slave
--source include/stop_slave.inc
RESET SLAVE;
RESET MASTER;
SET @@global.slave_compressed_protocol=1;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.1;
--source include/start_slave.inc
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var= slave_received_heartbeats;
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
--echo Heartbeat event received
SET @@global.slave_compressed_protocol=0;
--connection master
SET @@global.slave_compressed_protocol=0;
--echo


# Check received heartbeat events after reset of master
--echo *** Reset master ***
--connection slave

STOP SLAVE;
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.1;
--source include/start_slave.inc
let $rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
--connection master
RESET MASTER;
--enable_query_log
--source include/sync_slave_sql_with_master.inc
--sleep 2
let $rcvd_heartbeats_after= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $result= query_get_value(SELECT ($rcvd_heartbeats_after - $rcvd_heartbeats_before) > 0 AS Result, Result, 1);
--echo Heartbeat events are received after reset of master (1 means 'yes'): $result
--echo

# Reloaded master should restore heartbeat
--echo *** Reload master ***
--connection slave
STOP SLAVE;
RESET SLAVE;
RESET MASTER;
--replace_result $MASTER_MYPORT MASTER_PORT
--replace_column 2 ####
eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$MASTER_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=0.1;
--source include/start_slave.inc
# Wait until slave_received_heartbeats will be incremented
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var= slave_received_heartbeats;
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
--echo Heartbeat event received
--let $rpl_server_number= 1
--source include/rpl_restart_server.inc
# make sure IO thread has re-connected 
# due to slow valgrind env the following wait_for_status may time out
--let $rpl_allow_error= 1
--source include/wait_for_slave_io_to_start.inc
# Wait until slave_received_heartbeats will be incremented
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var= slave_received_heartbeats;
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
--echo Heartbeat event received
--echo

# Circular replication: demonstrating bidirectional hearbeat flow
--echo *** Circular replication ***
# Configure circular replication
--source include/rpl_reset.inc
--source include/stop_slave.inc
--let $rpl_topology= 1->2->1
--source include/rpl_change_topology.inc

#--connection slave
#--source include/stop_slave.inc
#let $slave_binlog= query_get_value(SHOW MASTER STATUS, File, 1);
--connection master
#--replace_result $SLAVE_MYPORT SLAVE_PORT $slave_binlog SLAVE_BINLOG
#eval CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=$SLAVE_MYPORT, MASTER_USER='root', MASTER_CONNECT_RETRY=$connect_retry, MASTER_HEARTBEAT_PERIOD=1, MASTER_LOG_FILE='$slave_binlog';

# BUG#12403008 RPL_HEARTBEAT_BASIC FAILS SPORADICALLY ON PUSHBUILD
# MASTER_HEARTBEAT_PERIOD had the default value (slave_net_timeout/2)
# so wait on "Heartbeat event received on master", that only waits for
# 1 minute, sometimes timeout before heartbeat arrives.
--replace_column 2 ####
CHANGE MASTER TO MASTER_HEARTBEAT_PERIOD=1;
--source include/start_slave.inc

# Insert data on master and on slave and make sure that it replicated for both directions
CREATE TABLE t1 (a INT PRIMARY KEY, b VARCHAR(10));
INSERT INTO t1 VALUES(1, 'on master');
--save_master_pos
--connection slave
## set slave period 1/10 of master's
--replace_column 2 ####
CHANGE MASTER TO MASTER_HEARTBEAT_PERIOD=0.1;
--source include/start_slave.inc
--sync_with_master
INSERT INTO t1 VALUES(2, 'on slave');
--let $sync_slave_connection= master
--source include/sync_slave_sql_with_master.inc
SELECT * FROM t1 ORDER BY a;
let $master_rcvd_heartbeats_before= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
--connection slave
SELECT * FROM t1 ORDER BY a;

# Wait for heartbeat event on master
--connection master
let $status_var= slave_received_heartbeats;
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
--echo Heartbeat event received on master

# Wait heartbeat events on slave
--connection slave
let $status_var= slave_received_heartbeats;
let $status_var_value= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);
let $status_var_comparsion= >;
--source include/wait_for_status_var.inc
--echo Heartbeat event received on slave
let $slave_rcvd_heartbeats= query_get_value(SHOW STATUS LIKE 'slave_received_heartbeats', Value, 1);

#
# Clean up and restore system variables
#
--echo *** Clean up ***
--connection master
#--source include/stop_slave.inc
DROP TABLE t1;
--connection slave
SET @@global.slave_net_timeout=@restore_slave_net_timeout;

#--let $rpl_only_running_threads= 1
--source include/rpl_end.inc
