Skip to content

Commit d114dbe

Browse files
committed
Const strings where appropriate and improved test for statistics
Changed the get_variable_id_for_name, and knows_variable_name to use const string &. Changed immutable strings to to be const as well. Added details comment to test_admin_stats-t.cpp test. In the test code, improved documentation, set a valid interval for metrics insertion, and added the commands to set the interval. Changed the sleep time to be the inteval plus a second should be long enough to let new metrics data be added. Put additional sleep before test 2. Fixed incorrect column index usage in test 4 result set. Reformulated test 5, to check the number of records inserted after new interval is correct.
1 parent e041be5 commit d114dbe

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

include/ProxySQL_Statistics.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ class ProxySQL_Statistics {
147147
* @param variable_name The string variable name identifier
148148
* @return Integer variable id for the given variable name
149149
*/
150-
int64_t get_variable_id_for_name(std::string variable_name);
150+
int64_t get_variable_id_for_name(const std::string & variable_name);
151151

152152
/** @brief If the variable_name_id_map is empty, then load its contents from all of the history_variables_lookup table records */
153153
void load_variable_name_id_map_if_empty();
154154

155155
/** @return True if the given variable_name is registered in the variable_name_id_map. */
156-
bool knows_variable_name(std::string variable_name) const;
156+
bool knows_variable_name(const std::string & variable_name) const;
157157

158158
private:
159159
/** @brief Map with the key being the variable_name and the value being the variable_id, used for history_mysql_variables data. Matches the history_mysql_variables_lookup. */

lib/ProxySQL_Statistics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,12 @@ void ProxySQL_Statistics::MySQL_Query_Cache_sets(SQLite3_result *resultset) {
10801080
}
10811081
}
10821082

1083-
bool ProxySQL_Statistics::knows_variable_name(std::string variable_name) const
1083+
bool ProxySQL_Statistics::knows_variable_name(const std::string & variable_name) const
10841084
{
10851085
return (variable_name_id_map.find(variable_name) != variable_name_id_map.end());
10861086
}
10871087

1088-
int64_t ProxySQL_Statistics::get_variable_id_for_name(std::string variable_name) {
1088+
int64_t ProxySQL_Statistics::get_variable_id_for_name(const std::string & variable_name) {
10891089
lock_guard<mutex> lock(mu);
10901090

10911091
int64_t variable_id = -1; // Negative value indicates not yet found.
@@ -1100,7 +1100,7 @@ int64_t ProxySQL_Statistics::get_variable_id_for_name(std::string variable_name)
11001100
char *error = NULL;
11011101

11021102
auto select_var_id = [&]() -> int64_t {
1103-
string var_id_query = "SELECT variable_id FROM history_mysql_status_variables_lookup WHERE variable_name=\"" + variable_name + "\"";
1103+
const string var_id_query = "SELECT variable_id FROM history_mysql_status_variables_lookup WHERE variable_name=\"" + variable_name + "\"";
11041104
statsdb_disk->execute_statement(var_id_query.c_str(), &error , &cols , &affected_rows , &result);
11051105

11061106
if (error) {

test/tap/tests/test_admin_stats-t.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/**
22
* @file test_admin_stats-t.cpp
33
* @brief This tests the Statistics module and its lookup code and tables.
4+
*
5+
* @details The following tests are performed :
6+
* 1. The lookup table has at least 50 rows generated after first metrics inserted
7+
* 2. There should be multiple distinct timestaps present in the history table
8+
* 3. The number of distinct variable_id's in the lookup and history tables should match
9+
* 4. Each variable_id has the same number of rows in history table
10+
* 5. The number of rows in the history table increases appropriately after insert interval changes
11+
*
412
* @date 2021-10-28
513
*/
614

@@ -18,6 +26,7 @@
1826
#include "utils.h"
1927

2028
using std::string;
29+
using std::to_string;
2130

2231
int main(int argc, char** argv) {
2332
CommandLine cl;
@@ -47,14 +56,16 @@ int main(int argc, char** argv) {
4756
}
4857

4958
// Setup the interval of how often new status entries are created
50-
uint16_t new_stats_interval_sec = 2;
59+
uint16_t new_stats_interval_sec = 5; // @note: valid values 5, 10, 30, 60, 120, 300
5160

52-
// @TODO: run command to set the interval
61+
// Run command to set the interval
62+
MYSQL_QUERY(proxysql_admin, ("SET admin-stats_mysql_connections=" + to_string(new_stats_interval_sec)).c_str());
63+
MYSQL_QUERY(proxysql_admin, "LOAD ADMIN VARIABLES TO RUNTIME");
5364

5465
// If on a fresh install, wait long enough for the first run of stats to be created
5566
// The lookup table will be empty until the first run!
56-
sleep(new_stats_interval_sec * 2);
57-
67+
sleep(new_stats_interval_sec + 1);
68+
5869
// Test 1: Lookup table has at least 50 rows
5970
int64_t lookup_row_count = 0;
6071
MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables_lookup");
@@ -72,7 +83,9 @@ int main(int argc, char** argv) {
7283
lookup_row_count
7384
);
7485

75-
// Test 2: Distinct timestamps in
86+
sleep(new_stats_interval_sec + 1);
87+
88+
// Test 2: There are multiple distinct timestaps present in history table
7689
int64_t distinct_timestamp_count = 0;
7790
MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(timestamp)) FROM history_mysql_status_variables");
7891
result = mysql_store_result(proxysql_admin);
@@ -118,20 +131,20 @@ int main(int argc, char** argv) {
118131
distinct_var_ids_in_lookup
119132
);
120133

121-
// Test 4: Check that for each variable_id has same number of rows in history table
134+
// Test 4: Each variable_id has same number of rows in history table
122135

123-
// @note: As the CI tests are done on a fresh install, these should match in this instance
136+
// As the CI tests are done on a fresh install, these should match in this instance
124137
// In practice, they could differ if new metrics variables are added.
125138

126139
std::vector<int64_t> rows_per_var_id;
127140
time_t two_mins_ago = time(nullptr) - 60*2;
128-
string query = "SELECT variable_id, COUNT(*) FROM history_mysql_status_variables WHERE timestamp < " + std::to_string(two_mins_ago) + " GROUP BY variable_id";
141+
const string query = "SELECT variable_id, COUNT(*) FROM history_mysql_status_variables WHERE timestamp < " + to_string(two_mins_ago) + " GROUP BY variable_id";
129142
MYSQL_QUERY(proxysql_admin, query.c_str());
130143
result = mysql_store_result(proxysql_admin);
131144

132145
for (int i = 0; i < result->row_count; i++) {
133146
row = mysql_fetch_row(result);
134-
rows_per_var_id.push_back(strtoll(row[0], nullptr, 10));
147+
rows_per_var_id.push_back(strtoll(row[1], nullptr, 10));
135148
}
136149

137150
mysql_free_result(result);
@@ -143,7 +156,7 @@ int main(int argc, char** argv) {
143156
"Each variable_id in the history table has the same number of rows."
144157
);
145158

146-
// Test 5: Number of rows in history table increases after connections stat changes
159+
// Test 5: Number of rows in history table increases appropriately after insert interval changes
147160
int64_t history_rows_before = 0;
148161
int64_t history_rows_after = 0;
149162

@@ -156,8 +169,13 @@ int main(int argc, char** argv) {
156169

157170
mysql_free_result(result);
158171

159-
MYSQL_QUERY(proxysql_admin, "SET admin-stats_mysql_connections=10");
160-
172+
// Increase interval and wait for next round of inserts.
173+
// distinct_var_ids_in_history should equal the # of records inserted.
174+
// If the interval isn't updated, then there'd be double what's expected.
175+
new_stats_interval_sec = 10;
176+
MYSQL_QUERY(proxysql_admin, ("SET admin-stats_mysql_connections=" + to_string(new_stats_interval_sec)).c_str());
177+
MYSQL_QUERY(proxysql_admin, "LOAD ADMIN VARIABLES TO RUNTIME");
178+
161179
sleep(new_stats_interval_sec + 1); // give it time to insert next round of stats
162180

163181
MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables");
@@ -170,10 +188,11 @@ int main(int argc, char** argv) {
170188
mysql_free_result(result);
171189

172190
ok(
173-
history_rows_before < history_rows_after,
174-
"Number of rows in history table increases after connections stat changes. Before: %lu After: %lu",
191+
(history_rows_before + distinct_var_ids_in_history) == history_rows_after,
192+
"Number of rows in history table increases correctly after insert interval change. Before: %lu After: %lu Difference should be: %lu",
175193
history_rows_before,
176-
history_rows_after
194+
history_rows_after,
195+
distinct_var_ids_in_history
177196
);
178197

179198
return exit_status();

0 commit comments

Comments
 (0)