[sheepdog] [PATCH] sheep: refactor oid_to_vnode[s] and cleanup obj_to_sheep[s]
Yunkai Zhang
yunkai.me at gmail.com
Sat Aug 4 15:00:17 CEST 2012
From: Yunkai Zhang <qiushu.zyk at taobao.com>
Note:
- this patch based on my previous patch:
"sheep: refactor get_nth_node() and get_vnode_pos()"
------------------------------------------------------- >8
Folds obj_to_sheep[s] into there callers oid_to_vnode[s] so that
every one should use oid_to_vnode[s] to map oid to vnode[s], which will
make code more descriptive.
Refactor the interface of oid_to_vnode[s] so that other caller who without
vnode_info but only have vnodes can reuse these functions.
Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
collie/vdi.c | 30 +++++++-----------------------
include/sheep.h | 39 ++++++++++++++++++++++++++++-----------
sheep/farm/trunk.c | 3 ++-
sheep/gateway.c | 6 ++++--
sheep/group.c | 23 -----------------------
sheep/ops.c | 3 ++-
sheep/recovery.c | 7 +++++--
sheep/request.c | 3 ++-
sheep/sheep_priv.h | 15 ---------------
9 files changed, 50 insertions(+), 79 deletions(-)
diff --git a/collie/vdi.c b/collie/vdi.c
index ad3583f..e4f4f65 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -795,12 +795,12 @@ static int vdi_object(int argc, char **argv)
static int print_obj_epoch(uint64_t oid)
{
- int i, j, fd, ret, idx;
+ int i, j, fd, ret;
struct sd_req hdr;
struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
unsigned rlen, wlen;
struct sd_vnode vnodes[SD_MAX_VNODES];
- int idx_buf[SD_MAX_COPIES];
+ struct sd_vnode *vnode_buf[SD_MAX_COPIES];
struct epoch_log *logs;
int vnodes_nr, nr_logs, log_length;
char host[128];
@@ -842,12 +842,11 @@ again:
printf("\nobj %"PRIx64" locations at epoch %d, copies = %d\n",
oid, logs[i].epoch, logs[i].nr_copies);
printf("---------------------------------------------------\n");
- obj_to_sheeps(vnodes, vnodes_nr, oid,
- logs[i].nr_copies, idx_buf);
+ oid_to_vnodes(vnodes, vnodes_nr, oid, logs[i].nr_copies,
+ vnode_buf);
for (j = 0; j < logs[i].nr_copies; j++) {
- idx = idx_buf[j];
- addr_to_str(host, sizeof(host), vnodes[idx].nid.addr,
- vnodes[idx].nid.port);
+ addr_to_str(host, sizeof(host), vnode_buf[j]->nid.addr,
+ vnode_buf[j]->nid.port);
printf("%s\n", host);
}
}
@@ -1389,20 +1388,6 @@ static void write_object_to(struct sd_vnode *vnode, uint64_t oid, void *buf)
}
}
-static void collie_oid_to_vnodes(struct sd_vnode *vnodes, int vnodes_nr,
- uint64_t oid, int nr_copies,
- struct sd_vnode **ret_vnodes)
-{
- int idx_buf[SD_MAX_COPIES], i, n;
-
- obj_to_sheeps(vnodes, vnodes_nr, oid, nr_copies, idx_buf);
-
- for (i = 0; i < nr_copies; i++) {
- n = idx_buf[i];
- ret_vnodes[i] = &vnodes[n];
- }
-}
-
/*
* Fix consistency of the replica of oid.
*
@@ -1415,8 +1400,7 @@ static void do_check_repair(uint64_t oid, int nr_copies)
void *buf, *buf_cmp;
int i;
- collie_oid_to_vnodes(sd_vnodes, sd_vnodes_nr,
- oid, nr_copies, tgt_vnodes);
+ oid_to_vnodes(sd_vnodes, sd_vnodes_nr, oid, nr_copies, tgt_vnodes);
buf = read_object_from(tgt_vnodes[0], oid);
for (i = 1; i < nr_copies; i++) {
buf_cmp = read_object_from(tgt_vnodes[i], oid);
diff --git a/include/sheep.h b/include/sheep.h
index 1d07b23..b0ad557 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -25,6 +25,17 @@ struct sd_vnode {
uint64_t id;
};
+struct vnode_info {
+ struct sd_vnode vnodes[SD_MAX_VNODES];
+ int nr_vnodes;
+
+ struct sd_node nodes[SD_MAX_NODES];
+ int nr_nodes;
+
+ int nr_zones;
+ int refcnt;
+};
+
#define TRACE_GRAPH_ENTRY 0x01
#define TRACE_GRAPH_RETURN 0x02
@@ -129,23 +140,29 @@ static inline int get_vnode_nth_idx(struct sd_vnode *entries,
return idxs[nth];
}
-static inline int obj_to_sheep(struct sd_vnode *entries, int nr_entries,
- uint64_t oid, int nth_idx)
+static inline struct sd_vnode *oid_to_vnode(struct sd_vnode *entries,
+ int nr_entries, uint64_t oid,
+ int copy_idx)
{
- return get_vnode_nth_idx(entries, nr_entries, oid, nth_idx);
+ int idx = get_vnode_nth_idx(entries, nr_entries, oid, copy_idx);
+
+ return &entries[idx];
}
-static inline void obj_to_sheeps(struct sd_vnode *entries, int nr_entries,
- uint64_t oid, int nr_copies, int *idxs)
+static inline void oid_to_vnodes(struct sd_vnode *entries, int nr_entries,
+ uint64_t oid, int nr_copies,
+ struct sd_vnode **vnodes)
{
- int nr_idxs = 0;
+ int idx, idxs[SD_MAX_COPIES], i;
- idxs[nr_idxs++] = get_vnode_first_idx(entries, nr_entries, oid);
+ idx = get_vnode_first_idx(entries, nr_entries, oid);
+ idxs[0] = idx;
+ vnodes[0] = &entries[idx];
- while (nr_idxs < nr_copies) {
- idxs[nr_idxs] = get_vnode_next_idx(entries, nr_entries,
- idxs, nr_idxs);
- nr_idxs++;
+ for (i = 1; i < nr_copies; i++) {
+ idx = get_vnode_next_idx(entries, nr_entries, idxs, i);
+ idxs[i] = idx;
+ vnodes[i] = &entries[idx];
}
}
diff --git a/sheep/farm/trunk.c b/sheep/farm/trunk.c
index 6201fef..6b5971b 100644
--- a/sheep/farm/trunk.c
+++ b/sheep/farm/trunk.c
@@ -247,7 +247,8 @@ static int oid_stale(uint64_t oid)
vnodes = get_vnode_info();
nr_copies = get_nr_copies(vnodes);
- oid_to_vnodes(vnodes, oid, nr_copies, obj_vnodes);
+ oid_to_vnodes(vnodes->vnodes, vnodes->nr_vnodes, oid,
+ nr_copies, obj_vnodes);
for (i = 0; i < nr_copies; i++) {
v = obj_vnodes[i];
if (vnode_is_local(v)) {
diff --git a/sheep/gateway.c b/sheep/gateway.c
index a1ca9b8..fe7ef2d 100644
--- a/sheep/gateway.c
+++ b/sheep/gateway.c
@@ -36,7 +36,8 @@ int gateway_read_obj(struct request *req)
return object_cache_handle_request(req);
nr_copies = get_nr_copies(req->vnodes);
- oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
+ oid_to_vnodes(req->vnodes->vnodes, req->vnodes->nr_vnodes, oid,
+ nr_copies, obj_vnodes);
for (i = 0; i < nr_copies; i++) {
v = obj_vnodes[i];
if (!vnode_is_local(v))
@@ -245,7 +246,8 @@ static int gateway_forward_request(struct request *req)
write_info_init(&wi);
wlen = hdr.data_length;
nr_copies = get_nr_copies(req->vnodes);
- oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
+ oid_to_vnodes(req->vnodes->vnodes, req->vnodes->nr_vnodes, oid,
+ nr_copies, obj_vnodes);
for (i = 0; i < nr_copies; i++) {
struct sockfd *sfd;
diff --git a/sheep/group.c b/sheep/group.c
index cb86050..acd3f65 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -177,29 +177,6 @@ void put_vnode_info(struct vnode_info *vnode_info)
}
}
-struct sd_vnode *oid_to_vnode(struct vnode_info *vnode_info, uint64_t oid,
- int copy_idx)
-{
- int idx = obj_to_sheep(vnode_info->vnodes, vnode_info->nr_vnodes,
- oid, copy_idx);
-
- return &vnode_info->vnodes[idx];
-}
-
-void oid_to_vnodes(struct vnode_info *vnode_info, uint64_t oid, int nr_copies,
- struct sd_vnode **vnodes)
-{
- int idx_buf[SD_MAX_COPIES], i, n;
-
- obj_to_sheeps(vnode_info->vnodes, vnode_info->nr_vnodes,
- oid, nr_copies, idx_buf);
-
- for (i = 0; i < nr_copies; i++) {
- n = idx_buf[i];
- vnodes[i] = &vnode_info->vnodes[n];
- }
-}
-
struct vnode_info *alloc_vnode_info(struct sd_node *nodes,
size_t nr_nodes)
{
diff --git a/sheep/ops.c b/sheep/ops.c
index f14d36b..861d449 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -643,7 +643,8 @@ static int read_copy_from_replica(struct vnode_info *vnodes, uint32_t epoch,
int rounded_rand, local = -1;
nr_copies = get_nr_copies(vnodes);
- oid_to_vnodes(vnodes, oid, nr_copies, obj_vnodes);
+ oid_to_vnodes(vnodes->vnodes, vnodes->nr_vnodes, oid,
+ nr_copies, obj_vnodes);
/* first try to read from local copy */
for (i = 0; i < nr_copies; i++) {
diff --git a/sheep/recovery.c b/sheep/recovery.c
index dd17edd..080a4b8 100644
--- a/sheep/recovery.c
+++ b/sheep/recovery.c
@@ -174,7 +174,9 @@ again:
/* Let's do a breadth-first search */
nr_copies = get_nr_copies(old);
for (i = 0; i < nr_copies; i++) {
- struct sd_vnode *tgt_vnode = oid_to_vnode(old, oid, i);
+ struct sd_vnode *tgt_vnode = oid_to_vnode(old->vnodes,
+ old->nr_vnodes,
+ oid, i);
if (is_invalid_vnode(tgt_vnode, rw->cur_vnodes->nodes,
rw->cur_vnodes->nr_nodes))
@@ -515,7 +517,8 @@ static void screen_object_list(struct recovery_work *rw,
nr_objs = get_nr_copies(rw->cur_vnodes);
for (i = 0; i < nr_oids; i++) {
- oid_to_vnodes(rw->cur_vnodes, oids[i], nr_objs, vnodes);
+ oid_to_vnodes(rw->cur_vnodes->vnodes, rw->cur_vnodes->nr_vnodes,
+ oids[i], nr_objs, vnodes);
for (j = 0; j < nr_objs; j++) {
if (!vnode_is_local(vnodes[j]))
continue;
diff --git a/sheep/request.c b/sheep/request.c
index 1f0f9a8..a673a63 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -31,7 +31,8 @@ static int is_access_local(struct request *req, uint64_t oid)
int i;
nr_copies = get_nr_copies(req->vnodes);
- oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
+ oid_to_vnodes(req->vnodes->vnodes, req->vnodes->nr_vnodes, oid,
+ nr_copies, obj_vnodes);
for (i = 0; i < nr_copies; i++) {
if (vnode_is_local(obj_vnodes[i]))
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 0c0e588..201d322 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -37,17 +37,6 @@ struct client_info {
int refcnt;
};
-struct vnode_info {
- struct sd_vnode vnodes[SD_MAX_VNODES];
- int nr_vnodes;
-
- struct sd_node nodes[SD_MAX_NODES];
- int nr_nodes;
-
- int nr_zones;
- int refcnt;
-};
-
struct request {
struct sd_req rq;
struct sd_rsp rp;
@@ -228,10 +217,6 @@ struct vnode_info *alloc_vnode_info(struct sd_node *nodes, size_t nr_nodes);
void put_vnode_info(struct vnode_info *vnodes);
struct vnode_info *get_vnode_info_epoch(uint32_t epoch);
-struct sd_vnode *oid_to_vnode(struct vnode_info *vnode_info, uint64_t oid,
- int copy_idx);
-void oid_to_vnodes(struct vnode_info *vnode_info, uint64_t oid, int nr_copies,
- struct sd_vnode **vnodes);
int get_nr_copies(struct vnode_info *vnode_info);
void resume_pending_requests(void);
--
1.7.11.2
More information about the sheepdog
mailing list