diff --git a/core/dcb.c b/core/dcb.c index b4f5464f9..23208a045 100644 --- a/core/dcb.c +++ b/core/dcb.c @@ -174,7 +174,8 @@ GWPROTOCOL *funcs; dcb_free(dcb); return NULL; } - server->stats.n_connections++; + atomic_add(&server->stats.n_connections, 1); + atomic_add(&server->stats.n_current, 1); poll_add_dcb(dcb); /* diff --git a/core/server.c b/core/server.c index 99611a6cf..35ad228db 100644 --- a/core/server.c +++ b/core/server.c @@ -119,6 +119,8 @@ printServer(SERVER *server) printf("\tServer: %s\n", server->name); printf("\tProtocol: %s\n", server->protocol); printf("\tPort: %d\n", server->port); + printf("\tTotal connections: %d\n", server->stats.n_connections); + printf("\tCurrent connections: %d\n", server->stats.n_current); } /** @@ -163,6 +165,7 @@ SERVER *ptr; dcb_printf(dcb, "\tProtocol: %s\n", ptr->protocol); dcb_printf(dcb, "\tPort: %d\n", ptr->port); dcb_printf(dcb, "\tNumber of connections: %d\n", ptr->stats.n_connections); + dcb_printf(dcb, "\tCurrent no. of connections: %d\n", ptr->stats.n_current); ptr = ptr->next; } spinlock_release(&server_spin); @@ -183,6 +186,7 @@ dprintServer(DCB *dcb, SERVER *server) dcb_printf(dcb, "\tProtocol: %s\n", server->protocol); dcb_printf(dcb, "\tPort: %d\n", server->port); dcb_printf(dcb, "\tNumber of connections: %d\n", server->stats.n_connections); + dcb_printf(dcb, "\tCurrent No. of connections: %d\n", server->stats.n_current); } /** diff --git a/include/server.h b/include/server.h index 5c719ed43..2e3f9d6f5 100644 --- a/include/server.h +++ b/include/server.h @@ -40,6 +40,7 @@ */ typedef struct { int n_connections; /**< Number of connections */ + int n_current; /**< Current connections */ } SERVER_STATS; /** @@ -58,9 +59,37 @@ typedef struct server { struct server *nextdb; /**< Next server in list attached to a service */ } SERVER; +/** + * Status bits in the server->status member. + * + * These are a bitmap of attributes that may be applied to a 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 */ +/** + * Is the server running - the macro returns true if the server is marked as running + * regardless of it's state as a master or slave + */ +#define SERVER_IS_RUNNING(server) ((server)->status & SERVER_RUNNING) +/** + * Is the server marked as down - the macro returns true if the server is beleived + * to be inoperable. + */ +#define SERVER_IS_DOWN(server) (((server)->status & SERVER_RUNNING) == 0) +/** + * Is the server a master? The server must be both running and marked as master + * in order for the macro to return true + */ +#define SERVER_IS_MASTER(server) \ + (((server)->status & (SERVER_RUNNING|SERVER_MASTER)) == (SERVER_RUNNING|SERVER_MASTER)) +/** + * Is the server a slave? The server must be both running and marked as a slave + * in order for the macro to return true + */ +#define SERVER_IS_SLAVE(server) \ + (((server)->status & (SERVER_RUNNING|SERVER_MASTER)) == SERVER_RUNNING) + extern SERVER *server_alloc(char *, char *, unsigned short); extern int server_free(SERVER *); diff --git a/modules/routing/debugcli.c b/modules/routing/debugcli.c index aaea4cf3d..61b4e9ec8 100644 --- a/modules/routing/debugcli.c +++ b/modules/routing/debugcli.c @@ -154,6 +154,8 @@ CLI_SESSION *client; client->next = inst->sessions; inst->sessions = client; spinlock_release(&inst->lock); + + session->state = SESSION_STATE_READY; return (void *)client; } diff --git a/modules/routing/readconnroute.c b/modules/routing/readconnroute.c index a7b4a45ab..e819215ce 100644 --- a/modules/routing/readconnroute.c +++ b/modules/routing/readconnroute.c @@ -193,8 +193,11 @@ int i; candidate = inst->servers[0]; for (i = 1; inst->servers[i]; i++) { - if (inst->servers[i] && inst->servers[i]->count < candidate->count) + if (inst->servers[i] && SERVER_IS_RUNNING(inst->servers[i]->server) + && inst->servers[i]->count < candidate->count) + { candidate = inst->servers[i]; + } } /* @@ -243,8 +246,9 @@ CLIENT_SESSION *session = (CLIENT_SESSION *)router_session; * Close the connection to the backend */ session->dcb->func.close(session->dcb); - atomic_add(&session->backend->count, -1); + atomic_add(&session->backend->server->stats.n_current, -1); + spinlock_acquire(&inst->lock); if (inst->connections == session) inst->connections = session->next;