Skip to content

Commit

Permalink
Move ProxySQL_Poll() to its own source file
Browse files Browse the repository at this point in the history
  • Loading branch information
renecannao committed Dec 8, 2023
1 parent 5181812 commit bd7846f
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 146 deletions.
45 changes: 0 additions & 45 deletions include/MySQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,6 @@ typedef struct _kill_queue_t {
std::vector<thr_id_usr *> 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<bool> 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,
Expand Down Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions include/ProxySQL_Poll.h
Original file line number Diff line number Diff line change
@@ -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<bool> 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
1 change: 1 addition & 0 deletions include/cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ 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 \
proxysql_find_charset.oo
proxysql_find_charset.oo ProxySQL_Poll.oo
OBJ_CXX = $(patsubst %,$(ODIR)/%,$(_OBJ_CXX))
HEADERS = ../include/*.h ../include/*.hpp

Expand Down
100 changes: 0 additions & 100 deletions lib/MySQL_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,106 +132,6 @@ __thread unsigned int __thread_MySQL_Thread_Variables_version;
volatile static unsigned int __global_MySQL_Thread_Variables_version;



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;i<len;i++) {
if (
myds[i] && // fix bug #278 . This should be caused by not initialized datastreams used to ping the backend
myds[i]->myds_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<len; i++) {
if (fds[i].fd==fd) {
return i;
}
}
return -1;
}


MySQL_Listeners_Manager::MySQL_Listeners_Manager() {
ifaces=new PtrArray();
}
Expand Down
107 changes: 107 additions & 0 deletions lib/ProxySQL_Poll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "StatCounters.h"
#include "MySQL_Data_Stream.h"
#include "ProxySQL_Poll.h"
#include "proxysql_structs.h"
#include <poll.h>
#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;i<len;i++) {
if (
myds[i] && // fix bug #278 . This should be caused by not initialized datastreams used to ping the backend
myds[i]->myds_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<len; i++) {
if (fds[i].fd==fd) {
return i;
}
}
return -1;
}

0 comments on commit bd7846f

Please sign in to comment.