diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 9ca92cf13a..e1915a549a 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -53,32 +53,6 @@ typedef struct _kill_queue_t { std::vector query_ids; } kill_queue_t; -class ProxySQL_Poll { - private: - void shrink(); - void expand(unsigned int more); - - public: - unsigned int len; - unsigned int size; - struct pollfd *fds; - MySQL_Data_Stream **myds; - unsigned long long *last_recv; - unsigned long long *last_sent; - std::atomic bootstrapping_listeners; - volatile int pending_listener_add; - volatile int pending_listener_del; - unsigned int poll_timeout; - unsigned long loops; - StatCounters *loop_counters; - - ProxySQL_Poll(); - ~ProxySQL_Poll(); - void add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time); - void remove_index_fast(unsigned int i); - int find_index(int fd); -}; - enum MySQL_Thread_status_variable { st_var_backend_stmt_prepare, st_var_backend_stmt_execute, @@ -256,25 +230,6 @@ class __attribute__((aligned(64))) MySQL_Thread typedef MySQL_Thread * create_MySQL_Thread_t(); typedef void destroy_MySQL_Thread_t(MySQL_Thread *); -class iface_info { - public: - char *iface; - char *address; - int port; - int fd; - iface_info(char *_i, char *_a, int p, int f) { - iface=strdup(_i); - address=strdup(_a); - port=p; - fd=f; - } - ~iface_info() { - free(iface); - free(address); - close(fd); - } -}; - class MySQL_Listeners_Manager { private: PtrArray *ifaces; diff --git a/include/MySQL_Variables.h b/include/MySQL_Variables.h index 95381bf2da..678d594a06 100644 --- a/include/MySQL_Variables.h +++ b/include/MySQL_Variables.h @@ -10,9 +10,6 @@ class MySQL_Session; -extern const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr); -extern MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char *name); -extern MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename); extern void print_backtrace(void); typedef bool (*verify_var)(MySQL_Session* session, int idx, uint32_t client_hash, uint32_t server_hash); diff --git a/include/ProxySQL_Poll.h b/include/ProxySQL_Poll.h new file mode 100644 index 0000000000..5cfa3d1a48 --- /dev/null +++ b/include/ProxySQL_Poll.h @@ -0,0 +1,50 @@ +#ifndef __CLASS_PROXYSQL_POLL +#define __CLASS_PROXYSQL_POLL + +//#include "MySQL_Data_Stream.h" + +class iface_info { + public: + char *iface; + char *address; + int port; + int fd; + iface_info(char *_i, char *_a, int p, int f) { + iface=strdup(_i); + address=strdup(_a); + port=p; + fd=f; + } + ~iface_info() { + free(iface); + free(address); + close(fd); + } +}; + +class ProxySQL_Poll { + private: + void shrink(); + void expand(unsigned int more); + + public: + unsigned int len; + unsigned int size; + struct pollfd *fds; + MySQL_Data_Stream **myds; + unsigned long long *last_recv; + unsigned long long *last_sent; + std::atomic bootstrapping_listeners; + volatile int pending_listener_add; + volatile int pending_listener_del; + unsigned int poll_timeout; + unsigned long loops; + StatCounters *loop_counters; + + ProxySQL_Poll(); + ~ProxySQL_Poll(); + void add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time); + void remove_index_fast(unsigned int i); + int find_index(int fd); +}; +#endif // __CLASS_PROXYSQL_POLL diff --git a/include/cpp.h b/include/cpp.h index f828d3452d..cb4c1dd8cd 100644 --- a/include/cpp.h +++ b/include/cpp.h @@ -2,6 +2,7 @@ #include "MySQL_Thread.h" #include "MySQL_Session.h" #include "mysql_backend.h" +#include "ProxySQL_Poll.h" //#include "MySQL_Data_Stream.h" #include "query_cache.hpp" #include "mysql_connection.h" diff --git a/include/proxysql_find_charset.h b/include/proxysql_find_charset.h new file mode 100644 index 0000000000..11ae949d48 --- /dev/null +++ b/include/proxysql_find_charset.h @@ -0,0 +1,7 @@ +#include "mysql.h" +#include "mariadb_com.h" + +MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char * const name); +MARIADB_CHARSET_INFO * proxysql_find_charset_collate_names(const char *csname, const char *collatename); +const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr); +MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename); diff --git a/lib/Makefile b/lib/Makefile index 73fb64ed14..23d5190f04 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -144,7 +144,8 @@ MYCXXFLAGS=-std=c++11 $(MYCFLAGS) $(PSQLCH) $(ENABLE_EPOLL) default: libproxysql.a .PHONY: default -_OBJ_CXX = ProxySQL_GloVars.oo network.oo debug.oo configfile.oo Query_Cache.oo SpookyV2.oo MySQL_Authentication.oo gen_utils.oo sqlite3db.oo mysql_connection.oo MySQL_HostGroups_Manager.oo mysql_data_stream.oo MySQL_Thread.oo MySQL_Session.oo MySQL_Protocol.oo mysql_backend.oo Query_Processor.oo ProxySQL_Admin.oo ProxySQL_Config.oo ProxySQL_Restapi.oo MySQL_Monitor.oo MySQL_Logger.oo thread.oo MySQL_PreparedStatement.oo ProxySQL_Cluster.oo ClickHouse_Authentication.oo ClickHouse_Server.oo ProxySQL_Statistics.oo Chart_bundle_js.oo ProxySQL_HTTP_Server.oo ProxySQL_RESTAPI_Server.oo font-awesome.min.css.oo main-bundle.min.css.oo set_parser.oo MySQL_Variables.oo c_tokenizer.oo proxysql_utils.oo proxysql_coredump.oo proxysql_sslkeylog.oo +_OBJ_CXX = ProxySQL_GloVars.oo network.oo debug.oo configfile.oo Query_Cache.oo SpookyV2.oo MySQL_Authentication.oo gen_utils.oo sqlite3db.oo mysql_connection.oo MySQL_HostGroups_Manager.oo mysql_data_stream.oo MySQL_Thread.oo MySQL_Session.oo MySQL_Protocol.oo mysql_backend.oo Query_Processor.oo ProxySQL_Admin.oo ProxySQL_Config.oo ProxySQL_Restapi.oo MySQL_Monitor.oo MySQL_Logger.oo thread.oo MySQL_PreparedStatement.oo ProxySQL_Cluster.oo ClickHouse_Authentication.oo ClickHouse_Server.oo ProxySQL_Statistics.oo Chart_bundle_js.oo ProxySQL_HTTP_Server.oo ProxySQL_RESTAPI_Server.oo font-awesome.min.css.oo main-bundle.min.css.oo set_parser.oo MySQL_Variables.oo c_tokenizer.oo proxysql_utils.oo proxysql_coredump.oo proxysql_sslkeylog.oo \ + proxysql_find_charset.oo ProxySQL_Poll.oo OBJ_CXX = $(patsubst %,$(ODIR)/%,$(_OBJ_CXX)) HEADERS = ../include/*.h ../include/*.hpp diff --git a/lib/MySQL_Protocol.cpp b/lib/MySQL_Protocol.cpp index ddcbc84228..c2d4828e21 100644 --- a/lib/MySQL_Protocol.cpp +++ b/lib/MySQL_Protocol.cpp @@ -32,8 +32,7 @@ extern ClickHouse_Authentication *GloClickHouseAuth; #define CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA 0x00200000 #endif -extern const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr); -MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char *name); +#include "proxysql_find_charset.h" #ifdef DEBUG static void __dump_pkt(const char *func, unsigned char *_ptr, unsigned int len) { diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 4dab93185e..29fa32b2e6 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -119,10 +119,7 @@ static const std::set mysql_variables_strings = { "wsrep_osu_method", }; -extern MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char * const name); -extern MARIADB_CHARSET_INFO * proxysql_find_charset_collate_names(const char *csname, const char *collatename); -extern const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr); -extern MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename); +#include "proxysql_find_charset.h" extern MySQL_Authentication *GloMyAuth; extern MySQL_LDAP_Authentication *GloMyLdapAuth; diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 18efd1f6a5..945ebe332f 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -105,71 +105,6 @@ mythr_g_st_vars_t MySQL_Thread_status_variables_gauge_array[] { extern mysql_variable_st mysql_tracked_variables[]; -const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr) { - const MARIADB_CHARSET_INFO * c = mariadb_compiled_charsets; - do { - if (c->nr == nr) { - return c; - } - ++c; - } while (c[0].nr != 0); - return NULL; -} - -MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char *name_) { - MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; - const char *name; - if (strcasecmp(name_,(const char *)"utf8mb3")==0) { - name = (const char *)"utf8"; - } else { - name = name_; - } - do { - if (!strcasecmp(c->csname, name)) { - return c; - } - ++c; - } while (c[0].nr != 0); - return NULL; -} - -MARIADB_CHARSET_INFO * proxysql_find_charset_collate_names(const char *csname_, const char *collatename_) { - MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; - char buf[64]; - const char *csname; - const char *collatename; - if (strcasecmp(csname_,(const char *)"utf8mb3")==0) { - csname = (const char *)"utf8"; - } else { - csname = csname_; - } - if (strncasecmp(collatename_,(const char *)"utf8mb3", 7)==0) { - memcpy(buf,(const char *)"utf8",4); - strcpy(buf+4,collatename_+7); - collatename = buf; - } else { - collatename = collatename_; - } - do { - if (!strcasecmp(c->csname, csname) && !strcasecmp(c->name, collatename)) { - return c; - } - ++c; - } while (c[0].nr != 0); - return NULL; -} - -MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename) { - MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; - do { - if (!strcasecmp(c->name, collatename)) { - return c; - } - ++c; - } while (c[0].nr != 0); - return NULL; -} - #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -197,113 +132,6 @@ __thread unsigned int __thread_MySQL_Thread_Variables_version; volatile static unsigned int __global_MySQL_Thread_Variables_version; - -static unsigned int near_pow_2 (unsigned int n) { - unsigned int i = 1; - while (i < n) i <<= 1; - return i ? i : n; -} - - -void ProxySQL_Poll::shrink() { - unsigned int new_size=near_pow_2(len+1); - fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd)); - myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *)); - last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long)); - last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long)); - size=new_size; -} - -void ProxySQL_Poll::expand(unsigned int more) { - if ( (len+more) > size ) { - unsigned int new_size=near_pow_2(len+more); - fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd)); - myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *)); - last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long)); - last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long)); - size=new_size; - } -} - -ProxySQL_Poll::ProxySQL_Poll() { - loop_counters=new StatCounters(15,10); - poll_timeout=0; - loops=0; - len=0; - pending_listener_add=0; - pending_listener_del=0; - bootstrapping_listeners = true; - size=MIN_POLL_LEN; - fds=(struct pollfd *)malloc(size*sizeof(struct pollfd)); - myds=(MySQL_Data_Stream **)malloc(size*sizeof(MySQL_Data_Stream *)); - last_recv=(unsigned long long *)malloc(size*sizeof(unsigned long long)); - last_sent=(unsigned long long *)malloc(size*sizeof(unsigned long long)); -} - - -ProxySQL_Poll::~ProxySQL_Poll() { - unsigned int i; - for (i=0;imyds_type==MYDS_LISTENER) { - delete myds[i]; - } - } - free(myds); - free(fds); - free(last_recv); - free(last_sent); - delete loop_counters; -} - - -void ProxySQL_Poll::add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time) { - if (len==size) { - expand(1); - } - myds[len]=_myds; - fds[len].fd=_fd; - fds[len].events=_events; - fds[len].revents=0; - if (_myds) { - _myds->mypolls=this; - _myds->poll_fds_idx=len; // fix a serious bug - } - last_recv[len]=monotonic_time(); - last_sent[len]=sent_time; - len++; -} - -void ProxySQL_Poll::remove_index_fast(unsigned int i) { - if ((int)i==-1) return; - myds[i]->poll_fds_idx=-1; // this prevents further delete - if (i != (len-1)) { - myds[i]=myds[len-1]; - fds[i].fd=fds[len-1].fd; - fds[i].events=fds[len-1].events; - fds[i].revents=fds[len-1].revents; - myds[i]->poll_fds_idx=i; // fix a serious bug - last_recv[i]=last_recv[len-1]; - last_sent[i]=last_sent[len-1]; - } - len--; - if ( ( len>MIN_POLL_LEN ) && ( size > len*MIN_POLL_DELETE_RATIO ) ) { - shrink(); - } -} - -int ProxySQL_Poll::find_index(int fd) { - unsigned int i; - for (i=0; i mysql_servers_tablenames = { diff --git a/lib/ProxySQL_Poll.cpp b/lib/ProxySQL_Poll.cpp new file mode 100644 index 0000000000..3a35c977ea --- /dev/null +++ b/lib/ProxySQL_Poll.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include "StatCounters.h" +#include "MySQL_Data_Stream.h" +#include "ProxySQL_Poll.h" +#include "proxysql_structs.h" +#include +#include "cpp.h" + +void ProxySQL_Poll::shrink() { + unsigned int new_size=l_near_pow_2(len+1); + fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd)); + myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *)); + last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long)); + last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long)); + size=new_size; +} + +void ProxySQL_Poll::expand(unsigned int more) { + if ( (len+more) > size ) { + unsigned int new_size=l_near_pow_2(len+more); + fds=(struct pollfd *)realloc(fds,new_size*sizeof(struct pollfd)); + myds=(MySQL_Data_Stream **)realloc(myds,new_size*sizeof(MySQL_Data_Stream *)); + last_recv=(unsigned long long *)realloc(last_recv,new_size*sizeof(unsigned long long)); + last_sent=(unsigned long long *)realloc(last_sent,new_size*sizeof(unsigned long long)); + size=new_size; + } +} + +ProxySQL_Poll::ProxySQL_Poll() { + loop_counters=new StatCounters(15,10); + poll_timeout=0; + loops=0; + len=0; + pending_listener_add=0; + pending_listener_del=0; + bootstrapping_listeners = true; + size=MIN_POLL_LEN; + fds=(struct pollfd *)malloc(size*sizeof(struct pollfd)); + myds=(MySQL_Data_Stream **)malloc(size*sizeof(MySQL_Data_Stream *)); + last_recv=(unsigned long long *)malloc(size*sizeof(unsigned long long)); + last_sent=(unsigned long long *)malloc(size*sizeof(unsigned long long)); +} + + +ProxySQL_Poll::~ProxySQL_Poll() { + unsigned int i; + for (i=0;imyds_type==MYDS_LISTENER) { + delete myds[i]; + } + } + free(myds); + free(fds); + free(last_recv); + free(last_sent); + delete loop_counters; +} + + +void ProxySQL_Poll::add(uint32_t _events, int _fd, MySQL_Data_Stream *_myds, unsigned long long sent_time) { + if (len==size) { + expand(1); + } + myds[len]=_myds; + fds[len].fd=_fd; + fds[len].events=_events; + fds[len].revents=0; + if (_myds) { + _myds->mypolls=this; + _myds->poll_fds_idx=len; // fix a serious bug + } + last_recv[len]=monotonic_time(); + last_sent[len]=sent_time; + len++; +} + +void ProxySQL_Poll::remove_index_fast(unsigned int i) { + if ((int)i==-1) return; + myds[i]->poll_fds_idx=-1; // this prevents further delete + if (i != (len-1)) { + myds[i]=myds[len-1]; + fds[i].fd=fds[len-1].fd; + fds[i].events=fds[len-1].events; + fds[i].revents=fds[len-1].revents; + myds[i]->poll_fds_idx=i; // fix a serious bug + last_recv[i]=last_recv[len-1]; + last_sent[i]=last_sent[len-1]; + } + len--; + if ( ( len>MIN_POLL_LEN ) && ( size > len*MIN_POLL_DELETE_RATIO ) ) { + shrink(); + } +} + +int ProxySQL_Poll::find_index(int fd) { + unsigned int i; + for (i=0; i + +const MARIADB_CHARSET_INFO * proxysql_find_charset_nr(unsigned int nr) { + const MARIADB_CHARSET_INFO * c = mariadb_compiled_charsets; + do { + if (c->nr == nr) { + return c; + } + ++c; + } while (c[0].nr != 0); + return NULL; +} + +MARIADB_CHARSET_INFO * proxysql_find_charset_name(const char *name_) { + MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; + const char *name; + if (strcasecmp(name_,(const char *)"utf8mb3")==0) { + name = (const char *)"utf8"; + } else { + name = name_; + } + do { + if (!strcasecmp(c->csname, name)) { + return c; + } + ++c; + } while (c[0].nr != 0); + return NULL; +} + +MARIADB_CHARSET_INFO * proxysql_find_charset_collate_names(const char *csname_, const char *collatename_) { + MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; + char buf[64]; + const char *csname; + const char *collatename; + if (strcasecmp(csname_,(const char *)"utf8mb3")==0) { + csname = (const char *)"utf8"; + } else { + csname = csname_; + } + if (strncasecmp(collatename_,(const char *)"utf8mb3", 7)==0) { + memcpy(buf,(const char *)"utf8",4); + strcpy(buf+4,collatename_+7); + collatename = buf; + } else { + collatename = collatename_; + } + do { + if (!strcasecmp(c->csname, csname) && !strcasecmp(c->name, collatename)) { + return c; + } + ++c; + } while (c[0].nr != 0); + return NULL; +} + +MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename) { + MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets; + do { + if (!strcasecmp(c->name, collatename)) { + return c; + } + ++c; + } while (c[0].nr != 0); + return NULL; +}