[sheepdog] [PATCH v4] zookeeper: fix bug for default cluster id.
Yu Yang
yuyanggg at gmail.com
Wed Mar 4 09:25:02 CET 2015
From: Yu Yang <yuyang at cmss.chinamobile.com>
Append DEFAULT_BASE to connection string when cluster id is not set.
And zk_prepare_root() is modified for the better use of pstrcpy.
Signed-off-by: Yu Yang <yuyang at cmss.chinamobile.com>
---
sheep/cluster/zookeeper.c | 26 ++++++++++++--------------
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 213bc9e..f73019a 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -1409,11 +1409,7 @@ static int zk_prepare_root(const char *hosts)
const char *p = strchr(hosts, '/');
int i = 0;
if (p) {
- if (strlen(p) >= MAX_NODE_STR_LEN) {
- pstrcpy(root, MAX_NODE_STR_LEN - 1, p);
- root[MAX_NODE_STR_LEN - 1] = '\0';
- } else
- pstrcpy(root, MAX_NODE_STR_LEN, p);
+ pstrcpy(root, MAX_NODE_STR_LEN, p);
while (hosts != p) {
conn[i++] = *hosts++;
if (i >= MAX_NODE_STR_LEN - 1)
@@ -1422,11 +1418,7 @@ static int zk_prepare_root(const char *hosts)
conn[i] = '\0';
} else {
pstrcpy(root, MAX_NODE_STR_LEN, DEFAULT_BASE);
- if (strlen(hosts) >= MAX_NODE_STR_LEN) {
- pstrcpy(conn, MAX_NODE_STR_LEN - 1, hosts);
- conn[MAX_NODE_STR_LEN - 1] = '\0';
- } else
- pstrcpy(conn, MAX_NODE_STR_LEN, hosts);
+ pstrcpy(conn, MAX_NODE_STR_LEN, hosts);
}
if (zk_connect(conn, zk_watcher, zk_timeout) < 0)
@@ -1444,6 +1436,7 @@ static int zk_init(const char *option)
{
char *hosts, *to, *p;
int ret, timeo;
+ char conn[MAX_NODE_STR_LEN];
if (!option) {
sd_err("You must specify zookeeper servers.");
@@ -1459,15 +1452,20 @@ static int zk_init(const char *option)
p = strstr(hosts, "timeout");
*--p = '\0';
}
- if (zk_prepare_root(hosts) != 0) {
- sd_err("failed to initialize zk server %s", hosts);
+ pstrcpy(conn, MAX_NODE_STR_LEN, hosts);
+ if (!strchr(conn, '/'))
+ strcat(conn, DEFAULT_BASE);
+ if (zk_prepare_root(conn) != 0) {
+ sd_err("failed to initialize zk server %s", conn);
return -1;
}
sd_info("version %d.%d.%d, address %s, timeout %d", ZOO_MAJOR_VERSION,
- ZOO_MINOR_VERSION, ZOO_PATCH_VERSION, hosts, zk_timeout);
- if (zk_connect(hosts, zk_watcher, zk_timeout) < 0)
+ ZOO_MINOR_VERSION, ZOO_PATCH_VERSION, conn, zk_timeout);
+ if (zk_connect(conn, zk_watcher, zk_timeout) < 0) {
+ sd_err("failed to initialize zk server %s", conn);
return -1;
+ }
timeo = zoo_recv_timeout(zhandle);
sd_info("the negociated session timeout is %d", timeo);
--
1.7.9.5
More information about the sheepdog
mailing list