Commit 4e0cb6db authored by Alfranio Correia's avatar Alfranio Correia

BUG#35542 Add option to sync master and relay log to disk after every event

BUG#31665 sync_binlog should cause relay logs to be synchronized

NOTE: Backporting the patch to next-mr.
      
Add sync_relay_log option to server, this option works for relay log 
the same as option sync_binlog for binlog. This option also synchronize
master info to disk when set to non-zero value.
            
Original patches from Sinisa and Mark, with some modifications
parent 63278c56
......@@ -49,8 +49,7 @@
LOGGER logger;
MYSQL_BIN_LOG mysql_bin_log;
ulong sync_binlog_counter= 0;
MYSQL_BIN_LOG mysql_bin_log(&sync_binlog_period);
static bool test_if_number(const char *str,
long *res, bool allow_wildcards);
......@@ -2410,9 +2409,10 @@ const char *MYSQL_LOG::generate_name(const char *log_name,
MYSQL_BIN_LOG::MYSQL_BIN_LOG()
MYSQL_BIN_LOG::MYSQL_BIN_LOG(uint *sync_period)
:bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
need_start_event(TRUE), m_table_map_version(0),
sync_period_ptr(sync_period),
is_relay_log(0),
description_event_for_exec(0), description_event_for_queue(0)
{
......@@ -3643,6 +3643,8 @@ bool MYSQL_BIN_LOG::append(Log_event* ev)
}
bytes_written+= ev->data_written;
DBUG_PRINT("info",("max_size: %lu",max_size));
if (flush_and_sync(0))
goto err;
if ((uint) my_b_append_tell(&log_file) > max_size)
new_file_without_locking();
......@@ -3673,6 +3675,8 @@ bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
bytes_written += len;
} while ((buf=va_arg(args,const char*)) && (len=va_arg(args,uint)));
DBUG_PRINT("info",("max_size: %lu",max_size));
if (flush_and_sync(0))
goto err;
if ((uint) my_b_append_tell(&log_file) > max_size)
new_file_without_locking();
......@@ -3682,17 +3686,21 @@ err:
DBUG_RETURN(error);
}
bool MYSQL_BIN_LOG::flush_and_sync()
bool MYSQL_BIN_LOG::flush_and_sync(bool *synced)
{
int err=0, fd=log_file.file;
if (synced)
*synced= 0;
safe_mutex_assert_owner(&LOCK_log);
if (flush_io_cache(&log_file))
return 1;
if (++sync_binlog_counter >= sync_binlog_period && sync_binlog_period)
uint sync_period= get_sync_period();
if (sync_period && ++sync_counter >= sync_period)
{
sync_binlog_counter= 0;
sync_counter= 0;
err=my_sync(fd, MYF(MY_WME));
if (synced)
*synced= 1;
}
return err;
}
......@@ -3983,7 +3991,7 @@ MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
if (file == &log_file)
{
error= flush_and_sync();
error= flush_and_sync(0);
if (!error)
{
signal_update();
......@@ -4169,7 +4177,8 @@ bool MYSQL_BIN_LOG::write(Log_event *event_info)
if (file == &log_file) // we are writing to the real log (disk)
{
if (flush_and_sync())
bool synced;
if (flush_and_sync(&synced))
goto err;
signal_update();
rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
......@@ -4425,7 +4434,7 @@ int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
DBUG_ASSERT(carry == 0);
if (sync_log)
flush_and_sync();
return flush_and_sync(0);
return 0; // All OK
}
......@@ -4472,7 +4481,8 @@ bool MYSQL_BIN_LOG::write_incident(THD *thd, bool lock)
ev.write(&log_file);
if (lock)
{
if (!error && !(error= flush_and_sync()))
bool synced;
if (!error && !(error= flush_and_sync(&synced)))
{
signal_update();
rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
......@@ -4560,7 +4570,8 @@ bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event,
if (incident && write_incident(thd, FALSE))
goto err;
if (flush_and_sync())
bool synced;
if (flush_and_sync(&synced))
goto err;
DBUG_EXECUTE_IF("half_binlogged_transaction", abort(););
if (cache->error) // Error on read
......
......@@ -269,6 +269,18 @@ class MYSQL_BIN_LOG: public TC_LOG, private MYSQL_LOG
ulonglong m_table_map_version;
/* pointer to the sync period variable, for binlog this will be
sync_binlog_period, for relay log this will be
sync_relay_log_period
*/
uint *sync_period_ptr;
uint sync_counter;
inline uint get_sync_period()
{
return *sync_period_ptr;
}
int write_to_file(IO_CACHE *cache);
/*
This is used to start writing to a new log file. The difference from
......@@ -296,7 +308,7 @@ public:
Format_description_log_event *description_event_for_exec,
*description_event_for_queue;
MYSQL_BIN_LOG();
MYSQL_BIN_LOG(uint *sync_period);
/*
note that there's no destructor ~MYSQL_BIN_LOG() !
The reason is that we don't want it to be automatically called
......@@ -378,7 +390,20 @@ public:
bool is_active(const char* log_file_name);
int update_log_index(LOG_INFO* linfo, bool need_update_threads);
void rotate_and_purge(uint flags);
bool flush_and_sync();
/**
Flush binlog cache and synchronize to disk.
This function flushes events in binlog cache to binary log file,
it will do synchronizing according to the setting of system
variable 'sync_binlog'. If file is synchronized, @c synced will
be set to 1, otherwise 0.
@param[out] synced if not NULL, set to 1 if file is synchronized, otherwise 0
@retval 0 Success
@retval other Failure
*/
bool flush_and_sync(bool *synced);
int purge_logs(const char *to_log, bool included,
bool need_mutex, bool need_update_threads,
ulonglong *decrease_log_space);
......
......@@ -1868,7 +1868,8 @@ extern ulong MYSQL_PLUGIN_IMPORT specialflag;
#endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */
#ifdef MYSQL_SERVER
extern ulong current_pid;
extern ulong expire_logs_days, sync_binlog_period, sync_binlog_counter;
extern ulong expire_logs_days;
extern uint sync_binlog_period, sync_relaylog_period;
extern ulong opt_tc_log_size, tc_log_max_pages_used, tc_log_page_size;
extern ulong tc_log_page_waits;
extern my_bool relay_log_purge, opt_innodb_safe_binlog, opt_innodb;
......
......@@ -552,7 +552,8 @@ ulong max_prepared_stmt_count;
*/
ulong prepared_stmt_count=0;
ulong thread_id=1L,current_pid;
ulong slow_launch_threads = 0, sync_binlog_period;
ulong slow_launch_threads = 0;
uint sync_binlog_period= 0, sync_relaylog_period= 0;
ulong expire_logs_days = 0;
ulong rpl_recovery_rank=0;
const char *log_output_str= "FILE";
......@@ -5667,7 +5668,8 @@ enum options_mysqld
OPT_SLAVE_EXEC_MODE,
OPT_GENERAL_LOG_FILE,
OPT_SLOW_QUERY_LOG_FILE,
OPT_IGNORE_BUILTIN_INNODB
OPT_IGNORE_BUILTIN_INNODB,
OPT_SYNC_RELAY_LOG
};
......@@ -6921,8 +6923,13 @@ The minimum value for this variable is 4096.",
{"sync-binlog", OPT_SYNC_BINLOG,
"Synchronously flush binary log to disk after every #th event. "
"Use 0 (default) to disable synchronous flushing.",
(uchar**) &sync_binlog_period, (uchar**) &sync_binlog_period, 0, GET_ULONG,
REQUIRED_ARG, 0, 0, ULONG_MAX, 0, 1, 0},
(uchar**) &sync_binlog_period, (uchar**) &sync_binlog_period, 0, GET_UINT,
REQUIRED_ARG, 0, 0, (longlong) UINT_MAX, 0, 1, 0},
{"sync-relay-log", OPT_SYNC_RELAY_LOG,
"Synchronously flush relay log to disk after every #th event. "
"Use 0 (default) to disable synchronous flushing.",
(uchar**) &sync_relaylog_period, (uchar**) &sync_relaylog_period, 0, GET_UINT,
REQUIRED_ARG, 0, 0, (longlong) UINT_MAX, 0, 1, 0},
{"sync-frm", OPT_SYNC_FRM, "Sync .frm to disk on create. Enabled by default.",
(uchar**) &opt_sync_frm, (uchar**) &opt_sync_frm, 0, GET_BOOL, NO_ARG, 1, 0,
0, 0, 0, 0},
......
......@@ -342,6 +342,7 @@ int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
{
IO_CACHE* file = &mi->file;
char lbuf[22];
int err= 0;
DBUG_ENTER("flush_master_info");
DBUG_PRINT("enter",("master_pos: %ld", (long) mi->master_log_pos));
......@@ -358,9 +359,17 @@ int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
When we come to this place in code, relay log may or not be initialized;
the caller is responsible for setting 'flush_relay_log_cache' accordingly.
*/
if (flush_relay_log_cache &&
flush_io_cache(mi->rli.relay_log.get_log_file()))
if (flush_relay_log_cache)
{
IO_CACHE *log_file= mi->rli.relay_log.get_log_file();
if (flush_io_cache(log_file))
DBUG_RETURN(2);
/* Sync to disk if --sync-relay-log is set */
if (sync_relaylog_period &&
my_sync(log_file->file, MY_WME))
DBUG_RETURN(2);
}
/*
We flushed the relay log BEFORE the master.info file, because if we crash
......@@ -388,7 +397,10 @@ int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
mi->password, mi->port, mi->connect_retry,
(int)(mi->ssl), mi->ssl_ca, mi->ssl_capath, mi->ssl_cert,
mi->ssl_cipher, mi->ssl_key, mi->ssl_verify_server_cert);
DBUG_RETURN(-flush_io_cache(file));
err= flush_io_cache(file);
if (sync_relaylog_period && !err)
err= my_sync(mi->fd, MYF(MY_WME));
DBUG_RETURN(-err);
}
......
......@@ -32,7 +32,8 @@ int init_strvar_from_file(char *var, int max_size, IO_CACHE *f,
Relay_log_info::Relay_log_info()
:Slave_reporting_capability("SQL"),
no_storage(FALSE), replicate_same_server_id(::replicate_same_server_id),
info_fd(-1), cur_log_fd(-1), save_temporary_tables(0),
info_fd(-1), cur_log_fd(-1), relay_log(&sync_relaylog_period),
save_temporary_tables(0),
#if HAVE_purify
is_fake(FALSE),
#endif
......
......@@ -1534,6 +1534,23 @@ static bool get_unsigned(THD *thd, set_var *var, ulonglong user_max,
}
bool sys_var_int_ptr::check(THD *thd, set_var *var)
{
var->save_result.ulong_value= (ulong) var->value->val_int();
return 0;
}
bool sys_var_int_ptr::update(THD *thd, set_var *var)
{
*value= (uint) var->save_result.ulong_value;
return 0;
}
void sys_var_int_ptr::set_default(THD *thd, enum_var_type type)
{
*value= (uint) option_limits->def_value;
}
sys_var_long_ptr::
sys_var_long_ptr(sys_var_chain *chain, const char *name_arg, ulong *value_ptr_arg,
sys_after_update_func after_update_arg)
......
......@@ -175,6 +175,27 @@ public:
{ return (uchar*) value; }
};
/**
Unsigned int system variable class
*/
class sys_var_int_ptr :public sys_var
{
public:
sys_var_int_ptr(sys_var_chain *chain, const char *name_arg,
uint *value_ptr_arg,
sys_after_update_func after_update_arg= NULL)
:sys_var(name_arg, after_update_arg),
value(value_ptr_arg)
{ chain_sys_var(chain); }
bool check(THD *thd, set_var *var);
bool update(THD *thd, set_var *var);
void set_default(THD *thd, enum_var_type type);
SHOW_TYPE show_type() { return SHOW_INT; }
uchar *value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
{ return (uchar*) value; }
private:
uint *value;
};
/*
A global ulong variable that is protected by LOCK_global_system_variables
......
......@@ -1753,15 +1753,6 @@ public:
*/
};
class sys_var_sync_binlog_period :public sys_var_long_ptr
{
public:
sys_var_sync_binlog_period(sys_var_chain *chain, const char *name_arg,
ulong *value_ptr)
:sys_var_long_ptr(chain, name_arg,value_ptr) {}
bool update(THD *thd, set_var *var);
};
static sys_var_chain vars = { NULL, NULL };
static sys_var_const sys_log_slave_updates(&vars, "log_slave_updates",
......@@ -1793,7 +1784,8 @@ static sys_var_const sys_slave_skip_errors(&vars, "slave_skip_errors",
(uchar*) slave_skip_error_names);
static sys_var_long_ptr sys_slave_trans_retries(&vars, "slave_transaction_retries",
&slave_trans_retries);
static sys_var_sync_binlog_period sys_sync_binlog_period(&vars, "sync_binlog", &sync_binlog_period);
static sys_var_int_ptr sys_sync_binlog_period(&vars, "sync_binlog", &sync_binlog_period);
static sys_var_int_ptr sys_sync_relaylog_period(&vars, "sync_relay_log", &sync_relaylog_period);
static sys_var_slave_skip_counter sys_slave_skip_counter(&vars, "sql_slave_skip_counter");
......@@ -1835,12 +1827,6 @@ bool sys_var_slave_skip_counter::update(THD *thd, set_var *var)
}
bool sys_var_sync_binlog_period::update(THD *thd, set_var *var)
{
sync_binlog_period= (ulong) var->save_result.ulonglong_value;
return 0;
}
int init_replication_sys_vars()
{
if (mysql_add_sys_var_chain(vars.first, my_long_options))
......
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