[sheepdog] [PATCH v2 3/3] gateway: init forward hrd in gateway_forward_request()

Liu Yuan namei.unix at gmail.com
Tue Jul 10 08:23:02 CEST 2012


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

- add a helper to init hdr
- add a map table to mape gateway opcode to peer opcode

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 include/internal_proto.h |    8 ++++----
 sheep/gateway.c          |   43 ++++++++++++++++++-------------------------
 sheep/ops.c              |   14 ++++++++++++++
 sheep/sheep_priv.h       |    1 +
 4 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/include/internal_proto.h b/include/internal_proto.h
index 583fce1..a523093 100644
--- a/include/internal_proto.h
+++ b/include/internal_proto.h
@@ -54,10 +54,10 @@
 #define SD_OP_FLUSH_DEL_CACHE  0x98
 #define SD_OP_GET_OBJ_LIST   0xA1
 #define SD_OP_GET_EPOCH      0xA2
-#define SD_OP_CREATE_AND_WRITE_PEER 0xa3
-#define SD_OP_READ_PEER      0xa4
-#define SD_OP_WRITE_PEER     0xa5
-#define SD_OP_REMOVE_PEER    0xa6
+#define SD_OP_CREATE_AND_WRITE_PEER 0xA3
+#define SD_OP_READ_PEER      0xA4
+#define SD_OP_WRITE_PEER     0xA5
+#define SD_OP_REMOVE_PEER    0xA6
 
 /* internal flags for hdr.flags, must be above 0x80 */
 #define SD_FLAG_CMD_RECOVERY 0x0080
diff --git a/sheep/gateway.c b/sheep/gateway.c
index 579f81a..ba8d2f5 100644
--- a/sheep/gateway.c
+++ b/sheep/gateway.c
@@ -217,7 +217,14 @@ write_info_advance(struct write_info *wi, struct sd_vnode *v,
 	wi->nr_sent++;
 }
 
-static int gateway_forward_request(struct request *req, struct sd_req *hdr)
+static inline void gateway_init_fwd_hdr(struct sd_req *fwd, struct sd_req *hdr)
+{
+	memcpy(fwd, hdr, sizeof(*fwd));
+	fwd->opcode = gateway_to_peer_opcode(hdr->opcode);
+	fwd->proto_ver = SD_SHEEP_PROTO_VER;
+}
+
+static int gateway_forward_request(struct request *req)
 {
 	int i, err_ret = SD_RES_SUCCESS, ret, local = -1;
 	unsigned wlen;
@@ -227,12 +234,16 @@ static int gateway_forward_request(struct request *req, struct sd_req *hdr)
 	uint64_t oid = req->rq.obj.oid;
 	int nr_copies;
 	struct write_info wi;
-	struct sd_op_template *op = get_sd_op(hdr->opcode);
+	struct sd_op_template *op;
+	struct sd_req hdr;
 
 	dprintf("%"PRIx64"\n", oid);
 
+	gateway_init_fwd_hdr(&hdr, &req->rq);
+	op = get_sd_op(hdr.opcode);
+
 	write_info_init(&wi);
-	wlen = hdr->data_length;
+	wlen = hdr.data_length;
 	nr_copies = get_nr_copies(req->vnodes);
 	oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
 
@@ -251,7 +262,7 @@ static int gateway_forward_request(struct request *req, struct sd_req *hdr)
 			break;
 		}
 
-		ret = send_req(sfd->fd, 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;
@@ -285,39 +296,21 @@ static int gateway_forward_request(struct request *req, struct sd_req *hdr)
 
 int gateway_write_obj(struct request *req)
 {
-	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));
-	hdr.opcode = SD_OP_WRITE_PEER;
-	hdr.proto_ver = SD_SHEEP_PROTO_VER;
-
-	return gateway_forward_request(req, &hdr);
+	return gateway_forward_request(req);
 }
 
 int gateway_create_and_write_obj(struct request *req)
 {
-	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));
-	hdr.opcode = SD_OP_CREATE_AND_WRITE_PEER;
-	hdr.proto_ver = SD_SHEEP_PROTO_VER;
-
-	return gateway_forward_request(req, &hdr);
+	return gateway_forward_request(req);
 }
 
 int gateway_remove_obj(struct request *req)
 {
-	struct sd_req hdr;
-
-	memcpy(&hdr, &req->rq, sizeof(hdr));
-	hdr.opcode = SD_OP_REMOVE_PEER;
-	hdr.proto_ver = SD_SHEEP_PROTO_VER;
-
-	return gateway_forward_request(req, &hdr);
+	return gateway_forward_request(req);
 }
diff --git a/sheep/ops.c b/sheep/ops.c
index 088d4e4..3855a6b 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -25,6 +25,7 @@
 #include "sheep_priv.h"
 #include "strbuf.h"
 #include "trace/trace.h"
+#include "util.h"
 
 enum sd_op_type {
 	SD_OP_TYPE_CLUSTER = 1, /* cluster operations */
@@ -1010,3 +1011,16 @@ int sheep_do_op_work(struct sd_op_template *op, struct request *req)
 {
 	return op->process_work(req);
 }
+
+static int map_table[] = {
+	[SD_OP_CREATE_AND_WRITE_OBJ] = SD_OP_CREATE_AND_WRITE_PEER,
+	[SD_OP_READ_OBJ] = SD_OP_READ_PEER,
+	[SD_OP_WRITE_OBJ] = SD_OP_WRITE_PEER,
+	[SD_OP_REMOVE_OBJ] = SD_OP_REMOVE_PEER,
+};
+
+int gateway_to_peer_opcode(int opcode)
+{
+	assert(opcode < ARRAY_SIZE(map_table));
+	return map_table[opcode];
+}
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 9b036b5..6ec9799 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -295,6 +295,7 @@ 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);
+int gateway_to_peer_opcode(int opcode);
 
 /* 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