[sheepdog] [PATCH 2/3] sheep: further refactor functions to use forward_read_obj_req()

Liu Yuan namei.unix at gmail.com
Wed May 30 04:42:37 CEST 2012


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


Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/object_cache.c |   84 +++++++++++-------------------------------
 sheep/ops.c          |  100 ++++++++------------------------------------------
 2 files changed, 37 insertions(+), 147 deletions(-)

diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index 7d2733d..0d4e258 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -446,18 +446,15 @@ out:
 }
 
 /* Fetch the object, cache it in success */
-int object_cache_pull(struct vnode_info *vnode_info, struct object_cache *oc,
-		uint32_t idx)
+int object_cache_pull(struct vnode_info *vnodes, struct object_cache *oc,
+		      uint32_t idx)
 {
-	int i, fd, ret = SD_RES_NO_MEM;
-	unsigned wlen = 0, rlen, data_length, read_len;
+	struct request read_req;
+	struct sd_req *hdr = &read_req.rq;
+	int ret = SD_RES_NO_MEM;
 	uint64_t oid;
-	struct sd_req hdr = { 0 };
-	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-	struct sd_vnode *v;
-	struct sd_vnode *vnodes[SD_MAX_COPIES];
+	uint32_t data_length;
 	void *buf;
-	int nr_copies;
 
 	if (idx & CACHE_VDI_BIT) {
 		oid = vid_to_vdi_oid(oc->vid);
@@ -472,66 +469,27 @@ int object_cache_pull(struct vnode_info *vnode_info, struct object_cache *oc,
 		eprintf("failed to allocate memory\n");
 		goto out;
 	}
+	memset(&read_req, 0, sizeof(read_req));
+	hdr->opcode = SD_OP_READ_OBJ;
+	hdr->data_length = data_length;
+	hdr->epoch = sys_epoch();
 
-	/* Check if we can read locally */
-	nr_copies = get_nr_copies(vnode_info);
-	oid_to_vnodes(vnode_info, oid, nr_copies, vnodes);
-
-	for (i = 0; i < nr_copies; i++) {
-		v = vnodes[i];
-		if (vnode_is_local(v)) {
-			struct siocb iocb = { 0 };
-			iocb.epoch = sys_epoch();
-			iocb.buf = buf;
-			iocb.length = data_length;
-			iocb.offset = 0;
-			ret = sd_store->read(oid, &iocb);
-			if (ret != SD_RES_SUCCESS)
-				goto pull_remote;
-			/* read succeed */
-			read_len = iocb.length;
-			dprintf("[local] %08"PRIx32"\n", idx);
-			goto out;
-		}
-	}
-
-pull_remote:
-	/* Okay, no luck, let's read remotely */
-	for (i = 0; i < nr_copies; i++) {
-		v = vnodes[i];
-
-		if (vnode_is_local(v))
-			continue;
-
-		hdr.opcode = SD_OP_READ_OBJ;
-		hdr.epoch = sys_epoch();
-		hdr.data_length = rlen = data_length;
-		hdr.flags = SD_FLAG_CMD_IO_LOCAL;
-		hdr.obj.oid = oid;
+	hdr->obj.oid = oid;
+	hdr->obj.offset = 0;
+	hdr->obj.copies = get_nr_copies(vnodes);
 
-		fd = get_sheep_fd(v->addr, v->port, v->node_idx,
-				  hdr.epoch);
-		if (fd < 0)
-			continue;
+	read_req.data = buf;
+	read_req.op = get_sd_op(hdr->opcode);
+	read_req.vnodes = vnodes;
 
-		ret = exec_req(fd, &hdr, buf, &wlen, &rlen);
-		if (ret) { /* network errors */
-			del_sheep_fd(fd);
-			ret = SD_RES_NETWORK_ERROR;
-		} else
-			ret = rsp->result;
-		read_len = rlen;
+	ret = forward_read_obj_req(&read_req);
 
-		dprintf("[remote] %08"PRIx32", res:%"PRIx32"\n", idx, ret);
-		if (ret != SD_RES_SUCCESS)
-			continue;
-		else
-			break;
+	if (ret == SD_RES_SUCCESS) {
+		dprintf("oid %"PRIx64"pulled successfully\n", oid);
+		ret = create_cache_object(oc, idx, buf, data_length);
 	}
-out:
-	if (ret == SD_RES_SUCCESS)
-		ret = create_cache_object(oc, idx, buf, read_len);
 	free(buf);
+out:
 	return ret;
 }
 
diff --git a/sheep/ops.c b/sheep/ops.c
index 57a2b11..e8a13e7 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -629,94 +629,26 @@ static int local_trace_cat_ops(const struct sd_req *req, struct sd_rsp *rsp, voi
 	return SD_RES_SUCCESS;
 }
 
-static int read_copy_from_replica(struct request *req, uint32_t epoch,
+static int read_copy_from_replica(struct vnode_info *vnodes, uint32_t epoch,
 				  uint64_t oid, char *buf)
 {
-	int i, j, nr_copies, ret;
-	struct sd_req hdr;
-	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
-	struct sd_vnode *obj_vnodes[SD_MAX_COPIES];
-	struct sd_vnode *v;
-	char name[128];
-	int rounded_rand, local = -1;
-
-	nr_copies = get_nr_copies(req->vnodes);
-	oid_to_vnodes(req->vnodes, oid, nr_copies, obj_vnodes);
-
-	/* first try to read from local copy */
-	for (i = 0; i < nr_copies; i++) {
-		struct siocb iocb;
-
-		v = obj_vnodes[i];
-		addr_to_str(name, sizeof(name), v->addr, 0);
-
-		if (vnode_is_local(v)) {
-			memset(&iocb, 0, sizeof(iocb));
-			iocb.epoch = epoch;
-			iocb.buf = buf;
-			iocb.length = SD_DATA_OBJ_SIZE;
-			iocb.offset = 0;
-			ret = sd_store->read(oid, &iocb);
-			if (ret != SD_RES_SUCCESS) {
-				local = i;
-				break;
-			}
-			goto out;
-		}
-	}
-
-	/* then read random copy from cluster for better load balance */
-	rounded_rand = random() % nr_copies;
-
-	for (i = 0; i < nr_copies; i++) {
-		unsigned wlen, rlen;
-		int fd;
-
-		j = (i + rounded_rand) % nr_copies;
-
-		/* bypass the local copy */
-		if (local == j)
-			continue;
-
-		v = obj_vnodes[j];
-		addr_to_str(name, sizeof(name), v->addr, 0);
-
-		fd = connect_to(name, v->port);
-		if (fd < 0)
-			continue;
-
-		rlen = SD_DATA_OBJ_SIZE;
-		wlen = 0;
-
-		memset(&hdr, 0, sizeof(hdr));
-		hdr.opcode = SD_OP_READ_OBJ;
-		hdr.flags = SD_FLAG_CMD_IO_LOCAL;
-		hdr.epoch = epoch;
-		hdr.data_length = rlen;
+	struct request read_req;
+	struct sd_req *hdr = &read_req.rq;
 
-		hdr.obj.oid = oid;
-		hdr.obj.offset = 0;
+	memset(&read_req, 0, sizeof(read_req));
+	hdr->opcode = SD_OP_READ_OBJ;
+	hdr->data_length = SD_DATA_OBJ_SIZE;
+	hdr->epoch = epoch;
 
-		ret = exec_req(fd, &hdr, buf, &wlen, &rlen);
+	hdr->obj.oid = oid;
+	hdr->obj.offset = 0;
+	hdr->obj.copies = get_nr_copies(vnodes);
 
-		close(fd);
-
-		if (ret) {
-			dprintf("%x, %x\n", ret, rsp->result);
-			continue;
-		}
-		switch (rsp->result) {
-		case SD_RES_SUCCESS:
-			ret = SD_RES_SUCCESS;
-			goto out;
-		default:
-			;
-		}
-	}
+	read_req.data = buf;
+	read_req.op = get_sd_op(hdr->opcode);
+	read_req.vnodes = vnodes;
 
-	ret = rsp->result;
-out:
-	return ret;
+	return forward_read_obj_req(&read_req);
 }
 
 static int store_remove_obj(struct request *req)
@@ -735,7 +667,7 @@ static int store_remove_obj(struct request *req)
 		ret =  SD_RES_EIO;
 	}
 	objlist_cache_remove(oid);
- out:
+out:
 	strbuf_release(&buf);
 	return ret;
 }
@@ -836,7 +768,7 @@ static int store_create_and_write_obj(struct request *req)
 			goto out;
 		}
 		if (hdr->data_length != SD_DATA_OBJ_SIZE) {
-			ret = read_copy_from_replica(req, hdr->epoch,
+			ret = read_copy_from_replica(req->vnodes, hdr->epoch,
 						     hdr->obj.cow_oid, buf);
 			if (ret != SD_RES_SUCCESS) {
 				eprintf("failed to read cow object\n");
-- 
1.7.10.2




More information about the sheepdog mailing list