summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorunknown <sven@riska.(none)>2007-12-14 14:40:45 +0100
committerunknown <sven@riska.(none)>2007-12-14 14:40:45 +0100
commit44efa9c18b949fa7078e44aef8aba75a4e8bb4db (patch)
tree2ab60f1a9c03c3267067622a25f5ba05ee32fb82 /sql
parent89a89950aeebaf46ff8332981997e80b99683ed6 (diff)
downloadmariadb-git-44efa9c18b949fa7078e44aef8aba75a4e8bb4db.tar.gz
BUG#26395: if crash during autocommit update to transactional table on master, slave fails
Now, every transaction (including autocommit transactions) starts with a BEGIN and ends with a COMMIT/ROLLBACK in the binlog. Added a test case, and updated lots of test case result files. mysql-test/r/multi_update.result: Updated result file mysql-test/r/sp_trans_log.result: Updated result file mysql-test/suite/binlog/r/binlog_innodb.result: Updated result file mysql-test/suite/binlog/r/binlog_multi_engine.result: Updated result file mysql-test/suite/binlog/r/binlog_stm_blackhole.result: Updated result file mysql-test/suite/binlog/r/binlog_stm_mix_innodb_myisam.result: Updated result file mysql-test/suite/ndb/r/ndb_binlog_format.result: Updated result file mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result: Updated result file mysql-test/suite/rpl/r/rpl_row_charset_innodb.result: Updated result file mysql-test/suite/rpl/r/rpl_row_create_table.result: Updated result file mysql-test/suite/rpl/r/rpl_row_log_innodb.result: Updated result file mysql-test/suite/rpl/r/rpl_switch_stm_row_mixed.result: Updated result file mysql-test/suite/rpl/r/rpl_truncate_3innodb.result: Updated result file mysql-test/suite/rpl/t/rpl_row_create_table.test: Updated result file mysql-test/suite/rpl_ndb/r/rpl_ndb_stm_innodb.result: Updated result file sql/log.cc: - Always write BEGIN and COMMIT around statements, even in autocommit mode. - Added comments for binlog_commit and binlog_rollback. sql/log_event.cc: Added debug trigger to avoid writing xid events to the binlog. mysql-test/suite/rpl_ndb/r/rpl_ndb_transaction.result: Results for new test case mysql-test/suite/rpl_ndb/t/rpl_ndb_transaction-master.opt: Options for new test case mysql-test/suite/rpl_ndb/t/rpl_ndb_transaction-slave.opt: Options for new test case mysql-test/suite/rpl_ndb/t/rpl_ndb_transaction.test: Added new test case.
Diffstat (limited to 'sql')
-rw-r--r--sql/log.cc110
-rw-r--r--sql/log_event.cc1
2 files changed, 82 insertions, 29 deletions
diff --git a/sql/log.cc b/sql/log.cc
index 9fdede9ef2c..b6916c855ab 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -1402,6 +1402,21 @@ static int binlog_prepare(handlerton *hton, THD *thd, bool all)
return 0;
}
+/**
+ This function is called once after each statement.
+
+ It has the responsibility to flush the transaction cache to the
+ binlog file on commits.
+
+ @param hton The binlog handlerton.
+ @param thd The client thread that executes the transaction.
+ @param all true if this is the last statement before a COMMIT
+ statement; false if either this is a statement in a
+ transaction but not the last, or if this is a statement
+ not inside a BEGIN block and autocommit is on.
+
+ @see handlerton::commit
+*/
static int binlog_commit(handlerton *hton, THD *thd, bool all)
{
DBUG_ENTER("binlog_commit");
@@ -1414,7 +1429,15 @@ static int binlog_commit(handlerton *hton, THD *thd, bool all)
trx_data->reset();
DBUG_RETURN(0);
}
- if (all)
+ /*
+ Write commit event if at least one of the following holds:
+ - the user sends an explicit COMMIT; or
+ - the autocommit flag is on, and we are not inside a BEGIN.
+ However, if the user has not sent an explicit COMMIT, and we are
+ either inside a BEGIN or run with autocommit off, then this is not
+ the end of a transaction and we should not write a commit event.
+ */
+ if (all || !(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
{
Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), TRUE, FALSE);
qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
@@ -1428,6 +1451,23 @@ static int binlog_commit(handlerton *hton, THD *thd, bool all)
}
}
+/**
+ This function is called when a transaction involving a transactional
+ table is rolled back.
+
+ It has the responsibility to flush the transaction cache to the
+ binlog file. However, if the transaction does not involve
+ non-transactional tables, nothing needs to be logged.
+
+ @param hton The binlog handlerton.
+ @param thd The client thread that executes the transaction.
+ @param all true if this is the last statement before a COMMIT
+ statement; false if either this is a statement in a
+ transaction but not the last, or if this is a statement
+ not inside a BEGIN block and autocommit is on.
+
+ @see handlerton::rollback
+*/
static int binlog_rollback(handlerton *hton, THD *thd, bool all)
{
DBUG_ENTER("binlog_rollback");
@@ -3967,9 +4007,11 @@ int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
IMPLEMENTATION
- To support transaction over replication, we wrap the transaction
with BEGIN/COMMIT or BEGIN/ROLLBACK in the binary log.
- We want to write a BEGIN/ROLLBACK block when a non-transactional table
- was updated in a transaction which was rolled back. This is to ensure
- that the same updates are run on the slave.
+ If a transaction that only involves transactional tables is
+ rolled back, we do not binlog it. However, we write a
+ BEGIN/ROLLBACK block when a non-transactional table was updated
+ in a transaction which was rolled back. This is to ensure that
+ the same updates are run on the slave.
*/
bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
@@ -3990,32 +4032,42 @@ bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
if (my_b_tell(cache) > 0)
{
/*
- Log "BEGIN" at the beginning of the transaction.
- which may contain more than 1 SQL statement.
+ Log "BEGIN" at the beginning of every transaction. Here, a
+ transaction is either a BEGIN..COMMIT block or a single
+ statement in autocommit mode.
*/
- if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
- {
- Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), TRUE, FALSE);
- /*
- Imagine this is rollback due to net timeout, after all statements of
- the transaction succeeded. Then we want a zero-error code in BEGIN.
- In other words, if there was a really serious error code it's already
- in the statement's events, there is no need to put it also in this
- internally generated event, and as this event is generated late it
- would lead to false alarms.
- This is safer than thd->clear_error() against kills at shutdown.
- */
- qinfo.error_code= 0;
- /*
- Now this Query_log_event has artificial log_pos 0. It must be adjusted
- to reflect the real position in the log. Not doing it would confuse the
- slave: it would prevent this one from knowing where he is in the
- master's binlog, which would result in wrong positions being shown to
- the user, MASTER_POS_WAIT undue waiting etc.
- */
- if (qinfo.write(&log_file))
- goto err;
- }
+ Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), TRUE, FALSE);
+ /*
+ Imagine this is rollback due to net timeout, after all
+ statements of the transaction succeeded. Then we want a
+ zero-error code in BEGIN. In other words, if there was a
+ really serious error code it's already in the statement's
+ events, there is no need to put it also in this internally
+ generated event, and as this event is generated late it would
+ lead to false alarms.
+
+ This is safer than thd->clear_error() against kills at shutdown.
+ */
+ qinfo.error_code= 0;
+ /*
+ Now this Query_log_event has artificial log_pos 0. It must be
+ adjusted to reflect the real position in the log. Not doing it
+ would confuse the slave: it would prevent this one from
+ knowing where he is in the master's binlog, which would result
+ in wrong positions being shown to the user, MASTER_POS_WAIT
+ undue waiting etc.
+ */
+ if (qinfo.write(&log_file))
+ goto err;
+
+ DBUG_EXECUTE_IF("crash_before_writing_xid",
+ {
+ if ((write_error= write_cache(cache, false, true)))
+ DBUG_PRINT("info", ("error writing binlog cache: %d",
+ write_error));
+ DBUG_PRINT("info", ("crashing before writing xid"));
+ abort();
+ });
if ((write_error= write_cache(cache, false, false)))
goto err;
diff --git a/sql/log_event.cc b/sql/log_event.cc
index f328f266c05..cdfd58535fb 100644
--- a/sql/log_event.cc
+++ b/sql/log_event.cc
@@ -4292,6 +4292,7 @@ Xid_log_event(const char* buf,
#ifndef MYSQL_CLIENT
bool Xid_log_event::write(IO_CACHE* file)
{
+ DBUG_EXECUTE_IF("do_not_write_xid", return 0;);
return write_header(file, sizeof(xid)) ||
my_b_safe_write(file, (uchar*) &xid, sizeof(xid));
}