dcb_final_free: Router session is not closed anymore when dcb is closed. Router session is shared among all dcbs and is closed and freed with session.
	dcb_connect: dcb's state must be switched fro DCB_STATE_ALLOC to DCB_STATE_DISCONNECTED before dcb_final_free can be called for it.
	dcb_close: poll_remove_dcb encapsulates dcb's state transition in the same way as poll_add_dcb. Removed state modification from dcb_close. Read return value of poll_remove_dcb and log accordingly.
	dcb_set state: remove dassert if dcb_set_state_nomutex returned false. False can be returned indicating that intented change didn't occur but the end state, for example, may be acceptable. Failures in state transitions are asserted in dcb_Set_state_nomutex.

poll.c
	poll_add_dcb: dcb state is now set here to either DCB_STATE_LISTENING or to DCB_STATE_POLLING according to dcb's role. Failures in state setting and in epoll_ctl are detected, logged and handled.
	poll_remove_dcb: Failures in state setting and epoll_ctl are detected, logged, and handled.

mysql_client_server_protocol.h 
	Removed macros MYSQL_FAILED_AUTHENTICATION & MYSQL_SUCCESFUL_AUTHENTICATION as they were not necessary and used with constant values 0 and 1 depending on the case.
	Renamed variable 'conn' to 'protocol' in cases where it meant protocol.

mysql_backend.c
	gw_read_backend_event: In case when there was nothing to read or read failed, backend dcb is closed because situation is assumed to be such that backend server closed its side of the socket. Removed macros MYSQL_FAILED/SUCCESFUL_AUTHENTICATION
	gw_create_backend_connection: Assigned protocol with fd which is connected to backend.
	backend_write_delayqueue: In case where dcb_write fails to write anything, close backend dcb to avoid it getting hanging.

mysql_client.c
	gw_read_client_event: Read return value of routeQuery and if it isn't == 1, call mysql_send_custom_error with client dcb and set client's protocol state to MYSQL_IDLE instead of MYSQL_ROUTING.
	gw_MySQLAccept: Static reply counter was erroneously used as a criteria for jumping to return point where return value was constantly 'success' (=0). Replaced static reply counter with private. Fixed return value in different cases.

mysql_common.c
	gw_receive_backend_auth: Changed to return boolean values indicating of success or failue. Used integers which were assigned to macroed values on the caller side. Added length check before accessing buffer. 

readconnroute.c
	Cut too long lines and removed statements with side effects.

skygw_debug.h
	Added macro STRPROTOCOLSTATE(s) to produce string representation of a given protocol state.
This commit is contained in:
vraatikka
2013-09-20 14:32:28 +03:00
parent c7b052aab3
commit 74aa3638f9
8 changed files with 530 additions and 303 deletions

View File

@ -90,29 +90,66 @@ poll_init()
int
poll_add_dcb(DCB *dcb)
{
int rc;
int rc = -1;
dcb_state_t old_state = DCB_STATE_UNDEFINED;
dcb_state_t new_state;
struct epoll_event ev;
CHK_DCB(dcb);
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = dcb;
/**
* Service listeners have different state than
* DCBs serving client requests.
* Choose new state according to the role of dcb.
*/
if (dcb->dcb_role == DCB_ROLE_SERVICE_LISTENER) {
dcb_set_state(dcb, DCB_STATE_LISTENING, &old_state);
} else if (dcb->dcb_role == DCB_ROLE_REQUEST_HANDLER) {
dcb_set_state(dcb, DCB_STATE_POLLING, &old_state);
}
rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, dcb->fd, &ev);
if (rc != 0) {
dcb_set_state(dcb, old_state, NULL);
if (dcb->dcb_role == DCB_ROLE_REQUEST_HANDLER) {
new_state = DCB_STATE_POLLING;
} else {
ss_dassert(dcb->dcb_role == DCB_ROLE_SERVICE_LISTENER);
new_state = DCB_STATE_LISTENING;
}
/**
* If dcb is in unexpected state, state change fails indicating that dcb
* is not polling anymore.
*/
if (dcb_set_state(dcb, new_state, &old_state)) {
rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, dcb->fd, &ev);
ss_dassert(rc == 0);
} else {
skygw_log_write(
LOGFILE_ERROR,
"%lu [poll_add_dcb] Unable to set new state for dcb %p "
"in state %s. Adding to poll set failed.",
pthread_self(),
dcb,
STRDCBSTATE(dcb->state));
goto return_rc;
}
if (rc != 0) {
int eno = errno;
errno = 0;
skygw_log_write(
LOGFILE_ERROR,
"%lu [poll_add_dcb] Adding dcb %p to poll set failed. "
"epoll_ctl failed due %d, %s.",
pthread_self(),
dcb,
STRDCBSTATE(dcb->state),
eno,
strerror(eno));
goto return_rc;
} else {
skygw_log_write(
LOGFILE_DEBUG,
"%lu [poll_add_dcb] Added dcb %p to "
"poll set.",
pthread_self(),
dcb);
}
return_rc:
return rc;
}
/**
@ -125,31 +162,60 @@ poll_add_dcb(DCB *dcb)
int
poll_remove_dcb(DCB *dcb)
{
struct epoll_event ev;
int rc;
struct epoll_event ev;
int rc = -1;
dcb_state_t old_state = DCB_STATE_UNDEFINED;
dcb_state_t new_state = DCB_STATE_NOPOLLING;
rc = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, dcb->fd, &ev);
if (rc == 0) {
skygw_log_write(
LOGFILE_TRACE,
"%lu [poll_remove_dcb] Removed dcb %p in state %s from "
"poll set.",
pthread_self(),
dcb,
STRDCBSTATE(dcb->state));
} else {
int eno = errno;
errno = 0;
skygw_log_write(
LOGFILE_TRACE,
"%lu [poll_remove_dcb] Removing dcb %p in state %s from "
"poll set failed due %d %s.",
pthread_self(),
dcb,
STRDCBSTATE(dcb->state),
eno,
strerror(eno));
CHK_DCB(dcb);
/**
* Set state to NOPOLLING and remove dcb from poll set.
*/
if (dcb_set_state(dcb, new_state, &old_state)) {
rc = epoll_ctl(epoll_fd, EPOLL_CTL_DEL, dcb->fd, &ev);
if (rc != 0) {
int eno = errno;
errno = 0;
skygw_log_write_flush(
LOGFILE_ERROR,
"%lu [poll_remove_dcb] epoll_ctl failed due %d, %s.",
pthread_self(),
eno,
strerror(eno));
}
ss_dassert(rc == 0);
}
/**
* This call was redundant, but the end result is correct.
*/
else if (old_state == new_state)
{
rc = 0;
goto return_rc;
}
/**
* State transition failed. This may be due some more serious error
* in how dcb is handled.
*/
else
{
skygw_log_write_flush(
LOGFILE_ERROR,
"%lu [poll_remove_dcb] Unable to set state %s for dcb %p "
"in state %s. Removing from poll set failed.",
pthread_self(),
STRDCBSTATE(new_state),
STRDCBSTATE(old_state));
ss_dassert(false);
goto return_rc;
}
/** Set bit for each maxscale thread */
bitmask_copy(&dcb->memdata.bitmask, poll_bitmask());
rc = 0;
return_rc:
return rc;
}
@ -198,7 +264,7 @@ poll_waitevents(void *arg)
}
#else
if (!no_op) {
skygw_log_write(LOGFILE_TRACE,
skygw_log_write(LOGFILE_DEBUG,
"%lu [poll_waitevents] MaxScale thread %d > "
"epoll_wait <",
pthread_self(),
@ -211,7 +277,7 @@ poll_waitevents(void *arg)
{
int eno = errno;
errno = 0;
skygw_log_write(LOGFILE_TRACE,
skygw_log_write(LOGFILE_DEBUG,
"%lu [poll_waitevents] epoll_wait returned "
"%d, errno %d",
pthread_self(),
@ -232,7 +298,7 @@ poll_waitevents(void *arg)
if (nfds > 0)
{
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu [poll_waitevents] epoll_wait found %d fds",
pthread_self(),
nfds);
@ -248,7 +314,7 @@ poll_waitevents(void *arg)
#if defined(SS_DEBUG)
if (dcb_fake_write_ev[dcb->fd] != 0) {
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] "
"Added fake events %d to ev %d.",
pthread_self(),
@ -265,8 +331,8 @@ poll_waitevents(void *arg)
ss_dassert(dcb->state != DCB_STATE_FREED);
ss_debug(spinlock_release(&dcb->dcb_initlock);)
skygw_log_write(
LOGFILE_TRACE,
skygw_log_write_flush(
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] event %d dcb %p",
pthread_self(),
thread_id,
@ -280,7 +346,7 @@ poll_waitevents(void *arg)
if (eno == 0) {
eno = dcb_fake_write_errno[dcb->fd];
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] "
"Added fake errno %d. %s",
pthread_self(),
@ -292,7 +358,7 @@ poll_waitevents(void *arg)
#endif
if (eno != 0) {
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] "
"EPOLLERR due %d, %s.",
pthread_self(),
@ -321,13 +387,6 @@ poll_waitevents(void *arg)
ss_info_dassert(!dcb->dcb_write_active,
"Write already active");
dcb->dcb_write_active = TRUE;
skygw_log_write(
LOGFILE_TRACE,
"%lu %d [poll_waitevents] "
"Write in fd %d",
pthread_self(),
thread_id,
dcb->fd);
atomic_add(&pollStats.n_write, 1);
dcb->func.write_ready(dcb);
dcb->dcb_write_active = FALSE;
@ -344,7 +403,7 @@ poll_waitevents(void *arg)
if (dcb->state == DCB_STATE_LISTENING)
{
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] "
"Accept in fd %d",
pthread_self(),
@ -352,15 +411,16 @@ poll_waitevents(void *arg)
dcb->fd);
atomic_add(&pollStats.n_accept, 1);
dcb->func.accept(dcb);
}
}
else
{
skygw_log_write(
LOGFILE_TRACE,
LOGFILE_DEBUG,
"%lu %d [poll_waitevents] "
"Read in fd %d",
"Read in dcb %p fd %d",
pthread_self(),
thread_id,
dcb,
dcb->fd);
atomic_add(&pollStats.n_read, 1);
dcb->func.read(dcb);