europelee created ZOOKEEPER-2409:
------------------------------------

             Summary: zookeeper recipes lock's c implement: function 
child_floor bug
                 Key: ZOOKEEPER-2409
                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-2409
             Project: ZooKeeper
          Issue Type: Bug
          Components: c client
    Affects Versions: 3.4.8
         Environment: zookeeper cluster with multi servers(not standalone 
mode), and multi clients connect to different zookeeper server.
            Reporter: europelee


1. start zookeeper cluster with multi servers
2. multi clients connect to different zookeeper server at the same time
3. clients use lock from zookeeper recipes lock's C implement.

for example:
Client A  create a node like x-025373e3a9960050-0000000067
and Client B create a node like x-015373e3a9960050-0000000068;
A is a lock owner now, then kill A,  as expect, B should become owner, but in 
fact B not.
Because in zoo_lock.c, function  zkr_lock_operation call child_floor to 
monitoring a pre node, but child_floor has bug, it caused B not check its 
prenode A.

B function child_floor just simply strcmp "x-025373e3a9960050-0000000067" with 
own node "x-015373e3a9960050-0000000068", 
it should only strcmp "0000000067" with "0000000068", not include session info.
besides, it is better that using binary search than travelling every node for 
looking for a pre node when there exists many nodes.
fix:
static char* child_floor(char **sorted_data, int len, char *element) {
    char* ret = NULL;

    int begin = 0;
    int end = len-1;
    int index = 0;

    while (begin <= end) {
        index = (begin+end)/2;
        int iCmpRet = strcmp(strrchr(sorted_data[index], '-')+1, 
strrchr(element, '-')+1);
        if (iCmpRet < 0) {
            begin = index + 1;
        }
        else {
            if (iCmpRet == 0) {
                if (index - 1 >= 0) {
                    ret = sorted_data[index-1];
                }
                break;
            }
            else {
                end = index - 1;
            }
        }
    }

    return ret;
}




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to