Skip to content

Commit

Permalink
Merge pull request #4726 from sysown/v3.0-postgres_read_only
Browse files Browse the repository at this point in the history
Second iteration on PostgreSQL monitoring POC - Read-Only Support
  • Loading branch information
renecannao authored Nov 11, 2024
2 parents 7c26ef2 + 12a5c31 commit 85d8445
Show file tree
Hide file tree
Showing 14 changed files with 1,206 additions and 647 deletions.
2 changes: 1 addition & 1 deletion include/PgSQL_HostGroups_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager<PgSQL_HGC> {
void replication_lag_action_inner(PgSQL_HGC *, const char*, unsigned int, int);
void replication_lag_action(const std::list<replication_lag_server_t>& pgsql_servers);
void read_only_action(char *hostname, int port, int read_only);
void read_only_action_v2(const std::list<read_only_server_t>& pgsql_servers);
void read_only_action_v2(const std::list<read_only_server_t>& pgsql_servers, bool writer_is_also_reader);
unsigned int get_servers_table_version();
void wait_servers_table_version(unsigned, unsigned);
bool shun_and_killall(char *hostname, int port);
Expand Down
10 changes: 9 additions & 1 deletion include/PgSQL_Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#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_SERVER_READ_ONLY_LOG "CREATE TABLE pgsql_server_read_only_log ( hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , time_start_us INT NOT NULL DEFAULT 0 , success_time_us INT DEFAULT 0 , read_only INT DEFAULT 1 , 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) )"
Expand All @@ -35,6 +37,8 @@ struct PgSQL_Monitor {
uint64_t connect_check_OK { 0 };
uint64_t ping_check_ERR { 0 };
uint64_t ping_check_OK { 0 };
uint64_t readonly_check_ERR { 0 };
uint64_t readonly_check_OK { 0 };
///////////////////////////////////////////////////////////////////////////

std::vector<table_def_t> tables_defs_monitor {
Expand All @@ -45,7 +49,11 @@ struct PgSQL_Monitor {
{
const_cast<char*>("pgsql_server_ping_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_PING_LOG)
}
},
{
const_cast<char*>("pgsql_server_read_only_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_READ_ONLY_LOG)
},
};

std::vector<table_def_t> tables_defs_monitor_internal {
Expand Down
4 changes: 4 additions & 0 deletions include/PgSQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,19 @@ class PgSQL_Threads_Handler

int monitor_history;
int monitor_connect_interval;
int monitor_connect_interval_window;
int monitor_connect_timeout;
//! Monitor ping interval. Unit: 'ms'.
int monitor_ping_interval;
int monitor_ping_interval_window;
int monitor_ping_max_failures;
//! Monitor ping timeout. Unit: 'ms'.
int monitor_ping_timeout;
//! Monitor aws rds topology discovery interval. Unit: 'one discovery check per X monitor_read_only checks'.
int monitor_aws_rds_topology_discovery_interval;
//! Monitor read only timeout. Unit: 'ms'.
int monitor_read_only_interval;
int monitor_read_only_interval_window;
//! Monitor read only timeout. Unit: 'ms'.
int monitor_read_only_timeout;
int monitor_read_only_max_timeout_count;
Expand Down Expand Up @@ -848,6 +851,7 @@ class PgSQL_Threads_Handler
int monitor_local_dns_resolver_queue_maxsize;
char* monitor_username;
char* monitor_password;
char* monitor_dbname;
char* monitor_replication_lag_use_percona_heartbeat;
int ping_interval_server_msec;
int ping_timeout_server;
Expand Down
3 changes: 2 additions & 1 deletion include/proxysql_glovars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class ProxySQL_GlobalVariables {
unsigned long long start_time;
bool gdbg;
bool nostart;
bool monitor;
bool my_monitor;
bool pg_monitor;
bool version_check;
#ifdef SO_REUSEPORT
bool reuseport;
Expand Down
11 changes: 11 additions & 0 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ enum PROXYSQL_MYSQL_ERR {
ER_PROXYSQL_AWS_HEALTH_CHECK_TIMEOUT = 9018,
ER_PROXYSQL_SRV_NULL_REPLICATION_LAG = 9019,
ER_PROXYSQL_CONNECT_TIMEOUT = 9020,
ER_PROXYSQL_READONLY_TIMEOUT = 9021,
};

enum proxysql_session_type {
Expand Down Expand Up @@ -1084,16 +1085,21 @@ __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_interval_window;
__thread int pgsql_thread___monitor_connect_timeout;
__thread int pgsql_thread___monitor_ping_interval;
__thread int pgsql_thread___monitor_ping_interval_window;
__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_interval_window;
__thread int pgsql_thread___monitor_read_only_timeout;
__thread int pgsql_thread___monitor_read_only_max_timeout_count;
__thread bool pgsql_thread___monitor_writer_is_also_reader;
__thread int pgsql_thread___monitor_threads;
__thread char* pgsql_thread___monitor_username;
__thread char* pgsql_thread___monitor_password;
__thread char* pgsql_thread___monitor_dbname;

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

Expand Down Expand Up @@ -1371,16 +1377,21 @@ 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_interval_window;
extern __thread int pgsql_thread___monitor_connect_timeout;
extern __thread int pgsql_thread___monitor_ping_interval;
extern __thread int pgsql_thread___monitor_ping_interval_window;
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_interval_window;
extern __thread int pgsql_thread___monitor_read_only_timeout;
extern __thread int pgsql_thread___monitor_read_only_max_timeout_count;
extern __thread bool pgsql_thread___monitor_writer_is_also_reader;
extern __thread int pgsql_thread___monitor_threads;
extern __thread char* pgsql_thread___monitor_username;
extern __thread char* pgsql_thread___monitor_password;
extern __thread char* pgsql_thread___monitor_dbname;

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

Expand Down
2 changes: 1 addition & 1 deletion lib/PgSQL_Data_Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,4 +1414,4 @@ int PgSQL_Data_Stream::buffer2array() {
queueIN.pkt.ptr = NULL;
}
return ret;
}
}
8 changes: 5 additions & 3 deletions lib/PgSQL_HostGroups_Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3600,7 +3600,9 @@ void PgSQL_HostGroups_Manager::read_only_action(char *hostname, int port, int re
* @param pgsql_servers List of servers having hostname, port and read only value.
*
*/
void PgSQL_HostGroups_Manager::read_only_action_v2(const std::list<read_only_server_t>& pgsql_servers) {
void PgSQL_HostGroups_Manager::read_only_action_v2(
const std::list<read_only_server_t>& pgsql_servers, bool writer_is_also_reader
) {

bool update_pgsql_servers_table = false;

Expand Down Expand Up @@ -3637,7 +3639,7 @@ void PgSQL_HostGroups_Manager::read_only_action_v2(const std::list<read_only_ser
proxy_debug(PROXY_DEBUG_MONITOR, 5, "Server '%s:%d' found with 'read_only=0', but not found as writer\n", hostname.c_str(), port);
host_server_mapping->copy_if_not_exists(HostGroup_Server_Mapping::Type::WRITER, HostGroup_Server_Mapping::Type::READER);

if (mysql_thread___monitor_writer_is_also_reader == false) {
if (writer_is_also_reader == false) {
// remove node from reader
host_server_mapping->clear(HostGroup_Server_Mapping::Type::READER);
}
Expand Down Expand Up @@ -3683,7 +3685,7 @@ void PgSQL_HostGroups_Manager::read_only_action_v2(const std::list<read_only_ser
// copy all reader nodes to writer
host_server_mapping->copy_if_not_exists(HostGroup_Server_Mapping::Type::WRITER, HostGroup_Server_Mapping::Type::READER);

if (mysql_thread___monitor_writer_is_also_reader == false) {
if (writer_is_also_reader == false) {
// remove node from reader
host_server_mapping->clear(HostGroup_Server_Mapping::Type::READER);
}
Expand Down
Loading

0 comments on commit 85d8445

Please sign in to comment.