吸毒的虫子 发表于 2015-9-7 09:40:36

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


1.基本介绍
  
分布式锁是控制分布式系统之间同步訪问共享资源的一种方式,须要相互排斥来防止彼此干扰来保证一致性。
  利用Zookeeper的强一致性能够完毕锁服务。Zookeeper的官方文档是列举了两种锁。独占锁和共享锁。
  独占锁保证不论什么时候都仅仅有一个进程能或者资源的读写权限。共享锁能够同一时候有多个读,可是同一时刻最多仅仅能有一个写,读和写是相互排斥的。
  


2.场景分析
  
我们准备来实现相互排斥的锁,依照官网的思路,给定一个锁的路径,如/Lock,全部要申请这个锁的进程都在/Lock文件夹下创建一个/Lock/lock-的暂时序列节点,并监控/Lock的子节点变化事件。当子节点发送变化时用get_children()获取子节点的列表,假设发现进程发现自己拥有最小的一个序号,则获得锁。
  处理业务完成后须要释放锁,此时仅仅须要删除该暂时节点就可以。简单来说就是永远是拥有最小序号的进程获得锁。
  


3.场景实践
  
使用锁有两个主要的函数,就是lock或unlock.定义为


[*]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函数返回的结构体指针的指针。
  
接下来在看详细的实现。
  
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函数。
  
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中创建的暂时序列节点删除就能够了。
  
接下来在看下模拟程序的功能。
  
> ./mylock -h
Usage : [-h][-p path][-s ip:port]
-h Show help
-p lock path
-s zookeeper server ip:port
For example:
mylock -s 172.17.0.36:2181 -p /Lock




  
模拟程序有3个选项。
  当中

-s:为Zookeeper的server的ip:port.

-p: 为锁的路径。
  
分别同一时候执行多个mylock程序,就能够看到各个程序之间是怎样获取锁的了。
  
最后是完整的代码:
  
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include&quot;zookeeper.h&quot;
#include&quot;zookeeper_log.h&quot;
char g_host= &quot;172.17.0.36:2181&quot;;
char g_path= &quot;/Lock&quot;;
typedef struct Lock
{
char lockpath;
char selfpath;
}Lock;
void print_usage();
void get_option(int argc,const char* argv[]);
/**********unitl*********************/
void print_usage()
{
printf(&quot;Usage : [-h][-p path][-s ip:port] \n&quot;);
printf(&quot;      -h Show help\n&quot;);
printf(&quot;      -p lock path\n&quot;);
printf(&quot;      -s zookeeper server ip:port\n&quot;);
printf(&quot;For example:\n&quot;);
printf(&quot;    mylock -s172.17.0.36:2181 -p /Lock\n&quot;);
}
void get_option(int argc,const char* argv[])
{
extern char    *optarg;
int            optch;
int            dem = 1;
const char    optstring[] = &quot;hp:s:&quot;;

while((optch = getopt(argc , (char * const *)argv , optstring)) != -1 )
{
switch( optch )
{
case 'h':
print_usage();
exit(-1);
case '?':
print_usage();
printf(&quot;unknown parameter: %c\n&quot;, optopt);
exit(-1);
case ':':
print_usage();
printf(&quot;need parameter: %c\n&quot;, 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={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,&quot;1.0&quot;,strlen(&quot;1.0&quot;),
&ZOO_OPEN_ACL_UNSAFE,0,
path_buffer,bufferlen);
if(ret != ZOK){
fprintf(stderr,&quot;failed to create the path %s!\n&quot;,path);
}else{
printf(&quot;create path %s successfully!\n&quot;,path);
}
}
if(ret == ZOK){
char child_path;
sprintf(child_path,&quot;%s/lock-&quot;,path);
ret = zoo_create(zkhandle,child_path,&quot;1.0&quot;,strlen(&quot;1.0&quot;),
&ZOO_OPEN_ACL_UNSAFE,ZOO_SEQUENCE|ZOO_EPHEMERAL,
path_buffer,bufferlen);
if(ret != ZOK){
fprintf(stderr,&quot;failed to create the path %s!\n&quot;,path);
}else{
printf(&quot;create path %s successfully!\n&quot;,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,&quot;error when get children of path %s\n&quot;,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,myseq) < 0){
ret = 0;
break;
}            
}
for(i = 0; i < children.count; ++i){
free(children.data);
children.data = 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,&quot;error when create lock %s.\n&quot;,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,&quot;error when release lock %s.\n&quot;,(*lock)->selfpath);
}
free(*lock);
*lock = NULL;
return ret;
}
return ZOK;
}
int main(int argc, const char *argv[])
{
int timeout = 30000;
char path_buffer;
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 *)&quot;lock Test&quot;, 0);
if (zkhandle ==NULL)
{
fprintf(stderr, &quot;Error when connecting to zookeeper servers...\n&quot;);
exit(EXIT_FAILURE);
}
int ret = zoo_exists(zkhandle,g_path,0,NULL);
if(ret != ZOK){
ret = zoo_create(zkhandle,g_path,&quot;1.0&quot;,strlen(&quot;1.0&quot;),
&ZOO_OPEN_ACL_UNSAFE,0,
path_buffer,bufferlen);
if(ret != ZOK){
fprintf(stderr,&quot;failed to create the path %s!\n&quot;,g_path);
}else{
printf(&quot;create path %s successfully!\n&quot;,g_path);
}
}
if(ret == ZOK ){
Lock *mylock = lock(zkhandle,g_path);
if(mylock){
printf(&quot;get lock of %s.\n&quot;,g_path);
printf(&quot;self path is %s.\n&quot;,mylock->selfpath);
printf(&quot;do something....\n&quot;);
getchar();
unlock(zkhandle,&mylock);
}   
}
zookeeper_close(zkhandle);
return 0;
}








         
版权声明:本文博客原创文章,博客,未经同意,不得转载。
页: [1]
查看完整版本: ZooKeeper实践方案:(7) 分布式锁