Skip to content

Commit 9f00827

Browse files
committed
Add a variable for appending a custom message to server read_only error
Summary: Proposing a way to set a custom message in the `--read-only (super)` error. Note: the variable can only be set when server is in read-only (super) state. p.s. I am hesitant to add a mutex for the variable, so we will get a snapshot of the current pointer of the variable (const char*) when printing the error. Squash with https://reviews.facebook.net/D61383 Test Plan: Added tests. Reviewers: santoshb Reviewed By: santoshb Subscribers: webscalesql-eng, ebergen Differential Revision: https://reviews.facebook.net/D61713
1 parent 424ce1f commit 9f00827

12 files changed

+107
-0
lines changed

mysql-test/r/bug58669.result

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ user1@localhost
1111
SHOW VARIABLES LIKE "read_only%";
1212
Variable_name Value
1313
read_only ON
14+
read_only_error_msg_extra
1415
read_only_slave ON
1516
INSERT INTO db1.t1 VALUES (1);
1617
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement

mysql-test/r/mysqld--help-notwin-profiling.result

+4
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,9 @@ The following options may be given as the first argument:
872872
--read-only Make all non-temporary tables read-only, with the
873873
exception for replication (slave) threads and users with
874874
the SUPER privilege
875+
--read-only-error-msg-extra[=name]
876+
Set this variable to print out extra error information,
877+
which will be appended to read_only error messages.
875878
--read-only-slave Blocks disabling read_only if the server is a slave. This
876879
is helpful in asserting that read_only is never disabled
877880
on a slave. Slave with read_only=0 may generate new GTID
@@ -1796,6 +1799,7 @@ range-alloc-block-size 4096
17961799
rbr-idempotent-tables (No default value)
17971800
read-buffer-size 131072
17981801
read-only FALSE
1802+
read-only-error-msg-extra
17991803
read-only-slave TRUE
18001804
read-rnd-buffer-size 262144
18011805
relay-log (No default value)

mysql-test/r/mysqld--help-notwin.result

+4
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@ The following options may be given as the first argument:
870870
--read-only Make all non-temporary tables read-only, with the
871871
exception for replication (slave) threads and users with
872872
the SUPER privilege
873+
--read-only-error-msg-extra[=name]
874+
Set this variable to print out extra error information,
875+
which will be appended to read_only error messages.
873876
--read-only-slave Blocks disabling read_only if the server is a slave. This
874877
is helpful in asserting that read_only is never disabled
875878
on a slave. Slave with read_only=0 may generate new GTID
@@ -1793,6 +1796,7 @@ range-alloc-block-size 4096
17931796
rbr-idempotent-tables (No default value)
17941797
read-buffer-size 131072
17951798
read-only FALSE
1799+
read-only-error-msg-extra
17961800
read-only-slave TRUE
17971801
read-rnd-buffer-size 262144
17981802
relay-log (No default value)

mysql-test/suite/rpl/r/rpl_read_only.result

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ insert into t1 values(1006);
121121
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement. Current master_host: 127.0.0.1, master_port: MASTER_PORT
122122
insert into t2 values(2006);
123123
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement. Current master_host: 127.0.0.1, master_port: MASTER_PORT
124+
set global read_only_error_msg_extra = "This is a custom message";
125+
insert into t1 values(1006);
126+
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement. Current master_host: 127.0.0.1, master_port: MASTER_PORT. This is a custom message
127+
insert into t2 values(2006);
128+
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement. Current master_host: 127.0.0.1, master_port: MASTER_PORT. This is a custom message
129+
set global read_only_error_msg_extra = default;
124130
drop user test;
125131
drop table t1;
126132
drop table t2;

mysql-test/suite/rpl/t/rpl_read_only.test

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ create user test;
1515

1616
connect (master2,127.0.0.1,test,,test,$MASTER_MYPORT,);
1717
connect (slave2,127.0.0.1,test,,test,$SLAVE_MYPORT,);
18+
connect (slave2_root,127.0.0.1,root,,test,$SLAVE_MYPORT,);
1819

1920
connection master1;
2021

@@ -119,6 +120,18 @@ insert into t1 values(1006);
119120
--replace_result $MASTER_MYPORT MASTER_PORT
120121
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
121122
insert into t2 values(2006);
123+
# set extra info for the error message
124+
connection slave2_root;
125+
set global read_only_error_msg_extra = "This is a custom message";
126+
connection slave2;
127+
--replace_result $MASTER_MYPORT MASTER_PORT
128+
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
129+
insert into t1 values(1006);
130+
--replace_result $MASTER_MYPORT MASTER_PORT
131+
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
132+
insert into t2 values(2006);
133+
connection slave2_root;
134+
set global read_only_error_msg_extra = default;
122135

123136
# Verify read_only cannot be disabled on slave
124137
connection slave;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set @@global.read_only_error_msg_extra='Not in read_only';
2+
ERROR HY000: The system variable read_only_error_msg_extra can only be set in read-only (super) status.
3+
set global read_only = true;
4+
set @@global.read_only_error_msg_extra = default;
5+
select @@global.read_only_error_msg_extra;
6+
@@global.read_only_error_msg_extra
7+
8+
set @saved_read_only_error_msg_extra = @@global.read_only_error_msg_extra;
9+
set @@global.read_only_error_msg_extra='This is a custom message';
10+
set global super_read_only = true;
11+
set @@global.read_only_error_msg_extra='This is another custom message';
12+
set @@global.read_only_error_msg_extra=1;
13+
ERROR 42000: Incorrect argument type to variable 'read_only_error_msg_extra'
14+
select @@session.read_only_error_msg_extra;
15+
ERROR HY000: Variable 'read_only_error_msg_extra' is a GLOBAL variable
16+
set @@session.read_only_error_msg_extra='This is a custom message';
17+
ERROR HY000: Variable 'read_only_error_msg_extra' is a GLOBAL variable and should be set with SET GLOBAL
18+
set global read_only_error_msg_extra = @saved_read_only_error_msg_extra;
19+
set global super_read_only = false;
20+
set global read_only = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--source include/not_embedded.inc
2+
3+
# Cannot set the value if server is not in read_only
4+
--error ER_VARIABLE_NOT_SETTABLE_WITHOUT_READ_ONLY
5+
set @@global.read_only_error_msg_extra='Not in read_only';
6+
7+
set global read_only = true;
8+
set @@global.read_only_error_msg_extra = default;
9+
select @@global.read_only_error_msg_extra;
10+
set @saved_read_only_error_msg_extra = @@global.read_only_error_msg_extra;
11+
12+
set @@global.read_only_error_msg_extra='This is a custom message';
13+
14+
set global super_read_only = true;
15+
set @@global.read_only_error_msg_extra='This is another custom message';
16+
17+
--error ER_WRONG_TYPE_FOR_VAR
18+
set @@global.read_only_error_msg_extra=1;
19+
20+
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
21+
select @@session.read_only_error_msg_extra;
22+
--error ER_GLOBAL_VARIABLE
23+
set @@session.read_only_error_msg_extra='This is a custom message';
24+
25+
set global read_only_error_msg_extra = @saved_read_only_error_msg_extra;
26+
27+
set global super_read_only = false;
28+
set global read_only = false;

sql/mysqld.cc

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ my_bool block_create_myisam = FALSE;
504504
my_bool block_create_memory = FALSE;
505505
my_bool block_create_no_primary_key = FALSE;
506506
my_bool read_only= 0, opt_readonly= 0;
507+
char* opt_read_only_error_msg_extra;
507508
my_bool super_read_only = 0, opt_super_readonly = 0;
508509
my_bool use_temp_pool, relay_log_purge;
509510
my_bool relay_log_recovery;

sql/mysqld.h

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ extern ulong slave_run_triggers_for_rbr;
296296
extern ulonglong slave_type_conversions_options;
297297
extern ulonglong admission_control_filter;
298298
extern my_bool read_only, opt_readonly, super_read_only, opt_super_readonly;
299+
extern char* opt_read_only_error_msg_extra;
299300
extern my_bool send_error_before_closing_timed_out_connection;
300301
extern my_bool allow_document_type;
301302
extern my_bool block_create_myisam;

sql/share/errmsg-utf8.txt

+3
Original file line numberDiff line numberDiff line change
@@ -7229,6 +7229,9 @@ ER_CONNECTION_TIMEOUT
72297229

72307230
ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
72317231
eng "The MySQL server is running with the %s option so it cannot execute this statement. %s"
7232+
7233+
ER_VARIABLE_NOT_SETTABLE_WITHOUT_READ_ONLY
7234+
eng "The system variable %.200s can only be set in read-only (super) status."
72327235
#
72337236
# End of 5.6 error messages.
72347237
#

sql/sql_parse.cc

+6
Original file line numberDiff line numberDiff line change
@@ -9920,6 +9920,12 @@ int get_active_master_info(std::string *str_ptr)
99209920
*str_ptr += active_mi->host;
99219921
*str_ptr += ", master_port: ";
99229922
*str_ptr += std::to_string(active_mi->port);
9923+
const char *extra_str = opt_read_only_error_msg_extra;
9924+
if (extra_str && extra_str[0])
9925+
{
9926+
*str_ptr += ". ";
9927+
*str_ptr += extra_str;
9928+
}
99239929
return ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO;
99249930
}
99259931
#endif

sql/sys_vars.cc

+20
Original file line numberDiff line numberDiff line change
@@ -5505,3 +5505,23 @@ static Sys_var_uint Sys_select_into_file_fsync_timeout(
55055505
"SELECT INTO OUTFILE",
55065506
SESSION_VAR(select_into_file_fsync_timeout), CMD_LINE(OPT_ARG),
55075507
VALID_RANGE(0, UINT_MAX), DEFAULT(0), BLOCK_SIZE(1));
5508+
5509+
static bool check_read_only_error_msg_extra(
5510+
sys_var *self, THD *thd, set_var *var)
5511+
{
5512+
if (!opt_readonly && !opt_super_readonly)
5513+
{
5514+
my_error(ER_VARIABLE_NOT_SETTABLE_WITHOUT_READ_ONLY,
5515+
MYF(0),
5516+
var->var->name.str);
5517+
return true;
5518+
}
5519+
return false;
5520+
}
5521+
static Sys_var_charptr Sys_read_only_error_msg_extra(
5522+
"read_only_error_msg_extra",
5523+
"Set this variable to print out extra error information, "
5524+
"which will be appended to read_only error messages.",
5525+
GLOBAL_VAR(opt_read_only_error_msg_extra), CMD_LINE(OPT_ARG),
5526+
IN_SYSTEM_CHARSET, DEFAULT(""), NO_MUTEX_GUARD, NOT_IN_BINLOG,
5527+
ON_CHECK(check_read_only_error_msg_extra));

0 commit comments

Comments
 (0)