[sheepdog] [PATCH 3/3] gateway: refactor write and remove functions
Liu Yuan
namei.unix at gmail.com
Sun Jul 8 07:02:39 CEST 2012
From: Liu Yuan <tailai.ly at taobao.com>
They share almost the same logic, so let's extract the core out and wrap it a
stand-alone function.
- add sheep_do_op_work()
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
sheep/gateway.c | 111 +++++++++++++---------------------------------------
sheep/ops.c | 5 +++
sheep/sheep_priv.h | 1 +
3 files changed, 34 insertions(+), 83 deletions(-)
diff --git a/sheep/gateway.c b/sheep/gateway.c
index b55df6e..ccd8e9b 100644
--- a/sheep/gateway.c
+++ b/sheep/gateway.c
@@ -216,33 +216,22 @@ write_info_advance(struct write_info *wi, struct sd_vnode *v,
wi->nr_sent++;
}
-static int do_gateway_write_obj(struct request *req, bool create)
+static int do_request(struct request *req, struct sd_req *hdr)
{
int i, err_ret = SD_RES_SUCCESS, ret, local = -1;
unsigned wlen;
- struct sd_req fwd_hdr;
struct sd_rsp *rsp = (struct sd_rsp *)&req->rp;
struct sd_vnode *v;
struct sd_vnode *obj_vnodes[SD_MAX_COPIES];
uint64_t oid = req->rq.obj.oid;
int nr_copies;
struct write_info wi;
+ struct sd_op_template *op = get_sd_op(hdr->opcode);
dprintf("%"PRIx64"\n", oid);
- if (sys->enable_write_cache && !req->local && !bypass_object_cache(req))
- return object_cache_handle_request(req);
-
write_info_init(&wi);
- memcpy(&fwd_hdr, &req->rq, sizeof(fwd_hdr));
- if (create)
- fwd_hdr.opcode = SD_OP_CREATE_AND_WRITE_PEER;
- else
- fwd_hdr.opcode = SD_OP_WRITE_PEER;
- fwd_hdr.proto_ver = SD_SHEEP_PROTO_VER;
-
- wlen = fwd_hdr.data_length;
-
+ wlen = hdr->data_length;
nr_copies = get_nr_copies(req->vnodes);
oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
@@ -261,7 +250,7 @@ static int do_gateway_write_obj(struct request *req, bool create)
break;
}
- ret = send_req(sfd->fd, &fwd_hdr, req->data, &wlen);
+ ret = send_req(sfd->fd, hdr, req->data, &wlen);
if (ret) {
sheep_del_sockfd(&v->nid, sfd);
err_ret = SD_RES_NETWORK_ERROR;
@@ -274,10 +263,8 @@ static int do_gateway_write_obj(struct request *req, bool create)
if (local != -1 && err_ret == SD_RES_SUCCESS) {
v = obj_vnodes[local];
- if (create)
- ret = peer_create_and_write_obj(req);
- else
- ret = peer_write_obj(req);
+ assert(op);
+ ret = sheep_do_op_work(op, req);
if (ret != SD_RES_SUCCESS) {
eprintf("fail to write local %"PRIx32"\n", ret);
@@ -295,6 +282,23 @@ static int do_gateway_write_obj(struct request *req, bool create)
return err_ret;
}
+static int do_gateway_write_obj(struct request *req, bool create)
+{
+ struct sd_req hdr;
+
+ if (sys->enable_write_cache && !req->local && !bypass_object_cache(req))
+ return object_cache_handle_request(req);
+
+ memcpy(&hdr, &req->rq, sizeof(hdr));
+ if (create)
+ hdr.opcode = SD_OP_CREATE_AND_WRITE_PEER;
+ else
+ hdr.opcode = SD_OP_WRITE_PEER;
+ hdr.proto_ver = SD_SHEEP_PROTO_VER;
+
+ return do_request(req, &hdr);
+}
+
int gateway_write_obj(struct request *req)
{
return do_gateway_write_obj(req, false);
@@ -307,70 +311,11 @@ int gateway_create_and_write_obj(struct request *req)
int gateway_remove_obj(struct request *req)
{
- int i, err_ret = SD_RES_SUCCESS, ret, local = -1;
- unsigned wlen;
- struct sd_req fwd_hdr;
- struct sd_rsp *rsp = (struct sd_rsp *)&req->rp;
- struct sd_vnode *v;
- struct sd_vnode *obj_vnodes[SD_MAX_COPIES];
- uint64_t oid = req->rq.obj.oid;
- int nr_copies;
- struct write_info wi;
-
- dprintf("%"PRIx64"\n", oid);
-
- write_info_init(&wi);
- memcpy(&fwd_hdr, &req->rq, sizeof(fwd_hdr));
- fwd_hdr.opcode = SD_OP_REMOVE_PEER;
- fwd_hdr.proto_ver = SD_SHEEP_PROTO_VER;
-
- wlen = fwd_hdr.data_length;
-
- nr_copies = get_nr_copies(req->vnodes);
- oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
-
- for (i = 0; i < nr_copies; i++) {
- struct sockfd *sfd;
-
- v = obj_vnodes[i];
- if (vnode_is_local(v)) {
- local = i;
- continue;
- }
+ struct sd_req hdr;
- sfd = sheep_get_sockfd(&v->nid);
- if (!sfd) {
- err_ret = SD_RES_NETWORK_ERROR;
- break;
- }
+ memcpy(&hdr, &req->rq, sizeof(hdr));
+ hdr.opcode = SD_OP_REMOVE_PEER;
+ hdr.proto_ver = SD_SHEEP_PROTO_VER;
- ret = send_req(sfd->fd, &fwd_hdr, req->data, &wlen);
- if (ret) {
- sheep_del_sockfd(&v->nid, sfd);
- err_ret = SD_RES_NETWORK_ERROR;
- dprintf("fail %d\n", ret);
- break;
- }
- write_info_advance(&wi, v, sfd);
- }
-
- if (local != -1 && err_ret == SD_RES_SUCCESS) {
- v = obj_vnodes[local];
-
- ret = peer_remove_obj(req);
-
- if (ret != SD_RES_SUCCESS) {
- eprintf("fail to write local %"PRIx32"\n", ret);
- err_ret = ret;
- }
- }
-
- dprintf("nr_sent %d, err %x\n", wi.nr_sent, err_ret);
- if (wi.nr_sent > 0) {
- ret = wait_forward_write(&wi, rsp);
- if (ret != SD_RES_SUCCESS)
- err_ret = ret;
- }
-
- return err_ret;
+ return do_request(req, &hdr);
}
diff --git a/sheep/ops.c b/sheep/ops.c
index 54cb346..f21ae69 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -797,3 +797,8 @@ int do_process_main(struct sd_op_template *op, const struct sd_req *req,
{
return op->process_main(req, rsp, data);
}
+
+int sheep_do_op_work(struct sd_op_template *op, struct request *req)
+{
+ return op->process_work(req);
+}
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index afbc361..9b036b5 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -294,6 +294,7 @@ int has_process_main(struct sd_op_template *op);
void do_process_work(struct work *work);
int do_process_main(struct sd_op_template *op, const struct sd_req *req,
struct sd_rsp *rsp, void *data);
+int sheep_do_op_work(struct sd_op_template *op, struct request *req);
/* Journal */
struct jrnl_descriptor *jrnl_begin(const void *buf, size_t count, off_t offset,
--
1.7.10.2
More information about the sheepdog
mailing list