Commit 0aa3aee0 authored by Alfranio Correia's avatar Alfranio Correia

BUG#46364 MyISAM transbuffer problems (NTM problem)

It is well-known that due to concurrency issues, a slave can become
inconsistent when a transaction contains updates to both transaction and
non-transactional tables in statement and mixed modes.

In a nutshell, the current code-base tries to preserve causality among the
statements by writing non-transactional statements to the txn-cache which
is flushed upon commit. However, modifications done to non-transactional
tables on behalf of a transaction become immediately visible to other
connections but may not immediately get into the binary log and therefore
consistency may be broken.

In general, it is impossible to automatically detect causality/dependency
among statements by just analyzing the statements sent to the server. This
happen because dependency may be hidden in the application code and it is
necessary to know a priori all the statements processed in the context of
a transaction such as in a procedure. Moreover, even for the few cases that
we could automatically address in the server, the computation effort
required could make the approach infeasible.

So, in this patch we introduce the option
    - "--binlog-direct-non-transactional-updates" that can be used to bypass
    the current behavior in order to write directly to binary log statements
    that change non-transactional tables.
parent fdf1441a
This diff is collapsed.
This diff is collapsed.
--binlog-direct-non-transactional-updates
################################################################################
# This test case checks if the option "binlog-direct-non-transactional-updates"
# makes non-transactional changes in the statement format to be written to the
# binary log as soon as the statement commits.
#
# In what follows, we use the include file rpl_mixing_engines.inc to generate
# sql commands from a format string. The format string consists of a sequence of
# 'codes' separated by spaces. Before it set of commands, we paste the expected
# sequence in the binary log. The following codes exist:
#
# - Define the scope of a transaction:
# B - Begin.
# C - Commit.
# R - Rollback.
#
# - Change only T-Tables:
# T - Updates a T-Table.
# T-trig - Updates T-Tables through a trigger.
# T-func - Updates T-Tables through a function.
# T-proc - Updates T-Tables through a procedure.
# eT - Fails while updating the first tuple in a T-Table.
# Te - Fails while updating an n-tuple (n > 1) in a T-Table.
# Te-trig - Fails while updating an n-tuple (n > 1) in a T-Table.
# Te-func - Fails while updating an n-tuple (n > 1) in a T-Table.
#
# - Change only N-Tables
# N - Updates a N-Table.
# N-trig - Updates N-Tables through a trigger.
# N-func - Updates N-Tables through a function.
# N-proc - Updates N-Tables through a procedure.
# eN - Fails while updating the first tuple in a N-Table.
# Ne - Fails while updating an n-tuple (n > 1) in a N-Table.
# Ne-trig - Fails while updating an n-tuple (n > 1) in a N-Table.
# Ne-func - Fails while updating an n-tuple (n > 1) in a N-Table.
################################################################################
--source include/have_binlog_format_statement.inc
--source include/master-slave.inc
--source include/have_innodb.inc
set @@session.binlog_direct_non_transactional_updates= TRUE;
--echo #########################################################################
--echo # CONFIGURATION
--echo #########################################################################
--let $engine_type= Innodb
SET @commands= 'configure';
--source extra/rpl_tests/rpl_mixing_engines.inc
--echo #########################################################################
--echo # 1 - BINLOG ORDER
--echo #########################################################################
connection master;
--echo
--echo
--echo
--echo
--echo #
--echo #3) Generates in the binlog what follows:
--echo # --> STMT "N B T C" entries, format S.
--echo #
SET @commands= 'B T N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-trig C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-func C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-proc C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-trig C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-func C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-proc C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-trig C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-func C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-proc C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-trig C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-func C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-proc C';
--source extra/rpl_tests/rpl_mixing_engines.inc
--echo
--echo
--echo
--echo
--echo #
--echo #3.e) Generates in the binlog what follows if T-* fails:
--echo # --> STMT "N" entry, format S.
--echo # Otherwise, what follows if N-* fails and a N-Table is changed:
--echo # --> STMT "N B T C" entries, format S.
--echo #
SET @commands= 'B eT N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B Te N C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T eN C';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T Ne C';
--source extra/rpl_tests/rpl_mixing_engines.inc
--echo
--echo
--echo
--echo
--echo #
--echo #4) Generates in the binlog what follows:
--echo # --> STMT "N B T R" entries, format S.
--echo #
SET @commands= 'B T N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-trig R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-func R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T N-proc R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-trig R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-func R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-trig N-proc R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-trig R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-func R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-func N-proc R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-trig R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-func R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T-proc N-proc R';
--source extra/rpl_tests/rpl_mixing_engines.inc
--echo
--echo
--echo
--echo
--echo #
--echo #4.e) Generates in the binlog what follows if T* fails:
--echo # --> STMT "B N C" entry, format S.
--echo # Otherwise, what follows if N* fails and a N-Table is changed:
--echo # --> STMT "N" entries, format S.
--echo #
SET @commands= 'B eT N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B Te N R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T eN R';
--source extra/rpl_tests/rpl_mixing_engines.inc
SET @commands= 'B T Ne R';
--source extra/rpl_tests/rpl_mixing_engines.inc
--echo ###################################################################################
--echo # CHECK CONSISTENCY
--echo ###################################################################################
connection master;
sync_slave_with_master;
--exec $MYSQL_DUMP --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/test-nmt-master.sql
--exec $MYSQL_DUMP_SLAVE --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/test-nmt-slave.sql
--diff_files $MYSQLTEST_VARDIR/tmp/test-nmt-master.sql $MYSQLTEST_VARDIR/tmp/test-nmt-slave.sql
--echo ###################################################################################
--echo # CLEAN
--echo ###################################################################################
SET @commands= 'clean';
--source extra/rpl_tests/rpl_mixing_engines.inc
...@@ -4284,12 +4284,20 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) ...@@ -4284,12 +4284,20 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info)
#if defined(USING_TRANSACTIONS) #if defined(USING_TRANSACTIONS)
/* /*
Should we write to the binlog cache or to the binlog on disk? Should we write to the binlog cache or to the binlog on disk?
Write to the binlog cache if: Write to the binlog cache if:
- it is already not empty (meaning we're in a transaction; note that the 1 - a transactional engine/table is updated (stmt_has_updated_trans_table == TRUE);
present event could be about a non-transactional table, but still we need 2 - or the event asks for it (cache_stmt == TRUE);
to write to the binlog cache in that case to handle updates to mixed 3 - or the cache is already not empty (meaning we're in a transaction;
trans/non-trans table types the best possible in binlogging) note that the present event could be about a non-transactional table, but
- or if the event asks for it (cache_stmt == TRUE). still we need to write to the binlog cache in that case to handle updates
to mixed trans/non-trans table types).
Write to the binlog on disk if only a non-transactional engine is
updated and:
1 - the binlog cache is empty or;
2 - --binlog-direct-non-transactional-updates is set and we are about to
use the statement format. When using the row format (cache_stmt == TRUE).
*/ */
if (opt_using_transactions && thd) if (opt_using_transactions && thd)
{ {
...@@ -4300,8 +4308,9 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info) ...@@ -4300,8 +4308,9 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info)
(binlog_trx_data*) thd_get_ha_data(thd, binlog_hton); (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
IO_CACHE *trans_log= &trx_data->trans_log; IO_CACHE *trans_log= &trx_data->trans_log;
my_off_t trans_log_pos= my_b_tell(trans_log); my_off_t trans_log_pos= my_b_tell(trans_log);
if (event_info->get_cache_stmt() || trans_log_pos != 0 || if (event_info->get_cache_stmt() || stmt_has_updated_trans_table(thd) ||
stmt_has_updated_trans_table(thd)) (!thd->variables.binlog_direct_non_trans_update &&
trans_log_pos != 0))
{ {
DBUG_PRINT("info", ("Using trans_log: cache: %d, trans_log_pos: %lu", DBUG_PRINT("info", ("Using trans_log: cache: %d, trans_log_pos: %lu",
event_info->get_cache_stmt(), event_info->get_cache_stmt(),
......
...@@ -5727,7 +5727,8 @@ enum options_mysqld ...@@ -5727,7 +5727,8 @@ enum options_mysqld
OPT_SLAVE_EXEC_MODE, OPT_SLAVE_EXEC_MODE,
OPT_GENERAL_LOG_FILE, OPT_GENERAL_LOG_FILE,
OPT_SLOW_QUERY_LOG_FILE, OPT_SLOW_QUERY_LOG_FILE,
OPT_IGNORE_BUILTIN_INNODB OPT_IGNORE_BUILTIN_INNODB,
OPT_BINLOG_DIRECT_NON_TRANS_UPDATE
}; };
...@@ -7071,6 +7072,10 @@ The minimum value for this variable is 4096.", ...@@ -7071,6 +7072,10 @@ The minimum value for this variable is 4096.",
(uchar**) &max_system_variables.net_wait_timeout, 0, GET_ULONG, (uchar**) &max_system_variables.net_wait_timeout, 0, GET_ULONG,
REQUIRED_ARG, NET_WAIT_TIMEOUT, 1, IF_WIN(INT_MAX32/1000, LONG_TIMEOUT), REQUIRED_ARG, NET_WAIT_TIMEOUT, 1, IF_WIN(INT_MAX32/1000, LONG_TIMEOUT),
0, 1, 0}, 0, 1, 0},
{"binlog-direct-non-transactional-updates", OPT_BINLOG_DIRECT_NON_TRANS_UPDATE,
"Causes updates to non-transactional engines using statement format to be written directly to binary log. Before using this option make sure that there are no dependencies between transactional and non-transactional tables such as in the statement INSERT INTO t_myisam SELECT * FROM t_innodb; otherwise, slaves may diverge from the master.",
(uchar**) &global_system_variables.binlog_direct_non_trans_update, (uchar**) &max_system_variables.binlog_direct_non_trans_update, 0, GET_BOOL, NO_ARG, 0,
0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
}; };
......
...@@ -181,6 +181,8 @@ static sys_var_long_ptr sys_binlog_cache_size(&vars, "binlog_cache_size", ...@@ -181,6 +181,8 @@ static sys_var_long_ptr sys_binlog_cache_size(&vars, "binlog_cache_size",
&binlog_cache_size); &binlog_cache_size);
static sys_var_thd_binlog_format sys_binlog_format(&vars, "binlog_format", static sys_var_thd_binlog_format sys_binlog_format(&vars, "binlog_format",
&SV::binlog_format); &SV::binlog_format);
static sys_var_thd_bool sys_binlog_direct_non_trans_update(&vars, "binlog_direct_non_transactional_updates",
&SV::binlog_direct_non_trans_update);
static sys_var_thd_ulong sys_bulk_insert_buff_size(&vars, "bulk_insert_buffer_size", static sys_var_thd_ulong sys_bulk_insert_buff_size(&vars, "bulk_insert_buffer_size",
&SV::bulk_insert_buff_size); &SV::bulk_insert_buff_size);
static sys_var_const_os sys_character_sets_dir(&vars, static sys_var_const_os sys_character_sets_dir(&vars,
......
...@@ -353,6 +353,7 @@ struct system_variables ...@@ -353,6 +353,7 @@ struct system_variables
ulong ndb_index_stat_cache_entries; ulong ndb_index_stat_cache_entries;
ulong ndb_index_stat_update_freq; ulong ndb_index_stat_update_freq;
ulong binlog_format; // binlog format for this thd (see enum_binlog_format) ulong binlog_format; // binlog format for this thd (see enum_binlog_format)
my_bool binlog_direct_non_trans_update;
/* /*
In slave thread we need to know in behalf of which In slave thread we need to know in behalf of which
thread the query is being run to replicate temp tables properly thread the query is being run to replicate temp tables properly
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment