Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/MySQL_Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ class MySQL_Session: public Base_Session<MySQL_Session, MySQL_Data_Stream, MySQL
void generate_status_one_hostgroup(int hid, std::string& s);
void reset_warning_hostgroup_flag_and_release_connection();
void set_previous_status_mode3(bool allow_execute=true);
char* get_current_query(int max_length = -1);

friend void SQLite3_Server_session_handler(MySQL_Session*, void *_pa, PtrSize_t *pkt);

Expand Down
22 changes: 19 additions & 3 deletions include/MySQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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

#include "MySQL_Variables.h"
#ifdef IDLE_THREADS
#include <sys/epoll.h>
Expand Down Expand Up @@ -441,10 +443,8 @@ class MySQL_Threads_Handler
int connect_timeout_server;
int connect_timeout_server_max;
int free_connections_pct;
int show_processlist_extended;
#ifdef IDLE_THREADS
int session_idle_ms;
bool session_idle_show_processlist;
#endif // IDLE_THREADS
bool sessions_sort;
char *default_schema;
Expand Down Expand Up @@ -561,6 +561,22 @@ class MySQL_Threads_Handler
int data_packets_history_size;
int handle_warnings;
int evaluate_replication_lag_on_servers_load;
/**
* The processlist variables are logically group under "mysql-" variables
* and they are kept under MySQL_Threads_Handler.
*
* Other than configuration load/save or sync activities, these variables
* are not utilized by MTH or MySQL_Thread for any other purpose and hence
* they are not associated with thread-local variables.
*
* At runtime, ProxySQL_Admin keeps a copy of these variables and uses them
* when collecting stats for stats_mysql_processlist.
*/
#ifdef IDLE_THREADS
bool session_idle_show_processlist;
#endif
int show_processlist_extended;
int processlist_max_query_length;
} variables;
struct {
unsigned int mirror_sessions_current;
Expand Down Expand Up @@ -678,7 +694,7 @@ class MySQL_Threads_Handler
void start_listeners();
void stop_listeners();
void signal_all_threads(unsigned char _c=0);
SQLite3_result * SQL3_Processlist();
SQLite3_result * SQL3_Processlist(processlist_config_t args);
SQLite3_result * SQL3_GlobalStatus(bool _memory);
bool kill_session(uint32_t _thread_session_id);
unsigned long long get_total_mirror_queue();
Expand Down
1 change: 1 addition & 0 deletions include/PgSQL_Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ class PgSQL_Session : public Base_Session<PgSQL_Session, PgSQL_Data_Stream, PgSQ
void detected_broken_connection(const char* file, unsigned int line, const char* func, const char* action, PgSQL_Connection* myconn, bool verbose = false);
void generate_status_one_hostgroup(int hid, std::string& s);
void set_previous_status_mode3(bool allow_execute = true);
char* get_current_query(int max_length = -1);

#if defined(__clang__)
template<typename SESS, typename DS, typename BE, typename THD>
Expand Down
24 changes: 21 additions & 3 deletions include/PgSQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <prometheus/gauge.h>

#include "proxysql.h"
#include "proxysql_admin.h"

#include "Base_Thread.h"
#include "ProxySQL_Poll.h"
#include "PgSQL_Variables.h"
Expand Down Expand Up @@ -949,10 +951,8 @@ class PgSQL_Threads_Handler
int connect_timeout_server;
int connect_timeout_server_max;
int free_connections_pct;
int show_processlist_extended;
#ifdef IDLE_THREADS
int session_idle_ms;
bool session_idle_show_processlist;
#endif // IDLE_THREADS
bool sessions_sort;
char* default_schema;
Expand Down Expand Up @@ -1058,6 +1058,22 @@ class PgSQL_Threads_Handler
int handle_warnings;
char* server_version;
char* server_encoding;
/**
* The processlist variables are logically group under "pgsql-" variables
* and they are kept under PgSQL_Threads_Handler.
*
* Other than configuration load/save or sync activities, these variables
* are not utilized by PTH or PgSQL_Thread for any other purpose and hence
* they are not associated with thread-local variables.
*
* At runtime, ProxySQL_Admin keeps a copy of these variables and uses them
* when collecting stats for stats_pgsql_processlist.
*/
#ifdef IDLE_THREADS
bool session_idle_show_processlist;
#endif
int show_processlist_extended;
int processlist_max_query_length;
} variables;
struct {
unsigned int mirror_sessions_current;
Expand Down Expand Up @@ -1507,6 +1523,8 @@ class PgSQL_Threads_Handler
/**
* @brief Retrieves a process list for all threads in the thread pool.
*
* @param args Processlist configuration of PgSQL.
*
* @return A `SQLite3_result` object containing the process list, or `NULL` if an error
* occurred.
*
Expand All @@ -1519,7 +1537,7 @@ class PgSQL_Threads_Handler
* object, allowing administrators to monitor active sessions and their status.
*
*/
SQLite3_result* SQL3_Processlist();
SQLite3_result* SQL3_Processlist(processlist_config_t args);

/**
* @brief Retrieves global status information for the thread pool.
Expand Down
20 changes: 18 additions & 2 deletions include/proxysql_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include "proxysql_typedefs.h"

#define PROCESSLIST_MAX_QUERY_LEN_DEFAULT 2 * 1024 * 1024 // 2 MiB
#define PROCESSLIST_MAX_QUERY_LEN_MIN 1 * 1024 // 1 KiB
#define PROCESSLIST_MAX_QUERY_LEN_MAX 32 * 1024 * 1024 // 32 MiB

typedef struct { uint32_t hash; uint32_t key; } t_symstruct;
class ProxySQL_Config;
class ProxySQL_Restapi;
Expand Down Expand Up @@ -237,6 +241,13 @@ struct peer_pgsql_servers_v2_t {
peer_pgsql_servers_v2_t(SQLite3_result*, const pgsql_servers_v2_checksum_t&);
};

struct processlist_config_t {
#ifdef IDLE_THREADS
bool show_idle_session;
#endif
int show_extended;
int max_query_length;
};

class ProxySQL_Admin {
private:
Expand Down Expand Up @@ -317,8 +328,6 @@ class ProxySQL_Admin {
int stats_mysql_eventslog_sync_buffer_to_disk;
int stats_system_cpu;
int stats_system_memory;
int mysql_show_processlist_extended;
int pgsql_show_processlist_extended;
bool restapi_enabled;
bool restapi_enabled_old;
int restapi_port;
Expand All @@ -335,6 +344,13 @@ class ProxySQL_Admin {
int coredump_generation_interval_ms;
int coredump_generation_threshold;
char* ssl_keylog_file;
/**
* Processlist configurations are owned by MySQL/PgSQL Threads_Handlers.
* At runtime, ProxySQL_Admin keeps a copy of those variables and uses them
* for collecting stats.
*/
processlist_config_t mysql_processlist;
processlist_config_t pgsql_processlist;
} variables;

unsigned long long last_p_memory_metrics_ts;
Expand Down
8 changes: 0 additions & 8 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,6 @@ PgSQL_HostGroups_Manager* PgHGM;

// PostgreSQL thread variables
__thread int pgsql_thread___authentication_method;
__thread int pgsql_thread___show_processlist_extended;
__thread char *pgsql_thread___server_version;
__thread char *pgsql_thread___server_encoding;
__thread bool pgsql_thread___have_ssl;
Expand Down Expand Up @@ -1203,7 +1202,6 @@ __thread int pgsql_thread___query_cache_size_MB;
__thread int pgsql_thread___query_cache_soft_ttl_pct;
__thread int pgsql_thread___query_cache_handle_warnings;

__thread bool pgsql_thread___session_idle_show_processlist;
__thread char* pgsql_thread___default_variables[PGSQL_NAME_LAST_LOW_WM];
__thread int pgsql_thread___handle_unknown_charset;
__thread int pgsql_thread___max_stmts_cache;
Expand Down Expand Up @@ -1288,11 +1286,9 @@ __thread bool mysql_thread___query_digests_keep_comment;
__thread int mysql_thread___query_digests_max_digest_length;
__thread int mysql_thread___query_digests_max_query_length;
__thread bool mysql_thread___parse_failure_logs_digest;
__thread int mysql_thread___show_processlist_extended;
__thread int mysql_thread___session_idle_ms;
__thread int mysql_thread___hostgroup_manager_verbose;
__thread bool mysql_thread___default_reconnect;
__thread bool mysql_thread___session_idle_show_processlist;
__thread bool mysql_thread___sessions_sort;
__thread bool mysql_thread___kill_backend_connection_when_disconnect;
__thread bool mysql_thread___client_session_track_gtid;
Expand Down Expand Up @@ -1391,7 +1387,6 @@ extern PgSQL_HostGroups_Manager *PgHGM;

//PostgreSQL Thread Variables
extern __thread int pgsql_thread___authentication_method;
extern __thread int pgsql_thread___show_processlist_extended;
extern __thread char *pgsql_thread___server_version;
extern __thread char* pgsql_thread___server_encoding;
extern __thread bool pgsql_thread___have_ssl;
Expand Down Expand Up @@ -1509,7 +1504,6 @@ extern __thread int pgsql_thread___query_cache_size_MB;
extern __thread int pgsql_thread___query_cache_soft_ttl_pct;
extern __thread int pgsql_thread___query_cache_handle_warnings;

extern __thread bool pgsql_thread___session_idle_show_processlist;
extern __thread char* pgsql_thread___default_variables[PGSQL_NAME_LAST_LOW_WM];
extern __thread int pgsql_thread___handle_unknown_charset;
extern __thread int pgsql_thread___max_stmts_cache;
Expand Down Expand Up @@ -1594,11 +1588,9 @@ extern __thread bool mysql_thread___query_digests_keep_comment;
extern __thread int mysql_thread___query_digests_max_digest_length;
extern __thread int mysql_thread___query_digests_max_query_length;
extern __thread bool mysql_thread___parse_failure_logs_digest;
extern __thread int mysql_thread___show_processlist_extended;
extern __thread int mysql_thread___session_idle_ms;
extern __thread int mysql_thread___hostgroup_manager_verbose;
extern __thread bool mysql_thread___default_reconnect;
extern __thread bool mysql_thread___session_idle_show_processlist;
extern __thread bool mysql_thread___sessions_sort;
extern __thread bool mysql_thread___kill_backend_connection_when_disconnect;
extern __thread bool mysql_thread___client_session_track_gtid;
Expand Down
28 changes: 24 additions & 4 deletions lib/Admin_FlushVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,15 @@ void ProxySQL_Admin::flush_mysql_variables___database_to_runtime(SQLite3DB *db,
assert(previous_default_charset);
assert(previous_default_collation_connection);
flush_GENERIC_variables__process__database_to_runtime("mysql", db, resultset, false, replace, {}, {"session_debug"}, {"forward_autocommit"},
{"default_collation_connection", "default_charset", "show_processlist_extended"},
{
"default_collation_connection",
"default_charset",
"show_processlist_extended",
#ifdef IDLE_THREADS
"session_idle_show_processlist",
#endif // IDLE_THREADS
"processlist_max_query_length"
},
[](const std::string& varname, const char *varvalue, SQLite3DB* db) {
if (varname == "default_collation_connection" || varname == "default_charset") {
char *val=GloMTH->get_variable((char *)varname.c_str());
Expand All @@ -448,10 +456,16 @@ void ProxySQL_Admin::flush_mysql_variables___database_to_runtime(SQLite3DB *db,
free(val);
}
} else if (varname == "show_processlist_extended") {
GloAdmin->variables.mysql_show_processlist_extended = atoi(varvalue);
GloAdmin->variables.mysql_processlist.show_extended = atoi(varvalue);
#ifdef IDLE_THREADS
} else if (varname == "session_idle_show_processlist") {
GloAdmin->variables.mysql_processlist.show_idle_session = atoi(varvalue);
#endif // IDLE_THREADS
} else if (varname == "processlist_max_query_length") {
GloAdmin->variables.mysql_processlist.max_query_length = atoi(varvalue);
}
}
);
);
char q[1000];
char * default_charset = GloMTH->get_variable_string((char *)"default_charset");
char * default_collation_connection = GloMTH->get_variable_string((char *)"default_collation_connection");
Expand Down Expand Up @@ -795,7 +809,13 @@ void ProxySQL_Admin::flush_pgsql_variables___database_to_runtime(SQLite3DB* db,
}
proxy_debug(PROXY_DEBUG_ADMIN, 4, "Set variable %s with value \"%s\"\n", r->fields[0], value);
if (strcmp(r->fields[0], (char*)"show_processlist_extended") == 0) {
variables.pgsql_show_processlist_extended = atoi(value);
variables.pgsql_processlist.show_extended = atoi(value);
#ifdef IDLE_THREADS
} else if (strcmp(r->fields[0], (char*)"session_idle_show_processlist") == 0) {
variables.pgsql_processlist.show_idle_session = atoi(value);
#endif // IDLE_THREADS
} else if (strcmp(r->fields[0], (char*)"processlist_max_query_length") == 0) {
variables.pgsql_processlist.max_query_length = atoi(value);
}
}
// }
Expand Down
6 changes: 3 additions & 3 deletions lib/Admin_Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3916,7 +3916,7 @@ void admin_session_handler(S* sess, void *_pa, PtrSize_t *pkt) {

if (query_no_space_length==strlen("SHOW PROCESSLIST") && !strncasecmp("SHOW PROCESSLIST",query_no_space, query_no_space_length)) {
l_free(query_length,query);
query=l_strdup("SELECT SessionID, user, db, hostgroup, command, time_ms, SUBSTR(info,0,100) info FROM stats_mysql_processlist");
query=l_strdup("SELECT SessionID, user, db, hostgroup, command, time_ms, info FROM stats_mysql_processlist");
query_length=strlen(query)+1;
goto __run_query;
}
Expand All @@ -3937,14 +3937,14 @@ void admin_session_handler(S* sess, void *_pa, PtrSize_t *pkt) {

if (query_no_space_length == strlen("SHOW PGSQL PROCESSLIST") && !strncasecmp("SHOW PGSQL PROCESSLIST", query_no_space, query_no_space_length)) {
l_free(query_length, query);
query = l_strdup("SELECT SessionID, user, database, hostgroup, backend_pid, backend_state, command, time_ms, SUBSTR(info,0,100) info FROM stats_pgsql_processlist");
query = l_strdup("SELECT SessionID, user, database, hostgroup, backend_pid, backend_state, command, time_ms, info FROM stats_pgsql_processlist");
query_length = strlen(query) + 1;
goto __run_query;
}

if (query_no_space_length == strlen("SHOW PGSQL ACTIVITY") && !strncasecmp("SHOW PGSQL ACTIVITY", query_no_space, query_no_space_length)) {
l_free(query_length, query);
query = l_strdup("SELECT datname, pid, usename, hostgroup, backend_pid, state, command, duration_ms, SUBSTR(query,0,100) query FROM stats_pgsql_stat_activity");
query = l_strdup("SELECT datname, pid, usename, hostgroup, backend_pid, state, command, duration_ms, query FROM stats_pgsql_stat_activity");
query_length = strlen(query) + 1;
goto __run_query;
}
Expand Down
39 changes: 39 additions & 0 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8116,3 +8116,42 @@ void MySQL_Session::reset_warning_hostgroup_flag_and_release_connection() {
warning_in_hg = -1;
}
}

char* MySQL_Session::get_current_query(int max_length) {
const char *query_ptr = NULL;
int query_len = 0;

if (!(mybe && mybe->server_myds && mybe->server_myds->myconn)) {
return NULL;
}

if (CurrentQuery.stmt_info == NULL) { // text protocol
query_ptr = mybe->server_myds->myconn->query.ptr;
query_len = mybe->server_myds->myconn->query.length;
} else { // prepared statement
query_ptr = CurrentQuery.stmt_info->query;
query_len = CurrentQuery.stmt_info->query_length;
}

bool trunc_query = false;
if (max_length > 0 && query_len > max_length) {
query_len = max_length;
trunc_query = true;
}

char *res = NULL;

if (query_len > 0) {
res = (char *) malloc(query_len + 1);
if (trunc_query) {
// for truncated queries, add three dots at the end
strncpy(res, query_ptr, query_len - 3);
strncpy(res + (query_len - 3), "...", 3);
} else {
strncpy(res, query_ptr, query_len);
}
res[query_len] = '\0';
}

return res;
}
Loading