From 28aba935261e575f73d398f2f8bd6f9bb29a4de8 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 8 Jul 2024 12:25:31 +0000 Subject: [PATCH 1/5] Fix bug #44 --- lib/ProxySQL_Admin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 61de0b506..d1088fc12 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -2512,9 +2512,9 @@ ProxySQL_Admin::ProxySQL_Admin() : variables.web_port_old = variables.web_port; variables.web_verbosity = 0; variables.p_memory_metrics_interval = 61; + all_modules_started = false; #ifdef DEBUG variables.debug=GloVars.global.gdbg; - all_modules_started = false; debug_output = 1; proxysql_set_admin_debug_output(debug_output); #endif /* DEBUG */ From ef3c40ddb186260842160194beaeeb1f168ee417 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 8 Jul 2024 12:26:50 +0000 Subject: [PATCH 2/5] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 413bab59e..2253f1a90 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ binaries/*deb binaries/*rpm tools/eventslog_reader_sample src/proxysql-save.cfg +src/*log* proxysql-2.0.0/ docker/images/proxysql/rhel-compliant/rpmmacros From 247342daa63f7f5cc596552bfccf089004511594 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 8 Jul 2024 13:12:44 +0000 Subject: [PATCH 3/5] Tweak of how quickly is started Without this patch, it seems very difficult to start ProxySQL with valgrind --- src/main.cpp | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 377227d06..f38ebee6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,7 @@ using json = nlohmann::json; #include #include +#include /* extern "C" MySQL_LDAP_Authentication * create_MySQL_LDAP_Authentication_func() { @@ -61,6 +62,14 @@ using std::string; using std::vector; +void sleep_iter(unsigned int iter) { + usleep(50*iter); +#ifdef RUNNING_ON_VALGRIND + usleep((1000+rand()%1000)*iter); +#endif // RUNNING_ON_VALGRIND +} + + volatile create_MySQL_LDAP_Authentication_t * create_MySQL_LDAP_Authentication = NULL; void * __mysql_ldap_auth; @@ -408,7 +417,7 @@ using namespace std; //__cmd_proxysql_config_file=NULL; #define MAX_EVENTS 100 -static volatile int load_; +std::atomic load_; //__thread l_sfp *__thr_sfp=NULL; //#ifdef DEBUG @@ -474,8 +483,9 @@ void * mysql_worker_thread_func(void *arg) { worker->init(); // worker->poll_listener_add(listen_fd); // worker->poll_listener_add(socket_fd); - __sync_fetch_and_sub(&load_,1); - do { usleep(50); } while (load_); + load_ -= 1; + unsigned int iter = 0; + do { sleep_iter(++iter); } while (load_); worker->run(); //delete worker; @@ -505,8 +515,9 @@ void * mysql_worker_thread_func_idles(void *arg) { worker->init(); // worker->poll_listener_add(listen_fd); // worker->poll_listener_add(socket_fd); - __sync_fetch_and_sub(&load_,1); - do { usleep(50); } while (load_); + load_ -= 1; + unsigned int iter = 0; + do { sleep_iter(++iter); } while (load_); worker->run(); //delete worker; @@ -537,8 +548,9 @@ void* pgsql_worker_thread_func(void* arg) { worker->init(); // worker->poll_listener_add(listen_fd); // worker->poll_listener_add(socket_fd); - __sync_fetch_and_sub(&load_, 1); - do { usleep(50); } while (load_); + load_ -= 1; + unsigned int iter = 0; + do { sleep_iter(++iter); } while (load_); worker->run(); //delete worker; @@ -568,8 +580,9 @@ void* pgsql_worker_thread_func_idles(void* arg) { worker->init(); // worker->poll_listener_add(listen_fd); // worker->poll_listener_add(socket_fd); - __sync_fetch_and_sub(&load_, 1); - do { usleep(50); } while (load_); + load_ -= 1; + unsigned int iter = 0; + do { sleep_iter(++iter); } while (load_); worker->run(); //delete worker; @@ -1331,11 +1344,8 @@ void ProxySQL_Main_init_phase3___start_all() { #endif } - do { /* nothing */ -#ifdef DEBUG - usleep(5+rand()%10); -#endif - } while (load_ != 1); + unsigned int iter = 0; + do { sleep_iter(++iter); } while (load_ != 1); load_ = 0; __sync_fetch_and_add(&GloMTH->status_variables.threads_initialized, 1); From 836755c1053a0c5758df5ce70e91d5d784d15844 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Mon, 8 Jul 2024 13:38:14 +0000 Subject: [PATCH 4/5] Fixes for various not initialized variables --- lib/Base_Session.cpp | 5 +++++ lib/PgSQL_Connection.cpp | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Base_Session.cpp b/lib/Base_Session.cpp index 7bd3ebe98..7ed8081ff 100644 --- a/lib/Base_Session.cpp +++ b/lib/Base_Session.cpp @@ -65,6 +65,11 @@ void Base_Session::init() { if constexpr (std::is_same_v) { sess_STMTs_meta = new MySQL_STMTs_meta(); SLDH = new StmtLongDataHandler(); + } else if constexpr (std::is_same_v) { + sess_STMTs_meta = NULL; + SLDH = NULL; + } else { + assert(0); } }; diff --git a/lib/PgSQL_Connection.cpp b/lib/PgSQL_Connection.cpp index 05b45bc6e..1e80fcf18 100644 --- a/lib/PgSQL_Connection.cpp +++ b/lib/PgSQL_Connection.cpp @@ -415,6 +415,7 @@ PgSQL_Connection_Placeholder::PgSQL_Connection_Placeholder() { } options.client_flag = 0; + options.server_capabilities = 0; options.compression_min_length=0; options.server_version=NULL; options.last_set_autocommit=-1; // -1 = never set @@ -4565,4 +4566,4 @@ bool PgSQL_Connection::is_connection_in_reusable_state() const { const bool conn_usable = !(txn_status == PQTRANS_UNKNOWN || txn_status == PQTRANS_ACTIVE); assert(!(conn_usable == false && is_error_present() == false)); return conn_usable; -} \ No newline at end of file +} From 0481ab59369c238cfd703dbbee50cbb0eb1b1c7c Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Tue, 9 Jul 2024 08:16:01 +0000 Subject: [PATCH 5/5] Adding some timers for debugging purposes Adding Timers in Base_Thread to count the number of time and elapsed time of when Session::handler() and Connection::handler() are called. This feature is disabled by default. Timers are enabled only if in proxysql_macros.h the following is defined: #define ENABLE_TIMER true The default: #define ENABLE_TIMER false --- include/Base_Session.h | 3 +++ include/Base_Thread.h | 9 +++++++++ include/proxysql_debug.h | 32 ++++++++++++++++++++++++++++++++ include/proxysql_macros.h | 8 +++++++- lib/PgSQL_Connection.cpp | 3 +++ lib/PgSQL_Session.cpp | 3 +++ 6 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/Base_Session.h b/include/Base_Session.h index ccf8aba5f..5dc818ed9 100644 --- a/include/Base_Session.h +++ b/include/Base_Session.h @@ -120,6 +120,9 @@ class Base_Session { */ void housekeeping_before_pkts(); virtual void create_new_session_and_reset_connection(DS *_myds) = 0; + + + }; #endif // CLASS_BASE_SESSION_H diff --git a/include/Base_Thread.h b/include/Base_Thread.h index 3ae475a76..5992caecb 100644 --- a/include/Base_Thread.h +++ b/include/Base_Thread.h @@ -77,6 +77,15 @@ class Base_Thread { template void ProcessAllMyDS_BeforePoll(); template void run_SetAllSession_ToProcess0(); + +#if ENABLE_TIMER + // for now this is not accessible via Admin/Prometheus , thus useful only with gdb + struct { + TimerCount Sessions_Handlers; + TimerCount Connections_Handlers; + } Timers; +#endif // ENABLE_TIMER + friend class MySQL_Thread; friend class PgSQL_Thread; }; diff --git a/include/proxysql_debug.h b/include/proxysql_debug.h index bf9fa013c..c6416497e 100644 --- a/include/proxysql_debug.h +++ b/include/proxysql_debug.h @@ -12,6 +12,38 @@ extern int gdbg; #ifndef __PROXYSQL_DEBUG_H #define __PROXYSQL_DEBUG_H +#include +#include +#include + +#include "proxysql_macros.h" + +#if ENABLE_TIMER // this is defined in proxysql_macros.h +class TimerCount { + public: + std::chrono::duration Timer = std::chrono::seconds(0); + unsigned int Count = 0; +}; + +class Timer { + public: + Timer(TimerCount& tc) : totalTime(tc.Timer) { + start = std::chrono::high_resolution_clock::now(); + tc.Count++; + } + + ~Timer() { + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = end - start; + totalTime += elapsed; + } + private: + //std::atomic>& totalTime; // If using atomic , use this instead + std::chrono::duration& totalTime; + std::chrono::time_point start; +}; +#endif // ENABLE_TIMER + #ifdef DEBUG #define PROXY_TRACE() { proxy_debug(PROXY_DEBUG_GENERIC,10,"TRACE\n"); } //#define PROXY_TRACE2() { proxy_info("TRACE\n"); } diff --git a/include/proxysql_macros.h b/include/proxysql_macros.h index d7090b1c6..f83cca736 100644 --- a/include/proxysql_macros.h +++ b/include/proxysql_macros.h @@ -1,4 +1,5 @@ - +#ifndef PROXYSQL_MACROS_H +#define PROXYSQL_MACROS_H #define strdup_null(__c) ( __c ? strdup(__c) : __c ) #define char_malloc (char *)malloc #define free_null(__c) { if(__c) { free(__c); __c=NULL; } } @@ -65,3 +66,8 @@ # define unlikely(x) !!(x) #endif #endif /* PROXYSQL_LIKELY */ + +#ifndef ENABLE_TIMER +#define ENABLE_TIMER false +#endif // ENABLE_TIMER +#endif // PROXYSQL_MACROS_H diff --git a/lib/PgSQL_Connection.cpp b/lib/PgSQL_Connection.cpp index 1e80fcf18..0d574c851 100644 --- a/lib/PgSQL_Connection.cpp +++ b/lib/PgSQL_Connection.cpp @@ -3022,6 +3022,9 @@ void PgSQL_Connection::next_event(PG_ASYNC_ST new_st) { PG_ASYNC_ST PgSQL_Connection::handler(short event) { +#if ENABLE_TIMER + Timer timer(myds->sess->thread->Timers.Connections_Handlers); +#endif // ENABLE_TIMER unsigned long long processed_bytes = 0; // issue #527 : this variable will store the amount of bytes processed during this event if (pgsql_conn == NULL) { // it is the first time handler() is being called diff --git a/lib/PgSQL_Session.cpp b/lib/PgSQL_Session.cpp index a0b33c1ef..fb4e2c721 100644 --- a/lib/PgSQL_Session.cpp +++ b/lib/PgSQL_Session.cpp @@ -4391,6 +4391,9 @@ void PgSQL_Session::handler_rc0_Process_GTID(PgSQL_Connection* myconn) { } int PgSQL_Session::handler() { +#if ENABLE_TIMER + Timer timer(thread->Timers.Sessions_Handlers); +#endif // ENABLE_TIMER int handler_ret = 0; bool prepared_stmt_with_no_params = false; bool wrong_pass = false;