Skip to content

Commit

Permalink
Merge pull request #4450 from sysown/v2.x_shun_replication_lag
Browse files Browse the repository at this point in the history
Restore shunned server (due to replication lag) state on commit, provided that the replication lag is below the maximum threshold.
  • Loading branch information
renecannao authored Feb 20, 2024
2 parents 6a6807c + 984582e commit 6e18b04
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/MySQL_HostGroups_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class MySrvC { // MySQL Server Container
unsigned int max_connections_used; // The maximum number of connections that has been opened
unsigned int connect_OK;
unsigned int connect_ERR;
int cur_replication_lag;
unsigned int cur_replication_lag_count;
// note that these variables are in microsecond, while user defines max latency in millisecond
unsigned int current_latency_us;
Expand Down
1 change: 1 addition & 0 deletions include/MySQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class MySQL_Threads_Handler
bool log_mysql_warnings_enabled;
int data_packets_history_size;
int handle_warnings;
int evaluate_replication_lag_on_servers_load;
} variables;
struct {
unsigned int mirror_sessions_current;
Expand Down
2 changes: 2 additions & 0 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ __thread bool mysql_thread___enable_load_data_local_infile;
__thread int mysql_thread___client_host_cache_size;
__thread int mysql_thread___client_host_error_counts;
__thread int mysql_thread___handle_warnings;
__thread int mysql_thread___evaluate_replication_lag_on_servers_load;

/* variables used for Query Cache */
__thread int mysql_thread___query_cache_size_MB;
Expand Down Expand Up @@ -1034,6 +1035,7 @@ extern __thread bool mysql_thread___enable_load_data_local_infile;
extern __thread int mysql_thread___client_host_cache_size;
extern __thread int mysql_thread___client_host_error_counts;
extern __thread int mysql_thread___handle_warnings;
extern __thread int mysql_thread___evaluate_replication_lag_on_servers_load;

/* variables used for Query Cache */
extern __thread int mysql_thread___query_cache_size_MB;
Expand Down
24 changes: 21 additions & 3 deletions lib/MySQL_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ MySrvC::MySrvC(
max_connections=_max_connections;
max_replication_lag=_max_replication_lag;
use_ssl=_use_ssl;
cur_replication_lag=0;
cur_replication_lag_count=0;
max_latency_us=_max_latency_ms*1000;
current_latency_us=0;
Expand Down Expand Up @@ -2026,9 +2027,25 @@ bool MySQL_HostGroups_Manager::commit(
mysrvc->weight=atoi(r->fields[14]);
}
if (atoi(r->fields[5])!=atoi(r->fields[15])) {
if (GloMTH->variables.hostgroup_manager_verbose)
proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n" , mysrvc->myhgc->hid , mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]) , mysrvc->status , atoi(r->fields[15]));
mysrvc->status=(MySerStatus)atoi(r->fields[15]);
bool change_server_status = true;
if (GloMTH->variables.evaluate_replication_lag_on_servers_load == 1) {
if (mysrvc->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG && // currently server is shunned due to replication lag
(MySerStatus)atoi(r->fields[15]) == MYSQL_SERVER_STATUS_ONLINE) { // new server status is online
if (mysrvc->cur_replication_lag != -2) { // Master server? Seconds_Behind_Master column is not present
const unsigned int new_max_repl_lag = atoi(r->fields[18]);
if (mysrvc->cur_replication_lag < 0 ||
(new_max_repl_lag > 0 &&
((unsigned int)mysrvc->cur_replication_lag > new_max_repl_lag))) { // we check if current replication lag is greater than new max_replication_lag
change_server_status = false;
}
}
}
}
if (change_server_status == true) {
if (GloMTH->variables.hostgroup_manager_verbose)
proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n", mysrvc->myhgc->hid, mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]), mysrvc->status, atoi(r->fields[15]));
mysrvc->status = (MySerStatus)atoi(r->fields[15]);
}
if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) {
mysrvc->shunned_automatic=false;
}
Expand Down Expand Up @@ -3669,6 +3686,7 @@ void MySQL_HostGroups_Manager::replication_lag_action_inner(MyHGC *myhgc, const
for (j=0; j<(int)myhgc->mysrvs->cnt(); j++) {
MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j);
if (strcmp(mysrvc->address,address)==0 && mysrvc->port==port) {
mysrvc->cur_replication_lag = current_replication_lag;
if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) {
if (
// (current_replication_lag==-1 )
Expand Down
4 changes: 4 additions & 0 deletions lib/MySQL_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ static char * mysql_thread_variables_names[]= {
(char *)"query_cache_stores_empty_result",
(char *)"data_packets_history_size",
(char *)"handle_warnings",
(char *)"evaluate_replication_lag_on_servers_load",
NULL
};

Expand Down Expand Up @@ -896,6 +897,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() {
variables.client_host_cache_size=0;
variables.client_host_error_counts=0;
variables.handle_warnings=1;
variables.evaluate_replication_lag_on_servers_load=1;
variables.connect_retries_on_failure=10;
variables.connection_delay_multiplex_ms=0;
variables.connection_max_age_ms=0;
Expand Down Expand Up @@ -2083,6 +2085,7 @@ char ** MySQL_Threads_Handler::get_variables_list() {
VariablesPointers_int["client_host_cache_size"] = make_tuple(&variables.client_host_cache_size, 0, 1024*1024, false);
VariablesPointers_int["client_host_error_counts"] = make_tuple(&variables.client_host_error_counts, 0, 1024*1024, false);
VariablesPointers_int["handle_warnings"] = make_tuple(&variables.handle_warnings, 0, 1, false);
VariablesPointers_int["evaluate_replication_lag_on_servers_load"] = make_tuple(&variables.evaluate_replication_lag_on_servers_load, 0, 1, false);

// logs
VariablesPointers_int["auditlog_filesize"] = make_tuple(&variables.auditlog_filesize, 1024*1024, 1*1024*1024*1024, false);
Expand Down Expand Up @@ -4011,6 +4014,7 @@ void MySQL_Thread::refresh_variables() {
mysql_thread___client_host_cache_size=GloMTH->get_variable_int((char *)"client_host_cache_size");
mysql_thread___client_host_error_counts=GloMTH->get_variable_int((char *)"client_host_error_counts");
mysql_thread___handle_warnings=GloMTH->get_variable_int((char*)"handle_warnings");
mysql_thread___evaluate_replication_lag_on_servers_load=GloMTH->get_variable_int((char*)"evaluate_replication_lag_on_servers_load");
#ifdef DEBUG
mysql_thread___session_debug=(bool)GloMTH->get_variable_int((char *)"session_debug");
#endif /* DEBUG */
Expand Down

0 comments on commit 6e18b04

Please sign in to comment.