GaleraHA support - R/W Splitting with Galera

GaleraHA support - R/W Splitting with Galera
This commit is contained in:
MassimilianoPinto 2014-05-26 10:29:34 +02:00
parent a819887b07
commit bf00ca8aba
3 changed files with 82 additions and 9 deletions

View File

@ -22,8 +22,9 @@
* @verbatim
* Revision History
*
* Date Who Description
* 18/06/13 Mark Riddoch Initial implementation
* Date Who Description
* 18/06/13 Mark Riddoch Initial implementation
* 21/05/14 Massimiliano Pinto Addition of node_id
*
* @endverbatim
*/
@ -67,6 +68,7 @@ SERVER *server;
server->nextdb = NULL;
server->monuser = NULL;
server->monpw = NULL;
server->node_id = -1;
spinlock_acquire(&server_spin);
server->next = allServers;
@ -197,6 +199,7 @@ char *stat;
free(stat);
dcb_printf(dcb, "\tProtocol: %s\n", ptr->protocol);
dcb_printf(dcb, "\tPort: %d\n", ptr->port);
dcb_printf(dcb, "\tNode Id: %d\n", ptr->node_id);
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;
@ -222,6 +225,7 @@ char *stat;
free(stat);
dcb_printf(dcb, "\tProtocol: %s\n", server->protocol);
dcb_printf(dcb, "\tPort: %d\n", server->port);
dcb_printf(dcb, "\tNode Id: %d\n", server->node_id);
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);
}

View File

@ -27,10 +27,11 @@
* @verbatim
* Revision History
*
* Date Who Description
* 14/06/13 Mark Riddoch Initial implementation
* 21/06/13 Mark Riddoch Addition of server status flags
* 22/07/13 Mark Riddoch Addition of JOINED status for Galera
* Date Who Description
* 14/06/13 Mark Riddoch Initial implementation
* 21/06/13 Mark Riddoch Addition of server status flags
* 22/07/13 Mark Riddoch Addition of JOINED status for Galera
* 20/05/14 Massimiliano Pinto Addition of node_id field
*
* @endverbatim
*/
@ -60,6 +61,7 @@ typedef struct server {
SERVER_STATS stats; /**< The server statistics */
struct server *next; /**< Next server */
struct server *nextdb; /**< Next server in list attached to a service */
long node_id; /**< Node id, server_id for M/S or local_index for Galera */
} SERVER;
/**
@ -99,7 +101,7 @@ typedef struct server {
* Is the server joined Galera node? The server must be running and joined.
*/
#define SERVER_IS_JOINED(server) \
(((server)->status & (SERVER_RUNNING|SERVER_MASTER|SERVER_SLAVE|SERVER_JOINED)) == (SERVER_RUNNING|SERVER_JOINED))
(((server)->status & (SERVER_RUNNING|SERVER_JOINED)) == (SERVER_RUNNING|SERVER_JOINED))
extern SERVER *server_alloc(char *, char *, unsigned short);
extern int server_free(SERVER *);

View File

@ -22,8 +22,10 @@
* @verbatim
* Revision History
*
* Date Who Description
* 22/07/13 Mark Riddoch Initial implementation
* Date Who Description
* 22/07/13 Mark Riddoch Initial implementation
* 21/05/14 Massimiliano Pinto Monitor sets a master server
* that has the lowest value of wsrep_local_index
*
* @endverbatim
*/
@ -280,6 +282,8 @@ MYSQL_RES *result;
int num_fields;
int isjoined = 0;
char *uname = defaultUser, *passwd = defaultPasswd;
unsigned long int server_version = 0;
char *server_string;
if (database->server->monuser != NULL)
{
@ -297,6 +301,7 @@ char *uname = defaultUser, *passwd = defaultPasswd;
uname, dpwd, NULL, database->server->port, NULL, 0) == NULL)
{
server_clear_status(database->server, SERVER_RUNNING);
database->server->node_id = -1;
free(dpwd);
return;
}
@ -306,6 +311,15 @@ char *uname = defaultUser, *passwd = defaultPasswd;
/* If we get this far then we have a working connection */
server_set_status(database->server, SERVER_RUNNING);
/* get server version from current server */
server_version = mysql_get_server_version(database->con);
/* get server version string */
server_string = (char *)mysql_get_server_info(database->con);
if (server_string) {
database->server->server_string = strdup(server_string);
}
/* Check if the the Galera FSM shows this node is joined to the cluster */
if (mysql_query(database->con, "SHOW STATUS LIKE 'wsrep_local_state_comment'") == 0
&& (result = mysql_store_result(database->con)) != NULL)
@ -319,6 +333,25 @@ char *uname = defaultUser, *passwd = defaultPasswd;
mysql_free_result(result);
}
/* Check the the Galera node index in the cluster */
if (mysql_query(database->con, "SHOW STATUS LIKE 'wsrep_local_index'") == 0
&& (result = mysql_store_result(database->con)) != NULL)
{
long local_index = -1;
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
local_index = strtol(row[1], NULL, 10);
if ((errno == ERANGE && (local_index == LONG_MAX
|| local_index == LONG_MIN)) || (errno != 0 && local_index == 0))
{
local_index = -1;
}
database->server->node_id = local_index;
}
mysql_free_result(result);
}
if (isjoined)
server_set_status(database->server, SERVER_JOINED);
else
@ -335,6 +368,7 @@ monitorMain(void *arg)
{
MYSQL_MONITOR *handle = (MYSQL_MONITOR *)arg;
MONITOR_SERVERS *ptr;
long master_id;
if (mysql_thread_init())
{
@ -347,6 +381,8 @@ MONITOR_SERVERS *ptr;
handle->status = MONITOR_RUNNING;
while (1)
{
master_id = -1;
if (handle->shutdown)
{
handle->status = MONITOR_STOPPING;
@ -358,8 +394,39 @@ MONITOR_SERVERS *ptr;
while (ptr)
{
monitorDatabase(ptr, handle->defaultUser, handle->defaultPasswd);
if (ptr->server->node_id >= 0 && SERVER_IS_JOINED(ptr->server)) {
if (ptr->server->node_id < master_id && master_id >= 0) {
master_id = ptr->server->node_id;
} else {
if (master_id < 0) {
master_id = ptr->server->node_id;
}
}
} else {
server_clear_status(ptr->server, SERVER_SLAVE);
server_clear_status(ptr->server, SERVER_MASTER);
}
ptr = ptr->next;
}
ptr = handle->databases;
/* this server loop sets Master and Slave roles */
while (ptr)
{
if (ptr->server->node_id >= 0 && master_id >= 0) {
if (SERVER_IS_JOINED(ptr->server) && (ptr->server->node_id == master_id)) {
server_set_status(ptr->server, SERVER_MASTER);
server_clear_status(ptr->server, SERVER_SLAVE);
} else if (SERVER_IS_JOINED(ptr->server) && (ptr->server->node_id > master_id)) {
server_set_status(ptr->server, SERVER_SLAVE);
server_clear_status(ptr->server, SERVER_MASTER);
}
}
ptr = ptr->next;
}
thread_millisleep(10000);
}
}