From f79a0c2a1f84b2053c020e2a229c290b0bfbe95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 26 Dec 2014 08:42:12 +0000 Subject: [PATCH] More work on issue #71 Converted MySQL_Hostgroup::MSHGEs from vector to PtrArray --- include/mysql_backend.h | 30 ++++++++++++---- include/mysql_session.h | 4 +-- lib/mysql_backend.cpp | 78 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 17 deletions(-) diff --git a/include/mysql_backend.h b/include/mysql_backend.h index 2dcf77ae0..82fd22c92 100644 --- a/include/mysql_backend.h +++ b/include/mysql_backend.h @@ -145,6 +145,11 @@ class MySQL_Hostgroup_Entry { class MySQL_Hostgroup { private: + void add(MySQL_Hostgroup_Entry *mshge); + void add(MySQL_Server *msptr, unsigned int _weight=1); + bool del(MySQL_Hostgroup_Entry *mshge); + bool del(MySQL_Server *msptr); +/* void add(MySQL_Hostgroup_Entry *mshge) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Adding MySQL_Hostgroup_Entry %p to Hostgroup %p with HID %d\n", mshge, this, hostgroup_id); MSHGEs.push_back(mshge); @@ -175,18 +180,27 @@ class MySQL_Hostgroup { } return false; }; +*/ public: unsigned int hostgroup_id; - std::vector MSHGEs; + //std::vector MSHGEs; + PtrArray *MSHGEs; MySQL_Hostgroup(unsigned int hid) { hostgroup_id=hid; + MSHGEs = new PtrArray(); }; ~MySQL_Hostgroup() { - for (std::vector::iterator it = MSHGEs.begin() ; it != MSHGEs.end(); ++it) { - MySQL_Hostgroup_Entry *mshge=*it; +// for (std::vector::iterator it = MSHGEs.begin() ; it != MSHGEs.end(); ++it) { +// MySQL_Hostgroup_Entry *mshge=*it; +// delete mshge; +// }; + while (MSHGEs->len) { + MySQL_Hostgroup_Entry *mshge=(MySQL_Hostgroup_Entry *)MSHGEs->remove_index_fast(0); delete mshge; - }; + } }; + MySQL_Hostgroup_Entry * MSHGE_find(MySQL_Server *msptr); +/* MySQL_Hostgroup_Entry * MSHGE_find(MySQL_Server *msptr) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Searching MySQL_Hostgroup_Entry for MySQL_Server %p from Hostgroup %p with HID %d\n", msptr, this, hostgroup_id); for (std::vector::iterator it = MSHGEs.begin(); it != MSHGEs.end(); ++it) { @@ -199,6 +213,7 @@ class MySQL_Hostgroup { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "MySQL_Hostgroup_Entry not found\n"); return NULL; }; +*/ MySQL_Hostgroup_Entry * server_add(MySQL_Server *msptr, unsigned int _weight) { MySQL_Hostgroup_Entry *mshge=NULL; proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Trying to add MySQL_Server %p to MSHGE %p with HID %d\n",msptr, this, hostgroup_id); @@ -213,9 +228,10 @@ class MySQL_Hostgroup { } return mshge; }; - size_t servers_in_hostgroup() { - return MSHGEs.size(); - }; + size_t servers_in_hostgroup(); +// size_t servers_in_hostgroup() { +// return MSHGEs.size(); +// }; void set_HG_entry_status(MySQL_Hostgroup_Entry *mshge, enum proxysql_server_status _status) { mshge->set_status(_status); }; diff --git a/include/mysql_session.h b/include/mysql_session.h index c9747263b..23c4cf1d8 100644 --- a/include/mysql_session.h +++ b/include/mysql_session.h @@ -56,8 +56,8 @@ class MySQL_Session int handler(); MySQL_Backend * find_backend(int); - MySQL_Backend * create_backend(int, MySQL_Data_Stream *); - MySQL_Backend * find_or_create_backend(int, MySQL_Data_Stream *); + MySQL_Backend * create_backend(int, MySQL_Data_Stream *_myds=NULL); + MySQL_Backend * find_or_create_backend(int, MySQL_Data_Stream *_myds=NULL); void reset_all_backends(); void writeout(); diff --git a/lib/mysql_backend.cpp b/lib/mysql_backend.cpp index 400f99a36..d7911dcbc 100644 --- a/lib/mysql_backend.cpp +++ b/lib/mysql_backend.cpp @@ -71,13 +71,13 @@ MySQL_Hostgroup_Entry * MySQL_HostGroups_Handler::MSHGE_find(unsigned int hid, M proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Searching MSHGE for MySQL_Server %p into HID %d\n", srv, hid); MySQL_Hostgroup *myhg=(MySQL_Hostgroup *)MyHostGroups->index(hid); return myhg->MSHGE_find(srv); -}; +} MySQL_Hostgroup_Entry * MySQL_HostGroups_Handler::MSHGE_find(unsigned int hid, char *add, uint16_t p) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Searching MSHGE for MySQL server %s:%d into HID %d\n", add, p, hid); MySQL_Server *srv=server_find(add,p); if (srv==NULL) return NULL; // server not found return MSHGE_find(hid,srv); -}; +} void MySQL_HostGroups_Handler::insert_hostgroup(MySQL_Hostgroup *myhg) { @@ -89,17 +89,17 @@ void MySQL_HostGroups_Handler::insert_hostgroup(MySQL_Hostgroup *myhg) { //MyHostGroups.insert(MyHostGroups.begin()+p,myhg); //MyHostGroups[p]=myhg; MyHostGroups->pdata[p]=myhg; -}; +} void * MySQL_Backend::operator new(size_t size) { return l_alloc(size); -}; +} void MySQL_Backend::operator delete(void *ptr) { l_free(sizeof(MySQL_Backend),ptr); -}; +} MySQL_Backend::MySQL_Backend() { hostgroup_id=-1; @@ -107,10 +107,10 @@ MySQL_Backend::MySQL_Backend() { myconn=NULL; server_bytes_at_cmd.bytes_recv=0; server_bytes_at_cmd.bytes_sent=0; -}; +} MySQL_Backend::~MySQL_Backend() { -}; +} void MySQL_Backend::reset() { if (server_myds) { @@ -126,5 +126,67 @@ void MySQL_Backend::reset() { } }; myconn=NULL; -}; +} + + + + +void MySQL_Hostgroup::add(MySQL_Hostgroup_Entry *mshge) { + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Adding MySQL_Hostgroup_Entry %p to Hostgroup %p with HID %d\n", mshge, this, hostgroup_id); + MSHGEs->add(mshge); +} + +void MySQL_Hostgroup::add(MySQL_Server *msptr, unsigned int _weight) { + MySQL_Hostgroup_Entry *mshge=new MySQL_Hostgroup_Entry(hostgroup_id, msptr, _weight); + this->add(mshge); +} + +bool MySQL_Hostgroup::del(MySQL_Hostgroup_Entry *mshge) { + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Deleting MySQL_Hostgroup_Entry %p from Hostgroup %p with HID %d\n", mshge, this, hostgroup_id); + MySQL_Hostgroup_Entry *it=NULL; + unsigned int i; + for (i=0; i< MSHGEs->len; i++) { + it=(MySQL_Hostgroup_Entry *)MSHGEs->index(i); + if (it==mshge) { + it=(MySQL_Hostgroup_Entry *)MSHGEs->remove_index_fast(i); + delete it; + return true; + } + } + return false; +} + +bool MySQL_Hostgroup::del(MySQL_Server *msptr) { + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Deleting MySQL_Hostgroup_Entry with MySQL_Server %p from Hostgroup %p with HID %d\n", msptr, this, hostgroup_id); + MySQL_Hostgroup_Entry *it=NULL; + unsigned int i; + for (i=0; i< MSHGEs->len; i++) { + it=(MySQL_Hostgroup_Entry *)MSHGEs->index(i); + if (it->MSptr==msptr) { + it=(MySQL_Hostgroup_Entry *)MSHGEs->remove_index_fast(i); + delete it; + return true; + } + } + return false; +} + +MySQL_Hostgroup_Entry * MySQL_Hostgroup::MSHGE_find(MySQL_Server *msptr) { + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "Searching MySQL_Hostgroup_Entry for MySQL_Server %p from Hostgroup %p with HID %d\n", msptr, this, hostgroup_id); + MySQL_Hostgroup_Entry *it=NULL; + unsigned int i; + for (i=0; i< MSHGEs->len; i++) { + it=(MySQL_Hostgroup_Entry *)MSHGEs->index(i); + if (it->MSptr==msptr) { + return it; + } + } + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 4, "MySQL_Hostgroup_Entry not found\n"); + return NULL; +} + + +size_t MySQL_Hostgroup::servers_in_hostgroup() { + return MSHGEs->len; +}