[sheepdog] [PATCH 1/2] zookeeper: remove master from zookeeper

Kai Zhang kyle at zelin.io
Thu Aug 8 04:20:50 CEST 2013


On Aug 8, 2013, at 9:01 AM, Kai Zhang <kyle at zelin.io> wrote:

> 
> On Aug 8, 2013, at 7:08 AM, MORITA Kazutaka <morita.kazutaka at gmail.com> wrote:
> 
>>> 
>>> -static int zk_get_least_seq(const char *parent, char *least_seq_path,
>>> -			    int path_len, void *buf, int *buf_len)
>>> +static int zk_acquire_lock(const char *path)
>>> {
>>> -	char path[MAX_NODE_STR_LEN], *p, *tmp;
>>> -	struct String_vector strs;
>>> -	int rc, least_seq = INT_MAX , seq;
>>> -
>>> +	int rc;
>>> 	while (true) {
>>> -		RETURN_IF_ERROR(zk_get_children(parent, &strs), "");
>>> -
>>> -		FOR_EACH_ZNODE(parent, path, &strs) {
>>> -			p = strrchr(path, '/');
>>> -			seq = strtol(++p, &tmp, 10);
>>> -			if (seq < least_seq)
>>> -				least_seq = seq;
>>> -		}
>>> -
>>> -		snprintf(path, MAX_NODE_STR_LEN, "%s/%010"PRId32,
>>> -			 parent, least_seq);
>>> -		rc = zk_get_data(path, buf, buf_len);
>>> +		rc = zk_create_node(path, "", 0, &ZOO_OPEN_ACL_UNSAFE,
>>> +				    ZOO_EPHEMERAL, NULL, 0);
>>> 		switch (rc) {
>>> 		case ZOK:
>>> -			strncpy(least_seq_path, path, path_len);
>>> -			return ZOK;
>>> -		case ZNONODE:
>>> +			return rc;
>>> +		case ZNODEEXISTS:
>>> +			sleep(1);
>> 
>> It will take a very long time if we start many sheep daemons at the
>> same time?  I wonder if we should implement a complete distributed
>> lock based on ZooKeeper recipes:
>> 
>> http://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Locks
>> 
> 
> I think this is a better choice.


On the second thought, as I browse the code in zookeeper recipes, I think the
code is not strong enough.
For example, the lock function is based on creating sequence znode. However,
if error occurs during the creation, it is not easy to retry it. And the code in
the recipes didn't handle it at all.
(The code I found is in version 3.4.5, seems the logic in the code is not same as 
described in the website).

I would suggest that we use a little number for sleeping, e.g. 50ms.
And check the existence before creating the lock znode.
(checking existence is much cheeper than creating)

This may be simple but not the best choice. 
If it is a big issue in production, I think we can make it better later in other way.

Thanks,
Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wpkg.org/pipermail/sheepdog/attachments/20130808/396d6340/attachment-0004.html>


More information about the sheepdog mailing list