Skip to content

Commit

Permalink
MDEV-31273: Eliminate Log_event::checksum_alg
Browse files Browse the repository at this point in the history
This is a preparatory commit for pre-computing checksums outside of
holding LOCK_log, no functional changes.

Which checksum algorithm is used (if any) when writing an event does not
belong in the event, it is a property of the log being written to.

Instead decide the checksum algorithm when constructing the
Log_event_writer object, and store it there.

Introduce a client-only Log_event::read_checksum_alg to be able to
print the checksum read, and a
Format_description_log_event::source_checksum_alg which is the
checksum algorithm (if any) to use when reading events from a log.

Also eliminate some redundant `enum` keywords on the enum_binlog_checksum_alg
type.

Reviewed-by: Monty <monty@mariadb.org>
Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
  • Loading branch information
knielsen committed Oct 26, 2023
1 parent 77bd1be commit 8eee980
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
RESET MASTER;
#
# MDEV-30698 Cover missing test cases for mariadb-binlog options
# --raw [and] --flashback
Expand Down
2 changes: 2 additions & 0 deletions mysql-test/suite/binlog/t/binlog_mysqlbinlog_raw_flush.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
--source include/linux.inc
--source include/have_log_bin.inc

RESET MASTER;

--echo #
--echo # MDEV-30698 Cover missing test cases for mariadb-binlog options
--echo # --raw [and] --flashback
Expand Down
82 changes: 50 additions & 32 deletions sql/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3863,23 +3863,21 @@ Event_log::write_description_event(enum_binlog_checksum_alg checksum_alg,
bool encrypt, bool dont_set_created,
bool is_relay_log)
{
Format_description_log_event s(BINLOG_VERSION);
Format_description_log_event s(BINLOG_VERSION, NULL, checksum_alg);
/*
don't set LOG_EVENT_BINLOG_IN_USE_F for SEQ_READ_APPEND io_cache
as we won't be able to reset it later
*/
if (io_cache_type == WRITE_CACHE)
s.flags |= LOG_EVENT_BINLOG_IN_USE_F;
s.checksum_alg= checksum_alg;
if (is_relay_log)
s.set_relay_log_event();

crypto.scheme = 0;
DBUG_ASSERT(s.checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
if (!s.is_valid())
return -1;
s.dont_set_created= dont_set_created;
if (write_event(&s, 0, &log_file))
if (write_event(&s, checksum_alg, 0, &log_file))
return -1;

if (encrypt)
Expand All @@ -3897,8 +3895,7 @@ Event_log::write_description_event(enum_binlog_checksum_alg checksum_alg,
return -1;

Start_encryption_log_event sele(1, key_version, crypto.nonce);
sele.checksum_alg= s.checksum_alg;
if (write_event(&sele, 0, &log_file))
if (write_event(&sele, checksum_alg, 0, &log_file))
return -1;

// Start_encryption_log_event is written, enable the encryption
Expand Down Expand Up @@ -4172,7 +4169,8 @@ bool MYSQL_BIN_LOG::open(const char *log_name,
/* Don't set log_pos in event header */
description_event_for_queue->set_artificial_event();

if (write_event(description_event_for_queue))
if (write_event(description_event_for_queue,
description_event_for_queue->used_checksum_alg))
goto err;
bytes_written+= description_event_for_queue->data_written;
}
Expand Down Expand Up @@ -5616,17 +5614,19 @@ int MYSQL_BIN_LOG::new_file_impl()
*/
Rotate_log_event r(new_name + dirname_length(new_name), 0, LOG_EVENT_OFFSET,
is_relay_log ? Rotate_log_event::RELAY_LOG : 0);
enum_binlog_checksum_alg checksum_alg = BINLOG_CHECKSUM_ALG_UNDEF;
/*
The current relay-log's closing Rotate event must have checksum
value computed with an algorithm of the last relay-logged FD event.
*/
if (is_relay_log)
r.checksum_alg= relay_log_checksum_alg;
DBUG_ASSERT(!is_relay_log ||
relay_log_checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
checksum_alg= relay_log_checksum_alg;
else
checksum_alg= (enum_binlog_checksum_alg)binlog_checksum_options;
DBUG_ASSERT(checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
if ((DBUG_IF("fault_injection_new_file_rotate_event") &&
(error= close_on_error= TRUE)) ||
(error= write_event(&r)))
(error= write_event(&r, checksum_alg)))
{
DBUG_EXECUTE_IF("fault_injection_new_file_rotate_event", errno= 2;);
close_on_error= TRUE;
Expand Down Expand Up @@ -5743,10 +5743,22 @@ int MYSQL_BIN_LOG::new_file_impl()
DBUG_RETURN(error);
}

bool Event_log::write_event(Log_event *ev, binlog_cache_data *cache_data,
bool Event_log::write_event(Log_event *ev, binlog_cache_data *data,
IO_CACHE *file)
{
Log_event_writer writer(file, 0, &crypto);
return write_event(ev, ev->select_checksum_alg(), data, file);
}

bool MYSQL_BIN_LOG::write_event(Log_event *ev)
{
return write_event(ev, ev->select_checksum_alg(), 0, &log_file);
}

bool MYSQL_BIN_LOG::write_event(Log_event *ev,
enum_binlog_checksum_alg checksum_alg,
binlog_cache_data *cache_data, IO_CACHE *file)
{
Log_event_writer writer(file, 0, checksum_alg, &crypto);
if (crypto.scheme && file == &log_file)
{
writer.ctx= alloca(crypto.ctx_size);
Expand All @@ -5757,25 +5769,27 @@ bool Event_log::write_event(Log_event *ev, binlog_cache_data *cache_data,
return writer.write(ev);
}

bool MYSQL_BIN_LOG::append(Log_event *ev)
bool MYSQL_BIN_LOG::append(Log_event *ev,
enum_binlog_checksum_alg checksum_alg)
{
bool res;
mysql_mutex_lock(&LOCK_log);
res= append_no_lock(ev);
res= append_no_lock(ev, checksum_alg);
mysql_mutex_unlock(&LOCK_log);
return res;
}


bool MYSQL_BIN_LOG::append_no_lock(Log_event* ev)
bool MYSQL_BIN_LOG::append_no_lock(Log_event* ev,
enum_binlog_checksum_alg checksum_alg)
{
bool error = 0;
DBUG_ENTER("MYSQL_BIN_LOG::append");

mysql_mutex_assert_owner(&LOCK_log);
DBUG_ASSERT(log_file.type == SEQ_READ_APPEND);

if (write_event(ev))
if (write_event(ev, checksum_alg))
{
error=1;
goto err;
Expand Down Expand Up @@ -6175,7 +6189,8 @@ THD::binlog_start_trans_and_stmt()
uchar *buf= 0;
size_t len= 0;
IO_CACHE tmp_io_cache;
Log_event_writer writer(&tmp_io_cache, 0);
// Replicated events in writeset doesn't have checksum
Log_event_writer writer(&tmp_io_cache, 0, BINLOG_CHECKSUM_ALG_OFF, NULL);
if(!open_cached_file(&tmp_io_cache, mysql_tmpdir, TEMP_PREFIX,
128, MYF(MY_WME)))
{
Expand All @@ -6190,8 +6205,6 @@ THD::binlog_start_trans_and_stmt()
}
Gtid_log_event gtid_event(this, seqno, domain_id, true,
LOG_EVENT_SUPPRESS_USE_F, true, 0);
// Replicated events in writeset doesn't have checksum
gtid_event.checksum_alg= BINLOG_CHECKSUM_ALG_OFF;
gtid_event.server_id= server_id;
writer.write(&gtid_event);
wsrep_write_cache_buf(&tmp_io_cache, &buf, &len);
Expand Down Expand Up @@ -6392,7 +6405,7 @@ bool MYSQL_BIN_LOG::write_table_map(THD *thd, TABLE *table, bool with_annotate)
binlog_cache_data *cache_data= (cache_mngr->
get_binlog_cache_data(is_transactional));
IO_CACHE *file= &cache_data->cache_log;
Log_event_writer writer(file, cache_data);
Log_event_writer writer(file, cache_data, the_event.select_checksum_alg(), NULL);

if (with_annotate)
if (thd->binlog_write_annotated_row(&writer))
Expand Down Expand Up @@ -6578,7 +6591,8 @@ Event_log::flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event,

if (Rows_log_event* pending= cache_data->pending())
{
Log_event_writer writer(&cache_data->cache_log, cache_data);
Log_event_writer writer(&cache_data->cache_log, cache_data,
pending->select_checksum_alg(), NULL);

/*
Write pending event to the cache.
Expand Down Expand Up @@ -7704,11 +7718,13 @@ class CacheWriter: public Log_event_writer
public:
size_t remains;

CacheWriter(THD *thd_arg, IO_CACHE *file_arg, bool do_checksum,
CacheWriter(THD *thd_arg, IO_CACHE *file_arg,
enum_binlog_checksum_alg checksum_alg,
Binlog_crypt_data *cr)
: Log_event_writer(file_arg, 0, cr), remains(0), thd(thd_arg),
: Log_event_writer(file_arg, 0, checksum_alg, cr), remains(0), thd(thd_arg),
first(true)
{ checksum_len= do_checksum ? BINLOG_CHECKSUM_LEN : 0; }
{
}

~CacheWriter()
{ status_var_add(thd->status_var.binlog_bytes_written, bytes_written); }
Expand Down Expand Up @@ -7904,7 +7920,9 @@ int Event_log::write_cache(THD *thd, IO_CACHE *cache)
size_t val;
size_t end_log_pos_inc= 0; // each event processed adds BINLOG_CHECKSUM_LEN 2 t
uchar header[LOG_EVENT_HEADER_LEN];
CacheWriter writer(thd, get_log_file(), binlog_checksum_options, &crypto);
CacheWriter writer(thd, get_log_file(),
(enum_binlog_checksum_alg)binlog_checksum_options,
&crypto);

if (crypto.scheme)
{
Expand Down Expand Up @@ -9447,11 +9465,11 @@ void MYSQL_BIN_LOG::close(uint exiting)
{
Stop_log_event s;
// the checksumming rule for relay-log case is similar to Rotate
s.checksum_alg= is_relay_log ? relay_log_checksum_alg
: (enum_binlog_checksum_alg)binlog_checksum_options;
DBUG_ASSERT(!is_relay_log ||
relay_log_checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
write_event(&s);
enum_binlog_checksum_alg checksum_alg= is_relay_log ?
relay_log_checksum_alg :
(enum_binlog_checksum_alg)binlog_checksum_options;
DBUG_ASSERT(checksum_alg != BINLOG_CHECKSUM_ALG_UNDEF);
write_event(&s, checksum_alg);
bytes_written+= s.data_written;
flush_io_cache(&log_file);
update_binlog_end_pos();
Expand Down Expand Up @@ -11619,7 +11637,7 @@ bool Recovery_context::decide_or_assess(xid_recovery_member *member, int round,
if (truncate_gtid.seq_no == 0 /* was reset or never set */ ||
(truncate_set_in_1st && round == 2 /* reevaluted at round turn */))
{
if (set_truncate_coord(linfo, round, fdle->checksum_alg))
if (set_truncate_coord(linfo, round, fdle->used_checksum_alg))
return true;
}
else
Expand Down
14 changes: 10 additions & 4 deletions sql/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ class Event_log: public MYSQL_LOG
bool encrypt, bool dont_set_created,
bool is_relay_log);

bool write_event(Log_event *ev, binlog_cache_data *data, IO_CACHE *file);
bool write_event(Log_event *ev, enum enum_binlog_checksum_alg checksum_alg,
binlog_cache_data *data, IO_CACHE *file);
};

/**
Expand Down Expand Up @@ -993,11 +994,16 @@ class MYSQL_BIN_LOG: public TC_LOG, private Event_log

using Event_log::write_event;

bool write_event(Log_event *ev) { return write_event(ev, 0, &log_file); }
bool write_event(Log_event *ev, binlog_cache_data *data, IO_CACHE *file);
bool write_event(Log_event *ev, enum enum_binlog_checksum_alg checksum_alg)
{
return write_event(ev, checksum_alg, 0, &log_file);
}
bool write_event(Log_event *ev);

bool write_event_buffer(uchar* buf,uint len);
bool append(Log_event* ev);
bool append_no_lock(Log_event* ev);
bool append(Log_event* ev, enum enum_binlog_checksum_alg checksum_alg);
bool append_no_lock(Log_event* ev, enum enum_binlog_checksum_alg checksum_alg);

void mark_xids_active(ulong cookie, uint xid_count);
void mark_xid_done(ulong cookie, bool write_checkpoint);
Expand Down

0 comments on commit 8eee980

Please sign in to comment.