[Sheepdog] [PATCH] store nodeid persistently

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Sun Mar 28 17:17:33 CEST 2010


This can be applied to the top of the patch "[PATCH] get ip address
from corosync".

=
From: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
Date: Mon, 29 Mar 2010 00:06:36 +0900
Subject: [PATCH] store nodeid persistently

collie calculates the nodeid every time from the ip address it boots
up. This patch stores the nodeid persistently instead.

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
---
 collie/collie.h |    2 +
 collie/group.c  |   21 +++++++++------
 collie/store.c  |   74 +++++++++++++++++++++++++++++++-----------------------
 3 files changed, 57 insertions(+), 40 deletions(-)

diff --git a/collie/collie.h b/collie/collie.h
index f3f3425..2b3420f 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -109,6 +109,8 @@ int update_epoch_store(uint32_t epoch);
 
 int set_global_nr_copies(uint32_t copies);
 int get_global_nr_copies(uint32_t *copies);
+int set_nodeid(uint64_t nodeid);
+int get_nodeid(uint64_t *nodeid);
 
 #define DATA_OBJ_NR_WORKER_THREAD 4
 extern struct work_queue *dobj_queue;
diff --git a/collie/group.c b/collie/group.c
index e82a009..dfcde9a 100644
--- a/collie/group.c
+++ b/collie/group.c
@@ -670,6 +670,7 @@ static void vdi_op_done(struct vdi_op_message *msg)
 			eprintf("can't write epoch %u\n", sys->epoch);
 		update_epoch_store(sys->epoch);
 
+		set_nodeid(sys->this_node.id);
 		set_global_nr_copies(sys->nr_sobjs);
 
 		sys->status = SD_STATUS_OK;
@@ -1036,8 +1037,6 @@ int create_cluster(int port)
 	struct cpg_name group = { 8, "sheepdog" };
 	cpg_callbacks_t cb = { &sd_deliver, &sd_confch };
 	unsigned int nodeid = 0;
-	uint64_t hval;
-	int i;
 
 	ret = cpg_initialize(&cpg_handle, &cb);
 	if (ret != CS_OK) {
@@ -1077,12 +1076,18 @@ join_retry:
 	set_addr(nodeid);
 	sys->this_node.port = port;
 
-	hval = fnv_64a_buf(&sys->this_node.port, sizeof(sys->this_node.port),
-			   FNV1A_64_INIT);
-	for (i = ARRAY_SIZE(sys->this_node.addr) - 1; i >= 0; i--)
-		hval = fnv_64a_buf(&sys->this_node.addr[i], 1, hval);
-
-	sys->this_node.id = hval;
+	ret = get_nodeid(&sys->this_node.id);
+	if (!sys->this_node.id) {
+		uint64_t hval;
+		int i;
+
+		hval = fnv_64a_buf(&sys->this_node.port,
+				   sizeof(sys->this_node.port),
+				   FNV1A_64_INIT);
+		for (i = ARRAY_SIZE(sys->this_node.addr) - 1; i >= 0; i--)
+			hval = fnv_64a_buf(&sys->this_node.addr[i], 1, hval);
+		sys->this_node.id = hval;
+	}
 
 	sys->synchronized = 0;
 	sys->status = SD_STATUS_STARTUP;
diff --git a/collie/store.c b/collie/store.c
index 5c870b6..75ac12e 100644
--- a/collie/store.c
+++ b/collie/store.c
@@ -23,6 +23,7 @@
 
 #define ANAME_CTIME "user.sheepdog.ctime"
 #define ANAME_COPIES "user.sheepdog.copies"
+#define ANAME_NODEID "user.sheepdog.nodeid"
 
 static char *obj_path;
 static char *epoch_path;
@@ -1101,6 +1102,45 @@ again:
 	return 0;
 }
 
+
+static int attr(char *path, char *attr, void *var, int len, int set)
+{
+	int ret, fd;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return SD_RES_EIO;
+
+	if (set)
+		ret = fsetxattr(fd, attr, var, len, 0);
+	else
+		ret = fgetxattr(fd, attr, var, len);
+
+	close(fd);
+
+	if (set) {
+		if (ret) {
+			eprintf("use 'user_xattr' option?, %s\n", attr);
+			return SD_RES_SYSTEM_ERROR;
+		}
+	} else {
+		if (ret != len)
+			return SD_RES_SYSTEM_ERROR;
+	}
+
+	return SD_RES_SUCCESS;
+}
+
+int set_nodeid(uint64_t nodeid)
+{
+	return attr(epoch_path, ANAME_NODEID, &nodeid, sizeof(nodeid), 1);
+}
+
+int get_nodeid(uint64_t *nodeid)
+{
+	return attr(epoch_path, ANAME_NODEID, nodeid, sizeof(*nodeid), 0);
+}
+
 static int init_base_path(char *d, int *new)
 {
 	return init_path(d, new);
@@ -1269,42 +1309,12 @@ void epoch_queue_request(struct work *work, int idx)
 	}
 }
 
-static int global_nr_copies(uint32_t *copies, int set)
-{
-	int ret, fd;
-
-	fd = open(epoch_path, O_RDONLY);
-	if (fd < 0)
-		return SD_RES_EIO;
-
-	if (set)
-		ret = fsetxattr(fd, ANAME_COPIES, copies, sizeof(*copies), 0);
-	else
-		ret = fgetxattr(fd, ANAME_COPIES, copies, sizeof(*copies));
-
-	close(fd);
-
-	if (set) {
-		if (ret) {
-			eprintf("use 'user_xattr' option?\n");
-			return SD_RES_SYSTEM_ERROR;
-		}
-	} else {
-		if (ret != sizeof(*copies)) {
-			eprintf("use 'user_xattr' option?\n");
-			return SD_RES_SYSTEM_ERROR;
-		}
-	}
-
-	return SD_RES_SUCCESS;
-}
-
 int set_global_nr_copies(uint32_t copies)
 {
-	return global_nr_copies(&copies, 1);
+	return attr(epoch_path, ANAME_COPIES, &copies, sizeof(copies), 1);
 }
 
 int get_global_nr_copies(uint32_t *copies)
 {
-	return global_nr_copies(copies, 0);
+	return attr(epoch_path, ANAME_COPIES, copies, sizeof(*copies), 0);
 }
-- 
1.6.5




More information about the sheepdog mailing list