Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Saves runtime_mysql_servers when it is synced from the cluster and …
Browse files Browse the repository at this point in the history
…loads it during the boot process.
rahim-kanji committed Apr 10, 2023
1 parent 1122988 commit 3f813d8
Showing 2 changed files with 69 additions and 1 deletion.
50 changes: 49 additions & 1 deletion lib/ProxySQL_Admin.cpp
Original file line number Diff line number Diff line change
@@ -117,13 +117,15 @@ static const vector<string> mysql_firewall_tablenames = {
static const vector<string> mysql_query_rules_tablenames = { "mysql_query_rules", "mysql_query_rules_fast_routing" };
static const vector<string> scheduler_tablenames = { "scheduler" };
static const vector<string> proxysql_servers_tablenames = { "proxysql_servers" };
static const vector<string> runtime_mysql_servers_tablenames = { "runtime_mysql_servers" };

static unordered_map<string, const vector<string>&> module_tablenames = {
{ "mysql_servers", mysql_servers_tablenames },
{ "mysql_firewall", mysql_firewall_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<string>& 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();
}

20 changes: 20 additions & 0 deletions lib/ProxySQL_Cluster.cpp
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 3f813d8

Please sign in to comment.