Skip to content

Commit fc61684

Browse files
committed
Extract 'Session_Regex' from classes into isolated files
This commit performs some header cleanup and isolates 'Session_Regex' ('match_regexes') into isolated files. This separation of utilities allows unit testing for all those function/classes which are not necessary bounded to deeply interconnected classes logic (like 'MySQL_Session', 'MySQL_Variables', etc...). Attempting to use these classes in unit tests will inevitable result in linking issues due to their interconnection and current relationships with globals. Meanwhile, the utility files can be isolated and linked with any test file. Following these principles, 'match_regexes' are now global objects (singletons), which are isolated and complete by construction. Same goes for 'ignore_vars' and 'variables_regexp' used for 'SET' statements matching. Some additional unnecessary headers cleanup is also performed in the commit.
1 parent 6924983 commit fc61684

32 files changed

+317
-217
lines changed

include/Base_Session_Utils.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef BASE_SESSION_UTILS_H
2+
#define BASE_SESSION_UTILS_H
3+
4+
/**
5+
* @class Session_Regex
6+
* @brief Encapsulates regex operations for session handling.
7+
*
8+
* This class is used for matching patterns in SQL queries, specifically for
9+
* settings like sql_log_bin, sql_mode, and time_zone.
10+
* See issues #509 , #815 and #816
11+
*/
12+
class Session_Regex {
13+
private:
14+
void* opt;
15+
void* re;
16+
char* s;
17+
public:
18+
Session_Regex(const char* p);
19+
~Session_Regex();
20+
bool match(const char* m);
21+
};
22+
23+
#endif // BASE_SESSION_UTILS_H

include/Base_Thread.h

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef CLASS_BASE_THREAD_H
22
#define CLASS_BASE_THREAD_H
33

4-
#include "proxysql.h"
4+
#ifdef DEBUG
5+
#include <atomic>
6+
#endif
7+
8+
#include "proxysql_structs.h"
59

610
typedef struct _thr_id_username_t {
711
uint32_t id;
@@ -14,25 +18,6 @@ typedef struct _kill_queue_t {
1418
std::vector<thr_id_usr *> query_ids;
1519
} kill_queue_t;
1620

17-
/**
18-
* @class Session_Regex
19-
* @brief Encapsulates regex operations for session handling.
20-
*
21-
* This class is used for matching patterns in SQL queries, specifically for
22-
* settings like sql_log_bin, sql_mode, and time_zone.
23-
* See issues #509 , #815 and #816
24-
*/
25-
class Session_Regex {
26-
private:
27-
void* opt;
28-
void* re;
29-
char* s;
30-
public:
31-
Session_Regex(const char* p);
32-
~Session_Regex();
33-
bool match(const char* m);
34-
};
35-
3621
class MySQL_Thread;
3722
class PgSQL_Thread;
3823

@@ -53,7 +38,6 @@ class Base_Thread {
5338
bool epoll_thread;
5439
int shutdown;
5540
PtrArray *mysql_sessions;
56-
Session_Regex **match_regexes;
5741
Base_Thread();
5842
~Base_Thread();
5943
template<typename T, typename S>

include/MySQL_Session.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,6 @@ class MySQL_Session: public Base_Session<MySQL_Session, MySQL_Data_Stream, MySQL
379379
MySQL_STMTs_meta *sess_STMTs_meta;
380380
StmtLongDataHandler *SLDH;
381381

382-
Session_Regex **match_regexes;
383-
384382
ProxySQL_Node_Address * proxysql_node_address; // this is used ONLY for Admin, and only if the other party is another proxysql instance part of a cluster
385383
bool use_ldap_auth;
386384

include/MySQL_Session_Utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MYSQL_SESSION_UTILS_H
2+
#define MYSQL_SESSION_UTILS_H
3+
4+
#include "Base_Session_Utils.h"
5+
6+
#include <array>
7+
8+
extern std::array<Session_Regex,4> mysql_match_regexes;
9+
10+
#endif // MYSQL_SESSION_UTILS_H

include/MySQL_Thread.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class __attribute__((aligned(64))) MySQL_Thread : public Base_Thread
111111
std::map<unsigned int, unsigned int> sessmap;
112112
#endif // IDLE_THREADS
113113

114-
//Session_Regex **match_regexes;
115-
116114
#ifdef IDLE_THREADS
117115
void worker_thread_assigns_sessions_to_idle_thread(MySQL_Thread *thr);
118116
void worker_thread_gets_sessions_from_idle_thread();

include/MySQL_Variables.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#ifndef MYSQL_VARIABLES_H
22
#define MYSQL_VARIABLES_H
33

4-
#include "proxysql.h"
5-
#include "cpp.h"
4+
#include "proxysql_structs.h"
65

76
#include <cstdint>
7+
#include <string>
88
#include <vector>
9-
#include <memory>
109

1110
class MySQL_Session;
1211

13-
extern void print_backtrace(void);
14-
1512
typedef bool (*verify_var)(MySQL_Session* session, int idx, uint32_t client_hash, uint32_t server_hash);
1613
typedef bool (*update_var)(MySQL_Session* session, int idx, int &_rc);
1714

@@ -26,10 +23,6 @@ class MySQL_Variables {
2623
static verify_var verifiers[SQL_NAME_LAST_HIGH_WM];
2724
static update_var updaters[SQL_NAME_LAST_HIGH_WM];
2825

29-
public:
30-
std::string variables_regexp;
31-
// ignore_vars is a list of all variables that proxysql will parse but ignore its value
32-
std::vector<std::string> ignore_vars;
3326
public:
3427
MySQL_Variables();
3528

include/MySQL_Variables_Utils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef MYSQL_VARIABLES_UTILS_H
2+
#define MYSQL_VARIABLES_UTILS_H
3+
4+
#include <string>
5+
#include <vector>
6+
7+
/**
8+
* @brief Gets all the variables for ProxySQL to recognize but ignore.
9+
*/
10+
const std::vector<std::string>& get_mysql_ignore_vars();
11+
/**
12+
* @brief Gets a regex with all the variables ProxySQL should recognize for SET statements.
13+
*/
14+
const std::string& get_mysql_variables_regexp();
15+
16+
#endif

include/PgSQL_Session.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ class PgSQL_Session : public Base_Session<PgSQL_Session, PgSQL_Data_Stream, PgSQ
502502
// MySQL_STMTs_meta* sess_STMTs_meta;
503503
// StmtLongDataHandler* SLDH;
504504

505-
Session_Regex** match_regexes;
506505
CopyCmdMatcher* copy_cmd_matcher;
507506

508507
ProxySQL_Node_Address* proxysql_node_address; // this is used ONLY for Admin, and only if the other party is another proxysql instance part of a cluster

include/PgSQL_Session_Utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef PGSQL_SESSION_UTILS_H
2+
#define PGSQL_SESSION_UTILS_H
3+
4+
#include "Base_Session_Utils.h"
5+
6+
#include <array>
7+
8+
extern std::array<Session_Regex,4> pgsql_match_regexes;
9+
10+
#endif // PGSQL_SESSION_UTILS_H

include/PgSQL_Thread.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ class __attribute__((aligned(64))) PgSQL_Thread : public Base_Thread
157157
std::map<unsigned int, unsigned int> sessmap;
158158
#endif // IDLE_THREADS
159159

160-
//Session_Regex** match_regexes;
161-
162160
#ifdef IDLE_THREADS
163161
void worker_thread_assigns_sessions_to_idle_thread(PgSQL_Thread * thr);
164162
void worker_thread_gets_sessions_from_idle_thread();

0 commit comments

Comments
 (0)