From 16ef9472f8ab8bbce85b34c5a3e79b724d8fdf01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Mon, 15 Jun 2020 17:11:58 +0200 Subject: [PATCH] Re-enable PXC node after pxc_maint_mode is DISABLED #2711 This commit also add `port` in various ORDER BY --- lib/MySQL_HostGroups_Manager.cpp | 57 ++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 138ee313f7..1ff20bcd2e 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -4550,7 +4550,7 @@ void MySQL_HostGroups_Manager::converge_group_replication_config(int _writer_hos char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC"; + q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC, port DESC"; query=(char *)malloc(strlen(q)+256); sprintf(query, q, info->writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup, info->offline_hostgroup); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -4618,7 +4618,7 @@ void MySQL_HostGroups_Manager::converge_group_replication_config(int _writer_hos resultset=NULL; } if (info->writer_is_also_reader==2) { - q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC"; + q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC, port DESC"; query=(char *)malloc(strlen(q)+256); sprintf(query, q, info->writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup, info->offline_hostgroup); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -4747,7 +4747,7 @@ void MySQL_HostGroups_Manager::update_galera_set_offline(char *_hostname, int _p char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup WHERE hostname='%s' AND port=%d"; + q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=reader_hostgroup WHERE hostname='%s' AND port=%d AND status=0"; query=(char *)malloc(strlen(q)+strlen(_hostname)+1024); // increased this buffer as it is used for other queries too sprintf(query,q,_hostname,_port); mydb->execute_statement(query, &error , &cols , &affected_rows , &resultset); @@ -4798,7 +4798,13 @@ void MySQL_HostGroups_Manager::update_galera_set_offline(char *_hostname, int _p mydb->execute(query); //free(query); } else { - q=(char *)"UPDATE mysql_servers_incoming SET status=1 WHERE hostname='%s' AND port=%d"; + q=(char *)"INSERT OR IGNORE INTO mysql_servers_incoming SELECT %d, hostname, port, gtid_port, weight, status, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d, %d)"; + sprintf(query,q,info->offline_hostgroup,_hostname,_port,_writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup); + mydb->execute(query); + q=(char *)"DELETE FROM mysql_servers_incoming WHERE hostname='%s' AND port=%d AND hostgroup_id in (%d, %d)"; + sprintf(query,q,_hostname,_port, info->backup_writer_hostgroup, info->reader_hostgroup); + mydb->execute(query); + q=(char *)"UPDATE mysql_servers_incoming SET status=1 WHERE hostname='%s' AND port=%d AND hostgroup_id = %d"; sprintf(query,q,_hostname,_port,_writer_hostgroup); mydb->execute(query); } @@ -4981,7 +4987,7 @@ void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _po char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d"; + q=(char *)"SELECT hostgroup_id,status FROM mysql_servers JOIN mysql_galera_hostgroups ON hostgroup_id=writer_hostgroup OR hostgroup_id=reader_hostgroup OR hostgroup_id=backup_writer_hostgroup OR hostgroup_id=offline_hostgroup WHERE hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(_hostname)+32); sprintf(query,q,_hostname,_port); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -5020,7 +5026,9 @@ void MySQL_HostGroups_Manager::update_galera_set_writer(char *_hostname, int _po SQLite3_row *r=*it; int hostgroup=atoi(r->fields[0]); if (hostgroup==_writer_hostgroup) { - found_writer=true; + int status=atoi(r->fields[1]); + if (status==0) + found_writer=true; } if (read_HG>=0) { if (hostgroup==read_HG) { @@ -5200,7 +5208,7 @@ void MySQL_HostGroups_Manager::converge_galera_config(int _writer_hostgroup) { char *query=NULL; char *q=NULL; char *error=NULL; - q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC"; + q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC, port DESC"; query=(char *)malloc(strlen(q)+256); sprintf(query, q, info->writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup, info->offline_hostgroup); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset); @@ -5222,15 +5230,35 @@ void MySQL_HostGroups_Manager::converge_galera_config(int _writer_hostgroup) { } if (num_writers > info->max_writers) { // there are more writers than allowed int to_move=num_writers-info->max_writers; + int to_keep = info->max_writers; if (GloMTH->variables.hostgroup_manager_verbose > 1) { proxy_info("Galera: max_writers=%d , moving %d nodes from writer HG %d to backup HG %d\n", info->max_writers, to_move, info->writer_hostgroup, info->backup_writer_hostgroup); } - for (std::vector::reverse_iterator it = resultset->rows.rbegin() ; it != resultset->rows.rend(); ++it) { + //for (std::vector::reverse_iterator it = resultset->rows.rbegin() ; it != resultset->rows.rend(); ++it) { + // note: we change the iterator from reverse_iterator to forward iterator + for (std::vector::iterator it = resultset->rows.begin() ; it != resultset->rows.end(); ++it) { SQLite3_row *r=*it; - if (to_move) { - int hostgroup=atoi(r->fields[0]); - if (hostgroup==info->writer_hostgroup) { - q=(char *)"UPDATE OR REPLACE mysql_servers_incoming SET status=0, hostgroup_id=%d WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; + int hostgroup=atoi(r->fields[0]); + if (hostgroup==info->writer_hostgroup) { + if (to_keep) { + q=(char *)"UPDATE OR REPLACE mysql_servers_incoming SET status=0 WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; + query=(char *)malloc(strlen(q)+strlen(r->fields[1])+128); + sprintf(query,q,info->writer_hostgroup,r->fields[1],atoi(r->fields[2])); + mydb->execute(query); + free(query); + to_keep--; + continue; + } + if (to_move) { + // if the server is already in writer hostgroup, we set to shunned #2656 + q=(char *)"UPDATE OR REPLACE mysql_servers_incoming SET status=1 WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; + query=(char *)malloc(strlen(q)+strlen(r->fields[1])+128); + sprintf(query,q,info->writer_hostgroup,r->fields[1],atoi(r->fields[2])); + mydb->execute(query); + free(query); + //q=(char *)"UPDATE OR REPLACE mysql_servers_incoming SET status=0, hostgroup_id=%d WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; + // we copy the server from the writer hostgroup in the backup writer hostgroup #2656 + q=(char *)"INSERT OR IGNORE INTO mysql_servers_incoming SELECT %d, hostname, port, gtid_port, weight, 0, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM mysql_servers_incoming WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(r->fields[1])+128); sprintf(query,q,info->backup_writer_hostgroup,info->writer_hostgroup,r->fields[1],atoi(r->fields[2])); mydb->execute(query); @@ -5253,6 +5281,9 @@ void MySQL_HostGroups_Manager::converge_galera_config(int _writer_hostgroup) { q=(char *)"UPDATE OR REPLACE mysql_servers_incoming SET status=0, hostgroup_id=%d WHERE hostgroup_id=%d AND hostname='%s' AND port=%d"; query=(char *)malloc(strlen(q)+strlen(r->fields[1])+128); sprintf(query,q,info->writer_hostgroup,info->backup_writer_hostgroup,r->fields[1],atoi(r->fields[2])); + if (GloMTH->variables.hostgroup_manager_verbose) { + proxy_info("Galera: %s\n", query); + } mydb->execute(query); free(query); to_move--; @@ -5329,7 +5360,7 @@ void MySQL_HostGroups_Manager::converge_galera_config(int _writer_hostgroup) { resultset=NULL; } if (info->writer_is_also_reader==2) { - q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC"; + q=(char *)"SELECT hostgroup_id,hostname,port FROM mysql_servers_incoming WHERE status=0 AND hostgroup_id IN (%d, %d, %d, %d) ORDER BY weight DESC, hostname DESC, port DESC"; query=(char *)malloc(strlen(q)+256); sprintf(query, q, info->writer_hostgroup, info->backup_writer_hostgroup, info->reader_hostgroup, info->offline_hostgroup); mydb->execute_statement(query, &error, &cols , &affected_rows , &resultset);