From 962116f1b646f9a60e734cb019cddbb6084acf04 Mon Sep 17 00:00:00 2001 From: Jared Jetsel Date: Fri, 19 Nov 2021 15:08:36 -0800 Subject: [PATCH] Fixed typo, memory leak, and documentation. * Fixed typo for variable_id in history_mysql_status_variables table definition to be INT NOT NULL as intended. * Added comments before debug ifdefs to explain their purpose * Optimized get_variable_id_for_name by performing one less search by using iterator insteaed of knows_variable_name() call. * Fixed memory leak by deleting resultset before return in lambda. --- include/ProxySQL_Statistics.hpp | 2 +- lib/ProxySQL_Admin.cpp | 2 ++ lib/ProxySQL_Statistics.cpp | 12 +++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/ProxySQL_Statistics.hpp b/include/ProxySQL_Statistics.hpp index 01992103d1..7e356b8196 100644 --- a/include/ProxySQL_Statistics.hpp +++ b/include/ProxySQL_Statistics.hpp @@ -14,7 +14,7 @@ #define STATSDB_SQLITE_TABLE_MYSQL_CONNECTIONS STATSDB_SQLITE_TABLE_MYSQL_CONNECTIONS_V2_0 -#define STATSDB_SQLITE_TABLE_HISTORY_MYSQL_STATUS_VARIABLES_V2_4_0 "CREATE TABLE history_mysql_status_variables (timestamp INT NOT NULL , variable_id VARCHAR NOT NULL , variable_value VARCHAR NOT NULL , PRIMARY KEY (timestamp, variable_id))" +#define STATSDB_SQLITE_TABLE_HISTORY_MYSQL_STATUS_VARIABLES_V2_4_0 "CREATE TABLE history_mysql_status_variables (timestamp INT NOT NULL , variable_id INT NOT NULL , variable_value VARCHAR NOT NULL , PRIMARY KEY (timestamp, variable_id))" #define STATSDB_SQLITE_TABLE_HISTORY_MYSQL_STATUS_VARIABLES STATSDB_SQLITE_TABLE_HISTORY_MYSQL_STATUS_VARIABLES_V2_4_0 diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 4cb6536a39..3d6f8a3a5d 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -5081,6 +5081,8 @@ static void * admin_main_loop(void *arg) SQLite3_result * resultset=MyHGM->SQL3_Get_ConnPool_Stats(); if (resultset) { SQLite3_result * resultset2 = NULL; + + // In debug, run the code to generate metrics so that it can be tested even if the web interface plugin isn't loaded. #ifdef DEBUG if (true) { #else diff --git a/lib/ProxySQL_Statistics.cpp b/lib/ProxySQL_Statistics.cpp index bbc5457c73..205ca5cb7d 100644 --- a/lib/ProxySQL_Statistics.cpp +++ b/lib/ProxySQL_Statistics.cpp @@ -644,6 +644,8 @@ void ProxySQL_Statistics::system_memory_sets() { void ProxySQL_Statistics::MyHGM_Handler_sets(SQLite3_result *resultset1, SQLite3_result *resultset2) { MyHGM_Handler_sets_v1(resultset1); + +// In debug, enable metrics features for debugging and testing even if the web interface plugin is not loaded. #ifdef DEBUG if (resultset2) { #else @@ -793,6 +795,7 @@ void ProxySQL_Statistics::MyHGM_Handler_sets_v1(SQLite3_result *resultset) { void ProxySQL_Statistics::MySQL_Threads_Handler_sets(SQLite3_result *resultset) { MySQL_Threads_Handler_sets_v1(resultset); +// In debug, enable metrics features for debugging and testing even if the web interface plugin is not loaded. #ifdef DEBUG if (true) { #else @@ -1089,9 +1092,10 @@ int64_t ProxySQL_Statistics::get_variable_id_for_name(const std::string & variab lock_guard lock(mu); int64_t variable_id = -1; // Negative value indicates not yet found. + auto it = variable_name_id_map.find(variable_name); - if (knows_variable_name(variable_name)) { - variable_id = variable_name_id_map[variable_name]; + if (it != variable_name_id_map.end()) { + variable_id = it->second; } else { // No matching variable_id found in map. Try loading from the SQLite lookup table on disk SQLite3_result *result = NULL; @@ -1114,7 +1118,9 @@ int64_t ProxySQL_Statistics::get_variable_id_for_name(const std::string & variab if (result->rows_count > 0) { // matching variable_id for variable_name in lookup table SQLite3_row *r = result->rows[0]; - return strtoll(r->fields[0], NULL, 10); + int64_t found_variable_id = strtoll(r->fields[0], NULL, 10); + delete result; + return found_variable_id; } delete result; result = NULL;