Skip to content

Commit

Permalink
Merge pull request #4680 from sysown/v3.0-postgres_monitor_poc
Browse files Browse the repository at this point in the history
Initial POC for PostgreSQL monitoring support
  • Loading branch information
JavierJF authored Sep 27, 2024
2 parents 16b8396 + 1ebe70b commit 42bb0cf
Show file tree
Hide file tree
Showing 15 changed files with 1,963 additions and 179 deletions.
2 changes: 1 addition & 1 deletion include/PgSQL_HostGroups_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager<PgSQL_HGC> {
/**
* @brief Mutex used to guard 'pgsql_servers_to_monitor' resulset.
*/
std::mutex pgsql_servers_to_monitor_mutex;
std::mutex pgsql_servers_to_monitor_mutex {};
/**
* @brief Resulset containing the latest 'pgsql_servers' present in 'mydb'.
* @details This resulset should be updated via 'update_table_pgsql_servers_for_monitor' each time actions
Expand Down
71 changes: 71 additions & 0 deletions include/PgSQL_Monitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef __PGSQL_MONITOR_H
#define __PGSQL_MONITOR_H

#include "libpq-fe.h"

#include "sqlite3db.h"
#include "proxysql_structs.h"

#include <cassert>
#include <mutex>
#include <vector>

#define MONITOR_SQLITE_TABLE_PGSQL_SERVER_CONNECT_LOG "CREATE TABLE pgsql_server_connect_log (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , time_start_us INT NOT NULL DEFAULT 0 , connect_success_time_us INT DEFAULT 0 , connect_error VARCHAR , PRIMARY KEY (hostname, port, time_start_us))"

#define MONITOR_SQLITE_TABLE_PGSQL_SERVER_PING_LOG "CREATE TABLE pgsql_server_ping_log ( hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , time_start_us INT NOT NULL DEFAULT 0 , ping_success_time_us INT DEFAULT 0 , ping_error VARCHAR , PRIMARY KEY (hostname, port, time_start_us))"

#define MONITOR_SQLITE_TABLE_PGSQL_SERVERS "CREATE TABLE pgsql_servers (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , status INT CHECK (status IN (0, 1, 2, 3, 4)) NOT NULL DEFAULT 0 , use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0 , PRIMARY KEY (hostname, port) )"

#define MONITOR_SQLITE_TABLE_PROXYSQL_SERVERS "CREATE TABLE proxysql_servers (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 6032 , weight INT CHECK (weight >= 0) NOT NULL DEFAULT 0 , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (hostname, port) )"

struct PgSQL_Monitor {
// @brief Flags if monitoring threads should be shutdown.
bool shutdown = false;
// @brief Mutex to hold to update `monitor_internal.pgsql_servers`
std::mutex pgsql_srvs_mutex {};
// @brief Mutex to hold to update/read `pgsql_servers` to monitor
std::mutex pgsql_srvs_to_monitor_mutex {};
// @brief Used to access monitor database
SQLite3DB monitordb {};
// @brief Used to access internal monitor database
SQLite3DB monitor_internal_db {};
// Internal counters for metrics
///////////////////////////////////////////////////////////////////////////
uint64_t connect_check_ERR { 0 };
uint64_t connect_check_OK { 0 };
uint64_t ping_check_ERR { 0 };
uint64_t ping_check_OK { 0 };
///////////////////////////////////////////////////////////////////////////

std::vector<table_def_t> tables_defs_monitor {
{
const_cast<char*>("pgsql_server_connect_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_CONNECT_LOG)
},
{
const_cast<char*>("pgsql_server_ping_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_PING_LOG)
}
};

std::vector<table_def_t> tables_defs_monitor_internal {
{
const_cast<char*>("pgsql_servers"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVERS)
}
};

PgSQL_Monitor();
};

struct pgsql_conn_t {
PGconn* conn { nullptr };
int fd { 0 };
uint64_t last_used { 0 };
ASYNC_ST state { ASYNC_ST::ASYNC_CONNECT_FAILED };
mf_unique_ptr<char> err {};
};

void* PgSQL_monitor_scheduler_thread();

#endif
6 changes: 5 additions & 1 deletion include/PgSQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "proxysql.h"
#include "Base_Thread.h"
#include "cpp.h"
#include "ProxySQL_Poll.h"
#include "PgSQL_Variables.h"
#ifdef IDLE_THREADS
Expand Down Expand Up @@ -825,6 +824,7 @@ class PgSQL_Threads_Handler
//! Read only check timeout. Unit: 'ms'.
int monitor_replication_lag_timeout;
int monitor_replication_lag_count;
/* TODO: Remove
int monitor_groupreplication_healthcheck_interval;
int monitor_groupreplication_healthcheck_timeout;
int monitor_groupreplication_healthcheck_max_timeout_count;
Expand All @@ -836,9 +836,13 @@ class PgSQL_Threads_Handler
int monitor_query_interval;
int monitor_query_timeout;
int monitor_slave_lag_when_null;
*/
int monitor_threads;
/* TODO: Remove
int monitor_threads_min;
int monitor_threads_max;
int monitor_threads_queue_maxsize;
*/
int monitor_local_dns_cache_ttl;
int monitor_local_dns_cache_refresh_interval;
int monitor_local_dns_resolver_queue_maxsize;
Expand Down
1 change: 0 additions & 1 deletion include/PgSQL_Variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define PGSQL_VARIABLES_H

#include "proxysql.h"
#include "cpp.h"

#include <cstdint>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions include/cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "sqlite3db.h"
//#include "StatCounters.h"
#include "MySQL_Monitor.hpp"
#include "PgSQL_Monitor.hpp"
//#include "MySQL_Protocol.h"
//#include "MySQL_Authentication.hpp"
//#include "MySQL_LDAP_Authentication.hpp"
Expand Down
69 changes: 18 additions & 51 deletions include/proxysql_debug.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@

/*
#ifdef DEBUG
#ifndef DEBUG_EXTERN
#define DEBUG_EXTERN
extern debug_level *gdbg_lvl;
extern int gdbg;
#endif
#endif
*/

#ifndef __PROXYSQL_DEBUG_H
#define __PROXYSQL_DEBUG_H

Expand Down Expand Up @@ -46,7 +35,6 @@ class Timer {

#ifdef DEBUG
#define PROXY_TRACE() { proxy_debug(PROXY_DEBUG_GENERIC,10,"TRACE\n"); }
//#define PROXY_TRACE2() { proxy_info("TRACE\n"); }
#define PROXY_TRACE2()
#else
#define PROXY_TRACE()
Expand All @@ -64,7 +52,6 @@ class Timer {
} \
} while (0)
#elif defined(__linux__)
//#ifdef SYS_gettid
#define proxy_debug(module, verbosity, fmt, ...) \
do { if (GloVars.global.gdbg) { \
proxy_debug_func(module, verbosity, syscall(SYS_gettid), __FILE__, __LINE__, __func__ , fmt, ## __VA_ARGS__); \
Expand All @@ -76,9 +63,6 @@ class Timer {
#define proxy_debug(module, verbosity, fmt, ...)
#endif /* DEBUG */

/*
#ifdef DEBUG
*/
#define proxy_error(fmt, ...) \
do { \
time_t __timer; \
Expand Down Expand Up @@ -111,23 +95,7 @@ class Timer {
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", &__tm_info); \
proxy_error_func(0, "%s %s:%d:%s(): [ERROR] " fmt, __buffer, fi, li, fu , ## __VA_ARGS__); \
} while(0)
/*
#else
#define proxy_error(fmt, ...) \
do { \
time_t __timer; \
char __buffer[25]; \
struct tm *__tm_info; \
time(&__timer); \
__tm_info = localtime(&__timer); \
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", __tm_info); \
proxy_error_func("%s [ERROR] " fmt , __buffer , ## __VA_ARGS__); \
} while(0)
#endif
*/
/*
#ifdef DEBUG
*/

#define proxy_warning(fmt, ...) \
do { \
time_t __timer; \
Expand All @@ -150,20 +118,6 @@ class Timer {
proxy_error_func(ecode, "%s %s:%d:%s(): [WARNING] " fmt, __buffer, __FILE__, __LINE__, __func__ , ## __VA_ARGS__); \
} while(0)

/*
#else
#define proxy_warning(fmt, ...) \
do { \
time_t __timer; \
char __buffer[25]; \
struct tm *__tm_info; \
time(&__timer); \
__tm_info = localtime(&__timer); \
strftime(__buffer, 25, "%Y-%m-%d %H:%M:%S", __tm_info); \
proxy_error_func("%s [WARNING] " fmt , __buffer , ## __VA_ARGS__); \
} while(0)
#endif
*/
#ifdef DEBUG
#define proxy_info(fmt, ...) \
do { \
Expand Down Expand Up @@ -211,13 +165,26 @@ class Timer {
#endif

#ifdef DEBUG
//void *debug_logger();
#endif

#define NULL_DB_MSG "The pointer to sqlite3 database is NULL. Cannot get error message."

#define ASSERT_SQLITE_OK(rc, db) \
do { \
if (rc!=SQLITE_OK) { \
proxy_error("SQLite3 error with return code %d. Error message: %s. Shutting down.\n", rc, db?(*proxy_sqlite3_errmsg)(db->get_db()):"The pointer to sqlite3 database is null. Cannot get error message."); \
proxy_error( \
"SQLite3 error. Shutting down rc=%d msg='%s'\n", \
rc, db ? (*proxy_sqlite3_errmsg)(db->get_db()) : NULL_DB_MSG); \
assert(0); \
} \
} while(0)

#define ASSERT_SQLITE3_OK(rc, db) \
do { \
if (rc!=SQLITE_OK) { \
proxy_error( \
"SQLite3 error. Shutting down rc=%d msg='%s'\n", \
rc, db ? (*proxy_sqlite3_errmsg)(db) : NULL_DB_MSG); \
assert(0); \
} \
} while(0)
Expand All @@ -243,12 +210,12 @@ SQLite3_result* proxysql_get_message_stats(bool reset=false);
*/
void proxysql_init_debug_prometheus_metrics();


class SQLite3DB;
/**
* @brief Set or unset if Admin has debugdb_disk fully initialized
*/
void proxysql_set_admin_debugdb_disk(SQLite3DB *_db);

void proxysql_set_admin_debug_output(unsigned int _do);

#endif
#endif // DEBUG
31 changes: 31 additions & 0 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ enum PROXYSQL_MYSQL_ERR {
ER_PROXYSQL_AWS_HEALTH_CHECK_CONN_TIMEOUT = 9017,
ER_PROXYSQL_AWS_HEALTH_CHECK_TIMEOUT = 9018,
ER_PROXYSQL_SRV_NULL_REPLICATION_LAG = 9019,
ER_PROXYSQL_CONNECT_TIMEOUT = 9020,
};

enum proxysql_session_type {
Expand Down Expand Up @@ -1079,6 +1080,21 @@ __thread char* pgsql_thread___firewall_whitelist_errormsg;
__thread bool pgsql_thread___firewall_whitelist_enabled;
__thread int pgsql_thread___query_processor_iterations;
__thread int pgsql_thread___query_processor_regex;

__thread bool pgsql_thread___monitor_enabled;
__thread int pgsql_thread___monitor_history;
__thread int pgsql_thread___monitor_connect_interval;
__thread int pgsql_thread___monitor_connect_timeout;
__thread int pgsql_thread___monitor_ping_interval;
__thread int pgsql_thread___monitor_ping_max_failures;
__thread int pgsql_thread___monitor_ping_timeout;
__thread int pgsql_thread___monitor_read_only_interval;
__thread int pgsql_thread___monitor_read_only_timeout;
__thread int pgsql_thread___monitor_read_only_max_timeout_count;
__thread int pgsql_thread___monitor_threads;
__thread char* pgsql_thread___monitor_username;
__thread char* pgsql_thread___monitor_password;

//---------------------------

__thread char *mysql_thread___default_schema;
Expand Down Expand Up @@ -1351,6 +1367,21 @@ extern __thread char* pgsql_thread___firewall_whitelist_errormsg;
extern __thread bool pgsql_thread___firewall_whitelist_enabled;
extern __thread int pgsql_thread___query_processor_iterations;
extern __thread int pgsql_thread___query_processor_regex;

extern __thread bool pgsql_thread___monitor_enabled;
extern __thread int pgsql_thread___monitor_history;
extern __thread int pgsql_thread___monitor_connect_interval;
extern __thread int pgsql_thread___monitor_connect_timeout;
extern __thread int pgsql_thread___monitor_ping_interval;
extern __thread int pgsql_thread___monitor_ping_max_failures;
extern __thread int pgsql_thread___monitor_ping_timeout;
extern __thread int pgsql_thread___monitor_read_only_interval;
extern __thread int pgsql_thread___monitor_read_only_timeout;
extern __thread int pgsql_thread___monitor_read_only_max_timeout_count;
extern __thread int pgsql_thread___monitor_threads;
extern __thread char* pgsql_thread___monitor_username;
extern __thread char* pgsql_thread___monitor_password;

//---------------------------

extern __thread char *mysql_thread___default_schema;
Expand Down
1 change: 1 addition & 0 deletions lib/Base_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ template void Base_HostGroups_Manager<PgSQL_HGC>::wrlock();
template void Base_HostGroups_Manager<PgSQL_HGC>::wrunlock();

template SQLite3_result * Base_HostGroups_Manager<MyHGC>::execute_query(char*, char**);
template SQLite3_result * Base_HostGroups_Manager<PgSQL_HGC>::execute_query(char*, char**);

#if 0
#define SAFE_SQLITE3_STEP(_stmt) do {\
Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ _OBJ_CXX := ProxySQL_GloVars.oo network.oo debug.oo configfile.oo Query_Cache.oo
Base_Session.oo Base_Thread.oo \
proxy_protocol_info.oo \
proxysql_find_charset.oo ProxySQL_Poll.oo \
PgSQL_Protocol.oo PgSQL_Thread.oo PgSQL_Data_Stream.oo PgSQL_Session.oo PgSQL_Variables.oo PgSQL_HostGroups_Manager.oo PgSQL_Connection.oo PgSQL_Backend.oo PgSQL_Logger.oo PgSQL_Authentication.oo PgSQL_Error_Helper.oo
PgSQL_Protocol.oo PgSQL_Thread.oo PgSQL_Data_Stream.oo PgSQL_Session.oo PgSQL_Variables.oo PgSQL_HostGroups_Manager.oo PgSQL_Connection.oo PgSQL_Backend.oo PgSQL_Logger.oo PgSQL_Authentication.oo PgSQL_Error_Helper.oo PgSQL_Monitor.oo

OBJ_CXX := $(patsubst %,$(ODIR)/%,$(_OBJ_CXX))
HEADERS := ../include/*.h ../include/*.hpp
Expand Down
Loading

0 comments on commit 42bb0cf

Please sign in to comment.