[sheepdog] [PATCH v2 2/5] dog: use struct node_id to send requests

Liu Yuan namei.unix at gmail.com
Tue Sep 10 04:53:49 CEST 2013


following two functions's interface get changed:
- dog_exec_req(&nid ...)
- send_light_req(&nid ...)

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/cluster.c   |   18 +++++++++---------
 dog/common.c    |   21 ++++++++-------------
 dog/dog.c       |   14 ++++++++++----
 dog/dog.h       |    8 +++-----
 dog/farm/farm.c |    2 +-
 dog/node.c      |   27 +++++++++------------------
 dog/trace.c     |    8 ++++----
 dog/vdi.c       |   34 ++++++++++++++--------------------
 8 files changed, 58 insertions(+), 74 deletions(-)

diff --git a/dog/cluster.c b/dog/cluster.c
index 1ce5147..256734d 100644
--- a/dog/cluster.c
+++ b/dog/cluster.c
@@ -43,7 +43,7 @@ static int list_store(void)
 	sd_init_req(&hdr, SD_OP_GET_STORE_LIST);
 	hdr.data_length = 512;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -82,7 +82,7 @@ static int cluster_format(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_READ_VDIS);
 	hdr.data_length = sizeof(vdi_inuse);
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, &vdi_inuse);
+	ret = dog_exec_req(&sd_nid, &hdr, &vdi_inuse);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 	if (rsp->result != SD_RES_SUCCESS) {
@@ -107,7 +107,7 @@ static int cluster_format(int argc, char **argv)
 	hdr.flags |= SD_FLAG_CMD_WRITE;
 
 	printf("using backend %s store\n", store_name);
-	ret = dog_exec_req(sdhost, sdport, &hdr, store_name);
+	ret = dog_exec_req(&sd_nid, &hdr, store_name);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -139,7 +139,7 @@ static int cluster_info(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
 	hdr.data_length = log_length;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, logs);
+	ret = dog_exec_req(&sd_nid, &hdr, logs);
 	if (ret < 0)
 		goto error;
 
@@ -195,7 +195,7 @@ static int cluster_shutdown(int argc, char **argv)
 
 	sd_init_req(&hdr, SD_OP_SHUTDOWN);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret) {
 		sd_err("failed to execute request");
 		return EXIT_FAILURE;
@@ -384,7 +384,7 @@ static int cluster_force_recover(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_FORCE_RECOVER);
 	hdr.data_length = sizeof(nodes);
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, nodes);
+	ret = dog_exec_req(&sd_nid, &hdr, nodes);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -404,7 +404,7 @@ static int cluster_disable_recover(int argc, char **argv)
 
 	sd_init_req(&hdr, SD_OP_DISABLE_RECOVER);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret)
 		return EXIT_FAILURE;
 
@@ -419,7 +419,7 @@ static int cluster_enable_recover(int argc, char **argv)
 
 	sd_init_req(&hdr, SD_OP_ENABLE_RECOVER);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret)
 		return EXIT_FAILURE;
 
@@ -467,7 +467,7 @@ static int cluster_reweight(int argc, char **argv)
 	struct sd_req hdr;
 
 	sd_init_req(&hdr, SD_OP_REWEIGHT);
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret)
 		return EXIT_FAILURE;
 	return EXIT_SUCCESS;
diff --git a/dog/common.c b/dog/common.c
index ce022ed..83777af 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -69,7 +69,7 @@ int sd_read_object(uint64_t oid, void *data, unsigned int datalen,
 	if (direct)
 		hdr.flags |= SD_FLAG_CMD_DIRECT;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, data);
+	ret = dog_exec_req(&sd_nid, &hdr, data);
 	if (ret < 0) {
 		sd_err("Failed to read object %" PRIx64, oid);
 		return SD_RES_EIO;
@@ -111,7 +111,7 @@ int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
 	hdr.obj.cow_oid = cow_oid;
 	hdr.obj.offset = offset;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, data);
+	ret = dog_exec_req(&sd_nid, &hdr, data);
 	if (ret < 0) {
 		sd_err("Failed to write object %" PRIx64, oid);
 		return SD_RES_EIO;
@@ -140,7 +140,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void *data)
 	sd_init_req(&req, SD_OP_READ_VDIS);
 	req.data_length = sizeof(vdi_inuse);
 
-	ret = dog_exec_req(sdhost, sdport, &req, &vdi_inuse);
+	ret = dog_exec_req(&sd_nid, &req, &vdi_inuse);
 	if (ret < 0)
 		goto out;
 	if (rsp->result != SD_RES_SUCCESS) {
@@ -185,17 +185,12 @@ out:
 	return ret;
 }
 
-int dog_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
-		    void *buf)
+int dog_exec_req(const struct node_id *nid, struct sd_req *hdr, void *buf)
 {
-	struct node_id nid = {};
 	struct sockfd *sfd;
 	int ret;
 
-	memcpy(nid.addr, addr, sizeof(nid.addr));
-	nid.port = port;
-
-	sfd = sockfd_cache_get(&nid);
+	sfd = sockfd_cache_get(nid);
 	if (!sfd)
 		return -1;
 
@@ -206,15 +201,15 @@ int dog_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
 	 */
 	ret = exec_req(sfd->fd, hdr, buf, NULL, 0, UINT32_MAX);
 
-	sockfd_cache_put(&nid, sfd);
+	sockfd_cache_put(nid, sfd);
 
 	return ret ? -1 : 0;
 }
 
 /* Light request only contains header, without body content. */
-int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port)
+int send_light_req(const struct node_id *nid, struct sd_req *hdr)
 {
-	int ret = dog_exec_req(addr, port, hdr, NULL);
+	int ret = dog_exec_req(nid, hdr, NULL);
 	struct sd_rsp *rsp = (struct sd_rsp *)hdr;
 
 	if (ret == -1)
diff --git a/dog/dog.c b/dog/dog.c
index b2c56ff..9ec41c1 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -22,9 +22,11 @@
 #define EPOLL_SIZE 4096
 
 static const char program_name[] = "dog";
-/* default sdhost is "127.0.0.1" */
-uint8_t sdhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1 };
-int sdport = SD_LISTEN_PORT;
+struct node_id sd_nid = {
+	/* default sdhost is "127.0.0.1" */
+	.addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1 },
+	.port = SD_LISTEN_PORT,
+};
 bool highlight = true;
 bool raw_output;
 bool verbose;
@@ -65,7 +67,7 @@ int update_node_list(int max_nodes)
 
 	hdr.data_length = size;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		goto out;
 
@@ -345,6 +347,8 @@ int main(int argc, char **argv)
 	const char *short_options;
 	char *p;
 	const struct sd_option *sd_opts;
+	uint8_t sdhost[16];
+	int sdport;
 
 	install_crash_handler(crash_handler);
 
@@ -370,6 +374,7 @@ int main(int argc, char **argv)
 				sd_err("Invalid ip address %s", optarg);
 				return EXIT_FAILURE;
 			}
+			memcpy(sd_nid.addr, sdhost, sizeof(sdhost));
 			break;
 		case 'p':
 			sdport = strtol(optarg, &p, 10);
@@ -377,6 +382,7 @@ int main(int argc, char **argv)
 				sd_err("Invalid port number '%s'", optarg);
 				exit(EXIT_USAGE);
 			}
+			sd_nid.port = sdport;
 			break;
 		case 'r':
 			raw_output = true;
diff --git a/dog/dog.h b/dog/dog.h
index 97cfb0d..e1c499e 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -49,8 +49,7 @@ struct subcommand {
 };
 void subcommand_usage(char *cmd, char *subcmd, int status);
 
-extern uint8_t sdhost[16];
-extern int sdport;
+extern struct node_id sd_nid;
 extern bool highlight;
 extern bool raw_output;
 extern bool verbose;
@@ -72,9 +71,8 @@ int sd_read_object(uint64_t oid, void *data, unsigned int datalen,
 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 dog_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
-		    void *data);
-int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port);
+int dog_exec_req(const struct node_id *, struct sd_req *hdr, void *data);
+int send_light_req(const struct node_id *, struct sd_req *hdr);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes);
 void confirm(const char *message);
diff --git a/dog/farm/farm.c b/dog/farm/farm.c
index d034c7a..3ac0f4a 100644
--- a/dog/farm/farm.c
+++ b/dog/farm/farm.c
@@ -186,7 +186,7 @@ static int notify_vdi_add(uint32_t vdi_id, uint32_t nr_copies)
 	hdr.vdi_state.copies = nr_copies;
 	hdr.vdi_state.set_bitmap = true;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 
 	if (ret < 0)
 		sd_err("Fail to notify vdi add event(%"PRIx32", %d)", vdi_id,
diff --git a/dog/node.c b/dog/node.c
index 5a6a62c..2c1450f 100644
--- a/dog/node.c
+++ b/dog/node.c
@@ -58,9 +58,7 @@ static int node_info(int argc, char **argv)
 
 		sd_init_req(&req, SD_OP_STAT_SHEEP);
 
-		ret = send_light_req(&req, sd_nodes[i].nid.addr,
-				     sd_nodes[i].nid.port);
-
+		ret = send_light_req(&sd_nodes[i].nid, &req);
 		if (!ret) {
 			int ratio = (int)(((double)(rsp->node.store_size -
 						    rsp->node.store_free) /
@@ -110,7 +108,7 @@ static int get_recovery_state(struct recovery_state *state)
 	sd_init_req(&req, SD_OP_STAT_RECOVERY);
 	req.data_length = sizeof(*state);
 
-	ret = dog_exec_req(sdhost, sdport, &req, state);
+	ret = dog_exec_req(&sd_nid, &req, state);
 	if (ret < 0) {
 		sd_err("Failed to execute request");
 		return -1;
@@ -205,8 +203,7 @@ static int node_recovery(int argc, char **argv)
 		sd_init_req(&req, SD_OP_STAT_RECOVERY);
 		req.data_length = sizeof(state);
 
-		ret = dog_exec_req(sd_nodes[i].nid.addr,
-				      sd_nodes[i].nid.port, &req, &state);
+		ret = dog_exec_req(&sd_nodes[i].nid, &req, &state);
 		if (ret < 0)
 			return EXIT_SYSFAIL;
 		if (rsp->result != SD_RES_SUCCESS) {
@@ -253,8 +250,7 @@ static int node_kill(int argc, char **argv)
 
 	sd_init_req(&req, SD_OP_KILL_NODE);
 
-	ret = send_light_req(&req, sd_nodes[node_id].nid.addr,
-			     sd_nodes[node_id].nid.port);
+	ret = send_light_req(&sd_nodes[node_id].nid, &req);
 	if (ret) {
 		sd_err("Failed to execute request");
 		exit(EXIT_FAILURE);
@@ -274,7 +270,7 @@ static int node_stat(int argc, char **argv)
 again:
 	sd_init_req(&hdr, SD_OP_STAT);
 	hdr.data_length = sizeof(stat);
-	ret = dog_exec_req(sdhost, sdport, &hdr, &stat);
+	ret = dog_exec_req(&sd_nid, &hdr, &stat);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -314,7 +310,7 @@ static int node_md_info(struct node_id *nid)
 	sd_init_req(&hdr, SD_OP_MD_INFO);
 	hdr.data_length = sizeof(info);
 
-	ret = dog_exec_req(nid->addr, nid->port, &hdr, &info);
+	ret = dog_exec_req(nid, &hdr, &info);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -343,13 +339,8 @@ static int md_info(int argc, char **argv)
 
 	fprintf(stdout, "Id\tSize\tUsed\tAvail\tUse%%\tPath\n");
 
-	if (!node_cmd_data.all_nodes) {
-		struct node_id nid = {.port = sdport};
-
-		memcpy(nid.addr, sdhost, sizeof(nid.addr));
-
-		return node_md_info(&nid);
-	}
+	if (!node_cmd_data.all_nodes)
+		return node_md_info(&sd_nid);
 
 	for (i = 0; i < sd_nodes_nr; i++) {
 		fprintf(stdout, "Node %d:\n", i);
@@ -378,7 +369,7 @@ static int do_plug_unplug(char *disks, bool plug)
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = strlen(disks) + 1;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, disks);
+	ret = dog_exec_req(&sd_nid, &hdr, disks);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
diff --git a/dog/trace.c b/dog/trace.c
index 34ea993..806a3dd 100644
--- a/dog/trace.c
+++ b/dog/trace.c
@@ -92,7 +92,7 @@ read_buffer:
 	sd_init_req(&hdr, SD_OP_TRACE_READ_BUF);
 	hdr.data_length = TRACE_BUF_LEN;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -123,7 +123,7 @@ static int trace_enable(int argc, char **argv)
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = strlen(tracer) + 1;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, (void *)tracer);
+	ret = dog_exec_req(&sd_nid, &hdr, (void *)tracer);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -155,7 +155,7 @@ static int trace_disable(int argc, char **argv)
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = strlen(tracer) + 1;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, (void *)tracer);
+	ret = dog_exec_req(&sd_nid, &hdr, (void *)tracer);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -186,7 +186,7 @@ static int trace_status(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_TRACE_STATUS);
 	hdr.data_length = sizeof(buf);
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 	switch (rsp->result) {
diff --git a/dog/vdi.c b/dog/vdi.c
index a31d8c5..82821ac 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -330,8 +330,7 @@ static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, unsigne
 
 		hdr.obj.oid = oid;
 
-		ret = dog_exec_req(sd_nodes[i].nid.addr,
-				      sd_nodes[i].nid.port, &hdr, buf);
+		ret = dog_exec_req(&sd_nodes[i].nid, &hdr, buf);
 		if (ret < 0)
 			continue;
 		switch (rsp->result) {
@@ -421,7 +420,7 @@ static int find_vdi_name(const char *vdiname, uint32_t snapid, const char *tag,
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.vdi.snapid = snapid;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		return -1;
 
@@ -488,7 +487,7 @@ int do_vdi_create(const char *vdiname, int64_t vdi_size,
 	hdr.vdi.vdi_size = vdi_size;
 	hdr.vdi.copies = nr_copies;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, buf);
+	ret = dog_exec_req(&sd_nid, &hdr, buf);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -762,7 +761,7 @@ static int do_vdi_delete(const char *vdiname, int snap_id, const char *snap_tag)
 	sd_init_req(&hdr, SD_OP_DELETE_CACHE);
 	hdr.obj.oid = vid_to_vdi_oid(vid);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret) {
 		sd_err("failed to execute request");
 		return EXIT_FAILURE;
@@ -777,7 +776,7 @@ static int do_vdi_delete(const char *vdiname, int snap_id, const char *snap_tag)
 	if (snap_tag)
 		pstrcpy(data + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag);
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, data);
+	ret = dog_exec_req(&sd_nid, &hdr, data);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -917,7 +916,7 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 	sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
 	hdr.data_length = log_length;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, logs);
+	ret = dog_exec_req(&sd_nid, &hdr, logs);
 	if (ret < 0)
 		goto error;
 
@@ -1055,7 +1054,7 @@ static int find_vdi_attr_oid(const char *vdiname, const char *tag, uint32_t snap
 	if (delete)
 		hdr.flags |= SD_FLAG_CMD_DEL;
 
-	ret = dog_exec_req(sdhost, sdport, &hdr, &vattr);
+	ret = dog_exec_req(&sd_nid, &hdr, &vattr);
 	if (ret < 0)
 		return SD_RES_EIO;
 
@@ -1357,9 +1356,7 @@ static void *read_object_from(const struct sd_vnode *vnode, uint64_t oid)
 
 	hdr.obj.oid = oid;
 
-	ret = dog_exec_req(vnode->node->nid.addr, vnode->node->nid.port,
-			   &hdr, buf);
-
+	ret = dog_exec_req(&vnode->node->nid, &hdr, buf);
 	if (ret < 0)
 		exit(EXIT_SYSFAIL);
 
@@ -1395,9 +1392,7 @@ static void write_object_to(const struct sd_vnode *vnode, uint64_t oid,
 	hdr.data_length = get_objsize(oid);
 	hdr.obj.oid = oid;
 
-	ret = dog_exec_req(vnode->node->nid.addr, vnode->node->nid.port,
-			   &hdr, buf);
-
+	ret = dog_exec_req(&vnode->node->nid, &hdr, buf);
 	if (ret < 0)
 		exit(EXIT_SYSFAIL);
 
@@ -1477,8 +1472,7 @@ static void vdi_hash_check_work(struct work *work)
 	hdr.obj.oid = info->oid;
 	hdr.obj.tgt_epoch = sd_epoch;
 
-	ret = dog_exec_req(vcw->vnode->node->nid.addr,
-			   vcw->vnode->node->nid.port, &hdr, NULL);
+	ret = dog_exec_req(&vcw->vnode->node->nid, &hdr, NULL);
 	if (ret < 0)
 		exit(EXIT_SYSFAIL);
 
@@ -1951,7 +1945,7 @@ static int vdi_cache_flush(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_FLUSH_VDI);
 	hdr.obj.oid = vid_to_vdi_oid(vid);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret) {
 		sd_err("failed to execute request");
 		return EXIT_FAILURE;
@@ -1978,7 +1972,7 @@ static int vdi_cache_delete(int argc, char **argv)
 	sd_init_req(&hdr, SD_OP_DELETE_CACHE);
 	hdr.obj.oid = vid_to_vdi_oid(vid);
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret) {
 		sd_err("failed to execute request");
 		return EXIT_FAILURE;
@@ -2012,7 +2006,7 @@ static int vdi_cache_info(int argc, char **argv)
 
 	sd_init_req(&hdr, SD_OP_GET_CACHE_INFO);
 	hdr.data_length = sizeof(info);
-	ret = dog_exec_req(sdhost, sdport, &hdr, &info);
+	ret = dog_exec_req(&sd_nid, &hdr, &info);
 	if (ret < 0)
 		return EXIT_SYSFAIL;
 
@@ -2070,7 +2064,7 @@ static int vdi_cache_purge(int argc, char **argv)
 			". Continue? [yes/no]: ");
 	}
 
-	ret = send_light_req(&hdr, sdhost, sdport);
+	ret = send_light_req(&sd_nid, &hdr);
 	if (ret) {
 		sd_err("failed to execute request");
 		return EXIT_FAILURE;
-- 
1.7.9.5




More information about the sheepdog mailing list