From cb26490a06edfef21094a0db78d77153094c0680 Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Fri, 21 Jun 2013 10:45:39 +0200 Subject: [PATCH] Addition of server status flags Addition of dcb role API --- core/dcb.c | 12 ++++++++++++ core/depend.mk | 9 +++++---- core/gateway.c | 7 ++----- core/server.c | 27 +++++++++++++++++++++++++++ core/session.c | 9 ++++++++- include/dcb.h | 1 + include/gw.h | 1 - include/server.h | 9 ++++++++- 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/core/dcb.c b/core/dcb.c index d1d7a5ff5..96fa59c67 100644 --- a/core/dcb.c +++ b/core/dcb.c @@ -509,3 +509,15 @@ va_list args; buf->end = GWBUF_DATA(buf) + strlen(GWBUF_DATA(buf)) + 1; dcb->func.write(dcb, buf); } + +/** + * Determine the role that a DCB plays within a session. + * + * @param dcb + * @return Non-zero if the DCB is the client of the session + */ +int +dcb_isclient(DCB *dcb) +{ + return (dcb->session && dcb == dcb->session->client); +} diff --git a/core/depend.mk b/core/depend.mk index 8079b40d4..59a50e0b1 100644 --- a/core/depend.mk +++ b/core/depend.mk @@ -68,7 +68,8 @@ gateway.o: gateway.c ../include/gw.h /usr/include/stdio.h \ ../include/thread.h /usr/include/pthread.h /usr/include/sched.h \ /usr/include/bits/sched.h /usr/include/bits/setjmp.h ../include/buffer.h \ ../include/mysql_protocol.h ../include/dcb.h ../include/service.h \ - ../include/server.h ../include/session.h + ../include/server.h ../include/session.h ../include/modules.h \ + ../include/poll.h gateway_mysql_protocol.o: gateway_mysql_protocol.c ../include/gw.h \ /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h \ /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \ @@ -350,9 +351,9 @@ server.o: server.c /usr/include/stdio.h /usr/include/features.h \ /usr/include/bits/time.h /usr/include/sys/sysmacros.h \ /usr/include/bits/pthreadtypes.h /usr/include/alloca.h \ /usr/include/string.h /usr/include/xlocale.h ../include/session.h \ - ../include/server.h ../include/spinlock.h ../include/thread.h \ - /usr/include/pthread.h /usr/include/sched.h /usr/include/bits/sched.h \ - /usr/include/bits/setjmp.h ../include/dcb.h ../include/buffer.h + ../include/server.h ../include/dcb.h ../include/spinlock.h \ + ../include/thread.h /usr/include/pthread.h /usr/include/sched.h \ + /usr/include/bits/sched.h /usr/include/bits/setjmp.h ../include/buffer.h poll.o: poll.c /usr/include/stdio.h /usr/include/features.h \ /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \ /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \ diff --git a/core/gateway.c b/core/gateway.c index 5407d7281..097be29ac 100644 --- a/core/gateway.c +++ b/core/gateway.c @@ -38,8 +38,8 @@ #include #include #include - -void myfree(void** pp) { free(*pp); *pp = NULL; } +#include +#include /* basic signal handling */ static void sighup_handler (int i) { @@ -167,7 +167,6 @@ main(int argc, char **argv) { int daemon_mode = 1; sigset_t sigset; -int nfds; int n; unsigned short port = 4406; SERVICE *service1, *service2; @@ -249,8 +248,6 @@ SERVER *server1, *server2, *server3; serviceStart(service1); serviceStart(service2); - fprintf(stderr, ">> GATEWAY poll maxevents is %i\n", MAX_EVENTS); - while (1) { poll(); diff --git a/core/server.c b/core/server.c index a82941a69..c13a37642 100644 --- a/core/server.c +++ b/core/server.c @@ -59,6 +59,7 @@ SERVER *server; server->protocol = strdup(protocol); server->port = port; memset(&server->stats, sizeof(SERVER_STATS), 0); + server->status = 0; server->nextdb = NULL; spinlock_acquire(&server_spin); @@ -165,3 +166,29 @@ SERVER *ptr; } spinlock_release(&server_spin); } + +/** + * Convert a set of server status flags to a string, the returned + * string has been malloc'd and must be free'd by the caller + * + * @param SERVER The server to return the status of + * @return A string representation of the status flags + */ +char * +server_status(SERVER *server) +{ +char *status = NULL; + + if ((status = (char *)malloc(200)) == NULL) + return NULL; + status[0] = 0; + if (server->status & SERVER_RUNNING) + strcat(status, "Running, "); + else + strcat(status, "Down, "); + if (server->status & SERVER_MASTER) + strcat(status, "Master"); + else + strcat(status, "Slave"); + return status; +} diff --git a/core/session.c b/core/session.c index 00a30ef7c..89009a473 100644 --- a/core/session.c +++ b/core/session.c @@ -66,9 +66,16 @@ SESSION *session; session->state = SESSION_STATE_ALLOC; client->session = session; - // this will prevent the connect() backends via connect_dcb() for listening socket + /* + * Only create a router session if we are not the listening + * DCB. Creating a router session may create a connection to a + * backend server, depending upon the router module implementation + * and should be avoided for the listener session + */ if (client->state != DCB_STATE_LISTENING) + { session->router_session = service->router->newSession(service->router_instance, session); + } spinlock_acquire(&session_spin); session->next = allSessions; diff --git a/include/dcb.h b/include/dcb.h index fcc0a0bc2..74ebb9329 100644 --- a/include/dcb.h +++ b/include/dcb.h @@ -142,5 +142,6 @@ extern void dprintAllDCBs(DCB *); /* Debug to print all DCB in the system */ extern void dprintDCB(DCB *, DCB *); /* Debug to print a DCB in the system */ extern const char *gw_dcb_state2string(int); /* DCB state to string */ extern void dcb_printf(DCB *, const char *, ...); /* DCB version of printf */ +extern int dcb_isclient(DCB *); #endif diff --git a/include/gw.h b/include/gw.h index 04dd02a71..263509238 100644 --- a/include/gw.h +++ b/include/gw.h @@ -19,7 +19,6 @@ #include -#define MAX_EVENTS 1000000 #define EXIT_FAILURE 1 // network buffer is 32K diff --git a/include/server.h b/include/server.h index 66bdd9620..c1037e568 100644 --- a/include/server.h +++ b/include/server.h @@ -29,6 +29,7 @@ * * Date Who Description * 14/06/13 Mark Riddoch Initial implementation + * 21/06/13 Mark Riddoch Addition of server status flags * * @endverbatim */ @@ -51,14 +52,20 @@ typedef struct server { char *name; /**< Server name/IP address*/ unsigned short port; /**< Port to listen on */ char *protocol; /**< Protocol module to use */ + unsigned int status; /**< Status flag bitmap for the server */ SERVER_STATS stats; /**< The server statistics */ struct server *next; /**< Next server */ - struct server *nextdb; /**< Next server in lsit attached to a service */ + struct server *nextdb; /**< Next server in list attached to a service */ } SERVER; +#define SERVER_RUNNING 0x0001 /**<< The server is up and running */ +#define SERVER_MASTER 0x0002 /**<< The server is a master, i.e. can handle writes */ + + extern SERVER *server_alloc(char *, char *, unsigned short); extern int server_free(SERVER *); extern void printServer(SERVER *); extern void printAllServers(); extern void dprintAllServers(DCB *); +extern char *server_status(SERVER *); #endif