forked from mariadb-corporation/MaxScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_tune.cc
126 lines (97 loc) · 3.53 KB
/
auto_tune.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2022 MariaDB Corporation Ab
* Copyright (c) 2023 MariaDB plc, Finnish Branch
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2027-04-10
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <iostream>
#include <maxtest/testconnections.hh>
#include <maxtest/maxrest.hh>
using namespace std;
namespace
{
string get_parameter_value(MaxRest& rest, const char* zParameter)
{
auto json = rest.v1_services("RW-Split-Router");
auto parameters = json.at("/data/attributes/parameters");
return parameters.get_string(zParameter);
}
string get_server_variable(Connection& c, const char* zVariable)
{
string query = "SELECT @@global.";
query += zVariable;
Row row = c.row(query);
return row[0];
}
bool set_server_variable(Connection& c, const char* zVariable, const string& value)
{
string query = "SET @@global.";
query += zVariable;
query += " = ";
query += value;
return c.query(query);
}
}
string touch_connection_keepalive(const string& value)
{
// We know connection keepalive is 80% of the minimum wait_timeout
// value of all servers used by the service and we assume that it
// initially is the same everywhere. Thus, by reducing the value
// of wait_timeout on one, it should affect connection_keepalive
// of the service.
long l = std::stol(value);
l *= 0.8;
return std::to_string(l);
}
struct AutoTuneCase
{
const char* zMaxScale_parameter;
const char* zServer_variable;
string (*touch)(const string& value);
};
AutoTuneCase auto_tune_cases[] =
{
{
"connection_keepalive",
"wait_timeout",
touch_connection_keepalive
}
};
void check(TestConnections& test, MaxRest& rest, Connection& c, const AutoTuneCase& auto_tune_case)
{
string parameter_was = get_parameter_value(rest, auto_tune_case.zMaxScale_parameter);
string variable_was = get_server_variable(c, auto_tune_case.zServer_variable);
cout << "Variable: " << variable_was << ", parameter: " << parameter_was << endl;
string variable_is = auto_tune_case.touch(variable_was);
test.expect(set_server_variable(c, auto_tune_case.zServer_variable, variable_is),
"Could not update variable: %s", c.error());
// Currently the variable values are fetched by the Monitor every 10 seconds.
sleep(10);
test.maxscale->wait_for_monitor(2); // To make sure auto_tune has picked up the current values.
string parameter_is = get_parameter_value(rest, auto_tune_case.zMaxScale_parameter);
cout << "Variable: " << variable_is << ", parameter: " << parameter_is << endl;
test.expect(parameter_is != parameter_was, "Parameter value is still the same.");
// Restore the situation.
test.expect(set_server_variable(c, auto_tune_case.zServer_variable, variable_was),
"Could not reset variable: %s", c.error());
}
int main(int argc, char* argv[])
{
TestConnections test(argc, argv);
test.maxscale->wait_for_monitor(2); // To make sure auto_tune has picked up the current values.
MaxRest rest(&test);
Connection c = test.repl->get_connection(0);
test.expect(c.connect(), "Could not connect to MariaDB node: %s", c.error());
for (const auto& auto_tune_case : auto_tune_cases)
{
check(test, rest, c, auto_tune_case);
}
return test.global_result;
}