[sheepdog] [PATCH v2 3/3] collie: let collie use sockfd cache

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Fri Jul 19 07:52:18 CEST 2013


Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 collie/collie.c |    9 +++++++++
 collie/collie.h |    2 ++
 collie/common.c |   34 ++++++++++++++++++++--------------
 3 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/collie/collie.c b/collie/collie.c
index 2f8e9d0..60e6fe7 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -17,6 +17,7 @@
 #include "sheep.h"
 #include "collie.h"
 #include "util.h"
+#include "sockfd_cache.h"
 
 #define EPOLL_SIZE 4096
 
@@ -345,6 +346,7 @@ int main(int argc, char **argv)
 	const char *short_options;
 	char *p;
 	const struct sd_option *sd_opts;
+	struct work_queue *sockfd_wq;
 
 	install_crash_handler(crash_handler);
 
@@ -418,6 +420,13 @@ int main(int argc, char **argv)
 		exit(EXIT_SYSFAIL);
 	}
 
+	sockfd_wq = create_ordered_work_queue("sockfd");
+	if (!sockfd_wq) {
+		fprintf(stderr, "Failed to create workqueue for sockfd\n");
+		exit(EXIT_SYSFAIL);
+	}
+	init_sockfd(sockfd_wq);
+
 	ret = command_fn(argc, argv);
 	if (ret == EXIT_USAGE)
 		subcommand_usage(argv[1], argv[2], EXIT_USAGE);
diff --git a/collie/collie.h b/collie/collie.h
index e0984e9..156ac28 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -76,6 +76,8 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
 		    unsigned int datalen, uint64_t offset, uint32_t flags,
 		    int copies, bool create, bool direct);
 int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *data);
+int collie_exec_req_nid(const struct node_id *nid,
+			struct sd_req *hdr, void *buf);
 int send_light_req(struct sd_req *hdr, const char *host, int port);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes);
diff --git a/collie/common.c b/collie/common.c
index 4a57d92..0d05d27 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -11,6 +11,7 @@
 
 #include "collie.h"
 #include "sha1.h"
+#include "sockfd_cache.h"
 
 char *size_to_str(uint64_t _size, char *str, int str_size)
 {
@@ -196,26 +197,31 @@ out:
 	return ret;
 }
 
-int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *data)
+/* todo: unify collie_exec_req_nid() and collie_exec_req() */
+int collie_exec_req_nid(const struct node_id *nid,
+			struct sd_req *hdr, void *buf)
 {
-	int fd, ret;
-	struct sd_rsp *rsp = (struct sd_rsp *)hdr;
+	struct sockfd *sfd;
+	int ret;
 
-	fd = connect_to(host, port);
-	if (fd < 0) {
-		fprintf(stderr, "Failed to connect to %s:%d\n",
-			host, port);
+	sfd = sockfd_cache_get(nid);
+	if (!sfd)
 		return -1;
-	}
 
-	/* Retry hard for collie because we can't get the newest epoch */
-	ret = exec_req(fd, hdr, data, NULL, 0);
-	close(fd);
+	ret = exec_req(sfd->fd, hdr, buf, NULL, 0);
 
-	if (ret)
-		return -1;
+	sockfd_cache_put(nid, sfd);
+	return ret;
+}
+
+int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *buf)
+{
+	struct node_id nid;
 
-	return rsp->result;
+	memset(&nid, 0, sizeof(nid));
+	str_to_addr(host, nid.addr);
+	nid.port = port;
+	return collie_exec_req_nid((const struct node_id *)&nid, hdr, buf);
 }
 
 /* Light request only contains header, without body content. */
-- 
1.7.10.4




More information about the sheepdog mailing list