MySQL fallback servers

Roland Rosenfeld <[email protected]>
Newsgroups gmane.mail.perdition.user
Message-ID <[email protected]>
Hi!

Some years ago I developed a little patch that allows to add multiple
MySQL servers to the MySQL database backend, so if the first server
isn't reachable, perdition falls back to the second etc.

I was sure, that I sent this patch to Simon or to this list, but the
patch never appeared in the upstream source and when I checked all my
outbound mail, I didn't find a mail with this patch, so I seem to have
forgotten to do so.

Today I updated my patch to the current 1.17.1.  Maybe someone else
will find this patch useful.

Tschoeeee

        Roland

-- 
Roland Rosenfeld  -  Content Delivery  -  NED  -  Technik
NetCologne Gesellschaft für Telekommunikation mbH  -  HRB 25580, AG Köln
Am Coloneum 9   50829 Köln   Tel.: +49-221-2222-373   Fax: +49-221-2222-7373
Geschäftsführer: Werner Hanf, Karl-Heinz Zankel

______________________________________________
Perdition-users mailing list
[email protected]
http://lists.vergenet.net/listinfo/perdition-users
perdition-1.17.1-multiple_mysqlhosts.patch (text/x-diff, 4.1 KB)
diff -urNpd perdition-1.17.1/perdition/db/mysql/perditiondb_mysql.c perdition-1.17.1-ro/perdition/db/mysql/perditiondb_mysql.c
--- perdition-1.17.1/perdition/db/mysql/perditiondb_mysql.c	2005-06-22 07:50:04.000000000 +0200
+++ perdition-1.17.1-ro/perdition/db/mysql/perditiondb_mysql.c	2009-07-31 10:51:24.000000000 +0200
@@ -42,7 +42,9 @@
 
 
 static vanessa_dynamic_array_t *a=NULL;
-static char *dbhost          = PERDITIONDB_MYSQL_DEFAULT_DBHOST;
+static vanessa_dynamic_array_t *dbhosts_array=NULL;
+static size_t dbhosts_count = 0;
+static char *dbhosts         = PERDITIONDB_MYSQL_DEFAULT_DBHOSTS;
 static char *dbname          = PERDITIONDB_MYSQL_DEFAULT_DBNAME;
 static unsigned int dbport   = PERDITIONDB_MYSQL_DEFAULT_DBPORT;
 static char *dbtable         = PERDITIONDB_MYSQL_DEFAULT_DBTABLE;
@@ -92,10 +94,10 @@ int dbserver_fini(void){
  * dbserver_init
  * Parse options string.
  * pre: options_str: Options string. String is of the form
- * [dbhost[:port[:dbname[:dbtable[:dbuser[:dbpwd[:dbsrvcol[:dbusercol[:dbportcol]]]]]]]]]
+ * [dbhost1[,dbhost2[,...]][:port[:dbname[:dbtable[:dbuser[:dbpwd[:dbsrvcol[:dbusercol[:dbportcol]]]]]]]]]
  * post: Options string is parsed if not null into 
  *       static vanessa_dynamic_array_t a and 
- *       static char *dbhost, *dbname, *dbtable, *dbuser, *dbpwd are
+ *       static char *dbhosts, *dbname, *dbtable, *dbuser, *dbpwd are
  *       set to pointers insiside a or defaults as neccesary.
  * return:  0 on success
  *         -1 on db access error
@@ -130,8 +132,16 @@ int dbserver_init(char *options_str){
    }
 
    count=vanessa_dynamic_array_get_count(a);
-   if(count>PERDITIONDB_MYSQL_DBHOST){ 
-     dbhost=vanessa_dynamic_array_get_element(a, PERDITIONDB_MYSQL_DBHOST); 
+   if(count>PERDITIONDB_MYSQL_DBHOSTS){ 
+     dbhosts=vanessa_dynamic_array_get_element(a, PERDITIONDB_MYSQL_DBHOSTS); 
+     if ((dbhosts_array = vanessa_dynamic_array_split_str(dbhosts, 
+			  PERDITIONDB_MYSQL_HOSTS_DELIMITER)) == NULL) {
+       VANESSA_LOGGER_DEBUG("vanessa_dynamic_array_split_str");
+       a=NULL;
+       free(tmp_str);
+       return(-1);
+     }
+     dbhosts_count = vanessa_dynamic_array_get_count(dbhosts_array);
    }
    if(count>PERDITIONDB_MYSQL_DBNAME){ 
      dbname=vanessa_dynamic_array_get_element(a, PERDITIONDB_MYSQL_DBNAME); 
@@ -211,6 +221,7 @@ int dbserver_get(
    MYSQL_ROW row;
    char sqlstr[PERDITIONDB_MYSQL_QUERY_LENGTH];
    size_t servername_len;
+   size_t hcnt = 0;
 
    rc = mysql_init(&db);
    if(!rc){  
@@ -219,7 +230,13 @@ int dbserver_get(
      return(-1);
    }
 
-   rc = mysql_real_connect(&db,dbhost,dbuser,dbpwd,dbname,dbport,NULL,0);
+   while (hcnt < dbhosts_count 
+        && !(rc=mysql_real_connect(&db,
+                        vanessa_dynamic_array_get_element(dbhosts_array, hcnt),
+                        dbuser,dbpwd,dbname,dbport,NULL,0))) {
+     perditiondb_mysql_log("mysql_connect", &db);
+     hcnt++;
+   }
    if(!rc){  
      perditiondb_mysql_log("mysql_connect", &db);
      mysql_close(&db);
diff -urNpd perdition-1.17.1/perdition/db/mysql/perditiondb_mysql.h perdition-1.17.1-ro/perdition/db/mysql/perditiondb_mysql.h
--- perdition-1.17.1/perdition/db/mysql/perditiondb_mysql.h	2005-06-22 07:50:04.000000000 +0200
+++ perdition-1.17.1-ro/perdition/db/mysql/perditiondb_mysql.h	2009-07-31 09:49:25.000000000 +0200
@@ -43,8 +43,9 @@
 #include <vanessa_adt.h>
 
 #define PERDITIONDB_MYSQL_FIELD_DELIMITER   ':'
+#define PERDITIONDB_MYSQL_HOSTS_DELIMITER   ','
 #define PERDITIONDB_MYSQL_MAX_SLEEP         1800
-#define PERDITIONDB_MYSQL_DEFAULT_DBHOST    "localhost"
+#define PERDITIONDB_MYSQL_DEFAULT_DBHOSTS   "localhost"
 #define PERDITIONDB_MYSQL_DEFAULT_DBPORT    0
 #define PERDITIONDB_MYSQL_DEFAULT_DBNAME    "dbPerdition"
 #define PERDITIONDB_MYSQL_DEFAULT_DBTABLE   "tblPerdition"
@@ -54,7 +55,7 @@
 #define PERDITIONDB_MYSQL_DEFAULT_DBSRVCOL  "servername"
 #define PERDITIONDB_MYSQL_DEFAULT_DBPORTCOL "port"
 
-#define PERDITIONDB_MYSQL_DBHOST    0
+#define PERDITIONDB_MYSQL_DBHOSTS   0
 #define PERDITIONDB_MYSQL_DBPORT    1
 #define PERDITIONDB_MYSQL_DBNAME    2
 #define PERDITIONDB_MYSQL_DBTABLE   3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.