# ==== Purpose ====
#
# Check replication protocol packet size handling
#
# ==== Related bugs ====
# Bug#19402 SQL close to the size of the max_allowed_packet fails on slave
# BUG#23755: Replicated event larger that max_allowed_packet infinitely re-transmits
# BUG#42914: No LAST_IO_ERROR for max_allowed_packet errors
# BUG#55322: SHOW BINLOG EVENTS increases @@SESSION.MAX_ALLOWED_PACKET

# max-out size db name 
source include/master-slave.inc;
source include/have_binlog_format_row.inc;

# TODO: Fixing is handed over to Sergei.
# The test runs slow in MTS mode because of state of MTS at time of the graceful stop.
# In this case MTS can't stop immediately if there is a Worker that received a BEGIN but never COMMIT.
-- source include/not_mts_slave_parallel_workers.inc

call mtr.add_suppression("Slave I/O: Got a packet bigger than 'slave_max_allowed_packet' bytes, Error_code: 1153");
call mtr.add_suppression("Log entry on master is longer than slave_max_allowed_packet");
let $db= DB_NAME_OF_MAX_LENGTH_AKA_NAME_LEN_64_BYTES_____________________;
disable_warnings;
eval drop database if exists $db;
enable_warnings;
eval create database $db;

connection master;
let $old_max_allowed_packet= `SELECT @@global.max_allowed_packet`;
let $old_net_buffer_length= `SELECT @@global.net_buffer_length`;
let $old_slave_max_allowed_packet= `SELECT @@global.slave_max_allowed_packet`;
let $old_slave_max_allowed_packet_temp= `SELECT @@global.slave_max_allowed_packet`;
SET @@global.max_allowed_packet=10240;
SET @@global.net_buffer_length=1024;

--source include/sync_slave_sql_with_master.inc
# Restart slave for setting to take effect
source include/stop_slave.inc;
source include/start_slave.inc;

# Reconnect to master for new setting to take effect
disconnect master;

# alas, can't use eval here; if db name changed apply the change here
connect (master,localhost,root,,DB_NAME_OF_MAX_LENGTH_AKA_NAME_LEN_64_BYTES_____________________);

connection master;
select @@net_buffer_length, @@max_allowed_packet;

create table `t1` (`f1` LONGTEXT) ENGINE=MyISAM;

INSERT INTO `t1`(`f1`) VALUES ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2048');
--source include/sync_slave_sql_with_master.inc

eval select count(*) from `$db`.`t1` /* must be 1 */;

SHOW STATUS LIKE 'Slave_running';
select * from information_schema.session_status where variable_name= 'SLAVE_RUNNING';
connection master;
eval drop database $db;
--source include/sync_slave_sql_with_master.inc

#
# Bug #23755: Replicated event larger that max_allowed_packet infinitely re-transmits
#
# Check that a situation when the size of event on the master is greater than 
# max_allowed_packet on the slave does not lead to infinite re-transmits.

connection master;

# Change the max packet size on master

# Todo: improve over-max_allowed_packet size events block on the slave.
# The current size checking algorithm is not presize to allow large event
# to slip it. Reject happens according to the guard:
#   if (data_len > max(max_allowed_packet,
#       opt_binlog_rows_event_max_size + MAX_LOG_EVENT_HEADER))
# However, MAX_LOG_EVENT_HEADER is a conservative estimate so if the actual
# header size is less the extra data let in the slave.

# Adding the max size of the query log event status as
# MAX_DBS_IN_QUERY_MTS * (1 + NAME_LEN) to make the master not fail to read
# an event itself. The increased max_allowed_packet also takes care of the 
# increase in the connect string processing introduced due to WL#5924.

SET @@global.max_allowed_packet=4096 + (floor(64 * 3 * 254 / 1024) + 1) * 1024;
SET @@global.net_buffer_length=@@global.max_allowed_packet;

# Restart slave for new setting to take effect
connection slave;
source include/stop_slave.inc;
source include/start_slave.inc;

# Reconnect to master for new setting to take effect
disconnect master;
connect (master, localhost, root);
connection master;

CREATE TABLE `t1` (`f1` LONGTEXT) ENGINE=MyISAM;

--source include/sync_slave_sql_with_master.inc

connection master;

INSERT INTO t1 VALUES (REPEAT('a', @@global.max_allowed_packet));


#
# Bug#42914: The slave I/O thread must stop after trying to read the above
# event, However there is no Last_IO_Error report.
#

# The slave I/O thread must stop after trying to read the above event
connection slave;
# 1153 = ER_NET_PACKET_TOO_LARGE
--let $slave_io_errno= 1153
--let $show_slave_io_error= 1
--source include/wait_for_slave_io_error.inc

# TODO: this is needed because of BUG#55790. Remove once that is fixed.
--source include/stop_slave_sql.inc

#
# Bug#42914: On the master, if a binary log event is larger than
# max_allowed_packet, the error message ER_MASTER_FATAL_ERROR_READING_BINLOG
# is sent to a slave when it requests a dump from the master, thus leading the
# I/O thread to stop. However, there is no Last_IO_Error reported.
#

--let $rpl_only_running_threads= 1
--source include/rpl_reset.inc
--connection master
DROP TABLE t1;
--source include/sync_slave_sql_with_master.inc


connection master;
CREATE TABLE t1 (f1 int PRIMARY KEY, f2 LONGTEXT, f3 LONGTEXT) ENGINE=MyISAM;
--source include/sync_slave_sql_with_master.inc

connection master;
INSERT INTO t1(f1, f2, f3) VALUES(1, REPEAT('a', @@global.max_allowed_packet), REPEAT('b', @@global.max_allowed_packet));

connection slave;
# The slave I/O thread must stop after receiving
# 1153 = ER_NET_PACKET_TOO_LARGE
--let $slave_io_errno= 1153
--let $show_slave_io_error= 1
--source include/wait_for_slave_io_error.inc

# Remove the bad binlog and clear error status on slave.
STOP SLAVE;
RESET SLAVE;
RESET MASTER;
--connection master
RESET MASTER;


#
# BUG#55322: SHOW BINLOG EVENTS increases @@SESSION.MAX_ALLOWED_PACKET
#
# In BUG#55322, @@session.max_allowed_packet increased each time SHOW
# BINLOG EVENTS was issued. To verify that this bug is fixed, we
# execute SHOW BINLOG EVENTS twice and check that max_allowed_packet
# never changes.  We turn off the result log because we don't care
# about the contents of the binlog.

--disable_result_log
SET @max_allowed_packet_0= @@session.max_allowed_packet;
SHOW BINLOG EVENTS;
SET @max_allowed_packet_1= @@session.max_allowed_packet;
SHOW BINLOG EVENTS;
SET @max_allowed_packet_2= @@session.max_allowed_packet;
--enable_result_log
if (`SELECT NOT(@max_allowed_packet_0 = @max_allowed_packet_1 AND @max_allowed_packet_1 = @max_allowed_packet_2)`)
{
  --echo ERROR: max_allowed_packet changed after executing SHOW BINLOG EVENTS
  --source include/show_rpl_debug_info.inc
  SELECT @max_allowed_packet_0, @max_allowed_packet_1, @max_allowed_packet_2;
  --die @max_allowed_packet changed after executing SHOW BINLOG EVENTS
}

#
# BUG#55790
# This test verifies if the header of Table_map_log_event
# will be displayed by MYSQLBINLOG so long as it was written
# to the relay log, even if its rows events are discarded.
# And MYSQLBINLOG will print a warning for the missed
# rows event.
#

connection slave;

START SLAVE;
--source include/wait_for_slave_to_start.inc

connection master;
DROP TABLE t1;
select @@global.max_allowed_packet;
select @@global.net_buffer_length;
--source include/sync_slave_sql_with_master.inc
select @@global.max_allowed_packet;
select @@global.net_buffer_length;

--connection master
CREATE TABLE t1 (a TEXT) ENGINE=MyISAM;
--source include/sync_slave_sql_with_master.inc

# Create big row event.
--connection master
INSERT INTO t1 VALUES (REPEAT('a', @@global.max_allowed_packet));

# Slave IO thread should stop with error when trying to read the big event.
--connection slave
--echo # 1153 = ER_NET_PACKET_TOO_LARGE
--let $slave_io_errno= 1153
--let $show_slave_io_error= 1
--source include/wait_for_slave_io_error.inc

--echo # Record Read_Master_Log_Pos for the Table_map_log_event
--let $table_map_log_pos= query_get_value("SHOW SLAVE STATUS", Read_Master_Log_Pos, 1)

--let $MYSQLD_DATADIR= `SELECT @@datadir`
--let $start_pos= query_get_value("SHOW SLAVE STATUS", Relay_Log_Pos, 1)
--let $relay_log_file= query_get_value(SHOW SLAVE STATUS, Relay_Log_File, 1)
--let $outfile= $MYSQLTEST_VARDIR/tmp/b55790-mysqlbinlog
--let $i= 2

--let $old_slave_max_allowed_packet= `SELECT @@global.max_allowed_packet`
SET @@global.max_allowed_packet=1048576;
# Restart slave session for new settings to take effect.
--connect (slave2,127.0.0.1,root,,test,$SLAVE_MYPORT,)
--connection slave2

while($i)
{
  --let $flags=--start-position=$start_pos

  if($i==2)
  {
    --let $flags= $flags --base64-output=decode-rows -v
  }

  --exec $MYSQL_BINLOG $flags $MYSQLD_DATADIR/$relay_log_file 1> $outfile.1 2> $outfile.2
  -- let POS=$table_map_log_pos
  -- let FLGS=$flags
  -- let OUTFILE_1=$outfile.1

  -- perl
$found=0;
$file= $ENV{'OUTFILE_1'};
$table_map_log_pos= $ENV{'POS'};
$flags= $ENV{'FLGS'};

open(FILE, "$file") or die("Unable to open $file: $!\n");
while(<FILE>) {
  if (/end_log_pos $table_map_log_pos .*Table_map/) {
    $found=1;
    break;
  }
}

if (!$found)
{
  print "**** ERROR **** Table map 'end_log_pos $table_map_log_pos  Table_map' NOT FOUND in mysqlbinlog output ($flags $file).";
  seek(FILE, 0, 0);
  while($line= <FILE>) {
    print $line;
  }
}
close(FILE);
EOF

  # Check the warning is caused
  --cat_file $outfile.2

  # Remove unecessary files
  --remove_file $outfile.1
  --remove_file $outfile.2

  --dec $i
}

--echo ==== clean up ====
connection master;
DROP TABLE t1;
eval SET @@global.max_allowed_packet= $old_max_allowed_packet;
eval SET @@global.net_buffer_length= $old_net_buffer_length;
eval SET @@global.slave_max_allowed_packet= $old_slave_max_allowed_packet_temp;
# slave is stopped
connection slave;
eval SET @@global.max_allowed_packet= $old_slave_max_allowed_packet;
DROP TABLE t1;

# Clear Last_IO_Error

--source include/stop_slave_sql.inc
RESET SLAVE;

--source include/rpl_end.inc
# End of tests
