[sheepdog] [PATCH v4 12/18] collie/farm: impelement trunk object
Kai Zhang
kyle at zelin.io
Fri May 17 08:27:20 CEST 2013
moved trunk.c from sheep/farm to collie/farm
Changes:
1. trunk don't need to read file from plain_store.
2. farm.c will responsible for read objects and fill trunk entry.
3. trunk.c now just handles read/write trunk file.
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
collie/Makefile.am | 1 +
collie/farm/farm.h | 12 +++++++
collie/farm/trunk.c | 92 ++++++++-------------------------------------------
3 files changed, 27 insertions(+), 78 deletions(-)
diff --git a/collie/Makefile.am b/collie/Makefile.am
index a68fc00..5943cd9 100644
--- a/collie/Makefile.am
+++ b/collie/Makefile.am
@@ -24,6 +24,7 @@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
sbin_PROGRAMS = collie
collie_SOURCES = farm/object_rb_tree.c farm/sha1_file.c farm/snap.c \
+ farm/trunk.c \
collie.c common.c treeview.c vdi.c node.c cluster.c
if BUILD_TRACE
diff --git a/collie/farm/farm.h b/collie/farm/farm.h
index f81bf0b..e08d3e8 100644
--- a/collie/farm/farm.h
+++ b/collie/farm/farm.h
@@ -30,6 +30,13 @@ enum obj_type {
VDI,
};
+struct trunk_entry {
+ uint64_t oid;
+ enum obj_type type;
+ int nr_copies;
+ unsigned char sha1[SHA1_LEN];
+};
+
struct sha1_file_hdr {
char tag[TAG_LEN];
uint64_t size;
@@ -45,6 +52,11 @@ static inline char *get_object_directory(void)
return farm_obj_dir;
}
+/* trunk.c */
+int trunk_init(void);
+int trunk_file_write(unsigned char *trunk_sha1, struct strbuf *trunk_entries);
+void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *);
+
/* snap.c */
int snap_init(const char *path);
void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr);
diff --git a/collie/farm/trunk.c b/collie/farm/trunk.c
index eaa4193..63f40c1 100644
--- a/collie/farm/trunk.c
+++ b/collie/farm/trunk.c
@@ -1,7 +1,9 @@
/*
* Copyright (C) 2011 Taobao Inc.
+ * Copyright (C) 2013 Zelin.io
*
* Liu Yuan <namei.unix at gmail.com>
+ * Kai Zhang <kyle at zelin.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
@@ -25,80 +27,16 @@
#include "list.h"
#include "util.h"
#include "sheepdog_proto.h"
-#include "sheep_priv.h"
-static int fill_entry_new_sha1(struct trunk_entry *entry)
-{
- struct strbuf buf = STRBUF_INIT;
- int fd, ret = 0;
- struct sha1_file_hdr hdr = { .priv = 0 };
-
- memcpy(hdr.tag, TAG_DATA, TAG_LEN);
- strbuf_addstr(&buf, get_object_path(entry->oid));
- strbuf_addf(&buf, "/%016" PRIx64, entry->oid);
- fd = open(buf.buf, O_RDONLY);
- if (fd < 0) {
- sd_dprintf("%m, %s", buf.buf);
- ret = -1;
- goto out;
- }
- strbuf_reset(&buf);
- if (!strbuf_read(&buf, fd, SD_DATA_OBJ_SIZE) == SD_DATA_OBJ_SIZE) {
- sd_dprintf("strbuf_read fail to read full");
- ret = -1;
- goto out_close;
- }
- hdr.size = buf.len;
- strbuf_insert(&buf, 0, &hdr, sizeof(hdr));
-
- if (sha1_file_write((void *)buf.buf, buf.len, entry->sha1) < 0) {
- ret = -1;
- goto out_close;
- }
- sd_dprintf("data sha1:%s, %"PRIx64, sha1_to_hex(entry->sha1),
- entry->oid);
-out_close:
- close(fd);
-out:
- strbuf_release(&buf);
- return ret;
-}
-
-static int inc_object_nr(uint64_t oid, char *wd, void *arg)
-{
- uint64_t *object_nr = arg;
-
- (*object_nr)++;
-
- return SD_RES_SUCCESS;
-}
-
-static int init_trunk_entry(uint64_t oid, char *path, void *arg)
-{
- struct trunk_entry entry = {};
- struct strbuf *buf = arg;
-
- entry.oid = oid;
- if (fill_entry_new_sha1(&entry) < 0)
- return SD_RES_UNKNOWN;
-
- strbuf_add(buf, &entry, sizeof(struct trunk_entry));
- return SD_RES_SUCCESS;
-}
-
-int trunk_file_write(unsigned char *outsha1)
+int trunk_file_write(unsigned char *trunk_sha1, struct strbuf *trunk_entries)
{
struct strbuf buf;
struct sha1_file_hdr hdr = {};
uint64_t data_size, object_nr = 0;
- int ret = 0;
+ int ret = -1;
- /* Add the hdr first */
- for_each_object_in_wd(inc_object_nr, false, &object_nr);
- if (ret != SD_RES_SUCCESS) {
- ret = -1;
- goto out;
- }
+ /* Init trunk hdr */
+ object_nr = get_obj_nr();
data_size = sizeof(struct trunk_entry) * object_nr;
hdr.size = data_size;
hdr.priv = object_nr;
@@ -106,17 +44,15 @@ int trunk_file_write(unsigned char *outsha1)
strbuf_init(&buf, sizeof(hdr) + data_size);
strbuf_add(&buf, &hdr, sizeof(hdr));
- ret = for_each_object_in_wd(init_trunk_entry, false, &buf);
- if (ret != SD_RES_SUCCESS) {
- ret = -1;
- goto out;
- }
+ /* trunk entries */
+ strbuf_addbuf(&buf, trunk_entries);
- if (sha1_file_write((void *)buf.buf, buf.len, outsha1) < 0) {
- ret = -1;
+ /* write to sha1 file */
+ if (sha1_file_write((void *)buf.buf, buf.len, trunk_sha1) < 0)
goto out;
- }
- sd_dprintf("trunk sha1: %s", sha1_to_hex(outsha1));
+
+ sd_dprintf("wrote trunk sha1: %s", sha1_to_hex(trunk_sha1));
+ ret = 0;
out:
strbuf_release(&buf);
return ret;
@@ -126,7 +62,7 @@ void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr)
{
void *buffer;
- sd_dprintf("%s", sha1_to_hex(sha1));
+ sd_dprintf("reading trunk sha1: %s", sha1_to_hex(sha1));
buffer = sha1_file_read(sha1, outhdr);
if (!buffer)
return NULL;
--
1.7.1
More information about the sheepdog
mailing list