[sheepdog] [PATCH v4] zookeeper: fix bug for default cluster id.

Liu Yuan namei.unix at gmail.com
Wed Mar 4 09:37:38 CET 2015


On Wed, Mar 04, 2015 at 04:25:02PM +0800, Yu Yang wrote:
> 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);

Here we cat 'DEFAULT_BASE' to "conn", so it must contain '/', right? But
zk_prepare_root() will check if conn contains '/' again, seems redundent check.

Thanks,
Yuan

> +	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
> 
> -- 
> sheepdog mailing list
> sheepdog at lists.wpkg.org
> https://lists.wpkg.org/mailman/listinfo/sheepdog



More information about the sheepdog mailing list