Skip to content

Commit

Permalink
MDEV-27832 disable binary logging for SQL SERVICE.
Browse files Browse the repository at this point in the history
Binary logging is now disabled for the queries run by SQL SERVICE.
The binlogging can be turned on with the 'SET SQL_LOG_BIN=On' query.

Conflicts:
	sql/sql_prepare.cc

Conflicts:
	sql/sql_prepare.cc
  • Loading branch information
Alexey Botchkov committed Nov 5, 2023
1 parent 801b45b commit 3a8eb40
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
29 changes: 29 additions & 0 deletions mysql-test/suite/plugins/r/test_sql_service.result
Expand Up @@ -2,6 +2,26 @@ install plugin test_sql_service soname 'test_sql_service';
show status like 'test_sql_service_passed';
Variable_name Value
Test_sql_service_passed 1
set global test_sql_service_execute_sql_global= 'create table test.t1 select 1 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_local= 'insert into test.t1 select 2 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 3 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 4 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 5 as a, @@sql_auto_is_null';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 6 as a, @@sql_auto_is_null';
select * from t1 order by a;
a @@SQL_LOG_BIN
1 0
2 0
3 1
4 0
5 1
6 0
drop table t1;
SET SQL_LOG_BIN=0;
set global test_sql_service_run_test= 1;
show status like 'test_sql_service_passed';
Variable_name Value
Expand Down Expand Up @@ -68,3 +88,12 @@ drop table t1;
uninstall plugin test_sql_service;
Warnings:
Warning 1620 Plugin is busy and will be uninstalled on shutdown
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
master-bin.000001 # Gtid # # BEGIN GTID #-#-#
master-bin.000001 # Annotate_rows # # insert into test.t1 select 3 as a, @@SQL_LOG_BIN
master-bin.000001 # Table_map # # table_id: # (test.t1)
master-bin.000001 # Write_rows_v1 # # table_id: # flags: STMT_END_F
master-bin.000001 # Query # # COMMIT
master-bin.000001 # Gtid # # GTID #-#-#
master-bin.000001 # Query # # use `test`; DROP TABLE `t1` /* generated by server */
21 changes: 21 additions & 0 deletions mysql-test/suite/plugins/t/test_sql_service.test
@@ -1,4 +1,5 @@
--source include/not_embedded.inc
--source include/have_log_bin.inc

if (!$TEST_SQL_SERVICE_SO) {
skip No TEST_SQL_SERVICE plugin;
Expand All @@ -11,6 +12,20 @@ source include/wait_until_count_sessions.inc;
install plugin test_sql_service soname 'test_sql_service';
show status like 'test_sql_service_passed';

set global test_sql_service_execute_sql_global= 'create table test.t1 select 1 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_local= 'insert into test.t1 select 2 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 3 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET SQL_LOG_BIN=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 4 as a, @@SQL_LOG_BIN';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=1';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 5 as a, @@sql_auto_is_null';
set global test_sql_service_execute_sql_global= 'SET sql_auto_is_null=0';
set global test_sql_service_execute_sql_global= 'insert into test.t1 select 6 as a, @@sql_auto_is_null';
select * from t1 order by a;
drop table t1;
SET SQL_LOG_BIN=0;

set global test_sql_service_run_test= 1;
show status like 'test_sql_service_passed';

Expand Down Expand Up @@ -58,3 +73,9 @@ drop table t1;

uninstall plugin test_sql_service;

# Check that statements were executed/binlogged in correct order.
source include/show_binlog_events.inc;
# --replace_column 2 # 5 #
# --replace_regex /xid=[0-9]+/xid=XX/ /GTID [0-9]+-[0-9]+-[0-9]+/GTID #-#-#/
# SHOW BINLOG EVENTS LIMIT 3,100;

5 changes: 1 addition & 4 deletions sql/sql_class.cc
Expand Up @@ -1287,10 +1287,7 @@ void THD::init()
wsrep_desynced_backup_stage= false;
#endif /* WITH_WSREP */

if (variables.sql_log_bin)
variables.option_bits|= OPTION_BIN_LOG;
else
variables.option_bits&= ~OPTION_BIN_LOG;
set_binlog_bit();

variables.sql_log_bin_off= 0;

Expand Down
7 changes: 7 additions & 0 deletions sql/sql_class.h
Expand Up @@ -2934,6 +2934,13 @@ class THD: public THD_count, /* this must be first */
auto_inc_intervals_forced.empty(); // in case of multiple SET INSERT_ID
auto_inc_intervals_forced.append(next_id, ULONGLONG_MAX, 0);
}
inline void set_binlog_bit()
{
if (variables.sql_log_bin)
variables.option_bits |= OPTION_BIN_LOG;
else
variables.option_bits &= ~OPTION_BIN_LOG;
}

ulonglong limit_found_rows;

Expand Down
22 changes: 21 additions & 1 deletion sql/sql_prepare.cc
Expand Up @@ -5404,12 +5404,26 @@ class Protocol_local : public Protocol_text
Security_context empty_ctx;
ulonglong client_capabilities;

my_bool do_log_bin;

Protocol_local(THD *thd_arg, THD *new_thd_arg, ulong prealloc) :
Protocol_text(thd_arg, prealloc),
cur_data(0), first_data(0), data_tail(&first_data), alloc(0),
new_thd(new_thd_arg)
new_thd(new_thd_arg), do_log_bin(FALSE)
{}

void set_binlog_vars(my_bool *sav_log_bin)
{
*sav_log_bin= thd->variables.sql_log_bin;
thd->variables.sql_log_bin= do_log_bin;
thd->set_binlog_bit();
}
void restore_binlog_vars(my_bool sav_log_bin)
{
do_log_bin= thd->variables.sql_log_bin;
thd->variables.sql_log_bin= sav_log_bin;
thd->set_binlog_bit();
}
protected:
bool net_store_data(const uchar *from, size_t length);
bool net_store_data_cs(const uchar *from, size_t length,
Expand Down Expand Up @@ -6026,6 +6040,9 @@ loc_advanced_command(MYSQL *mysql, enum enum_server_command command,
Security_context *ctx_orig= p->thd->security_ctx;
ulonglong cap_orig= p->thd->client_capabilities;
MYSQL_LEX_STRING sql_text;
my_bool log_bin_orig;
p->set_binlog_vars(&log_bin_orig);

DBUG_ASSERT(current_thd == p->thd);
sql_text.str= (char *) arg;
sql_text.length= arg_length;
Expand All @@ -6034,6 +6051,7 @@ loc_advanced_command(MYSQL *mysql, enum enum_server_command command,
result= con.execute_direct(p, sql_text);
p->thd->client_capabilities= cap_orig;
p->thd->security_ctx= ctx_orig;
p->restore_binlog_vars(log_bin_orig);
}
if (skip_check)
result= 0;
Expand Down Expand Up @@ -6193,6 +6211,8 @@ extern "C" MYSQL *mysql_real_connect_local(MYSQL *mysql)
new_thd->query_cache_is_applicable= 0;
new_thd->variables.wsrep_on= 0;
new_thd->client_capabilities= client_flag;
new_thd->variables.sql_log_bin= 0;
new_thd->set_binlog_bit();
/*
TOSO: decide if we should turn the auditing off
for such threads.
Expand Down
5 changes: 1 addition & 4 deletions sql/sys_vars.cc
Expand Up @@ -4225,10 +4225,7 @@ static bool fix_sql_log_bin_after_update(sys_var *self, THD *thd,
{
DBUG_ASSERT(type == OPT_SESSION);

if (thd->variables.sql_log_bin)
thd->variables.option_bits |= OPTION_BIN_LOG;
else
thd->variables.option_bits &= ~OPTION_BIN_LOG;
thd->set_binlog_bit();

return FALSE;
}
Expand Down

0 comments on commit 3a8eb40

Please sign in to comment.