Skip to content

Commit

Permalink
MDEV-31273: Replace Log_event::writer with function parameter
Browse files Browse the repository at this point in the history
This is a preparatory patch for precomputing binlog checksums outside
of holding LOCK_log, no functional changes.

Replace Log_event::writer with just passing the writer object as a
function parameter to Log_event::write().

This is mainly for code clarity. Having to set ev->writer before every
call to ev->write() is error-prone (what if it's forgotten in some
code place?), while passing it as parameter as usual makes it explicit
how the dataflow is.

As a minor point, it also improves the code, as the compiler now can
save the function parameter in a register across nested calls (when it
is a class member, compiler needs to reload across nested calls in
case the object would be modified during the call).

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 d8dda7c commit 77bd1be
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 157 deletions.
92 changes: 45 additions & 47 deletions sql/log_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -1304,8 +1304,6 @@ class Log_event
*/
enum enum_binlog_checksum_alg checksum_alg;

Log_event_writer *writer;

#ifdef MYSQL_SERVER
THD* thd;

Expand Down Expand Up @@ -1464,24 +1462,26 @@ class Log_event
static void operator delete(void*, void*) { }

#ifdef MYSQL_SERVER
bool write_header(size_t event_data_length);
bool write_data(const uchar *buf, size_t data_length)
bool write_header(Log_event_writer *writer, size_t event_data_length);
bool write_data(Log_event_writer *writer, const uchar *buf, size_t data_length)
{ return writer->write_data(buf, data_length); }
bool write_data(const char *buf, size_t data_length)
{ return write_data((uchar*)buf, data_length); }
bool write_footer()
bool write_data(Log_event_writer *writer, const char *buf, size_t data_length)
{ return write_data(writer, (uchar*)buf, data_length); }
bool write_footer(Log_event_writer *writer)
{ return writer->write_footer(); }

my_bool need_checksum();

virtual bool write()
virtual bool write(Log_event_writer *writer)
{
return write_header(get_data_size()) || write_data_header() ||
write_data_body() || write_footer();
return write_header(writer, get_data_size()) ||
write_data_header(writer) ||
write_data_body(writer) ||
write_footer(writer);
}
virtual bool write_data_header()
virtual bool write_data_header(Log_event_writer *writer)
{ return 0; }
virtual bool write_data_body()
virtual bool write_data_body(Log_event_writer *writer)
{ return 0; }

/* Return start of query time or current time */
Expand Down Expand Up @@ -2230,8 +2230,8 @@ class Query_log_event: public Log_event
static int begin_event(String *packet, ulong ev_offset,
enum enum_binlog_checksum_alg checksum_alg);
#ifdef MYSQL_SERVER
bool write();
virtual bool write_post_header_for_derived() { return FALSE; }
bool write(Log_event_writer *writer);
virtual bool write_post_header_for_derived(Log_event_writer *writer) { return FALSE; }
#endif
bool is_valid() const { return query != 0; }

Expand Down Expand Up @@ -2311,7 +2311,7 @@ class Query_compressed_log_event:public Query_log_event{
ulong query_length,
bool using_trans, bool direct, bool suppress_use,
int error);
virtual bool write();
virtual bool write(Log_event_writer *writer);
#endif
};

Expand Down Expand Up @@ -2376,14 +2376,14 @@ class Start_encryption_log_event : public Log_event
memcpy(nonce, nonce_arg, BINLOG_NONCE_LENGTH);
}

bool write_data_body()
bool write_data_body(Log_event_writer *writer)
{
uchar scheme_buf= crypto_scheme;
uchar key_version_buf[BINLOG_KEY_VERSION_LENGTH];
int4store(key_version_buf, key_version);
return write_data(&scheme_buf, sizeof(scheme_buf)) ||
write_data(key_version_buf, sizeof(key_version_buf)) ||
write_data(nonce, BINLOG_NONCE_LENGTH);
return write_data(writer, &scheme_buf, sizeof(scheme_buf)) ||
write_data(writer, key_version_buf, sizeof(key_version_buf)) ||
write_data(writer, nonce, BINLOG_NONCE_LENGTH);
}
#else
bool print(FILE* file, PRINT_EVENT_INFO* print_event_info);
Expand Down Expand Up @@ -2536,7 +2536,7 @@ class Format_description_log_event: public Log_event
Log_event_type get_type_code() { return FORMAT_DESCRIPTION_EVENT;}
my_off_t get_header_len(my_off_t) { return LOG_EVENT_MINIMAL_HEADER_LEN; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#ifdef HAVE_REPLICATION
void pack_info(Protocol* protocol);
#endif /* HAVE_REPLICATION */
Expand Down Expand Up @@ -2652,7 +2652,7 @@ Intvar_log_event(THD* thd_arg,uchar type_arg, ulonglong val_arg,
const char* get_var_type_name();
int get_data_size() { return 9; /* sizeof(type) + sizeof(val) */;}
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#endif
bool is_valid() const { return 1; }
bool is_part_of_group() { return 1; }
Expand Down Expand Up @@ -2732,7 +2732,7 @@ class Rand_log_event: public Log_event
Log_event_type get_type_code() { return RAND_EVENT;}
int get_data_size() { return 16; /* sizeof(ulonglong) * 2*/ }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#endif
bool is_valid() const { return 1; }
bool is_part_of_group() { return 1; }
Expand Down Expand Up @@ -2812,7 +2812,7 @@ class Xid_log_event: public Xid_apply_log_event
Log_event_type get_type_code() { return XID_EVENT;}
int get_data_size() { return sizeof(xid); }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#endif

private:
Expand Down Expand Up @@ -2962,7 +2962,7 @@ class XA_prepare_log_event: public Xid_apply_log_event
}

#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#endif

private:
Expand Down Expand Up @@ -3031,7 +3031,7 @@ class User_var_log_event: public Log_event
~User_var_log_event() = default;
Log_event_type get_type_code() { return USER_VAR_EVENT;}
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
/*
Getter and setter for deferred User-event.
Returns true if the event is not applied directly
Expand Down Expand Up @@ -3183,7 +3183,7 @@ class Rotate_log_event: public Log_event
int get_data_size() { return ident_len + ROTATE_HEADER_LEN;}
bool is_valid() const { return new_log_ident != 0; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
#endif

private:
Expand Down Expand Up @@ -3217,7 +3217,7 @@ class Binlog_checkpoint_log_event: public Log_event
int get_data_size() { return binlog_file_len + BINLOG_CHECKPOINT_HEADER_LEN;}
bool is_valid() const { return binlog_file_name != 0; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
enum_skip_reason do_shall_skip(rpl_group_info *rgi);
#endif
};
Expand Down Expand Up @@ -3382,7 +3382,7 @@ class Gtid_log_event: public Log_event
}
bool is_valid() const { return seq_no != 0; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
static int make_compatible_event(String *packet, bool *need_dummy_event,
ulong ev_offset, enum enum_binlog_checksum_alg checksum_alg);
static bool peek(const uchar *event_start, size_t event_len,
Expand Down Expand Up @@ -3500,7 +3500,7 @@ class Gtid_list_log_event: public Log_event
bool is_valid() const { return list != NULL; }
#if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
bool to_packet(String *packet);
bool write();
bool write(Log_event_writer *writer);
virtual int do_apply_event(rpl_group_info *rgi);
enum_skip_reason do_shall_skip(rpl_group_info *rgi);
#endif
Expand Down Expand Up @@ -3553,7 +3553,7 @@ class Append_block_log_event: public Log_event
int get_data_size() { return block_len + APPEND_BLOCK_HEADER_LEN ;}
bool is_valid() const { return block != 0; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
const char* get_db() { return db; }
#endif

Expand Down Expand Up @@ -3594,7 +3594,7 @@ class Delete_file_log_event: public Log_event
int get_data_size() { return DELETE_FILE_HEADER_LEN ;}
bool is_valid() const { return file_id != 0; }
#ifdef MYSQL_SERVER
bool write();
bool write(Log_event_writer *writer);
const char* get_db() { return db; }
#endif

Expand Down Expand Up @@ -3694,7 +3694,7 @@ class Execute_load_query_log_event: public Query_log_event

ulong get_post_header_size_for_derived();
#ifdef MYSQL_SERVER
bool write_post_header_for_derived();
bool write_post_header_for_derived(Log_event_writer *writer);
#endif

private:
Expand Down Expand Up @@ -3762,8 +3762,8 @@ class Annotate_rows_log_event: public Log_event
virtual bool is_part_of_group() { return 1; }

#ifndef MYSQL_CLIENT
virtual bool write_data_header();
virtual bool write_data_body();
virtual bool write_data_header(Log_event_writer *writer);
virtual bool write_data_body(Log_event_writer *writer);
#endif

#if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
Expand Down Expand Up @@ -4430,8 +4430,8 @@ class Table_map_log_event : public Log_event
virtual int get_data_size() { return (uint) m_data_size; }
#ifdef MYSQL_SERVER
virtual int save_field_metadata();
virtual bool write_data_header();
virtual bool write_data_body();
virtual bool write_data_header(Log_event_writer *writer);
virtual bool write_data_body(Log_event_writer *writer);
virtual const char *get_db() { return m_dbnam; }
#endif

Expand Down Expand Up @@ -4690,9 +4690,9 @@ class Rows_log_event : public Log_event
#endif

#ifdef MYSQL_SERVER
virtual bool write_data_header();
virtual bool write_data_body();
virtual bool write_compressed();
virtual bool write_data_header(Log_event_writer *writer);
virtual bool write_data_body(Log_event_writer *writer);
virtual bool write_compressed(Log_event_writer *writer);
virtual const char *get_db() { return m_table->s->db.str; }
#endif
/*
Expand Down Expand Up @@ -5008,7 +5008,7 @@ class Write_rows_compressed_log_event : public Write_rows_log_event
#if defined(MYSQL_SERVER)
Write_rows_compressed_log_event(THD*, TABLE*, ulong table_id,
bool is_transactional);
virtual bool write();
virtual bool write(Log_event_writer *writer);
#endif
#ifdef HAVE_REPLICATION
Write_rows_compressed_log_event(const uchar *buf, uint event_len,
Expand Down Expand Up @@ -5097,7 +5097,7 @@ class Update_rows_compressed_log_event : public Update_rows_log_event
#if defined(MYSQL_SERVER)
Update_rows_compressed_log_event(THD*, TABLE*, ulong table_id,
bool is_transactional);
virtual bool write();
virtual bool write(Log_event_writer *writer);
#endif
#ifdef HAVE_REPLICATION
Update_rows_compressed_log_event(const uchar *buf, uint event_len,
Expand Down Expand Up @@ -5187,7 +5187,7 @@ class Delete_rows_compressed_log_event : public Delete_rows_log_event
public:
#if defined(MYSQL_SERVER)
Delete_rows_compressed_log_event(THD*, TABLE*, ulong, bool is_transactional);
virtual bool write();
virtual bool write(Log_event_writer *writer);
#endif
#ifdef HAVE_REPLICATION
Delete_rows_compressed_log_event(const uchar *buf, uint event_len,
Expand Down Expand Up @@ -5277,8 +5277,8 @@ class Incident_log_event : public Log_event {
#ifdef MYSQL_SERVER
void pack_info(Protocol*);

virtual bool write_data_header();
virtual bool write_data_body();
virtual bool write_data_header(Log_event_writer *writer);
virtual bool write_data_body(Log_event_writer *writer);
#endif

Incident_log_event(const uchar *buf, uint event_len,
Expand Down Expand Up @@ -5415,9 +5415,7 @@ class Heartbeat_log_event: public Log_event

inline int Log_event_writer::write(Log_event *ev)
{
ev->writer= this;
int res= ev->write();
IF_DBUG(ev->writer= 0,); // writer must be set before every Log_event::write
int res= ev->write(this);
add_status(ev->logged_status());
return res;
}
Expand Down

0 comments on commit 77bd1be

Please sign in to comment.