[Sheepdog] [PATCH 3/3] sheep: use do_process_work() in io request

Liu Yuan namei.unix at gmail.com
Sun Nov 20 12:11:27 CET 2011


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

Since we already have a low level framework to handle requests, let's use
it to handle io requests too.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/ops.c        |    4 +++
 sheep/sheep_priv.h |    5 ++++
 sheep/store.c      |   63 ++++++++++++++++++++++++---------------------------
 3 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/sheep/ops.c b/sheep/ops.c
index 9287c71..fd836c1 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -476,18 +476,22 @@ static struct sd_op_template sd_ops[] = {
 	/* I/O operations */
 	[SD_OP_CREATE_AND_WRITE_OBJ] = {
 		.type = SD_OP_TYPE_IO,
+		.process_work = store_create_and_write_obj,
 	},
 
 	[SD_OP_READ_OBJ] = {
 		.type = SD_OP_TYPE_IO,
+		.process_work = store_read_obj,
 	},
 
 	[SD_OP_WRITE_OBJ] = {
 		.type = SD_OP_TYPE_IO,
+		.process_work = store_write_obj,
 	},
 
 	[SD_OP_REMOVE_OBJ] = {
 		.type = SD_OP_TYPE_IO,
+		.process_work = store_remove_obj,
 	},
 };
 
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 2cef322..ae6dfd7 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -234,6 +234,11 @@ int get_cluster_copies(uint8_t *copies);
 int set_cluster_flags(uint16_t flags);
 int get_cluster_flags(uint16_t *flags);
 
+extern int store_create_and_write_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data);
+extern int store_write_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data);
+extern int store_read_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data);
+extern int store_remove_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data);
+
 #define NR_GW_WORKER_THREAD 4
 #define NR_IO_WORKER_THREAD 4
 
diff --git a/sheep/store.c b/sheep/store.c
index 1b23eab..82bc039 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -504,6 +504,7 @@ int write_object_local(uint64_t oid, char *data, unsigned int datalen,
 	hdr->offset = offset;
 	hdr->data_length = datalen;
 	req->data = data;
+	req->op = get_sd_op(hdr->opcode);
 
 	ret = do_local_rw(req, epoch);
 
@@ -534,6 +535,7 @@ int read_object_local(uint64_t oid, char *data, unsigned int datalen,
 	hdr->offset = offset;
 	hdr->data_length = datalen;
 	req->data = data;
+	req->op = get_sd_op(hdr->opcode);
 
 	ret = do_local_rw(req, epoch);
 
@@ -543,9 +545,10 @@ int read_object_local(uint64_t oid, char *data, unsigned int datalen,
 	return ret;
 }
 
-static int store_remove_obj(struct request *req, uint32_t epoch)
+int store_remove_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data)
 {
-	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
+	struct sd_obj_req *hdr = (struct sd_obj_req *)req;
+	uint32_t epoch = hdr->epoch;
 	char path[1024];
 
 	snprintf(path, sizeof(path), "%s%08u/%016" PRIx64, obj_path,
@@ -561,11 +564,13 @@ static int store_remove_obj(struct request *req, uint32_t epoch)
 	return SD_RES_SUCCESS;
 }
 
-static int store_read_obj(struct request *req, uint32_t epoch)
+int store_read_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data)
 {
-	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
-	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&req->rp;
+	struct sd_obj_req *hdr = (struct sd_obj_req *)req;
+	struct sd_obj_rsp *rsps = (struct sd_obj_rsp *)rsp;
+	struct request *request = (struct request *)data;
 	int ret;
+	uint32_t epoch = hdr->epoch;
 	struct siocb iocb;
 
 	memset(&iocb, 0, sizeof(iocb));
@@ -575,28 +580,28 @@ static int store_read_obj(struct request *req, uint32_t epoch)
 	if (ret != SD_RES_SUCCESS)
 		return ret;
 
-	iocb.buf = req->data;
+	iocb.buf = request->data;
 	iocb.length = hdr->data_length;
 	iocb.offset = hdr->offset;
 	ret = store.read(hdr->oid, &iocb);
 	if (ret != SD_RES_SUCCESS)
 		goto out;
 
-	rsp->data_length = hdr->data_length;
-	rsp->copies = sys->nr_sobjs;
+	rsps->data_length = hdr->data_length;
+	rsps->copies = sys->nr_sobjs;
 out:
 	store.close(hdr->oid, &iocb);
 	return ret;
 }
 
-static int do_write_obj(struct siocb *iocb, struct request *req, uint32_t epoch)
+static int do_write_obj(struct siocb *iocb, struct sd_obj_req *req, uint32_t epoch, void *data)
 {
-	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
+	struct sd_obj_req *hdr = (struct sd_obj_req *)req;
 	uint64_t oid = hdr->oid;
 	int ret = SD_RES_SUCCESS;
 	void *jd = NULL;
 
-	iocb->buf = req->data;
+	iocb->buf = data;
 	iocb->length = hdr->data_length;
 	iocb->offset = hdr->offset;
 	if (is_vdi_obj(oid)) {
@@ -604,7 +609,7 @@ static int do_write_obj(struct siocb *iocb, struct request *req, uint32_t epoch)
 
 		snprintf(path, sizeof(path), "%s%08u/%016" PRIx64, obj_path,
 			 epoch, oid);
-		jd = jrnl_begin(req->data, hdr->data_length,
+		jd = jrnl_begin(data, hdr->data_length,
 				   hdr->offset, path, jrnl_path);
 		if (!jd)
 			return SD_RES_EIO;
@@ -616,10 +621,12 @@ static int do_write_obj(struct siocb *iocb, struct request *req, uint32_t epoch)
 	return ret;
 }
 
-static int store_write_obj(struct request *req, uint32_t epoch)
+int store_write_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data)
 {
-	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
+	struct sd_obj_req *hdr = (struct sd_obj_req *)req;
+	struct request *request = (struct request *)data;
 	int ret;
+	uint32_t epoch = hdr->epoch;
 	struct siocb iocb;
 
 	memset(&iocb, 0, sizeof(iocb));
@@ -629,16 +636,18 @@ static int store_write_obj(struct request *req, uint32_t epoch)
 	if (ret != SD_RES_SUCCESS)
 		return ret;
 
-	ret = do_write_obj(&iocb, req, epoch);
+	ret = do_write_obj(&iocb, hdr, epoch, request->data);
 
 	store.close(hdr->oid, &iocb);
 	return ret;
 }
 
-static int store_create_and_write_obj(struct request *req, uint32_t epoch)
+int store_create_and_write_obj(const struct sd_req *req, struct sd_rsp *rsp, void *data)
 {
-	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
+	struct sd_obj_req *hdr = (struct sd_obj_req *)req;
+	struct request *request = (struct request *)data;
 	int ret;
+	uint32_t epoch = hdr->epoch;
 	char *buf = NULL;
 	struct siocb iocb;
 
@@ -657,7 +666,7 @@ static int store_create_and_write_obj(struct request *req, uint32_t epoch)
 		dprintf("%" PRIu64 ", %" PRIx64 "\n", hdr->oid, hdr->cow_oid);
 
 		buf = xzalloc(SD_DATA_OBJ_SIZE);
-		ret = read_copy_from_cluster(req, hdr->epoch, hdr->cow_oid, buf,
+		ret = read_copy_from_cluster(request, hdr->epoch, hdr->cow_oid, buf,
 				hdr->copies);
 		if (ret != SD_RES_SUCCESS) {
 			eprintf("failed to read cow object\n");
@@ -671,7 +680,7 @@ static int store_create_and_write_obj(struct request *req, uint32_t epoch)
 			goto out;
 		}
 	}
-	ret = do_write_obj(&iocb, req, epoch);
+	ret = do_write_obj(&iocb, hdr, epoch, request->data);
 out:
 	free(buf);
 	store.close(hdr->oid, &iocb);
@@ -683,22 +692,10 @@ static int do_local_rw(struct request *req, uint32_t epoch)
 	struct sd_obj_req *hdr = (struct sd_obj_req *)&req->rq;
 	int ret = SD_RES_SUCCESS;
 
+	hdr->epoch = epoch;
 	dprintf("%x, %" PRIx64" , %u\n", hdr->opcode, hdr->oid, epoch);
 
-	switch (hdr->opcode) {
-	case SD_OP_WRITE_OBJ:
-		ret = store_write_obj(req, epoch);
-		break;
-	case SD_OP_REMOVE_OBJ:
-		ret = store_remove_obj(req, epoch);
-		break;
-	case SD_OP_READ_OBJ:
-		ret = store_read_obj(req, epoch);
-		break;
-	case SD_OP_CREATE_AND_WRITE_OBJ:
-		ret = store_create_and_write_obj(req, epoch);
-		break;
-	}
+	ret = do_process_work(req->op, &req->rq, &req->rp, req);
 
 	if (ret == SD_RES_NO_OBJ && hdr->flags & SD_FLAG_CMD_RECOVERY) {
 		struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&req->rp;
-- 
1.7.8.rc3




More information about the sheepdog mailing list