From 3f813d85a9180ec8619a04edb425c12aea365979 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Mon, 10 Apr 2023 12:00:32 +0500 Subject: [PATCH] * Saves runtime_mysql_servers when it is synced from the cluster and loads it during the boot process. --- lib/ProxySQL_Admin.cpp | 50 +++++++++++++++++++++++++++++++++++++++- lib/ProxySQL_Cluster.cpp | 20 ++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index d09511671d..f2349ddee1 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -117,6 +117,7 @@ static const vector mysql_firewall_tablenames = { static const vector mysql_query_rules_tablenames = { "mysql_query_rules", "mysql_query_rules_fast_routing" }; static const vector scheduler_tablenames = { "scheduler" }; static const vector proxysql_servers_tablenames = { "proxysql_servers" }; +static const vector runtime_mysql_servers_tablenames = { "runtime_mysql_servers" }; static unordered_map&> module_tablenames = { { "mysql_servers", mysql_servers_tablenames }, @@ -124,6 +125,7 @@ static unordered_map&> module_tablenames = { { "mysql_query_rules", mysql_query_rules_tablenames }, { "scheduler", scheduler_tablenames }, { "proxysql_servers", proxysql_servers_tablenames }, + { "runtime_mysql_servers", runtime_mysql_servers_tablenames }, }; static void BQE1(SQLite3DB *db, const vector& tbs, const string& p1, const string& p2, const string& p3) { @@ -6050,6 +6052,7 @@ bool ProxySQL_Admin::init() { #endif /* PROXYSQLCLICKHOUSE */ insert_into_tables_defs(tables_defs_config,"mysql_servers", ADMIN_SQLITE_TABLE_MYSQL_SERVERS); + insert_into_tables_defs(tables_defs_config,"runtime_mysql_servers", ADMIN_SQLITE_TABLE_RUNTIME_MYSQL_SERVERS); insert_into_tables_defs(tables_defs_config,"mysql_users", ADMIN_SQLITE_TABLE_MYSQL_USERS); insert_into_tables_defs(tables_defs_config,"mysql_replication_hostgroups", ADMIN_SQLITE_TABLE_MYSQL_REPLICATION_HOSTGROUPS); insert_into_tables_defs(tables_defs_config,"mysql_group_replication_hostgroups", ADMIN_SQLITE_TABLE_MYSQL_GROUP_REPLICATION_HOSTGROUPS); @@ -10334,6 +10337,7 @@ void ProxySQL_Admin::__insert_or_ignore_maintable_select_disktable() { void ProxySQL_Admin::__insert_or_replace_maintable_select_disktable() { admindb->execute("PRAGMA foreign_keys = OFF"); BQE1(admindb, mysql_servers_tablenames, "", "INSERT OR REPLACE INTO main.", " SELECT * FROM disk."); + BQE1(admindb, runtime_mysql_servers_tablenames, "", "INSERT OR REPLACE INTO main.", " SELECT * FROM disk."); BQE1(admindb, mysql_query_rules_tablenames, "", "INSERT OR REPLACE INTO main.", " SELECT * FROM disk."); admindb->execute("INSERT OR REPLACE INTO main.mysql_users SELECT * FROM disk.mysql_users"); BQE1(admindb, mysql_firewall_tablenames, "", "INSERT OR REPLACE INTO main.", " SELECT * FROM disk."); @@ -10413,6 +10417,7 @@ void ProxySQL_Admin::__delete_disktable() { void ProxySQL_Admin::__insert_or_replace_disktable_select_maintable() { BQE1(admindb, mysql_servers_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); + BQE1(admindb, runtime_mysql_servers_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); BQE1(admindb, mysql_query_rules_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); admindb->execute("INSERT OR REPLACE INTO disk.mysql_users SELECT * FROM main.mysql_users"); BQE1(admindb, mysql_firewall_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); @@ -10566,8 +10571,51 @@ void ProxySQL_Admin::init_clickhouse_users() { #endif /* PROXYSQLCLICKHOUSE */ void ProxySQL_Admin::init_mysql_servers() { + + char* error = NULL; + int cols = 0; + int affected_rows = 0; + SQLite3_result* runtime_mysql_servers = nullptr; + + const char* query = "SELECT hostgroup_id, hostname, port, gtid_port, status, weight, compression, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment FROM main.runtime_mysql_servers ORDER BY hostgroup_id, hostname, port"; + mysql_servers_wrlock(); - load_mysql_servers_to_runtime(); + + admindb->execute_statement(query, &error, &cols, &affected_rows, &runtime_mysql_servers); + + if (runtime_mysql_servers) { + if (runtime_mysql_servers->rows_count) { + // Remove 'OFFLINE_HARD' servers since they are not relevant to propagate to other Cluster + // nodes, or relevant for checksum computation. + const size_t init_row_count = runtime_mysql_servers->rows_count; + size_t rm_rows_count = 0; + const auto is_offline_server = [&rm_rows_count](SQLite3_row* row) { + if (strcasecmp(row->fields[4], "OFFLINE_HARD") == 0) { + rm_rows_count += 1; + return true; + } else { + return false; + } + }; + runtime_mysql_servers->rows.erase( + std::remove_if(runtime_mysql_servers->rows.begin(), runtime_mysql_servers->rows.end(), is_offline_server), + runtime_mysql_servers->rows.end() + ); + runtime_mysql_servers->rows_count = init_row_count - rm_rows_count; + + } else { + delete runtime_mysql_servers; + runtime_mysql_servers = nullptr; + } + } + + const incoming_servers_t incoming_servers{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, runtime_mysql_servers}; + load_mysql_servers_to_runtime(incoming_servers); + + //if (runtime_mysql_servers) { + // MyHGM->servers_add(runtime_mysql_servers); + // MyHGM->commit(runtime_mysql_servers, {}, nullptr, {}, true); + //} mysql_servers_wrunlock(); } diff --git a/lib/ProxySQL_Cluster.cpp b/lib/ProxySQL_Cluster.cpp index 1fddef4749..e9a2f828a8 100644 --- a/lib/ProxySQL_Cluster.cpp +++ b/lib/ProxySQL_Cluster.cpp @@ -1737,6 +1737,18 @@ void ProxySQL_Cluster::pull_runtime_mysql_servers_from_peer(const runtime_mysql_ MyHGM->servers_add(runtime_mysql_servers_resultset.get()); proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Updating runtime_mysql_servers from peer %s:%d", hostname, port); MyHGM->commit(runtime_mysql_servers_resultset.release(), peer_runtime_mysql_server, nullptr, {}, true); + + if (GloProxyCluster->cluster_mysql_servers_save_to_disk == true) { + proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Saving Runtime MySQL Servers to Database\n"); + GloAdmin->save_mysql_servers_runtime_to_database(true); + proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Saving to disk Runtime MySQL Servers from peer %s:%d\n", hostname, port); + proxy_info("Cluster: Saving to disk Runtime MySQL Servers from peer %s:%d\n", hostname, port); + GloAdmin->flush_GENERIC__from_to("runtime_mysql_servers", "memory_to_disk"); + } else { + proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Not saving to disk Runtime MySQL Servers from peer %s:%d failed.\n", hostname, port); + proxy_info("Cluster: Not saving to disk Runtime MySQL Servers from peer %s:%d failed.\n", hostname, port); + } + GloAdmin->mysql_servers_wrunlock(); // free result @@ -2177,6 +2189,14 @@ void ProxySQL_Cluster::pull_mysql_servers_v2_from_peer(const mysql_servers_v2_ch proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Saving to disk MySQL Servers v2 from peer %s:%d\n", hostname, port); proxy_info("Cluster: Saving to disk MySQL Servers v2 from peer %s:%d\n", hostname, port); GloAdmin->flush_GENERIC__from_to("mysql_servers", "memory_to_disk"); + + if (fetch_runtime_mysql_servers == true) { + proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Saving Runtime MySQL Servers to Database\n"); + GloAdmin->save_mysql_servers_runtime_to_database(true); + proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Saving to disk Runtime MySQL Servers from peer %s:%d\n", hostname, port); + proxy_info("Cluster: Saving to disk Runtime MySQL Servers from peer %s:%d\n", hostname, port); + GloAdmin->flush_GENERIC__from_to("runtime_mysql_servers", "memory_to_disk"); + } } else { proxy_debug(PROXY_DEBUG_CLUSTER, 5, "Not saving to disk MySQL Servers from peer %s:%d failed.\n", hostname, port); proxy_info("Cluster: Not saving to disk MySQL Servers from peer %s:%d failed.\n", hostname, port);