[sheepdog] [PATCH 2/3] sheep: access config entries with one function call

MORITA Kazutaka morita.kazutaka at gmail.com
Wed Jul 3 08:49:22 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Currently, we have to use multiple (set|get)_cluster_* functions to
read/write a cluster setting from the config file.  This introduces
set_cluster_config and get_cluster_config to simplify the codes.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 sheep/config.c     |   56 +++++++++++-----------------------------------------
 sheep/group.c      |   43 ++++++++--------------------------------
 sheep/ops.c        |   25 ++---------------------
 sheep/sheep_priv.h |   10 ++--------
 sheep/store.c      |    4 ----
 5 files changed, 24 insertions(+), 114 deletions(-)

diff --git a/sheep/config.c b/sheep/config.c
index 26840e9..da0cd33 100644
--- a/sheep/config.c
+++ b/sheep/config.c
@@ -111,6 +111,7 @@ int init_config_file(void)
 		goto out;
 	}
 	ret = 0;
+	get_cluster_config(&sys->cinfo);
 out:
 	close(fd);
 
@@ -131,57 +132,24 @@ void init_config_path(const char *base_path)
 	snprintf(config_path, len, "%s" CONFIG_PATH, base_path);
 }
 
-int set_cluster_ctime(uint64_t ct)
-{
-	config.ctime = ct;
-
-	return write_config();
-}
-
-uint64_t get_cluster_ctime(void)
-{
-	return config.ctime;
-}
-
-int set_cluster_copies(uint8_t copies)
-{
-	config.copies = copies;
-
-	return write_config();
-}
-
-int get_cluster_copies(uint8_t *copies)
-{
-	*copies = config.copies;
-
-	return SD_RES_SUCCESS;
-}
-
-int set_cluster_flags(uint16_t flags)
-{
-	config.flags = flags;
-
-	return write_config();
-}
-
-int get_cluster_flags(uint16_t *flags)
-{
-	*flags = config.flags;
-
-	return SD_RES_SUCCESS;
-}
-
-int set_cluster_store(const char *name)
+int set_cluster_config(const struct cluster_info *cinfo)
 {
+	config.ctime = cinfo->ctime;
+	config.copies = cinfo->nr_copies;
+	config.flags = cinfo->flags;
 	memset(config.store, 0, sizeof(config.store));
-	pstrcpy((char *)config.store, sizeof(config.store), name);
+	pstrcpy((char *)config.store, sizeof(config.store),
+		(char *)cinfo->store);
 
 	return write_config();
 }
 
-int get_cluster_store(char *buf)
+int get_cluster_config(struct cluster_info *cinfo)
 {
-	memcpy(buf, config.store, sizeof(config.store));
+	cinfo->ctime = config.ctime;
+	cinfo->nr_copies = config.copies;
+	cinfo->flags = config.flags;
+	memcpy(cinfo->store, config.store, sizeof(config.store));
 
 	return SD_RES_SUCCESS;
 }
diff --git a/sheep/group.c b/sheep/group.c
index 4c0cc42..cb87711 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -496,18 +496,12 @@ int epoch_log_read_remote(uint32_t epoch, struct sd_node *nodes, int len,
 
 static int cluster_sanity_check(struct join_message *jm)
 {
-	uint64_t local_ctime = get_cluster_ctime();
 	uint32_t local_epoch = get_latest_epoch();
-	uint8_t local_nr_copies;
 
-	if (get_cluster_copies(&local_nr_copies)) {
-		sd_eprintf("failed to get nr_copies");
-		return CJ_RES_FAIL;
-	}
-
-	if (jm->cinfo.ctime != local_ctime) {
+	if (jm->cinfo.ctime != sys->cinfo.ctime) {
 		sd_eprintf("joining node ctime doesn't match: %"
-			   PRIu64 " vs %" PRIu64, jm->cinfo.ctime, local_ctime);
+			   PRIu64 " vs %" PRIu64, jm->cinfo.ctime,
+			   sys->cinfo.ctime);
 		return CJ_RES_FAIL;
 	}
 
@@ -517,9 +511,9 @@ static int cluster_sanity_check(struct join_message *jm)
 		return CJ_RES_FAIL;
 	}
 
-	if (jm->cinfo.nr_copies != local_nr_copies) {
+	if (jm->cinfo.nr_copies != sys->cinfo.nr_copies) {
 		sd_eprintf("joining node nr_copies doesn't match: %u vs %u",
-			   jm->cinfo.nr_copies, local_nr_copies);
+			   jm->cinfo.nr_copies, sys->cinfo.nr_copies);
 		return CJ_RES_FAIL;
 	}
 
@@ -753,10 +747,6 @@ static void setup_backend_store(const char *store, bool need_purge)
 		ret = sd_store->init();
 		if (ret != SD_RES_SUCCESS)
 			panic("failed to initialize store");
-
-		ret = set_cluster_store(sd_store->name);
-		if (ret != SD_RES_SUCCESS)
-			panic("failed to store into config file");
 	}
 
 	/*
@@ -774,8 +764,6 @@ static void finish_join(const struct join_message *msg,
 			const struct sd_node *joined,
 			const struct sd_node *nodes, size_t nr_nodes)
 {
-	int ret;
-
 	sys->join_finished = true;
 	sys->cinfo.epoch = msg->cinfo.epoch;
 
@@ -783,16 +771,7 @@ static void finish_join(const struct join_message *msg,
 		update_exceptional_node_list(get_latest_epoch(), msg);
 
 	if (msg->cinfo.store[0]) {
-		/*
-		 * We don't need backend for gateway-only node, but need to save
-		 * store name.  Otherwise, the node cannot notify the store name
-		 * when it become master
-		 */
-		if (sys->gateway_only) {
-			ret = set_cluster_store((char *)msg->cinfo.store);
-			if (ret != SD_RES_SUCCESS)
-				panic("failed to store into config file");
-		} else
+		if (!sys->gateway_only)
 			setup_backend_store((char *)msg->cinfo.store,
 					    !!msg->inc_epoch);
 	}
@@ -882,9 +861,7 @@ static void update_cluster_info(const struct join_message *msg,
 			sys->cinfo.nr_copies = msg->cinfo.nr_copies;
 			sys->cinfo.flags = msg->cinfo.flags;
 
-			set_cluster_copies(sys->cinfo.nr_copies);
-			set_cluster_flags(sys->cinfo.flags);
-			set_cluster_ctime(msg->cinfo.ctime);
+			set_cluster_config(&sys->cinfo);
 			/*FALLTHROUGH*/
 		case SD_STATUS_WAIT_FOR_JOIN:
 			sys->cinfo.disable_recovery =
@@ -1014,7 +991,7 @@ enum cluster_join_result sd_check_join_cb(const struct sd_node *joining,
 			return CJ_RES_FAIL;
 
 		sys->cinfo.epoch = epoch;
-		jm->cinfo.ctime = get_cluster_ctime();
+		jm->cinfo.ctime = sys->cinfo.ctime;
 
 		if (nr_entries == 1)
 			jm->cluster_status = SD_STATUS_OK;
@@ -1288,10 +1265,6 @@ int create_cluster(int port, int64_t zone, int nr_vnodes,
 	sys->cinfo.epoch = get_latest_epoch();
 	if (sys->cinfo.epoch) {
 		sys->status = SD_STATUS_WAIT_FOR_JOIN;
-		get_cluster_copies(&sys->cinfo.nr_copies);
-		get_cluster_flags(&sys->cinfo.flags);
-		sys->cinfo.ctime = get_cluster_ctime();
-		get_cluster_store((char *)sys->cinfo.store);
 
 		sys->cinfo.nr_nodes = epoch_log_read(sys->cinfo.epoch,
 						     sys->cinfo.nodes,
diff --git a/sheep/ops.c b/sheep/ops.c
index 86df149..3d92c52 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -255,8 +255,6 @@ static int cluster_make_fs(const struct sd_req *req, struct sd_rsp *rsp,
 	ret = sd_store->format();
 	if (ret != SD_RES_SUCCESS)
 		return ret;
-	if (set_cluster_store(store_name) < 0)
-		return SD_RES_EIO;
 
 	ret = sd_store->init();
 	if (ret != SD_RES_SUCCESS)
@@ -267,10 +265,7 @@ static int cluster_make_fs(const struct sd_req *req, struct sd_rsp *rsp,
 	if (!sys->cinfo.nr_copies)
 		sys->cinfo.nr_copies = SD_DEFAULT_COPIES;
 	sys->cinfo.ctime = req->cluster.ctime;
-
-	set_cluster_ctime(sys->cinfo.ctime);
-	set_cluster_copies(sys->cinfo.nr_copies);
-	set_cluster_flags(sys->cinfo.flags);
+	set_cluster_config(&sys->cinfo);
 
 	for (i = 1; i <= latest_epoch; i++)
 		remove_epoch(i);
@@ -439,7 +434,7 @@ static int local_stat_cluster(struct request *req)
 		log = (struct epoch_log *)req->data + i;
 		memset(log, 0, sizeof(*log));
 		log->epoch = epoch;
-		log->ctime = get_cluster_ctime();
+		log->ctime = sys->cinfo.ctime;
 		nr_nodes = epoch_log_read_with_timestamp(epoch, log->nodes,
 							 sizeof(log->nodes),
 							 (time_t *)&log->time);
@@ -545,8 +540,6 @@ static int cluster_force_recover_main(const struct sd_req *req,
 {
 	struct vnode_info *old_vnode_info, *vnode_info;
 	int ret = SD_RES_SUCCESS;
-	uint8_t c;
-	uint16_t f;
 	struct sd_node *nodes = data;
 	size_t nr_nodes = rsp->data_length / sizeof(*nodes);
 
@@ -555,20 +548,6 @@ static int cluster_force_recover_main(const struct sd_req *req,
 		return SD_RES_FORCE_RECOVER;
 	}
 
-	ret = get_cluster_copies(&c);
-	if (ret) {
-		sd_printf(SDOG_EMERG, "cannot get cluster copies");
-		goto err;
-	}
-	ret = get_cluster_flags(&f);
-	if (ret) {
-		sd_printf(SDOG_EMERG, "cannot get cluster flags");
-		goto err;
-	}
-
-	sys->cinfo.nr_copies = c;
-	sys->cinfo.flags = f;
-
 	sys->cinfo.epoch++; /* some nodes are left, so we get a new epoch */
 	ret = log_current_epoch();
 	if (ret) {
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 804aa1c..d194292 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -271,12 +271,8 @@ int update_epoch_log(uint32_t epoch, struct sd_node *nodes, size_t nr_nodes);
 int log_current_epoch(void);
 
 extern char *config_path;
-int set_cluster_copies(uint8_t copies);
-int get_cluster_copies(uint8_t *copies);
-int set_cluster_flags(uint16_t flags);
-int get_cluster_flags(uint16_t *flags);
-int set_cluster_store(const char *name);
-int get_cluster_store(char *buf);
+int set_cluster_config(const struct cluster_info *cinfo);
+int get_cluster_config(struct cluster_info *cinfo);
 int set_node_space(uint64_t space);
 int get_node_space(uint64_t *space);
 
@@ -291,8 +287,6 @@ int epoch_log_read_remote(uint32_t epoch, struct sd_node *nodes, int len,
 uint32_t get_latest_epoch(void);
 void init_config_path(const char *base_path);
 int init_config_file(void);
-int set_cluster_ctime(uint64_t ctime);
-uint64_t get_cluster_ctime(void);
 int get_obj_list(const struct sd_req *, struct sd_rsp *, void *);
 int objlist_cache_cleanup(uint32_t vid);
 
diff --git a/sheep/store.c b/sheep/store.c
index f233f35..bc9531b 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -268,11 +268,7 @@ static int init_epoch_path(const char *base_path)
 int init_store_driver(bool is_gateway)
 {
 	char driver_name[STORE_LEN], *p;
-	int ret;
 
-	ret = get_cluster_store((char *)sys->cinfo.store);
-	if (ret != SD_RES_SUCCESS)
-		return ret;
 	pstrcpy(driver_name, sizeof(driver_name), (char *)sys->cinfo.store);
 
 	p = memchr(driver_name, '\0', STORE_LEN);
-- 
1.7.9.5




More information about the sheepdog mailing list