[sheepdog] [PATCH 11/14] simplify addr_to_str with thread local storage

MORITA Kazutaka morita.kazutaka at gmail.com
Sat Aug 10 17:58:52 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/cluster.c   |    4 +---
 collie/node.c      |   13 +++++--------
 collie/vdi.c       |   38 +++++++++++++++++---------------------
 include/net.h      |    7 ++-----
 include/sheep.h    |    3 +--
 lib/net.c          |    7 ++++---
 lib/sockfd_cache.c |   31 +++++++++----------------------
 sheep/group.c      |    5 ++---
 sheep/recovery.c   |    8 +++-----
 9 files changed, 44 insertions(+), 72 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 19746c3..30f69dd 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -156,7 +156,6 @@ static int cluster_info(int argc, char **argv)
 	nr_logs = rsp->data_length / sizeof(struct epoch_log);
 	for (i = 0; i < nr_logs; i++) {
 		int j;
-		char name[128];
 		const struct sd_node *entry;
 
 		ti = logs[i].time;
@@ -173,8 +172,7 @@ static int cluster_info(int argc, char **argv)
 			entry = logs[i].nodes + j;
 			printf("%s%s",
 			       (j == 0) ? "" : ", ",
-			       addr_to_str(name, sizeof(name),
-					   entry->nid.addr, entry->nid.port));
+			       addr_to_str(entry->nid.addr, entry->nid.port));
 		}
 		printf("]\n");
 	}
diff --git a/collie/node.c b/collie/node.c
index 891f6e2..998f834 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -33,13 +33,11 @@ static int node_list(int argc, char **argv)
 	if (!raw_output)
 		printf("  Id   Host:Port         V-Nodes       Zone\n");
 	for (i = 0; i < sd_nodes_nr; i++) {
-		char data[128];
-
-		addr_to_str(data, sizeof(data), sd_nodes[i].nid.addr,
-			    sd_nodes[i].nid.port);
+		const char *host = addr_to_str(sd_nodes[i].nid.addr,
+					       sd_nodes[i].nid.port);
 
 		printf(raw_output ? "%d %s %d %u\n" : "%4d   %-20s\t%2d%11u\n",
-		       i, data, sd_nodes[i].nr_vnodes, sd_nodes[i].zone);
+		       i, host, sd_nodes[i].nr_vnodes, sd_nodes[i].zone);
 	}
 
 	return EXIT_SUCCESS;
@@ -199,7 +197,6 @@ static int node_recovery(int argc, char **argv)
 	}
 
 	for (i = 0; i < sd_nodes_nr; i++) {
-		char host[128];
 		struct sd_req req;
 		struct recovery_state state;
 
@@ -214,8 +211,8 @@ static int node_recovery(int argc, char **argv)
 			return EXIT_SYSFAIL;
 
 		if (state.in_recovery) {
-			addr_to_str(host, sizeof(host),
-					sd_nodes[i].nid.addr, sd_nodes[i].nid.port);
+			const char *host = addr_to_str(sd_nodes[i].nid.addr,
+						       sd_nodes[i].nid.port);
 			if (raw_output)
 				printf("%d %s %d %d %"PRIu64" %"PRIu64"\n", i,
 				       host, sd_nodes[i].nr_vnodes,
diff --git a/collie/vdi.c b/collie/vdi.c
index 9991beb..05682f7 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -234,11 +234,11 @@ static void get_oid(uint32_t vid, const char *name, const char *tag,
 	}
 }
 
-typedef int (*obj_parser_func_t)(char *sheep, uint64_t oid,
-				  struct sd_rsp *rsp, char *buf, void *data);
+typedef int (*obj_parser_func_t)(const char *sheep, uint64_t oid,
+				 struct sd_rsp *rsp, char *buf, void *data);
 
-static int do_print_obj(char *sheep, uint64_t oid, struct sd_rsp *rsp,
-			 char *buf, void *data)
+static int do_print_obj(const char *sheep, uint64_t oid, struct sd_rsp *rsp,
+			char *buf, void *data)
 {
 	switch (rsp->result) {
 	case SD_RES_SUCCESS:
@@ -267,8 +267,8 @@ struct get_data_oid_info {
 	unsigned idx;
 };
 
-static int get_data_oid(char *sheep, uint64_t oid, struct sd_rsp *rsp,
-			 char *buf, void *data)
+static int get_data_oid(const char *sheep, uint64_t oid, struct sd_rsp *rsp,
+			char *buf, void *data)
 {
 	struct get_data_oid_info *info = data;
 	struct sd_inode *inode = (struct sd_inode *)buf;
@@ -300,7 +300,6 @@ static int get_data_oid(char *sheep, uint64_t oid, struct sd_rsp *rsp,
 
 static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, unsigned size)
 {
-	char name[128];
 	int i, ret, cb_ret;
 	char *buf;
 
@@ -324,9 +323,9 @@ static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, unsigne
 		untrim_zero_blocks(buf, rsp->obj.offset, rsp->data_length,
 				   size);
 
-		addr_to_str(name, sizeof(name), sd_nodes[i].nid.addr,
-			    sd_nodes[i].nid.port);
-		cb_ret = func(name, oid, rsp, buf, data);
+		cb_ret = func(addr_to_str(sd_nodes[i].nid.addr,
+					  sd_nodes[i].nid.port),
+			      oid, rsp, buf, data);
 		if (cb_ret)
 			break;
 	}
@@ -888,7 +887,6 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 	const struct sd_vnode *vnode_buf[SD_MAX_COPIES];
 	struct epoch_log *logs;
 	int vnodes_nr, nr_logs, log_length;
-	char host[128];
 
 	log_length = sd_epoch * sizeof(struct epoch_log);
 	logs = xmalloc(log_length);
@@ -918,10 +916,9 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 		 */
 		if (logs[i].nr_nodes < nr_copies) {
 			for (j = 0; j < logs[i].nr_nodes; j++) {
-				addr_to_str(host, sizeof(host),
-					    logs[i].nodes[j].nid.addr,
-					    logs[i].nodes[j].nid.port);
-				printf("%s\n", host);
+				const struct node_id *n = &logs[i].nodes[j].nid;
+
+				printf("%s\n", addr_to_str(n->addr, n->port));
 			}
 			continue;
 		}
@@ -929,9 +926,9 @@ static int do_track_object(uint64_t oid, uint8_t nr_copies)
 					    logs[i].nr_nodes, vnodes);
 		oid_to_vnodes(vnodes, vnodes_nr, oid, nr_copies, vnode_buf);
 		for (j = 0; j < nr_copies; j++) {
-			addr_to_str(host, sizeof(host), vnode_buf[j]->nid.addr,
-				    vnode_buf[j]->nid.port);
-			printf("%s\n", host);
+			const struct node_id *n = &vnode_buf[j]->nid;
+
+			printf("%s\n", addr_to_str(n->addr, n->port));
 		}
 	}
 
@@ -1448,7 +1445,6 @@ static void vdi_hash_check_work(struct work *work)
 	struct vdi_check_work *vcw = container_of(work, struct vdi_check_work,
 						  work);
 	struct vdi_check_info *info = vcw->info;
-	char host[HOST_NAME_MAX];
 	int ret;
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
@@ -1473,8 +1469,8 @@ static void vdi_hash_check_work(struct work *work)
 		break;
 	default:
 		sd_err("failed to read %" PRIx64 " from %s, %s", info->oid,
-		       addr_to_str(host, sizeof(host), vcw->vnode->nid.addr,
-				   vcw->vnode->nid.port), sd_strerror(ret));
+		       addr_to_str(vcw->vnode->nid.addr, vcw->vnode->nid.port),
+		       sd_strerror(ret));
 		exit(EXIT_FAILURE);
 	}
 }
diff --git a/include/net.h b/include/net.h
index 238c636..2782e44 100644
--- a/include/net.h
+++ b/include/net.h
@@ -62,7 +62,7 @@ int create_listen_ports(const char *bindaddr, int port,
 int create_unix_domain_socket(const char *unix_path,
 			      int (*callback)(int, void *), void *data);
 
-char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port);
+const char *addr_to_str(const uint8_t *addr, uint16_t port);
 uint8_t *str_to_addr(const char *ipstr, uint8_t *addr);
 char *sockaddr_in_to_str(struct sockaddr_in *sockaddr);
 int set_nonblocking(int fd);
@@ -80,10 +80,7 @@ int do_writev2(int fd, void *hdr, size_t hdr_len, void *body, size_t body_len);
 
 static inline int connect_to_addr(const uint8_t *addr, int port)
 {
-	char name[INET6_ADDRSTRLEN];
-
-	addr_to_str(name, sizeof(name), addr, 0);
-	return connect_to(name, port);
+	return connect_to(addr_to_str(addr, 0), port);
 }
 
 #endif
diff --git a/include/sheep.h b/include/sheep.h
index 295cb24..219add9 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -318,7 +318,6 @@ static inline int nodes_to_vnodes(struct sd_node *nodes, int nr,
 static inline char *node_to_str(const struct sd_node *id)
 {
 	static char str[MAX_NODE_STR_LEN];
-	char name[MAX_NODE_STR_LEN];
 	int af = AF_INET6;
 	const uint8_t *addr = id->nid.addr;
 
@@ -333,7 +332,7 @@ static inline char *node_to_str(const struct sd_node *id)
 
 	snprintf(str, sizeof(str), "%s ip:%s port:%d",
 		(af == AF_INET) ? "IPv4" : "IPv6",
-		addr_to_str(name, sizeof(name), id->nid.addr, 0), id->nid.port);
+		addr_to_str(id->nid.addr, 0), id->nid.port);
 
 	return str;
 }
diff --git a/lib/net.c b/lib/net.c
index 720624f..82b6e3a 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -421,8 +421,9 @@ int exec_req(int sockfd, struct sd_req *hdr, void *data,
 	return 0;
 }
 
-char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port)
+const char *addr_to_str(const uint8_t *addr, uint16_t port)
 {
+	static __thread char str[HOST_NAME_MAX + 8];
 	int af = AF_INET6;
 	int addr_start_idx = 0;
 	const char *ret;
@@ -437,13 +438,13 @@ char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port)
 			addr_start_idx = 12;
 		}
 	}
-	ret = inet_ntop(af, addr + addr_start_idx, str, size);
+	ret = inet_ntop(af, addr + addr_start_idx, str, sizeof(str));
 	if (unlikely(ret == NULL))
 		panic("failed to convert addr to string, %m");
 
 	if (port) {
 		int  len = strlen(str);
-		snprintf(str + len, size - len, ":%d", port);
+		snprintf(str + len, sizeof(str) - len, ":%d", port);
 	}
 
 	return str;
diff --git a/lib/sockfd_cache.c b/lib/sockfd_cache.c
index e5fc11c..c9404ea 100644
--- a/lib/sockfd_cache.c
+++ b/lib/sockfd_cache.c
@@ -147,10 +147,7 @@ static struct sockfd_cache_entry *sockfd_cache_grab(const struct node_id *nid,
 	sd_read_lock(&sockfd_cache.lock);
 	entry = sockfd_cache_search(nid);
 	if (!entry) {
-		char name[INET6_ADDRSTRLEN];
-
-		addr_to_str(name, sizeof(name), nid->addr, 0);
-		sd_debug("failed node %s:%d", name, nid->port);
+		sd_debug("failed node %s", addr_to_str(nid->addr, nid->port));
 		goto out;
 	}
 
@@ -255,7 +252,6 @@ void sockfd_cache_add_group(const struct sd_node *nodes, int nr)
 void sockfd_cache_add(const struct node_id *nid)
 {
 	struct sockfd_cache_entry *new;
-	char name[INET6_ADDRSTRLEN];
 	int n, i;
 
 	sd_write_lock(&sockfd_cache.lock);
@@ -272,8 +268,7 @@ void sockfd_cache_add(const struct node_id *nid)
 	}
 	sd_unlock(&sockfd_cache.lock);
 	n = uatomic_add_return(&sockfd_cache.count, 1);
-	addr_to_str(name, sizeof(name), nid->addr, 0);
-	sd_debug("%s:%d, count %d", name, nid->port, n);
+	sd_debug("%s, count %d", addr_to_str(nid->addr, nid->port), n);
 }
 
 static uatomic_bool fds_in_grow;
@@ -355,10 +350,7 @@ static struct sockfd *sockfd_cache_get_long(const struct node_id *nid)
 	struct sockfd *sfd;
 	bool use_io = nid->io_port ? true : false;
 	const uint8_t *addr = use_io ? nid->io_addr : nid->addr;
-	char name[INET6_ADDRSTRLEN];
 	int fd, idx = -1, port = use_io ? nid->io_port : nid->port;
-
-	addr_to_str(name, sizeof(name), addr, 0);
 grab:
 	entry = sockfd_cache_grab(nid, &idx);
 	if (!entry) {
@@ -377,13 +369,14 @@ grab:
 
 	check_idx(idx);
 	if (entry->fds[idx].fd != -1) {
-		sd_debug("%s:%d, idx %d", name, port, idx);
+		sd_debug("%s, idx %d", addr_to_str(addr, port), idx);
 		goto out;
 	}
 
 	/* Create a new cached connection for this node */
-	sd_debug("create cache connection %s:%d idx %d", name, port, idx);
-	fd = connect_to(name, port);
+	sd_debug("create cache connection %s idx %d", addr_to_str(addr, port),
+		 idx);
+	fd = connect_to_addr(addr, port);
 	if (fd < 0) {
 		if (use_io) {
 			sd_err("fallback to non-io connection");
@@ -409,10 +402,8 @@ static void sockfd_cache_put_long(const struct node_id *nid, int idx)
 	const uint8_t *addr = use_io ? nid->io_addr : nid->addr;
 	int port = use_io ? nid->io_port : nid->port;
 	struct sockfd_cache_entry *entry;
-	char name[INET6_ADDRSTRLEN];
 
-	addr_to_str(name, sizeof(name), addr, 0);
-	sd_debug("%s:%d idx %d", name, port, idx);
+	sd_debug("%s idx %d", addr_to_str(addr, port), idx);
 
 	sd_read_lock(&sockfd_cache.lock);
 	entry = sockfd_cache_search(nid);
@@ -427,10 +418,8 @@ static void sockfd_cache_close(const struct node_id *nid, int idx)
 	const uint8_t *addr = use_io ? nid->io_addr : nid->addr;
 	int port = use_io ? nid->io_port : nid->port;
 	struct sockfd_cache_entry *entry;
-	char name[INET6_ADDRSTRLEN];
 
-	addr_to_str(name, sizeof(name), addr, 0);
-	sd_debug("%s:%d idx %d", name, port, idx);
+	sd_debug("%s idx %d", addr_to_str(addr, port), idx);
 
 	sd_write_lock(&sockfd_cache.lock);
 	entry = sockfd_cache_search(nid);
@@ -511,15 +500,13 @@ void sockfd_cache_put(const struct node_id *nid, struct sockfd *sfd)
 /* Delete all sockfd connected to the node, when node is crashed. */
 void sockfd_cache_del_node(const struct node_id *nid)
 {
-	char name[INET6_ADDRSTRLEN];
 	int n;
 
 	if (!sockfd_cache_destroy(nid))
 		return;
 
 	n = uatomic_sub_return(&sockfd_cache.count, 1);
-	addr_to_str(name, sizeof(name), nid->addr, 0);
-	sd_debug("%s:%d, count %d", name, nid->port, n);
+	sd_debug("%s, count %d", addr_to_str(nid->addr, nid->port), n);
 }
 
 /*
diff --git a/sheep/group.c b/sheep/group.c
index 6dcafec..acd6f41 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -736,7 +736,6 @@ main_fn bool sd_join_handler(const struct sd_node *joining,
 {
 	struct cluster_info *cinfo = opaque;
 	enum sd_status status;
-	char str[MAX_NODE_STR_LEN];
 
 	/*
 	 * If nr_nodes is 0, the joining node is the first member of the cluster
@@ -760,8 +759,8 @@ main_fn bool sd_join_handler(const struct sd_node *joining,
 	cinfo->proto_ver = SD_SHEEP_PROTO_VER;
 
 	sd_debug("%s: cluster_status = 0x%x",
-		 addr_to_str(str, sizeof(str), joining->nid.addr,
-			     joining->nid.port), cinfo->status);
+		 addr_to_str(joining->nid.addr, joining->nid.port),
+		 cinfo->status);
 
 	return true;
 }
diff --git a/sheep/recovery.c b/sheep/recovery.c
index 3487dbe..91c1a10 100644
--- a/sheep/recovery.c
+++ b/sheep/recovery.c
@@ -663,15 +663,13 @@ static void finish_object_list(struct work *work)
 static uint64_t *fetch_object_list(struct sd_node *e, uint32_t epoch,
 				   size_t *nr_oids)
 {
-	char name[128];
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	size_t buf_size = list_buffer_size;
 	uint64_t *buf = xmalloc(buf_size);
 	int ret;
 
-	addr_to_str(name, sizeof(name), e->nid.addr, 0);
-	sd_debug("%s %"PRIu32, name, e->nid.port);
+	sd_debug("%s", addr_to_str(e->nid.addr, e->nid.port));
 
 retry:
 	sd_init_req(&hdr, SD_OP_GET_OBJ_LIST);
@@ -687,8 +685,8 @@ retry:
 		buf = xrealloc(buf, buf_size);
 		goto retry;
 	default:
-		sd_alert("cannot get object list from %s:%d", name,
-			 e->nid.port);
+		sd_alert("cannot get object list from %s",
+			 addr_to_str(e->nid.addr, e->nid.port));
 		sd_alert("some objects may be not recovered at epoch %d",
 			 epoch);
 		free(buf);
-- 
1.7.9.5



More information about the sheepdog mailing list