Skip to content

Commit

Permalink
Add current master host:port to server read only error when available
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tianx committed Aug 8, 2016
1 parent 1476c29 commit 270854f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions mysql-test/suite/rpl/r/rpl_read_only.result
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ a
2004
2005
insert into t1 values(1006);
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement
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
insert into t2 values(2006);
ERROR HY000: The MySQL server is running with the --read-only option so it cannot execute this statement
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
drop user test;
drop table t1;
drop table t2;
Expand Down
6 changes: 4 additions & 2 deletions mysql-test/suite/rpl/t/rpl_read_only.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ select * from t2;

# Non root user can not write on the slave
connection slave2;
--error ER_OPTION_PREVENTS_STATEMENT
--replace_result $MASTER_MYPORT MASTER_PORT
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
insert into t1 values(1006);
--error ER_OPTION_PREVENTS_STATEMENT
--replace_result $MASTER_MYPORT MASTER_PORT
--error ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
insert into t2 values(2006);

# Verify read_only cannot be disabled on slave
Expand Down
7 changes: 5 additions & 2 deletions sql/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "discover.h" // writefrm
#include "log_event.h" // *_rows_log_event
#include "rpl_filter.h"
#include "rpl_slave.h"
#include <myisampack.h>
#include "transaction.h"
#include <errno.h>
Expand Down Expand Up @@ -1493,13 +1494,15 @@ int ha_commit_trans(THD *thd, bool all, bool async,
// repository tables.
if (rw_trans && check_ro(thd) && !ignore_global_read_lock)
{
std::string extra_info;
int nr = get_active_master_info(&extra_info);
if (opt_super_readonly)
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
}
else
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
}
ha_rollback_trans(thd, all);
error= 1;
Expand Down
2 changes: 2 additions & 0 deletions sql/share/errmsg-utf8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7227,6 +7227,8 @@ ER_GIT_HASH
ER_CONNECTION_TIMEOUT
eng "Closed connection: idle timeout after %us"

ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO
eng "The MySQL server is running with the %s option so it cannot execute this statement. %s"
#
# End of 5.6 error messages.
#
33 changes: 29 additions & 4 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3137,13 +3137,15 @@ mysql_execute_command(THD *thd,
*/
if (deny_updates_if_read_only_option(thd, all_tables))
{
std::string extra_info;
int nr = get_active_master_info(&extra_info);
if (opt_super_readonly)
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
}
else
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
}
DBUG_RETURN(-1);
}
Expand Down Expand Up @@ -4132,13 +4134,15 @@ case SQLCOM_PREPARE:
break;
if (check_ro(thd) && some_non_temp_table_to_be_updated(thd, all_tables))
{
std::string extra_info;
int nr = get_active_master_info(&extra_info);
if (opt_super_readonly)
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only (super)");
my_error(nr, MYF(0), "--read-only (super)", extra_info.c_str());
}
else
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
my_error(nr, MYF(0), "--read-only", extra_info.c_str());
}
break;
}
Expand Down Expand Up @@ -9900,3 +9904,24 @@ THD* get_opt_thread_with_data_lock(THD *thd, ulong thread_id)

return ret_thd;
}

// This function will generate the string containing current master host and
// port info if available.
//
// Return value:
// ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO, if master info is available.
// ER_OPTION_PREVENTS_STATEMENT, if master info is not available.
int get_active_master_info(std::string *str_ptr)
{
#ifdef HAVE_REPLICATION
if (str_ptr && active_mi && active_mi->host && active_mi->host[0])
{
*str_ptr = "Current master_host: ";
*str_ptr += active_mi->host;
*str_ptr += ", master_port: ";
*str_ptr += std::to_string(active_mi->port);
return ER_OPTION_PREVENTS_STATEMENT_EXTRA_INFO;
}
#endif
return ER_OPTION_PREVENTS_STATEMENT;
}
2 changes: 2 additions & 0 deletions sql/sql_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,6 @@ inline bool is_supported_parser_charset(const CHARSET_INFO *cs)

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

int get_active_master_info(std::string *str);

#endif /* SQL_PARSE_INCLUDED */

0 comments on commit 270854f

Please sign in to comment.