git » nmdb » commit 1bd1299

Implement passive mode.

author Alberto Bertogli
2006-09-13 04:00:14 UTC
committer Alberto Bertogli
2006-09-13 04:00:14 UTC
parent fd12c478ea9621acf25fa029ae130c9f98e0c1b0

Implement passive mode.
Taken from the updated manpage:

"Enable passive mode, where the server never replies requests, but acts upon
them. It's useful when you want to have a live database mirror, in case the
main node fails. It still requires a manual restart to became active, but at
least you have a recent up-to-date database. Be aware that it's not widely
tested (although it should work fine), and do not start an active and a
passive server in the same machine (it misbehaves under some circumnstances,
and doesn't make much sense anyway)."

nmdb/common.h +1 -0
nmdb/main.c +7 -1
nmdb/nmdb.1 +11 -1
nmdb/tipc.c +13 -0

diff --git a/nmdb/common.h b/nmdb/common.h
index 6aadc70..81a220d 100644
--- a/nmdb/common.h
+++ b/nmdb/common.h
@@ -18,6 +18,7 @@ struct  {
 	int tipc_upper;
 	int numobjs;
 	int foreground;
+	int passive;
 	char *dbname;
 } settings;
 
diff --git a/nmdb/main.c b/nmdb/main.c
index d0b3688..eae7f3a 100644
--- a/nmdb/main.c
+++ b/nmdb/main.c
@@ -23,6 +23,7 @@ static void help() {
 	  "  -L upper	upper TIPC port number (= lower)\n"
 	  "  -c nobj	max. number of objects to be cached, in thousands (128)\n"
 	  "  -f		don't fork and stay in the foreground\n"
+	  "  -p		enable passive mode, for redundancy purposes (read docs.)\n"
 	  "  -h		show this help\n"
 	  "\n"
 	  "Please report bugs to Alberto Bertogli (albertito@gmail.com)\n"
@@ -39,13 +40,15 @@ static int load_settings(int argc, char **argv)
 	settings.tipc_upper = -1;
 	settings.numobjs = -1;
 	settings.foreground = 0;
+	settings.passive = 0;
 
 	settings.dbname = malloc(strlen(DEFDBNAME) + 1);
 	strcpy(settings.dbname, DEFDBNAME);
 
-	while ((c = getopt(argc, argv, "d:l:L:c:fh?")) != -1) {
+	while ((c = getopt(argc, argv, "d:l:L:c:fph?")) != -1) {
 		switch(c) {
 		case 'd':
+			free(settings.dbname);
 			settings.dbname = malloc(strlen(optarg) + 1);
 			strcpy(settings.dbname, optarg);
 			break;
@@ -61,6 +64,9 @@ static int load_settings(int argc, char **argv)
 		case 'f':
 			settings.foreground = 1;
 			break;
+		case 'p':
+			settings.passive = 1;
+			break;
 		case 'h':
 		case '?':
 			help();
diff --git a/nmdb/nmdb.1 b/nmdb/nmdb.1
index 95eded3..d08455d 100644
--- a/nmdb/nmdb.1
+++ b/nmdb/nmdb.1
@@ -2,7 +2,7 @@
 .SH NAME
 nmdb - A TIPC-based database manager
 .SH SYNOPSYS
-nmdb [-d dbpath] [-l lower] [-L upper] [-c nobj] [-f] [-h]
+nmdb [-d dbpath] [-l lower] [-L upper] [-c nobj] [-f] [-p] [-h]
 .SH DESCRIPTION
 
 nmdb is a TIPC-based database manager.
@@ -58,6 +58,16 @@ hold 128 thousand objects.
 Stay in the foreground, don't fork. Useful for debugging. The default is to
 fork.
 .TP
+.B "-p"
+Enable passive mode, where the server never replies requests, but acts upon
+them. It's useful when you want to have a live database mirror, in case the
+main node fails. It still requires a manual restart to became active, but at
+least you have a recent up-to-date database. Be aware that it's not widely
+tested (although it should work fine), and do
+.I not
+start an active and a passive server in the same machine (it misbehaves under
+some circumnstances, and doesn't make much sense anyway).
+.TP
 .B "-h"
 Show a brief help.
 
diff --git a/nmdb/tipc.c b/nmdb/tipc.c
index 91feab0..1148e48 100644
--- a/nmdb/tipc.c
+++ b/nmdb/tipc.c
@@ -30,6 +30,9 @@ static void rep_send_error(const struct req_info *req, const unsigned int code)
 	int r, c;
 	unsigned char minibuf[3 * 4];
 
+	if (settings.passive)
+		return;
+
 	/* Network format: ID (4), REP_ERR (4), error code (4) */
 	r = htonl(REP_ERR);
 	c = htonl(code);
@@ -51,6 +54,10 @@ static int rep_send(const struct req_info *req, const unsigned char *buf,
 		const size_t size)
 {
 	int rv;
+
+	if (settings.passive)
+		return 1;
+
 	rv = sendto(req->fd, buf, size, 0,
 			(struct sockaddr *) req->clisa, req->clilen);
 	if (rv < 0) {
@@ -68,6 +75,9 @@ static void mini_reply(struct req_info *req, uint32_t reply)
 	 * malloc() overhead. */
 	unsigned char minibuf[8];
 
+	if (settings.passive)
+		return;
+
 	reply = htonl(reply);
 	memcpy(minibuf, &(req->id), 4);
 	memcpy(minibuf + 4, &reply, 4);
@@ -347,6 +357,9 @@ static void parse_get(struct req_info *req, int impact_db)
 	unsigned char *val = NULL;
 	size_t vsize = 0;
 
+	if (settings.passive)
+		return;
+
 	ksize = * (uint32_t *) req->payload;
 	ksize = ntohl(ksize);
 	if (req->psize < ksize) {