From 33421ca9971567dc4a1b7375949dec1002ca42fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 26 Dec 2014 09:21:59 +0000 Subject: [PATCH] Initial work on issue #78 Removed 127.0.0.1 from mysql_connection.cpp (issue #70) --- include/mysql_backend.h | 2 ++ lib/mysql_backend.cpp | 29 +++++++++++++++++++++++++++++ lib/mysql_connection.cpp | 3 ++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/mysql_backend.h b/include/mysql_backend.h index 82fd22c92..af4a47c82 100644 --- a/include/mysql_backend.h +++ b/include/mysql_backend.h @@ -244,6 +244,7 @@ class MySQL_Hostgroup { } return mshge; }; + MySQL_Hostgroup_Entry * get_random_hostgroup_entry(); }; class MySQL_HostGroups_Handler { @@ -385,6 +386,7 @@ class MySQL_HostGroups_Handler { return MyHostGroups[hid]->set_HG_entry_status(msptr,_status); }; */ + MySQL_Hostgroup_Entry * get_random_hostgroup_entry(unsigned int hid); }; diff --git a/lib/mysql_backend.cpp b/lib/mysql_backend.cpp index d7911dcbc..085ae2be7 100644 --- a/lib/mysql_backend.cpp +++ b/lib/mysql_backend.cpp @@ -92,6 +92,15 @@ void MySQL_HostGroups_Handler::insert_hostgroup(MySQL_Hostgroup *myhg) { } +MySQL_Hostgroup_Entry * MySQL_HostGroups_Handler::get_random_hostgroup_entry(unsigned int hid) { + MySQL_Hostgroup_Entry *mshge=NULL; + rdlock(); + MySQL_Hostgroup *mshg=(MySQL_Hostgroup *)MyHostGroups->index(hid); + mshge=mshg->get_random_hostgroup_entry(); + rdunlock(); + return mshge; +} + void * MySQL_Backend::operator new(size_t size) { return l_alloc(size); @@ -190,3 +199,23 @@ size_t MySQL_Hostgroup::servers_in_hostgroup() { return MSHGEs->len; } + +MySQL_Hostgroup_Entry * MySQL_Hostgroup::get_random_hostgroup_entry() { + unsigned int i; + unsigned int sum=0; + MySQL_Hostgroup_Entry *mshge; + for (i=0; ilen; i++) { + mshge=(MySQL_Hostgroup_Entry *)MSHGEs->index(i); + sum+=mshge->weight; + } + unsigned int j=rand()%sum; + sum=0; + for (i=0; ilen; i++) { + mshge=(MySQL_Hostgroup_Entry *)MSHGEs->index(i); + sum+=mshge->weight; + if (j<=sum) { + return mshge; + } + } + return NULL; +} diff --git a/lib/mysql_connection.cpp b/lib/mysql_connection.cpp index a112722bd..7f95ca91b 100644 --- a/lib/mysql_connection.cpp +++ b/lib/mysql_connection.cpp @@ -59,7 +59,8 @@ int MySQL_Connection::assign_mshge(unsigned int hid) { // FIXME */ MyHGH->rdlock(); if (mshge) { free_mshge(); } - MySQL_Hostgroup_Entry *_mshge=MyHGH->MSHGE_find(hid,(char *)"127.0.0.1", 3306); + //MySQL_Hostgroup_Entry *_mshge=MyHGH->MSHGE_find(hid,(char *)"127.0.0.1", 3306); + MySQL_Hostgroup_Entry *_mshge=MyHGH->get_random_hostgroup_entry(hid); assert(_mshge); if (_mshge) { set_mshge(_mshge);