ZooKeeper实践方案:(7) 分布式锁

2022-07-05 21:03:27 浏览数 (1)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

1.基本介绍

分布式锁是控制分布式系统之间同步訪问共享资源的一种方式,须要相互排斥来防止彼此干扰来保证一致性。

利用Zookeeper的强一致性能够完毕锁服务。Zookeeper的官方文档是列举了两种锁。独占锁和共享锁。

独占锁保证不论什么时候都仅仅有一个进程能或者资源的读写权限。共享锁能够同一时候有多个读,可是同一时刻最多仅仅能有一个写,读和写是相互排斥的。

2.场景分析

我们准备来实现相互排斥的锁,依照官网的思路,给定一个锁的路径,如/Lock,全部要申请这个锁的进程都在/Lock文件夹下创建一个/Lock/lock-的暂时序列节点,并监控/Lock的子节点变化事件。当子节点发送变化时用get_children()获取子节点的列表,假设发现进程发现自己拥有最小的一个序号,则获得锁。

处理业务完成后须要释放锁,此时仅仅须要删除该暂时节点就可以。简单来说就是永远是拥有最小序号的进程获得锁。

3.场景实践

使用锁有两个主要的函数,就是lockunlock.定义为

  • Lock *lock(zhandle_t *zkhandle,const char *path) lock函数有两个參数,一个是zookeeper_init返回的句柄zkhandle,还有一个是锁的路径,假设成功则返回一个Lock的结构体指针。并同一时候获得锁,否则返回NULL。
  • int unlock(zhandle_t *zkhandle,Lock * *lock) unlock函数也有两个參数。一个是zookeeper_init返回的句柄zkhandle,还有一个是lock函数返回的结构体指针的指针

接下来在看详细的实现。

代码语言:javascript复制
Lock *lock(zhandle_t *zkhandle,const char *path){    Lock *lock = create_lock(zkhandle,path);    if(lock != NULL){        while(try_lock(zkhandle,lock) == 0){            sleep(1);        }    }else{        fprintf(stderr,"error when create lock %s.n",path);    }    return lock;}
  • create_lock:负责锁的初始化。主要功能是负责创建{path}的节点已经{path}/lock-的暂时序列节点。 {path}假设存在则不再创建。
  • try_lock:尝试加锁,这个函数不会等待,失败和成功都马上返回。其主要功能是获取{path}的子节点列表,并查看自己是否是拥有最小序列号的节点。假设是则返回1,否则返回0;

lock函数初始化锁后,会持续的尝试加锁,直到成功。尽管我是这样实现的。可是过于简单粗暴(哈哈)。假设拿不到锁的话。持续就会堵塞在lock函数。

代码语言:javascript复制
int unlock(zhandle_t *zkhandle,Lock * *lock){    if(*lock){        int ret = zoo_delete(zkhandle,(*lock)->selfpath,-1);        if(ret != ZOK){            fprintf(stderr,"error when release lock %s.n",(*lock)->selfpath);        }        free(*lock);        *lock = NULL;        return ret;    }    return ZOK;}

unlock函数就很easy了。就是将create_lock中创建的暂时序列节点删除就能够了。

接下来在看下模拟程序的功能。

代码语言:javascript复制
> ./mylock -hUsage : [mylock] [-h]  [-p path][-s ip:port]         -h Show help        -p lock path        -s zookeeper server ip:portFor example:    mylock -s 172.17.0.36:2181 -p /Lock

模拟程序有3个选项。

当中 -s:为Zookeeper的server的ip:port. -p: 为锁的路径。

分别同一时候执行多个mylock程序,就能够看到各个程序之间是怎样获取锁的了。

最后是完整的代码:

代码语言:javascript复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* #include<stdio.h>  #include<string.h>  #include<unistd.h>#include"zookeeper.h"  #include"zookeeper_log.h"  char g_host[512]= "172.17.0.36:2181";  char g_path[512]= "/Lock";typedef struct Lock{    char lockpath[1024];    char selfpath[1024];}Lock;void print_usage();void get_option(int argc,const char* argv[]);/**********unitl*********************/  void print_usage(){    printf("Usage : [mylock] [-h]  [-p path][-s ip:port] n");    printf("        -h Show helpn");    printf("        -p lock pathn");    printf("        -s zookeeper server ip:portn");    printf("For example:n");    printf("    mylock -s172.17.0.36:2181 -p /Lockn");}void get_option(int argc,const char* argv[]){    extern char    *optarg;    int            optch;    int            dem = 1;    const char    optstring[] = "hp:s:";    while((optch = getopt(argc , (char * const *)argv , optstring)) != -1 )    {        switch( optch )        {        case 'h':            print_usage();            exit(-1);        case '?':            print_usage();            printf("unknown parameter: %cn", optopt);            exit(-1);        case ':':            print_usage();            printf("need parameter: %cn", optopt);            exit(-1);        case 's':            strncpy(g_host,optarg,sizeof(g_host));            break;        case 'p':            strncpy(g_path,optarg,sizeof(g_path));            break;        default:            break;        }    }} Lock *create_lock(zhandle_t *zkhandle,const char *path){    char path_buffer[512]={0};    int bufferlen = sizeof(path_buffer);    Lock * lock = NULL;    int ret = zoo_exists(zkhandle,path,0,NULL);     if(ret != ZOK){        ret = zoo_create(zkhandle,path,"1.0",strlen("1.0"),                            &ZOO_OPEN_ACL_UNSAFE,0,                            path_buffer,bufferlen);          if(ret != ZOK){            fprintf(stderr,"failed to create the path %s!n",path);        }else{            printf("create path %s successfully!n",path);        }    }    if(ret == ZOK){        char child_path[512];        sprintf(child_path,"%s/lock-",path);        ret = zoo_create(zkhandle,child_path,"1.0",strlen("1.0"),                            &ZOO_OPEN_ACL_UNSAFE,ZOO_SEQUENCE|ZOO_EPHEMERAL,                            path_buffer,bufferlen);          if(ret != ZOK){            fprintf(stderr,"failed to create the path %s!n",path);        }else{            printf("create path %s successfully!n",path);        }    }    if(ret == ZOK){        lock = (Lock *)malloc(sizeof(Lock));        strcpy(lock->lockpath,path);        strcpy(lock->selfpath,path_buffer);    }    return lock;}int try_lock(zhandle_t *zkhandle,Lock *lock){    struct String_vector children;    int i = 0;    int ret = zoo_get_children(zkhandle,lock->lockpath,0,&children);    if(ret != ZOK){        fprintf(stderr,"error when get children of path %sn",lock->lockpath);        ret = -1;    }else{        char *myseq = rindex(lock->selfpath,'/');        if (myseq != NULL) myseq  = 1;        ret = 1;        for(i = 0; i < children.count;   i){            if(strcmp(children.data[i],myseq) < 0){                ret = 0;                break;            }                    }        for(i = 0; i < children.count;   i){            free(children.data[i]);            children.data[i] = NULL;        }    }    return ret;}Lock *lock(zhandle_t *zkhandle,const char *path){    Lock *lock = create_lock(zkhandle,path);    if(lock != NULL){        while(try_lock(zkhandle,lock) == 0){            sleep(1);        }    }else{        fprintf(stderr,"error when create lock %s.n",path);    }    return lock;}int unlock(zhandle_t *zkhandle,Lock * *lock){    if(*lock){        int ret = zoo_delete(zkhandle,(*lock)->selfpath,-1);        if(ret != ZOK){            fprintf(stderr,"error when release lock %s.n",(*lock)->selfpath);        }        free(*lock);        *lock = NULL;        return ret;    }    return ZOK;}int main(int argc, const char *argv[])  {      int timeout = 30000;      char path_buffer[512];      int bufferlen=sizeof(path_buffer);      zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); //设置日志级别,避免出现一些其它信息      get_option(argc,argv);    zhandle_t* zkhandle = zookeeper_init(g_host,NULL, timeout, 0, (char *)"lock Test", 0);      if (zkhandle ==NULL)      {          fprintf(stderr, "Error when connecting to zookeeper servers...n");          exit(EXIT_FAILURE);      }      int ret = zoo_exists(zkhandle,g_path,0,NULL);     if(ret != ZOK){        ret = zoo_create(zkhandle,g_path,"1.0",strlen("1.0"),                            &ZOO_OPEN_ACL_UNSAFE,0,                            path_buffer,bufferlen);          if(ret != ZOK){            fprintf(stderr,"failed to create the path %s!n",g_path);        }else{            printf("create path %s successfully!n",g_path);        }    }    if(ret == ZOK ){       Lock *mylock = lock(zkhandle,g_path);        if(mylock){            printf("get lock of %s.n",g_path);            printf("self path is %s.n",mylock->selfpath);            printf("do something....n");            getchar();            unlock(zkhandle,&mylock);        }        }    zookeeper_close(zkhandle);     return 0;}
*/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117603.html原文链接:https://javaforall.cn

0 人点赞