[sheepdog] [PATCH v5 6/7] sheep: journaling epoch file write

Liu Yuan namei.unix at gmail.com
Fri Apr 5 12:32:32 CEST 2013


From: Liu Yuan <tailai.ly at taobao.com>

Also bundle two writes into one.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/store.c |   49 ++++++++++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/sheep/store.c b/sheep/store.c
index 5b1e457..eaaa366 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -39,38 +39,45 @@ LIST_HEAD(store_drivers);
 
 int update_epoch_log(uint32_t epoch, struct sd_node *nodes, size_t nr_nodes)
 {
-	int fd, ret, len;
+	int fd, ret, len, nodes_len, flags = O_RDWR | O_CREAT | O_DSYNC;
 	time_t t;
-	char path[PATH_MAX];
+	char path[PATH_MAX], *buf;
 
 	sd_dprintf("update epoch: %d, %zd", epoch, nr_nodes);
 
+	/* Piggyback the epoch creation time for 'collie cluster info' */
+	time(&t);
+	nodes_len = nr_nodes * sizeof(struct sd_node);
+	len = nodes_len + sizeof(time_t);
+	buf = xmalloc(len);
+	memcpy(buf, nodes, nodes_len);
+	memcpy(buf + nodes_len, &t, sizeof(time_t));
+
+	if (uatomic_is_true(&sys->use_journal) &&
+	    journal_write_epoch(buf, len, epoch) != SD_RES_SUCCESS) {
+		sd_eprintf("turn off journaling");
+		uatomic_set_false(&sys->use_journal);
+		sync();
+	}
+
 	snprintf(path, sizeof(path), "%s%08u", epoch_path, epoch);
-	fd = open(path, O_RDWR | O_CREAT | O_DSYNC, def_fmode);
+	fd = open(path, flags, def_fmode);
 	if (fd < 0) {
-		ret = fd;
-		goto err_open;
+		free(buf);
+		return -1;
 	}
 
-	len = nr_nodes * sizeof(struct sd_node);
-	ret = xwrite(fd, (char *)nodes, len);
-	if (ret != len)
-		goto err;
-
-	/* Piggyback the epoch creation time for 'collie cluster info' */
-	time(&t);
-	len = sizeof(t);
-	ret = xwrite(fd, (char *)&t, len);
-	if (ret != len)
+	ret = xwrite(fd, buf, len);
+	if (ret != len) {
+		sd_eprintf("write failed %d, len %d", ret, len);
+		ret = -1;
 		goto err;
-
-	close(fd);
-	return 0;
+	}
+	ret = 0;
 err:
+	free(buf);
 	close(fd);
-err_open:
-	sd_dprintf("%m");
-	return -1;
+	return ret;
 }
 
 static int do_epoch_log_read(uint32_t epoch, struct sd_node *nodes, int len,
-- 
1.7.9.5




More information about the sheepdog mailing list