Skip to content

Commit 270854f

Browse files
committed
Add current master host:port to server read only error when available
Summary: When a server is in read_only, we append the current master_host and master_port to the error message, if the server is a slave. The master_host could be either hostname or ip address, depending on how a slave is connected to the master. A new generic error code `ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO` is added, so that we can append any additional info to the fixed error message. Test Plan: Updated rpl.rpl_read_only. Master_port is non-deterministic in the test, so I masked in the test result. Will update any test that comes up in the sandcastle if needed. Reviewers: santoshb Reviewed By: santoshb Subscribers: webscalesql-eng, ebergen Differential Revision: https://reviews.facebook.net/D61383
1 parent 1476c29 commit 270854f

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ a
118118
2004
119119
2005
120120
insert into t1 values(1006);
121-
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement
121+
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);
123-
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement
123+
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
124124
drop user test;
125125
drop table t1;
126126
drop table t2;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ select * from t2;
113113

114114
# Non root user can not write on the slave
115115
connection slave2;
116-
--error ER_OPTION_PREVENTS_STATEMENT
116+
--replace_result $MASTER_MYPORT MASTER_PORT
117+
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
117118
insert into t1 values(1006);
118-
--error ER_OPTION_PREVENTS_STATEMENT
119+
--replace_result $MASTER_MYPORT MASTER_PORT
120+
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
119121
insert into t2 values(2006);
120122

121123
# Verify read_only cannot be disabled on slave

sql/handler.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "discover.h" // writefrm
3333
#include "log_event.h" // *_rows_log_event
3434
#include "rpl_filter.h"
35+
#include "rpl_slave.h"
3536
#include <myisampack.h>
3637
#include "transaction.h"
3738
#include <errno.h>
@@ -1493,13 +1494,15 @@ int ha_commit_trans(THD *thd, bool all, bool async,
14931494
// repository tables.
14941495
if (rw_trans && check_ro(thd) && !ignore_global_read_lock)
14951496
{
1497+
std::string extra_info;
1498+
int nr = get_active_master_info(&extra_info);
14961499
if (opt_super_readonly)
14971500
{
1498-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
1501+
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
14991502
}
15001503
else
15011504
{
1502-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1505+
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
15031506
}
15041507
ha_rollback_trans(thd, all);
15051508
error= 1;

sql/share/errmsg-utf8.txt

+2
Original file line numberDiff line numberDiff line change
@@ -7227,6 +7227,8 @@ ER_GIT_HASH
72277227
ER_CONNECTION_TIMEOUT
72287228
eng "Closed connection: idle timeout after %us"
72297229

7230+
ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
7231+
eng "The MySQL server is running with the %s option so it cannot execute this statement. %s"
72307232
#
72317233
# End of 5.6 error messages.
72327234
#

sql/sql_parse.cc

+29-4
Original file line numberDiff line numberDiff line change
@@ -3137,13 +3137,15 @@ mysql_execute_command(THD *thd,
31373137
*/
31383138
if (deny_updates_if_read_only_option(thd, all_tables))
31393139
{
3140+
std::string extra_info;
3141+
int nr = get_active_master_info(&extra_info);
31403142
if (opt_super_readonly)
31413143
{
3142-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
3144+
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
31433145
}
31443146
else
31453147
{
3146-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
3148+
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
31473149
}
31483150
DBUG_RETURN(-1);
31493151
}
@@ -4132,13 +4134,15 @@ case SQLCOM_PREPARE:
41324134
break;
41334135
if (check_ro(thd) && some_non_temp_table_to_be_updated(thd, all_tables))
41344136
{
4137+
std::string extra_info;
4138+
int nr = get_active_master_info(&extra_info);
41354139
if (opt_super_readonly)
41364140
{
4137-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
4141+
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
41384142
}
41394143
else
41404144
{
4141-
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
4145+
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
41424146
}
41434147
break;
41444148
}
@@ -9900,3 +9904,24 @@ THD* get_opt_thread_with_data_lock(THD *thd, ulong thread_id)
99009904

99019905
return ret_thd;
99029906
}
9907+
9908+
// This function will generate the string containing current master host and
9909+
// port info if available.
9910+
//
9911+
// Return value:
9912+
// ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO, if master info is available.
9913+
// ER_OPTION_PREVENTS_STATEMENT, if master info is not available.
9914+
int get_active_master_info(std::string *str_ptr)
9915+
{
9916+
#ifdef HAVE_REPLICATION
9917+
if (str_ptr && active_mi && active_mi->host && active_mi->host[0])
9918+
{
9919+
*str_ptr = "Current master_host: ";
9920+
*str_ptr += active_mi->host;
9921+
*str_ptr += ", master_port: ";
9922+
*str_ptr += std::to_string(active_mi->port);
9923+
return ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO;
9924+
}
9925+
#endif
9926+
return ER_OPTION_PREVENTS_STATEMENT;
9927+
}

sql/sql_parse.h

+2
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,6 @@ inline bool is_supported_parser_charset(const CHARSET_INFO *cs)
263263

264264
extern "C" bool sqlcom_can_generate_row_events(const THD *thd);
265265

266+
int get_active_master_info(std::string *str);
267+
266268
#endif /* SQL_PARSE_INCLUDED */

0 commit comments

Comments
 (0)