From 09fdb0a2c2ad44dbc9a90a54e4973a24f5bc0806 Mon Sep 17 00:00:00 2001 From: counterpoint Date: Tue, 23 Jun 2015 14:58:53 +0100 Subject: [PATCH] Hopefully final tidy up. --- server/core/config.c | 4 +- server/core/dcb.c | 63 ++++++++++++----------- server/core/server.c | 43 +++++++++------- server/include/atomic.h | 1 + server/modules/include/blr.h | 1 + server/modules/monitor/mmmon.c | 1 + server/modules/protocol/mysql_backend.c | 1 + server/modules/protocol/mysql_client.c | 1 + server/modules/routing/binlog/blr_file.c | 8 +-- server/modules/routing/binlog/blr_slave.c | 27 ++++++---- server/modules/routing/debugcmd.c | 2 +- server/modules/routing/readconnroute.c | 1 + utils/CMakeLists.txt | 2 +- 13 files changed, 87 insertions(+), 68 deletions(-) diff --git a/server/core/config.c b/server/core/config.c index bc4c42210..f62591e47 100644 --- a/server/core/config.c +++ b/server/core/config.c @@ -41,9 +41,9 @@ * 30/10/14 Massimiliano Pinto Added disable_master_failback parameter * 07/11/14 Massimiliano Pinto Addition of monitor timeouts for connect/read/write * 20/02/15 Markus Mäkelä Added connection_timeout parameter for services - * 05/03/15 Massimiliano Pinto Added notification_feedback support + * 05/03/15 Massimiliano Pinto Added notification_feedback support * 20/04/15 Guillaume Lefranc Added available_when_donor parameter - * 22/04/15 Martin Brampton Added disable_master_role_setting parameter + * 22/04/15 Martin Brampton Added disable_master_role_setting parameter * * @endverbatim */ diff --git a/server/core/dcb.c b/server/core/dcb.c index 98436f72d..d85a48939 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -639,22 +639,9 @@ char *user; "%lu [dcb_connect] Failed to link to session, the " "session has been removed.\n", pthread_self()))); - dcb_final_free(dcb); + dcb_close(dcb); return NULL; } - /* - dcb->dcb_server_status = server->status; - dcb->state = DCB_STATE_ALLOC; - if (poll_add_dcb(dcb)) - { - LOGIF(LE, (skygw_log_write_flush( - LOGFILE_ERROR, - "%lu [dcb_connect] Failed to add DCB %p to polling.\n", - pthread_self(), dcb))); - dcb_final_free(dcb); - return NULL; - } - */ LOGIF(LD, (skygw_log_write( LOGFILE_DEBUG, "%lu [dcb_connect] Reusing a persistent connection, dcb %p\n", pthread_self(), dcb))); @@ -663,10 +650,10 @@ char *user; } else { - LOGIF(LD, (skygw_log_write( - LOGFILE_DEBUG, - "%lu [dcb_connect] Failed to find a reusable persistent connection.\n", - pthread_self()))); + LOGIF(LD, (skygw_log_write( + LOGFILE_DEBUG, + "%lu [dcb_connect] Failed to find a reusable persistent connection.\n", + pthread_self()))); } } @@ -745,7 +732,7 @@ char *user; /** Copy status field to DCB */ dcb->dcb_server_status = server->status; - ss_debug(dcb->dcb_port = server->port;) + dcb->dcb_port = server->port; /** * backend_dcb is connected to backend server, and once backend_dcb @@ -760,9 +747,9 @@ char *user; rc = poll_add_dcb(dcb); if (rc == DCBFD_CLOSED) { - dcb_set_state(dcb, DCB_STATE_DISCONNECTED, NULL); - dcb_final_free(dcb); - return NULL; + dcb_set_state(dcb, DCB_STATE_DISCONNECTED, NULL); + dcb_final_free(dcb); + return NULL; } /** * The dcb will be addded into poll set by dcb->func.connect @@ -2025,6 +2012,7 @@ dcb_close(DCB *dcb) * Add DCB to persistent pool if it qualifies, close otherwise * * @param dcb The DCB to go to persistent pool or be closed + * @return bool - whether the DCB was added to the pool * */ static bool @@ -2114,7 +2102,9 @@ printDCB(DCB *dcb) if (dcb->remote) printf("\tConnected to: %s\n", dcb->remote); if (dcb->user) - printf("\tUsername to: %s\n", dcb->user); + printf("\tUsername: %s\n", dcb->user); + if (dcb->protoname) + printf("\tProtocol: %s\n", dcb->protoname); if (dcb->writeq) printf("\tQueued write data: %d\n",gwbuf_length(dcb->writeq)); char *statusname = server_status(dcb->server); @@ -2196,6 +2186,9 @@ dprintOneDCB(DCB *pdcb, DCB *dcb) if (dcb->user) dcb_printf(pdcb, "\tUsername: %s\n", dcb->user); + if (dcb->protoname) + dcb_printf(pdcb, "\tProtocol: %s\n", + dcb->protoname); if (dcb->writeq) dcb_printf(pdcb, "\tQueued write data: %d\n", gwbuf_length(dcb->writeq)); @@ -2225,7 +2218,7 @@ dprintOneDCB(DCB *pdcb, DCB *dcb) char buff[20]; struct tm * timeinfo; timeinfo = localtime (&dcb->persistentstart); - strftime(buff, sizeof(buff), "%b %d %H:%M", timeinfo); + strftime(buff, sizeof(buff), "%b %d %H:%M:%S", timeinfo); dcb_printf(pdcb, "\t\tAdded to persistent pool: %s", buff); } } @@ -2256,7 +2249,7 @@ DCB *dcb; } /** - * Diagnotic routine to print DCB data in a tabular form. + * Diagnostic routine to print DCB data in a tabular form. * * @param pdcb DCB to print results to */ @@ -2339,6 +2332,9 @@ dprintDCB(DCB *pdcb, DCB *dcb) if (dcb->user) dcb_printf(pdcb, "\tUsername: %s\n", dcb->user); + if (dcb->protoname) + dcb_printf(pdcb, "\tProtocol: %s\n", + dcb->protoname); dcb_printf(pdcb, "\tOwning Session: %p\n", dcb->session); if (dcb->writeq) dcb_printf(pdcb, "\tQueued write data: %d\n", gwbuf_length(dcb->writeq)); @@ -2396,7 +2392,7 @@ dprintDCB(DCB *pdcb, DCB *dcb) char buff[20]; struct tm * timeinfo; timeinfo = localtime (&dcb->persistentstart); - strftime(buff, sizeof(buff), "%b %d %H:%M", timeinfo); + strftime(buff, sizeof(buff), "%b %d %H:%M:%S", timeinfo); dcb_printf(pdcb, "\t\tAdded to persistent pool: %s", buff); } } @@ -3069,6 +3065,9 @@ dcb_null_auth(DCB *dcb, SERVER *server, SESSION *session, GWBUF *buf) * Check persistent pool for expiry or excess size and count * * @param dcb The DCB being closed. + * @param cleanall Boolean, if true the whole pool is cleared for the + * server related to the given DCB + * @return A count of the DCBs remaining in the pool */ int dcb_persistent_clean_count(DCB *dcb, bool cleanall) @@ -3083,15 +3082,16 @@ dcb_persistent_clean_count(DCB *dcb, bool cleanall) CHK_SERVER(server); spinlock_acquire(&server->persistlock); - persistentdcb = server->persistent; + persistentdcb = server->persistent; while (persistentdcb) { CHK_DCB(persistentdcb); - nextdcb = persistentdcb->nextpersistent; + nextdcb = persistentdcb->nextpersistent; if (cleanall - || persistentdcb-> dcb_errhandle_called - || count >= server->persistpoolmax - || time(NULL) - persistentdcb->persistentstart > server->persistmaxtime) + || persistentdcb-> dcb_errhandle_called + || count >= server->persistpoolmax + || (time(NULL) - persistentdcb->persistentstart) > server->persistmaxtime) { + /* Remove from persistent pool */ if (previousdcb) { previousdcb->nextpersistent = nextdcb; } @@ -3099,6 +3099,7 @@ dcb_persistent_clean_count(DCB *dcb, bool cleanall) { server->persistent = nextdcb; } + /* Add removed DCBs to disposal list for processing outside spinlock */ persistentdcb->nextpersistent = disposals; disposals = persistentdcb; atomic_add(&server->stats.n_persistent, -1); diff --git a/server/core/server.c b/server/core/server.c index 8c16f3fe9..de7463c25 100644 --- a/server/core/server.c +++ b/server/core/server.c @@ -141,8 +141,9 @@ SERVER *server; /** * Get a DCB from the persistent connection pool, if possible * - * @param server The server to set the name on - * @param user The name of the user needing the connection + * @param server The server to set the name on + * @param user The name of the user needing the connection + * @param protocol The name of the protocol needed for the connection */ DCB * server_get_persistent(SERVER *server, char *user, const char *protocol) @@ -154,7 +155,12 @@ server_get_persistent(SERVER *server, char *user, const char *protocol) spinlock_acquire(&server->persistlock); dcb = server->persistent; while (dcb) { - if (dcb->user && dcb->protoname && !dcb-> dcb_errhandle_called && 0 == strcmp(dcb->user, user) && 0 == strcmp(dcb->protoname, protocol)) + if (dcb->user + && dcb->protoname + && !dcb-> dcb_errhandle_called + && !(dcb->flags & DCBF_HUNG) + && 0 == strcmp(dcb->user, user) + && 0 == strcmp(dcb->protoname, protocol)) { if (NULL == previous) { @@ -171,21 +177,22 @@ server_get_persistent(SERVER *server, char *user, const char *protocol) atomic_add(&server->stats.n_current, 1); return dcb; } - else - { - LOGIF(LD, (skygw_log_write_flush( - LOGFILE_DEBUG, - "%lu [server_get_persistent] Rejected dcb " - "%p from pool, user %s looking for %s, protocol %s " - "looking for %s, error handle called %s.", - pthread_self(), - dcb, - dcb->user ? dcb->user : "NULL", - user, - dcb->protoname ? dcb->protoname : "NULL", - protocol, - dcb-> dcb_errhandle_called ? "true" : "false"))); - } + else + { + LOGIF(LD, (skygw_log_write_flush( + LOGFILE_DEBUG, + "%lu [server_get_persistent] Rejected dcb " + "%p from pool, user %s looking for %s, protocol %s " + "looking for %s, hung flag %s, error handle called %s.", + pthread_self(), + dcb, + dcb->user ? dcb->user : "NULL", + user, + dcb->protoname ? dcb->protoname : "NULL", + protocol, + (dcb->flags & DCBF_HUNG) ? "true" : "false", + dcb-> dcb_errhandle_called ? "true" : "false"))); + } previous = dcb; dcb = dcb->nextpersistent; } diff --git a/server/include/atomic.h b/server/include/atomic.h index f89506e6f..18f03bb83 100644 --- a/server/include/atomic.h +++ b/server/include/atomic.h @@ -26,6 +26,7 @@ * * Date Who Description * 10/06/13 Mark Riddoch Initial implementation + * 23/06/15 Martin Brampton Alternative for C++ * * @endverbatim */ diff --git a/server/modules/include/blr.h b/server/modules/include/blr.h index 62cda59a3..72171b253 100644 --- a/server/modules/include/blr.h +++ b/server/modules/include/blr.h @@ -34,6 +34,7 @@ #include #include +#include #define BINLOG_FNAMELEN 16 #define BLR_PROTOCOL "MySQLBackend" diff --git a/server/modules/monitor/mmmon.c b/server/modules/monitor/mmmon.c index c0d54595d..545a77ca2 100644 --- a/server/modules/monitor/mmmon.c +++ b/server/modules/monitor/mmmon.c @@ -126,6 +126,7 @@ startMonitor(void *arg,void* opt) handle->shutdown = 0; handle->id = MONITOR_DEFAULT_ID; handle->master = NULL; + handle->script = NULL; memset(handle->events,false,sizeof(handle->events)); spinlock_init(&handle->lock); } diff --git a/server/modules/protocol/mysql_backend.c b/server/modules/protocol/mysql_backend.c index 818824def..2fe4b9fd2 100644 --- a/server/modules/protocol/mysql_backend.c +++ b/server/modules/protocol/mysql_backend.c @@ -43,6 +43,7 @@ * 27/09/2013 Massimiliano Pinto Changed in gw_read_backend_event the check for dcb_read(), now is if rc < 0 * 24/10/2014 Massimiliano Pinto Added Mysql user@host @db authentication support * 10/11/2014 Massimiliano Pinto Client charset is passed to backend + * 19/06/2015 Martin Brampton Persistent connection handling * */ #include diff --git a/server/modules/protocol/mysql_client.c b/server/modules/protocol/mysql_client.c index 404066de5..4d9b91ef4 100644 --- a/server/modules/protocol/mysql_client.c +++ b/server/modules/protocol/mysql_client.c @@ -38,6 +38,7 @@ * 13/10/2014 Massimiliano Pinto Added: dbname authentication check * 10/11/2014 Massimiliano Pinto Added: client charset added to protocol struct * 29/05/2015 Markus Makela Added SSL support + * 11/06/2015 Martin Brampton COM_QUIT suppressed for persistent connections */ #include #include diff --git a/server/modules/routing/binlog/blr_file.c b/server/modules/routing/binlog/blr_file.c index ca92d6d06..68f47b1c3 100644 --- a/server/modules/routing/binlog/blr_file.c +++ b/server/modules/routing/binlog/blr_file.c @@ -210,9 +210,8 @@ int fd; close(router->binlog_fd); spinlock_acquire(&router->binlog_lock); strncpy(router->binlog_name, file,BINLOG_FNAMELEN); - blr_file_add_magic(router, fd); - spinlock_release(&router->binlog_lock); router->binlog_fd = fd; + spinlock_release(&router->binlog_lock); return 1; } @@ -254,12 +253,13 @@ int fd; LOGIF(LE, (skygw_log_write(LOGFILE_ERROR, "%s: binlog file %s has an invalid length %d.", router->service->name, path, router->binlog_position))); - close(fd); + close(fd); + spinlock_release(&router->binlog_lock); return; } } - spinlock_release(&router->binlog_lock); router->binlog_fd = fd; + spinlock_release(&router->binlog_lock); } /** diff --git a/server/modules/routing/binlog/blr_slave.c b/server/modules/routing/binlog/blr_slave.c index 4de69b8d7..ee7d52099 100644 --- a/server/modules/routing/binlog/blr_slave.c +++ b/server/modules/routing/binlog/blr_slave.c @@ -1693,6 +1693,9 @@ uint32_t chksum; binlognamelen = strlen(slave->binlogfile); len = 19 + 8 + 4 + binlognamelen; + /* no slave crc, remove 4 bytes */ + if (slave->nocrc) + len -= 4; // Build a fake rotate event resp = gwbuf_alloc(len + 5); @@ -1711,17 +1714,19 @@ uint32_t chksum; memcpy(ptr, slave->binlogfile, binlognamelen); ptr += binlognamelen; - /* - * Now add the CRC to the fake binlog rotate event. - * - * The algorithm is first to compute the checksum of an empty buffer - * and then the checksum of the event portion of the message, ie we do not - * include the length, sequence number and ok byte that makes up the first - * 5 bytes of the message. We also do not include the 4 byte checksum itself. - */ - chksum = crc32(0L, NULL, 0); - chksum = crc32(chksum, GWBUF_DATA(resp) + 5, hdr.event_size - 4); - encode_value(ptr, chksum, 32); + if (!slave->nocrc) { + /* + * Now add the CRC to the fake binlog rotate event. + * + * The algorithm is first to compute the checksum of an empty buffer + * and then the checksum of the event portion of the message, ie we do not + * include the length, sequence number and ok byte that makes up the first + * 5 bytes of the message. We also do not include the 4 byte checksum itself. + */ + chksum = crc32(0L, NULL, 0); + chksum = crc32(chksum, GWBUF_DATA(resp) + 5, hdr.event_size - 4); + encode_value(ptr, chksum, 32); + } slave->dcb->func.write(slave->dcb, resp); return 1; diff --git a/server/modules/routing/debugcmd.c b/server/modules/routing/debugcmd.c index 1e4606da6..058d16056 100644 --- a/server/modules/routing/debugcmd.c +++ b/server/modules/routing/debugcmd.c @@ -43,7 +43,7 @@ * 29/05/14 Mark Riddoch Add Filter support * 16/10/14 Mark Riddoch Add show eventq * 05/03/15 Massimiliano Pinto Added enable/disable feedback - * 27/05/15 Martin Brampton Add show persistent [server] + * 27/05/15 Martin Brampton Add show persistent [server] * * @endverbatim */ diff --git a/server/modules/routing/readconnroute.c b/server/modules/routing/readconnroute.c index 1961908e8..22c7001df 100644 --- a/server/modules/routing/readconnroute.c +++ b/server/modules/routing/readconnroute.c @@ -67,6 +67,7 @@ * 06/03/2014 Massimiliano Pinto Server connection counter is now updated in closeSession * 24/06/2014 Massimiliano Pinto New rules for selecting the Master server * 27/06/2014 Mark Riddoch Addition of server weighting + * 11/06/2015 Martin Brampton Remove decrement n_current (moved to dcb.c) * * @endverbatim */ diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 7f835c552..2ae917712 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(utils skygw_utils.cc) +add_library(utils skygw_utils.cc ../server/core/atomic.c) target_link_libraries(utils stdc++)