From 29d260e6bf419c7fad97dee18d88aa8942db1965 Mon Sep 17 00:00:00 2001 From: Massimiliano Pinto Date: Fri, 5 Jul 2013 10:36:59 +0200 Subject: [PATCH] Removed fprintf in backend operations. Some fprinf still available for gateway incoming connection and backend selection --- core/gw_utils.c | 2 +- modules/protocol/mysql_backend.c | 60 ++++++++++++++------------------ modules/protocol/mysql_client.c | 11 +++--- modules/protocol/mysql_common.c | 12 ++++--- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/core/gw_utils.c b/core/gw_utils.c index 39e0cb1c6..f9ab33b82 100644 --- a/core/gw_utils.c +++ b/core/gw_utils.c @@ -87,7 +87,7 @@ int gw_read_gwbuff(DCB *dcb, GWBUF **head, int b) { int n = -1; if (b <= 0) { - fprintf(stderr, "||| read_gwbuff called with 0 bytes for %i, closing\n", dcb->fd); + //fprintf(stderr, "||| read_gwbuff called with 0 bytes for %i, closing\n", dcb->fd); dcb->func.close(dcb); return 1; } diff --git a/modules/protocol/mysql_backend.c b/modules/protocol/mysql_backend.c index 252985913..dbb0df332 100644 --- a/modules/protocol/mysql_backend.c +++ b/modules/protocol/mysql_backend.c @@ -35,9 +35,10 @@ * 01/07/2013 Massimiliano Pinto Put Log Manager example code behind SS_DEBUG macros. * 03/07/2013 Massimiliano Pinto Added delayq for incoming data before mysql connection * 04/07/2013 Massimiliano Pinto Added asyncrhronous MySQL protocol connection to backend + * 05/07/2013 Massimiliano Pinto Added closeSession if backend auth fails */ -static char *version_str = "V1.0.0"; +static char *version_str = "V2.0.0"; int gw_mysql_connect(char *host, int port, char *dbname, char *user, uint8_t *passwd, MySQLProtocol *conn); static int gw_create_backend_connection(DCB *client_dcb, SERVER *server, SESSION *in_session); static int gw_read_backend_event(DCB* dcb); @@ -109,7 +110,6 @@ GetModuleObject() * @param dcb The backend Descriptor Control Block * @return 1 on operation, 0 for no action */ - static int gw_read_backend_event(DCB *dcb) { MySQLProtocol *client_protocol = NULL; MySQLProtocol *backend_protocol = NULL; @@ -122,9 +122,14 @@ static int gw_read_backend_event(DCB *dcb) { backend_protocol = (MySQLProtocol *) dcb->protocol; current_session = (MYSQL_session *)dcb->session->data; - fprintf(stderr, ">>> backend EPOLLIN from %i, protocol state [%s]\n", dcb->fd, gw_mysql_protocol_state2string(backend_protocol->state)); + //fprintf(stderr, ">>> backend EPOLLIN from %i, protocol state [%s]\n", dcb->fd, gw_mysql_protocol_state2string(backend_protocol->state)); - // backend is connected: read server handshake and write auth request and return + /* backend is connected: + * + * 1. read server handshake + * 2. and write auth request + * 3. and return + */ if (backend_protocol->state == MYSQL_CONNECTED) { gw_read_backend_handshake(backend_protocol); @@ -133,7 +138,8 @@ static int gw_read_backend_event(DCB *dcb) { return 1; } - // ready to check the authentication reply + /* ready to check the authentication reply from backend */ + if (backend_protocol->state == MYSQL_AUTH_RECV) { ROUTER_OBJECT *router = NULL; ROUTER *router_instance = NULL; @@ -147,11 +153,12 @@ static int gw_read_backend_event(DCB *dcb) { rsession = session->router_session; } + /* read backed auth reply */ rv = gw_receive_backend_auth(backend_protocol); switch (rv) { case MYSQL_FAILED_AUTHENTICATION: - fprintf(stderr, ">>>> Backend Auth failed for %i\n", dcb->fd); + fprintf(stderr, ">>>> Backend Auth failed for user [%s], fd %i\n", current_session->user, dcb->fd); backend_protocol->state = MYSQL_AUTH_FAILED; @@ -215,17 +222,11 @@ static int gw_read_backend_event(DCB *dcb) { static int gw_write_backend_event(DCB *dcb) { MySQLProtocol *backend_protocol = dcb->protocol; - fprintf(stderr, ">>> backend EPOLLOUT %i, protocol state [%s]\n", backend_protocol->fd, gw_mysql_protocol_state2string(backend_protocol->state)); - - if (backend_protocol->state == MYSQL_AUTH_FAILED) { - fprintf(stderr, ">>> Backend epollout auth failed, EXIT\n"); - return 0; - } + //fprintf(stderr, ">>> backend EPOLLOUT %i, protocol state [%s]\n", backend_protocol->fd, gw_mysql_protocol_state2string(backend_protocol->state)); // spinlock_acquire(&dcb->connectlock); if (backend_protocol->state == MYSQL_PENDING_CONNECT) { - //fprintf(stderr, ">>>> Now the backend %i is CONNECTED\n", backend_protocol->fd); backend_protocol->state = MYSQL_CONNECTED; // spinlock_release(&dcb->connectlock); @@ -250,15 +251,11 @@ gw_MySQLWrite_backend(DCB *dcb, GWBUF *queue) { MySQLProtocol *backend_protocol = dcb->protocol; - if (backend_protocol->state == MYSQL_AUTH_FAILED) { - fprintf(stderr, ">>> backend %i auth failed, EXIT\n", dcb->fd); - dcb_close(dcb); - return 0; - } - spinlock_acquire(&dcb->authlock); - // put incoming data to the delay queue unless backend is connected with auth ok + /** + * Now put the incoming data to the delay queue unless backend is connected with auth ok + */ if (backend_protocol->state != MYSQL_IDLE) { //fprintf(stderr, ">>> Writing in the backend %i delay queue\n", dcb->fd); @@ -269,7 +266,6 @@ gw_MySQLWrite_backend(DCB *dcb, GWBUF *queue) spinlock_release(&dcb->authlock); - // Normal flow of backend write; return dcb_write(dcb, queue); } @@ -279,7 +275,7 @@ gw_MySQLWrite_backend(DCB *dcb, GWBUF *queue) */ static int gw_error_backend_event(DCB *dcb) { - fprintf(stderr, "#### Handle Backend error function for %i\n", dcb->fd); + fprintf(stderr, ">>> Handle Backend error function for %i\n", dcb->fd); dcb_close(dcb); @@ -287,13 +283,9 @@ static int gw_error_backend_event(DCB *dcb) { } /* - * Create a new ackend connection. + * Create a new backend connection. * - * This routine will connect to a backend server - * - * - backend dcb allocation - * - MySQL session data fetch - * - backend connection using data in MySQL session + * This routine will connect to a backend server and it is called by dbc_connect in router->newSession * * @param backend The Backend DCB allocated from dcb_connect * @param server The selected server to connect to @@ -313,13 +305,15 @@ static int gw_create_backend_connection(DCB *backend, SERVER *server, SESSION *s backend->protocol = protocol; - // put the backend dcb in the protocol struct + /* put the backend dcb in the protocol struct */ protocol->descriptor = backend; s_data = (MYSQL_session *)session->client->data; - // let's try to connecte to a backend server, only connect sys call - // The socket descriptor is in Non Blocking status, this is set in the function + /** + * let's try to connect to a backend server, only connect sys call + * The socket descriptor is in Non Blocking status, this is set in the function + */ rv = gw_do_connect_to_backend(server->name, server->port, protocol); // we could also move later, this in to the gw_do_connect_to_backend using protocol->descriptor @@ -329,13 +323,13 @@ static int gw_create_backend_connection(DCB *backend, SERVER *server, SESSION *s switch (rv) { case 0: - fprintf(stderr, "Connected to backend mysql server: fd is %i\n", backend->fd); + //fprintf(stderr, "Connected to backend mysql server: fd is %i\n", backend->fd); protocol->state = MYSQL_CONNECTED; break; case 1: - fprintf(stderr, ">>> Connection is PENDING to backend mysql server: fd is %i\n", backend->fd); + //fprintf(stderr, ">>> Connection is PENDING to backend mysql server: fd is %i\n", backend->fd); protocol->state = MYSQL_PENDING_CONNECT; break; diff --git a/modules/protocol/mysql_client.c b/modules/protocol/mysql_client.c index d82fe22ae..e15761391 100644 --- a/modules/protocol/mysql_client.c +++ b/modules/protocol/mysql_client.c @@ -812,7 +812,7 @@ int gw_read_client_event(DCB* dcb) { if(!rsession) { if (mysql_command == '\x01') { /* COM_QUIT handling */ - fprintf(stderr, "COM_QUIT received with no connected backends from %i\n", dcb->fd); + //fprintf(stderr, "COM_QUIT received with no connected backends from %i\n", dcb->fd); (dcb->func).close(dcb); return 1; @@ -830,10 +830,10 @@ int gw_read_client_event(DCB* dcb) { /* COM_QUIT handling */ if (mysql_command == '\x01') { - fprintf(stderr, "COM_QUIT received for %i\n", dcb->fd); + //fprintf(stderr, "COM_QUIT received from %i and passed to backed\n", dcb->fd); /* this will propagate COM_QUIT to backend(s) */ - fprintf(stderr, "<<< Routing the COM_QUIT ...\n"); + //fprintf(stderr, "<<< Routing the COM_QUIT ...\n"); router->routeQuery(router_instance, rsession, queue); /* close client connection */ @@ -848,7 +848,7 @@ int gw_read_client_event(DCB* dcb) { /* writing in the backend buffer queue, via routeQuery */ - fprintf(stderr, "<<< Routing the Query ...\n"); + //fprintf(stderr, "<<< Routing the Query ...\n"); router->routeQuery(router_instance, rsession, queue); protocol->state = MYSQL_WAITING_RESULT; @@ -1057,7 +1057,8 @@ int gw_MySQLAccept(DCB *listener) { /* */ static int gw_error_client_event(DCB *dcb) { - fprintf(stderr, "#### Handle error function gw_error_client_event, for [%i] is [%s]\n", dcb->fd, gw_dcb_state2string(dcb->state)); + //fprintf(stderr, "#### Handle error function gw_error_client_event, for [%i] is [%s]\n", dcb->fd, gw_dcb_state2string(dcb->state)); + //dcb_close(dcb); return 1; } diff --git a/modules/protocol/mysql_common.c b/modules/protocol/mysql_common.c index 4651f26aa..92c7bcf4e 100644 --- a/modules/protocol/mysql_common.c +++ b/modules/protocol/mysql_common.c @@ -450,28 +450,30 @@ int gw_do_connect_to_backend(char *host, int port, MySQLProtocol *conn) { if (so < 0) { fprintf(stderr, "Error creating backend socket: [%s] %i\n", strerror(errno), errno); - // this is an error + /* this is an error */ return -1; } setipaddress(&serv_addr.sin_addr, host); serv_addr.sin_port = htons(port); + /* set NON BLOCKING here */ setnonblocking(so); if ((rv = connect(so, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) < 0) { - // If connection is not yet completed just return 1 + /* If connection is not yet completed just return 1 */ if (errno == EINPROGRESS) { - fprintf(stderr, ">>> Connection is not yet completed for backend server [%s:%i]: errno %i, %s: RV = [%i]\n", host, port, errno, strerror(errno), rv); + //fprintf(stderr, ">>> Connection is not yet completed for backend server [%s:%i]: errno %i, %s: RV = [%i]\n", host, port, errno, strerror(errno), rv); + return 1; } else { - // this is a real error + /* this is a real error */ fprintf(stderr, ">>> ERROR connecting to backend server [%s:%i]: errno %i, %s: RV = [%i]\n", host, port, errno, strerror(errno), rv); return -1; } } - // connection succesfully completed + /* The connection succesfully completed now */ return 0; }