Skip to content

Commit

Permalink
cleanup: consolidate binlog-related THD::*_used into one bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvova authored and sanja-byelkin committed Aug 10, 2022
1 parent 4ce1470 commit c38b8f4
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 72 deletions.
6 changes: 3 additions & 3 deletions sql/item_func.cc
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
{
if (Item_int_func::fix_fields(thd, ref))
return TRUE;
thd->thread_specific_used= TRUE;
thd->used|= THD::THREAD_SPECIFIC_USED;
value= thd->variables.pseudo_thread_id;
return FALSE;
}
Expand Down Expand Up @@ -2818,9 +2818,9 @@ bool Item_func_rand::fix_fields(THD *thd,Item **ref)
Once events are forwarded rather than recreated,
the following can be skipped if inside the slave thread
*/
if (!thd->rand_used)
if (!(thd->used & THD::RAND_USED))
{
thd->rand_used= 1;
thd->used|= THD::RAND_USED;
thd->rand_saved_seed1= thd->rand.seed1;
thd->rand_saved_seed2= thd->rand.seed2;
}
Expand Down
10 changes: 5 additions & 5 deletions sql/item_timefunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ bool Item_func_from_days::get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzz
void Item_func_curdate_local::store_now_in_TIME(THD *thd, MYSQL_TIME *now_time)
{
thd->variables.time_zone->gmt_sec_to_TIME(now_time, thd->query_start());
thd->time_zone_used= 1;
thd->used |= THD::TIME_ZONE_USED;
}


Expand Down Expand Up @@ -1601,7 +1601,7 @@ void Item_func_curtime_local::store_now_in_TIME(THD *thd, MYSQL_TIME *now_time)
now_time->year= now_time->month= now_time->day= 0;
now_time->time_type= MYSQL_TIMESTAMP_TIME;
set_sec_part(thd->query_start_sec_part(), now_time, this);
thd->time_zone_used= 1;
thd->used|= THD::TIME_ZONE_USED;
}


Expand Down Expand Up @@ -1667,7 +1667,7 @@ void Item_func_now_local::store_now_in_TIME(THD *thd, MYSQL_TIME *now_time)
{
thd->variables.time_zone->gmt_sec_to_TIME(now_time, thd->query_start());
set_sec_part(thd->query_start_sec_part(), now_time, this);
thd->time_zone_used= 1;
thd->used|= THD::TIME_ZONE_USED;
}


Expand Down Expand Up @@ -1710,7 +1710,7 @@ void Item_func_sysdate_local::store_now_in_TIME(THD *thd, MYSQL_TIME *now_time)
my_hrtime_t now= my_hrtime();
thd->variables.time_zone->gmt_sec_to_TIME(now_time, hrtime_to_my_time(now));
set_sec_part(hrtime_sec_part(now), now_time, this);
thd->time_zone_used= 1;
thd->used|= THD::TIME_ZONE_USED;
}


Expand Down Expand Up @@ -2732,7 +2732,7 @@ String *Item_func_tochar::val_str(String* str)

bool Item_func_from_unixtime::fix_length_and_dec(THD *thd)
{
thd->time_zone_used= 1;
thd->used|= THD::TIME_ZONE_USED;
tz= thd->variables.time_zone;
Type_std_attributes::set(
Type_temporal_attributes_not_fixed_dec(MAX_DATETIME_WIDTH,
Expand Down
18 changes: 6 additions & 12 deletions sql/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,13 @@ bool Log_to_csv_event_handler::
uint field_index;
Silence_log_table_errors error_handler;
Open_tables_backup open_tables_backup;
bool save_time_zone_used;
THD::used_t save_time_zone_used= thd->used & THD::TIME_ZONE_USED;
DBUG_ENTER("log_general");

/*
CSV uses TIME_to_timestamp() internally if table needs to be repaired
which will set thd->time_zone_used
which will set TIME_ZONE_USED
*/
save_time_zone_used= thd->time_zone_used;

table_list.init_one_table(&MYSQL_SCHEMA_NAME, &GENERAL_LOG_NAME, 0,
TL_WRITE_CONCURRENT_INSERT);
Expand Down Expand Up @@ -942,7 +941,7 @@ bool Log_to_csv_event_handler::
if (need_close)
close_log_table(thd, &open_tables_backup);

thd->time_zone_used= save_time_zone_used;
thd->used= (thd->used & ~THD::TIME_ZONE_USED) | save_time_zone_used;
DBUG_RETURN(result);
}

Expand Down Expand Up @@ -989,19 +988,14 @@ bool Log_to_csv_event_handler::
Silence_log_table_errors error_handler;
Open_tables_backup open_tables_backup;
CHARSET_INFO *client_cs= thd->variables.character_set_client;
bool save_time_zone_used;
THD::used_t save_time_zone_used= thd->used & THD::TIME_ZONE_USED;
ulong query_time= (ulong) MY_MIN(query_utime/1000000, TIME_MAX_VALUE_SECONDS);
ulong lock_time= (ulong) MY_MIN(lock_utime/1000000, TIME_MAX_VALUE_SECONDS);
ulong query_time_micro= (ulong) (query_utime % 1000000);
ulong lock_time_micro= (ulong) (lock_utime % 1000000);
DBUG_ENTER("Log_to_csv_event_handler::log_slow");

thd->push_internal_handler(& error_handler);
/*
CSV uses TIME_to_timestamp() internally if table needs to be repaired
which will set thd->time_zone_used
*/
save_time_zone_used= thd->time_zone_used;

table_list.init_one_table(&MYSQL_SCHEMA_NAME, &SLOW_LOG_NAME, 0,
TL_WRITE_CONCURRENT_INSERT);
Expand Down Expand Up @@ -1129,7 +1123,7 @@ bool Log_to_csv_event_handler::
}
if (need_close)
close_log_table(thd, &open_tables_backup);
thd->time_zone_used= save_time_zone_used;
thd->used= (thd->used & ~THD::TIME_ZONE_USED) | save_time_zone_used;
DBUG_RETURN(result);
}

Expand Down Expand Up @@ -6853,7 +6847,7 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info, my_bool *with_annotate)
if (write_event(&e, cache_data, file))
goto err;
}
if (thd->rand_used)
if (thd->used & THD::RAND_USED)
{
Rand_log_event e(thd,thd->rand_saved_seed1,thd->rand_saved_seed2,
using_trans, direct);
Expand Down
11 changes: 6 additions & 5 deletions sql/log_event_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ bool Query_log_event::write()
}
}

if (thd && thd->query_start_sec_part_used)
if (thd && (thd->used & THD::QUERY_START_SEC_PART_USED))
{
*start++= Q_HRNOW;
get_time();
Expand Down Expand Up @@ -1415,8 +1415,8 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg,
bool direct, bool suppress_use, int errcode)

:Log_event(thd_arg,
(thd_arg->thread_specific_used ? LOG_EVENT_THREAD_SPECIFIC_F :
0) |
((thd_arg->used & THD::THREAD_SPECIFIC_USED)
? LOG_EVENT_THREAD_SPECIFIC_F : 0) |
(suppress_use ? LOG_EVENT_SUPPRESS_USE_F : 0),
using_trans),
data_buf(0), query(query_arg), catalog(thd_arg->catalog),
Expand Down Expand Up @@ -1502,7 +1502,7 @@ Query_log_event::Query_log_event(THD* thd_arg, const char* query_arg,
int2store(charset, thd_arg->variables.character_set_client->number);
int2store(charset+2, thd_arg->variables.collation_connection->number);
int2store(charset+4, thd_arg->variables.collation_server->number);
if (thd_arg->time_zone_used)
if (thd_arg->used & THD::TIME_ZONE_USED)
{
/*
Note that our event becomes dependent on the Time_zone object
Expand Down Expand Up @@ -2951,7 +2951,8 @@ Load_log_event::Load_log_event(THD *thd_arg, const sql_exchange *ex,
enum enum_duplicates handle_dup,
bool ignore, bool using_trans)
:Log_event(thd_arg,
thd_arg->thread_specific_used ? LOG_EVENT_THREAD_SPECIFIC_F : 0,
(thd_arg->used & THD::THREAD_SPECIFIC_USED)
? LOG_EVENT_THREAD_SPECIFIC_F : 0,
using_trans),
thread_id(thd_arg->thread_id),
slave_proxy_id((ulong)thd_arg->variables.pseudo_thread_id),
Expand Down
12 changes: 5 additions & 7 deletions sql/sp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,7 @@ bool AUTHID::read_from_mysql_proc_row(THD *thd, TABLE *table)
*/

int
Sp_handler::db_find_routine(THD *thd,
const Database_qualified_name *name,
Sp_handler::db_find_routine(THD *thd, const Database_qualified_name *name,
sp_head **sphp) const
{
TABLE *table;
Expand All @@ -697,7 +696,7 @@ Sp_handler::db_find_routine(THD *thd,
longlong created;
longlong modified;
Sp_chistics chistics;
bool saved_time_zone_used= thd->time_zone_used;
THD::used_t saved_time_zone_used= thd->used & THD::TIME_ZONE_USED;
bool trans_commited= 0;
sql_mode_t sql_mode;
Stored_program_creation_ctx *creation_ctx;
Expand Down Expand Up @@ -763,15 +762,14 @@ Sp_handler::db_find_routine(THD *thd,
thd->commit_whole_transaction_and_close_tables();
new_trans.restore_old_transaction();

ret= db_load_routine(thd, name, sphp,
sql_mode, params, returns, body, chistics, definer,
created, modified, NULL, creation_ctx);
ret= db_load_routine(thd, name, sphp, sql_mode, params, returns, body,
chistics, definer, created, modified, NULL, creation_ctx);
done:
/*
Restore the time zone flag as the timezone usage in proc table
does not affect replication.
*/
thd->time_zone_used= saved_time_zone_used;
thd->used= (thd->used & ~THD::TIME_ZONE_USED) | saved_time_zone_used;
if (!trans_commited)
{
if (table)
Expand Down
19 changes: 8 additions & 11 deletions sql/sql_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
protocol_text(this), protocol_binary(this), initial_status_var(0),
m_current_stage_key(0), m_psi(0),
in_sub_stmt(0), log_all_errors(0),
binlog_unsafe_warning_flags(0),
binlog_unsafe_warning_flags(0), used(0),
current_stmt_binlog_format(BINLOG_FORMAT_MIXED),
bulk_param(0),
table_map_for_update(0),
Expand All @@ -655,8 +655,6 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
is_fatal_error(0),
transaction_rollback_request(0),
is_fatal_sub_stmt_error(false),
rand_used(0),
time_zone_used(0),
in_lock_tables(0),
bootstrap(0),
derived_tables_processing(FALSE),
Expand Down Expand Up @@ -769,11 +767,10 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
security_ctx= &main_security_ctx;
no_errors= 0;
password= 0;
query_start_sec_part_used= 0;
count_cuted_fields= CHECK_FIELD_IGNORE;
killed= NOT_KILLED;
killed_err= 0;
is_slave_error= thread_specific_used= FALSE;
is_slave_error= FALSE;
my_hash_clear(&handler_tables_hash);
my_hash_clear(&ull_hash);
tmp_table=0;
Expand Down Expand Up @@ -2232,21 +2229,21 @@ void THD::cleanup_after_query()
thd_progress_end(this);

/*
Reset rand_used so that detection of calls to rand() will save random
Reset RAND_USED so that detection of calls to rand() will save random
seeds if needed by the slave.

Do not reset rand_used if inside a stored function or trigger because
Do not reset RAND_USED if inside a stored function or trigger because
only the call to these operations is logged. Thus only the calling
statement needs to detect rand() calls made by its substatements. These
substatements must not set rand_used to 0 because it would remove the
substatements must not set RAND_USED to 0 because it would remove the
detection of rand() by the calling statement.
*/
if (!in_sub_stmt) /* stored functions and triggers are a special case */
{
/* Forget those values, for next binlogger: */
stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
auto_inc_intervals_in_cur_stmt_for_binlog.empty();
rand_used= 0;
used&= ~THD::RAND_USED;
#ifndef EMBEDDED_LIBRARY
/*
Clean possible unused INSERT_ID events by current statement.
Expand Down Expand Up @@ -7537,7 +7534,7 @@ MYSQL_TIME THD::query_start_TIME()
MYSQL_TIME res;
variables.time_zone->gmt_sec_to_TIME(&res, query_start());
res.second_part= query_start_sec_part();
time_zone_used= 1;
used|= TIME_ZONE_USED;
return res;
}

Expand Down Expand Up @@ -8276,7 +8273,7 @@ Query_arena_stmt::~Query_arena_stmt()
bool THD::timestamp_to_TIME(MYSQL_TIME *ltime, my_time_t ts,
ulong sec_part, date_mode_t fuzzydate)
{
time_zone_used= 1;
used|= TIME_ZONE_USED;
if (ts == 0 && sec_part == 0)
{
if (fuzzydate & TIME_NO_ZERO_DATE)
Expand Down
12 changes: 7 additions & 5 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,12 @@ class THD: public THD_count, /* this must be first */
*/
uint32 binlog_unsafe_warning_flags;

typedef uint used_t;
enum { RAND_USED=1, TIME_ZONE_USED=2, QUERY_START_SEC_PART_USED=4,
THREAD_SPECIFIC_USED=8 };

used_t used;

#ifndef MYSQL_CLIENT
binlog_cache_mngr * binlog_setup_trx_data();
/*
Expand Down Expand Up @@ -3592,15 +3598,11 @@ class THD: public THD_count, /* this must be first */
Reset to FALSE when we leave the sub-statement mode.
*/
bool is_fatal_sub_stmt_error;
bool rand_used, time_zone_used;
bool query_start_sec_part_used;
/* for IS NULL => = last_insert_id() fix in remove_eq_conds() */
bool substitute_null_with_insert_id;
bool in_lock_tables;
bool bootstrap, cleanup_done, free_connection_done;

/** is set if some thread specific value(s) used in a statement. */
bool thread_specific_used;
/**
is set if a statement accesses a temporary table created through
CREATE TEMPORARY TABLE.
Expand Down Expand Up @@ -3908,7 +3910,7 @@ class THD: public THD_count, /* this must be first */
ulong sec_part, date_mode_t fuzzydate);
inline my_time_t query_start() { return start_time; }
inline ulong query_start_sec_part()
{ query_start_sec_part_used=1; return start_time_sec_part; }
{ used|= QUERY_START_SEC_PART_USED; return start_time_sec_part; }
MYSQL_TIME query_start_TIME();
time_round_mode_t temporal_round_mode() const
{
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ bool multi_delete::send_eof()
thd->clear_error();
else
errcode= query_error_code(thd, killed_status == NOT_KILLED);
thd->thread_specific_used= TRUE;
thd->used|= THD::THREAD_SPECIFIC_USED;
StatementBinlog stmt_binlog(thd, thd->binlog_need_stmt_format(transactional_tables));
if (unlikely(thd->binlog_query(THD::ROW_QUERY_TYPE,
thd->query(), thd->query_length(),
Expand Down
9 changes: 5 additions & 4 deletions sql/sql_insert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,8 @@ class delayed_row :public ilink {
ulong start_time_sec_part;
sql_mode_t sql_mode;
bool auto_increment_field_not_null;
bool ignore, log_query, query_start_sec_part_used;
bool ignore, log_query;
THD::used_t query_start_sec_part_used;
bool stmt_depends_on_first_successful_insert_id_in_prev_stmt;
ulonglong first_successful_insert_id_in_prev_stmt;
ulonglong forced_insert_id;
Expand Down Expand Up @@ -2938,7 +2939,7 @@ int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic,

row->start_time= thd->start_time;
row->start_time_sec_part= thd->start_time_sec_part;
row->query_start_sec_part_used= thd->query_start_sec_part_used;
row->query_start_sec_part_used= thd->used & THD::QUERY_START_SEC_PART_USED;
/*
those are for the binlog: LAST_INSERT_ID() has been evaluated at this
time, so record does not need it, but statement-based binlogging of the
Expand All @@ -2955,7 +2956,7 @@ int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic,
So we can get time_zone object from thread which handling delayed statement.
See the comment of my_tz_find() for detail.
*/
if (thd->time_zone_used)
if (thd->used & THD::TIME_ZONE_USED)
{
row->time_zone = thd->variables.time_zone;
}
Expand Down Expand Up @@ -3569,7 +3570,7 @@ bool Delayed_insert::handle_inserts(void)

thd.start_time=row->start_time;
thd.start_time_sec_part=row->start_time_sec_part;
thd.query_start_sec_part_used=row->query_start_sec_part_used;
thd.used= row->query_start_sec_part_used;
/*
To get the exact auto_inc interval to store in the binlog we must not
use values from the previous interval (of the previous rows).
Expand Down
6 changes: 2 additions & 4 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7575,8 +7575,8 @@ void THD::reset_for_next_command(bool do_clear_error)
}
#endif /* WITH_WSREP */

query_start_sec_part_used= 0;
is_fatal_error= time_zone_used= 0;
used= 0;
is_fatal_error= 0;
variables.option_bits&= ~OPTION_BINLOG_THIS_STMT;

/*
Expand All @@ -7595,14 +7595,12 @@ void THD::reset_for_next_command(bool do_clear_error)
transaction->all.reset();
}
DBUG_ASSERT(security_ctx== &main_security_ctx);
thread_specific_used= FALSE;

if (opt_bin_log)
reset_dynamic(&user_var_events);
DBUG_ASSERT(user_var_events_alloc == &main_mem_root);
enable_slow_log= true;
get_stmt_da()->reset_for_next_command();
rand_used= 0;
m_sent_row_count= m_examined_row_count= 0;
accessed_rows_and_keys= 0;

Expand Down
Loading

0 comments on commit c38b8f4

Please sign in to comment.