# Purpose of this test:
#    Check that server and InnoDB behave correct and give the right messages in
#    case it is tried to restart the server with
#    - plain wrong
#    - too small
#    - unsupported between too small and too high
#    - too high
#    - legal but not to the size during first server startup fitting
#    innodb page size. So every restart attempt with such a value
#    has to fail.
#
# This test should pass with calls of mtr and the following assignments
#    --mysqld=--innodb-page-size=4k
#    --mysqld=--innodb-page-size=4096
#    --mysqld=--innodb-page-size=8k
#    --mysqld=--innodb-page-size=8192
#    --mysqld=--innodb-page-size=16k
#    --mysqld=--innodb-page-size=16384
#    <no assignment of innodb-page-size at all>
#
# The current test is placed within the suite "innodb" though many tests for
# system variables are stored within the suite "sys_vars". The reason is the
# following:
#    The test is adjusted to be used with different legal innodb page sizes
#    assigned on command line.
#    Our current test collections run the test suite
#    - sys_vars   without assignment of innodb-page-size
#    - innodb     with several different assignments of innodb-page-size
#    Therefore placing this test within the suite "innodb" is better.
#
# Created: 2011-11-11 mleich
#

# Set this variable to
# -   0 for standard test runs (file with expected results is made for such).
# -   1 for debugging.(a result difference will show up)
let $test_debug= 0;

--echo # 0. Check and generate prerequites
#-----------------------------------------
--source include/not_embedded.inc
--source include/have_innodb.inc

# InnoDb page size values in 2011-11
# - supported: 4k, 8k, 16k
# - default:  16k
let $minimal_page_size=  4096;
let $default_page_size= 16384;
let $maximal_page_size= 16384;
let $legal_page_size_list= $minimal_page_size,8192,$maximal_page_size;
let $start_page_size=
`SELECT variable_value FROM INFORMATION_SCHEMA.GLOBAL_STATUS
WHERE LOWER(variable_name) = 'innodb_page_size'`;
if(`SELECT $start_page_size NOT IN ($legal_page_size_list)`)
{
   --echo # ERROR: The current innodb page size ($start_page_size) is not in
   --echo #        the list of expected legal page sizes : $legal_page_size_list
   --echo #        abort
   exit;
}
let $other_page_size= $minimal_page_size;
if($start_page_size == $minimal_page_size)
{
   let $other_page_size= $maximal_page_size;
}
let $other_page_size_k=    `SELECT $other_page_size DIV 1024`;
let $other_page_size_nk=       `SELECT CONCAT($other_page_size_k,'k')`;
let $other_page_size_nkaramel= `SELECT CONCAT($other_page_size_k,'karamel')`;
let $other_page_size_ncaramel= `SELECT CONCAT($other_page_size_k,'caramel')`;
if ($start_page_size < $maximal_page_size)
{
   let $out_range_page_size= `SELECT $maximal_page_size * 4`;
   let $adjust_page_size = $maximal_page_size;
}
if ($start_page_size > $minimal_page_size)
{
   let $out_range_page_size= `SELECT $minimal_page_size DIV 2`;
   let $adjust_page_size = $minimal_page_size;
}
let $illegal_pagesize= `SELECT $minimal_page_size + 1`;
let $zero_test= 1;
if ($start_page_size == $minimal_page_size)
{
   let $zero_test= 0;
}
if ($test_debug)
{
   --echo # --- Set in test script --------------------------
   --echo # minimal_page_size :        $minimal_page_size
   --echo # default_page_size :        $default_page_size
   --echo # maximal_page_size :        $maximal_page_size
   --echo # legal_page_size_list :     $legal_page_size_list
   --echo # --- Calculated ---------------------------------
   --echo # start_page_size :          $start_page_size
   --echo # other_page_size_k :        $other_page_size_k
   --echo # other_page_size_nk :       $other_page_size_nk
   --echo # other_page_size_nkaramel : $other_page_size_nkaramel
   --echo # other_page_size_ncaramel : $other_page_size_ncaramel
   --echo # out_range_page_size :      $out_range_page_size
   --echo # adjust_page_size :         $adjust_page_size
   --echo # illegal_pagesize :         $illegal_pagesize
   --echo # zero_test :                $zero_test
}

# We let our server restart attempts write to the file $error_log.
let $error_log= $MYSQLTEST_VARDIR/log/my_restart.err;
--error 0,1
--remove_file $error_log
# $error_log has to be processed by include/search_pattern_in_file.inc which
# contains Perl code requiring that the environment variable SEARCH_FILE points
# to this file.
let SEARCH_FILE= $error_log;
# Stop the server
let $restart_file= $MYSQLTEST_VARDIR/tmp/mysqld.1.expect;
--exec echo "wait" > $restart_file
--shutdown_server 10
--source include/wait_until_disconnected.inc


--echo # 1. Try to restart the server with some other legal innodb page size value.
--echo #    It must fail because we had a different value before.
#----------------------------------------------------------------------------------
--echo # 1.1 The value assigned is a number.
# Detailed explanations of what happens are placed nearby the checks.
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$other_page_size --loose-console > $error_log 2>&1
if ( $default_page_size != $other_page_size )
{
   # It gets detected that the assigned page size is not the default one
   let SEARCH_PATTERN= InnoDB: innodb-page-size has been changed from the default value $default_page_size to $other_page_size;
   --source include/search_pattern_in_file.inc
}
# We get depending on the platform either "./ibdata1" or ".\ibdata1".
let SEARCH_PATTERN=innodb-page-size mismatch in data file ..ibdata1;
--source include/search_pattern_in_file.inc
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc
# The server restart aborts
let SEARCH_PATTERN= \[ERROR\] Aborting;
--source include/search_pattern_in_file.inc
--echo # 1.2 The value assigned is a number followed by 'k'.
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$other_page_size_nk --loose-console > $error_log 2>&1
if ( $default_page_size != $other_page_size )
{
   # It gets detected that the assigned page size is not the default one
   let SEARCH_PATTERN= InnoDB: innodb-page-size has been changed from the default value $default_page_size to $other_page_size;
   --source include/search_pattern_in_file.inc
}
let SEARCH_PATTERN=innodb-page-size mismatch in data file ..ibdata1;

--source include/search_pattern_in_file.inc
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc
# The server restart aborts
let SEARCH_PATTERN= \[ERROR\] Aborting;
--source include/search_pattern_in_file.inc


--echo # 2. Try to restart the server with some innodb page size.which is either
--echo #    bigger than the maximum or smaller than the minimum supported one.
--echo #    It must fail because we had a different value before.
#-------------------------------------------------------------------------------
# Properties of the value:
# 1. not supported
# 2. below the smallest supported one
# The restart attempt has to fail.
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$out_range_page_size --loose-console > $error_log 2>&1
# The innodb page size gets raised to the lowest or biggest legal value.
let SEARCH_PATTERN= \[Warning\] option 'innodb-page-size': unsigned value $out_range_page_size adjusted to $adjust_page_size;
--source include/search_pattern_in_file.inc
if ( $default_page_size != $adjust_page_size )
{
   # It gets detected that the assigned page size is not the default one
   let SEARCH_PATTERN= InnoDB: innodb-page-size has been changed from the default value $default_page_size to $adjust_page_size;
   --source include/search_pattern_in_file.inc
}
# Some checks omitted because they are already in 1.
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc


--echo # 3. Try to restart the server with the plain wrong innodb page size value "garbage".
--echo #    The restart attempt has to
--echo #    - fail in case start of test page size <> the minimal legal page size.
--echo #    - to be successful in case start of test page size = minimal legal page size.
--echo #      In this case we omit the execution of the current sub test!
#-------------------------------------------------------------------------------------------
if ($zero_test)
{
   if ($test_debug)
   {
     --echo # We do not omit the test.
   }
--error 1
--exec $MYSQLD_CMD --innodb-page-size=garbage --loose-console > $error_log 2>&1
# General server properties cause that
# - the plain wrong value "garbage" assigned to innodb page size gets
#   mangled to 0.
# - the 0 which is below the smallest possible page size is adjusted
#   to the smallest possible page size
let SEARCH_PATTERN= \[Warning\] option 'innodb-page-size': unsigned value 0 adjusted to $minimal_page_size;
--source include/search_pattern_in_file.inc
# It gets detected that the innodb page size has been changed.
let SEARCH_PATTERN= InnoDB: innodb-page-size has been changed from the default value $default_page_size to $minimal_page_size;
--source include/search_pattern_in_file.inc
# Some checks omitted because they are already in 1.
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc
}


--echo # 4. Try to restart the server with some illegal innodb page size value
--echo #    being between minimum and maximum legal page size value.
--echo #    The restart attempt has to fail.
#------------------------------------------------------------------------------
# Properties of the value:
# 1. not supported
# 2. between smallest (4k) and biggest (16k) supported one
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$illegal_pagesize --loose-console > $error_log 2>&1
# InnoDB cannot handle this value.
let SEARCH_PATTERN= \[ERROR\] InnoDB: Invalid page size=$illegal_pagesize;
--source include/search_pattern_in_file.inc
# Some checks omitted because they are already in 1.
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc


--echo # 5. Try to restart the server with wrong innodb page size <number>karamel.
--echo #    <number>k is a legal page size.
#---------------------------------------------------------------------------------
# The restart attempt has to fail.
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$other_page_size_nkaramel --loose-console > $error_log 2>&1
# The plain wrong value "<number>karamel" assigned to innodb page size seems
# to get mangled to "<number>k" which is a legal value.
if ( $default_page_size != $adjust_page_size )
{
   # It gets detected that the assigned page size is not the default one
   let SEARCH_PATTERN= InnoDB: innodb-page-size has been changed from the default value $default_page_size to $other_page_size;
   --source include/search_pattern_in_file.inc
}
# But this change does not fit to the history of the data.
# Some checks omitted because they are already in 1.
# Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Aborting;
--source include/search_pattern_in_file.inc


--echo # 6. Try to restart the server with the plain wrong innodb page size <number>caramel
--echo #    <number>k is a legal page size.
--echo #    The restart attempt has to
--echo #    - fail in case start of test page size <> the minimal legal page size.
--echo #    - to be successful in case start of test page size = minimal legal page size.
--echo #      In this case we omit the execution of the current sub test!
#------------------------------------------------------------------------------------------
if ($zero_test)
{
   if ($test_debug)
   {
     --echo # We do not omit the test.
   }
--error 1
--exec $MYSQLD_CMD --innodb-page-size=$other_page_size_ncaramel --loose-console > $error_log 2>&1
# The server dislikes the assigned value.
let SEARCH_PATTERN= Unknown suffix 'c' used for variable 'innodb-page-size' \(value '$other_page_size_ncaramel'\);
--source include/search_pattern_in_file.inc
# The plain wrong value "<number>caramel" assigned to innodb page size gets
# mangled to 0.
let SEARCH_PATTERN= \[Warning\] option 'innodb-page-size': unsigned value 0 adjusted to $minimal_page_size;
--source include/search_pattern_in_file.inc
# Some other layer has also something to tell.
let SEARCH_PATTERN= mysqld.*: Error while setting value '$other_page_size_ncaramel' to 'innodb-page-size';
--source include/search_pattern_in_file.inc
#    Finally InnoDB becomes an Unknown/unsupported storage engine
let SEARCH_PATTERN= \[ERROR\] Unknown/unsupported storage engine: InnoDB;
--source include/search_pattern_in_file.inc
}

--echo # 7. Restart the server and cleanup
#-----------------------------------------
--enable_reconnect
--exec echo "restart" > $restart_file
--source include/wait_until_connected_again.inc
--error 0,1
--remove_file $restart_file
--remove_file $error_log


