in class MySQL_Connection_Pool:
- shared converted from int to bool
- added 2 functions MySQL_Connection_lookup()

in class MyConnArray:
- match() and add() are not private
- class MySQL_Connection_Pool is a friend class
- added function MyConn_find()

in class MySQL_Connection:
- class MyConnArray is a friend class
- MCA is private
- removed custom operator new() and delete() because MySQL_Connection_Pool can be shared
- all members don't use the custom memory allocator
- added private function is_expired . See issue #30
- member expire removed, and replaced with inserted_into_pool
pull/95/head
René Cannaò 12 years ago
parent bd7a5d1795
commit 9e6ce50739

@ -5,19 +5,22 @@
#include "cpp.h"
class MySQL_Connection {
public:
void * operator new(size_t);
void operator delete(void *);
private:
MyConnArray *MCA;
bool is_expired(unsigned long long timeout);
unsigned long long inserted_into_pool;
public:
// void * operator new(size_t);
// void operator delete(void *);
MySQL_Data_Stream *myds;
MYSQL myconn;
MySQL_Hostgroup_Entry *mshge;
unsigned long long expire;
bool reusable;
bool reusable;
MySQL_Connection();
~MySQL_Connection();
int assign_mshge(unsigned int);
void set_mshge(MySQL_Hostgroup_Entry *);
void free_mshge();
friend class MyConnArray;
};
#endif /* __CLASS_MYSQL_CONNECTION_H */

@ -19,32 +19,35 @@ struct _mysql_conns_array_t {
class MyConnArray {
private:
char *hostname;
char *username;
char *password;
char *db;
unsigned int port;
//GPtrArray *free_conns;
char *hostname;
char *username;
char *password;
char *db;
unsigned int port;
PtrArray *free_conns;
MyConnArray * match(const char *__hostname, const char *__username, const char *__password, const char *__db, unsigned int __port);
void add(MySQL_Connection *);
MySQL_Connection * MyConn_find();
public:
MyConnArray(const char *__hostname, const char *__username, const char *__password, const char *__db, unsigned int __port);
~MyConnArray();
MyConnArray * match(const char *__hostname, const char *__username, const char *__password, const char *__db, unsigned int __port);
void add(MySQL_Connection *);
friend class MySQL_Connection_Pool;
};
class MySQL_Connection_Pool {
private:
int shared;
spinlock mutex;
//GPtrArray *MyConnArrays;
PtrArray *MyConnArrays;
bool shared; //< TRUE for shared connection pool
spinlock mutex; //< used only for shared connection pool
PtrArray *MyConnArrays; //< Pointers array
MyConnArray * MyConnArray_find(const char *hostname, const char *username, const char *password, const char *db, unsigned int port);
MyConnArray * MyConnArray_create(const char *hostname, const char *username, const char *password, const char *db, unsigned int port);
public:
MySQL_Connection_Pool(int _shared=0);
MySQL_Connection_Pool(bool _shared=false);
~MySQL_Connection_Pool();
MyConnArray * MyConnArray_lookup(const char *hostname, const char *username, const char *password, const char *db, unsigned int port);
MySQL_Connection * MySQL_Connection_lookup(MyConnArray *MCA);
MySQL_Connection * MySQL_Connection_lookup(const char *hostname, const char *username, const char *password, const char *db, unsigned int port);
};
#endif /* __CLASS_MYSQL_CONNECTION_POOL_H */

@ -1381,10 +1381,12 @@ bool MySQL_Protocol::process_pkt_initial_handshake(unsigned char *pkt, unsigned
myc.server_capabilities=capabilities;
myc.charset=(const charset_info_st *)l_alloc(sizeof(struct charset_info_st));
//myc.charset=(const charset_info_st *)l_alloc(sizeof(struct charset_info_st));
myc.charset=(const charset_info_st *)malloc(sizeof(struct charset_info_st));
const_cast<charset_info_st *>(myc.charset)->nr=charset;
myc.thread_id=thread_id;
myc.server_version=l_strdup((const char *)version);
//myc.server_version=l_strdup((const char *)version);
myc.server_version=strdup((const char *)version);
myc.protocol_version=protocol;
memcpy(myc.scramble_buff,(const char *)salt1,strlen((char *)salt1));
@ -1471,18 +1473,17 @@ bool MySQL_Protocol::process_pkt_handshake_response(unsigned char *pkt, unsigned
if (ret==true) {
MYSQL &myc=(*myds)->myconn->myconn;
/*
myc.user=strdup((const char *)user);
if (password) myc.passwd=strdup(password);
if (db) myc.db=strdup((const char *)db);
*/
/*
myc.user=l_strdup((const char *)user);
if (password) myc.passwd=l_strdup(password);
if (db) myc.db=l_strdup((const char *)db);
*/
myc.server_capabilities=capabilities;
//myc.charset=(const charset_info_st *)malloc(sizeof(struct charset_info_st));
myc.charset=(const charset_info_st *)l_alloc(sizeof(struct charset_info_st));
myc.charset=(const charset_info_st *)malloc(sizeof(struct charset_info_st));
// myc.charset=(const charset_info_st *)l_alloc(sizeof(struct charset_info_st));
const_cast<charset_info_st *>(myc.charset)->nr=charset;
//myds->myconn->myconn

@ -16,7 +16,7 @@ static void term_handler(int sig) {
ProxySQL_GlobalVariables::~ProxySQL_GlobalVariables() {
//opt->reset();
opt->reset();
//opt->footer.clear();
delete opt;
delete confFile;

@ -1,7 +1,7 @@
#include "proxysql.h"
#include "cpp.h"
/*
void * MySQL_Connection::operator new(size_t size) {
return l_alloc(size);
}
@ -9,34 +9,33 @@ void * MySQL_Connection::operator new(size_t size) {
void MySQL_Connection::operator delete(void *ptr) {
l_free(sizeof(MySQL_Connection),ptr);
}
*/
MySQL_Connection::MySQL_Connection() {
memset(&myconn,0,sizeof(MYSQL));
MCA=NULL;
mshge=NULL;
myds=NULL;
expire=0;
inserted_into_pool=0;
reusable=false;
};
MySQL_Connection::~MySQL_Connection() {
if (myconn.host) free(myconn.host);
/*
if (myconn.user) free(myconn.user);
if (myconn.passwd) free(myconn.passwd);
if (myconn.db) free(myconn.db);
*/
/*
if (myconn.user) l_free_string(myconn.user);
if (myconn.passwd) l_free_string(myconn.passwd);
if (myconn.db) l_free_string(myconn.db);
*/
if (myconn.unix_socket) free(myconn.unix_socket);
//if (myconn.server_version) free(myconn.server_version);
if (myconn.server_version) free(myconn.server_version);
if (myconn.host_info) free(myconn.host_info);
if (myconn.info) free(myconn.info);
if (myconn.server_version) l_free_string(myconn.server_version);
//if (myconn.charset) free(const_cast<charset_info_st *>(myconn.charset));
if (myconn.charset) l_free(sizeof(charset_info_st), const_cast<charset_info_st *>(myconn.charset));
// if (myconn.server_version) l_free_string(myconn.server_version);
if (myconn.charset) free(const_cast<charset_info_st *>(myconn.charset));
// if (myconn.charset) l_free(sizeof(charset_info_st), const_cast<charset_info_st *>(myconn.charset));
// if (myconn.charset) free(myconn.charset);
if (mshge) {
__sync_add_and_fetch(&mshge->references,-1);
@ -68,3 +67,9 @@ int MySQL_Connection::assign_mshge(unsigned int hid) { // FIXME
MyHGH->rdunlock();
return 0;
};
bool MySQL_Connection::is_expired(unsigned long long timeout) {
// FIXME: here the check should be a sanity check
// FIXME: for now this is just a temporary (and stupid) check
return false;
}

@ -36,6 +36,34 @@ void MyConnArray::add(MySQL_Connection *myc) {
free_conns->add((void *)myc);
}
MySQL_Connection * MyConnArray::MyConn_find() {
unsigned int l;
MySQL_Connection *myc=NULL;
MySQL_Connection *_myc_tmp=NULL;
for (l=0; l<free_conns->len; l++) {
_myc_tmp=(MySQL_Connection *)free_conns->index(l);
if (_myc_tmp->is_expired(0)==false) { // FIXME: shouldn't pass 0
myc=(MySQL_Connection *)free_conns->remove_index_fast(l);
break;
}
}
return myc;
}
MySQL_Connection * MySQL_Connection_Pool::MySQL_Connection_lookup(MyConnArray *MCA) {
MySQL_Connection *myc=NULL;
myc=MCA->MyConn_find();
return myc;
}
MySQL_Connection * MySQL_Connection_Pool::MySQL_Connection_lookup(const char *hostname, const char *username, const char *password, const char *db, unsigned int port) {
MySQL_Connection *myc=NULL;
MyConnArray *MCA=MyConnArray_find(hostname, username, password, db, port);
if (MCA) {
myc=MCA->MyConn_find();
}
return myc;
}
MyConnArray * MySQL_Connection_Pool::MyConnArray_find(const char *hostname, const char *username, const char *password, const char *db, unsigned int port) {
unsigned int l;
@ -71,7 +99,7 @@ MyConnArray * MySQL_Connection_Pool::MyConnArray_lookup(const char *hostname, co
return MCA;
}
MySQL_Connection_Pool::MySQL_Connection_Pool(int _shared) {
MySQL_Connection_Pool::MySQL_Connection_Pool(bool _shared) {
shared=_shared;
spinlock_init(&mutex);
MyConnArrays= new PtrArray();

@ -223,6 +223,10 @@ int MySQL_Session::handler() {
}
MyHGHs->rdunlock();
*/
assert(server_myds);
assert(server_myds->myconn);
assert(server_myds->myconn->mshge);
assert(server_myds->myconn->mshge->MSptr);
server_fd=server_myds->myds_connect(server_myds->myconn->mshge->MSptr->address, server_myds->myconn->mshge->MSptr->port, &pending_connect);
server_myds->init((pending_connect==1 ? MYDS_BACKEND_NOT_CONNECTED : MYDS_BACKEND), this, server_fd);
thread->mypolls.add(POLLIN|POLLOUT, server_fd, server_myds, curtime);
@ -310,6 +314,7 @@ int MySQL_Session::handler() {
// fprintf(stderr,"Query to cache: %s\n", client_myds->query_SQL);
uint32_t resbuf=0;
unsigned char *aa=GloQC->get(client_myds->query_SQL,&resbuf);
//unsigned char *aa=NULL;
if (aa) {
l_free(pkt.size,pkt.ptr);
l_free(strlen((char *)client_myds->query_SQL)+1,client_myds->query_SQL);
@ -351,6 +356,10 @@ int MySQL_Session::handler() {
}
MyHGH->rdunlock();
*/
assert(server_myds);
assert(server_myds->myconn);
assert(server_myds->myconn->mshge);
assert(server_myds->myconn->mshge->MSptr);
server_fd=server_myds->myds_connect(server_myds->myconn->mshge->MSptr->address, server_myds->myconn->mshge->MSptr->port, &pending_connect);
// server_fd=server_myds->myds_connect((char *)"127.0.0.1", 3306, &pending_connect);
server_myds->init((pending_connect==1 ? MYDS_BACKEND_NOT_CONNECTED : MYDS_BACKEND), this, server_fd);

Loading…
Cancel
Save