curator分布式锁源码分析
2024-03-05 20:26:29   0  举报             
     
         
 Curator分布式锁源码分析主要研究了Curator框架在分布式锁方面的实现。Curator是Netflix开发的一个ZooKeeper客户端,它提供了一系列操作ZooKeeper的API,包括分布式锁的实现。在分析过程中,主要关注了Curator如何利用ZooKeeper的特性(如临时节点、有序节点等)来实现分布式锁的功能。同时,也探讨了Curator分布式锁在实际应用中的性能、优缺点以及适用场景。
    作者其他创作
 大纲/内容
 zookeeper会通知当前节点监听的监听器
  private final Watcher watcher = new Watcher(){@Overridepublic void process(WatchedEvent event){client.postSafeNotify(LockInternals.this);}};
  return
  while ( (client.getState() == CuratorFrameworkState.STARTED) && !haveTheLock )尝试加锁
  String  previousSequencePath = basePath + \"/\" + predicateResults.getPathToWatch();
  继续循环获取锁
  true
  对当前节点的上一个节点增加监听
  internals.releaseLock(lockData.lockPath);
  int newLockCount = lockData.lockCount.decrementAndGet();
  LockData lockData = threadData.get(currentThread);if ( lockData != null ){// re-enteringlockData.lockCount.incrementAndGet();return true;}
  lock.release();
  haveTheLock = true;
  唤醒
  StandardLockInternalsDriver.getsTheLock()
  client.getData().usingWatcher(watcher).forPath(previousSequencePath);
  return true加锁成功
  if ( newLockCount  0 )
  可重入锁直接减1
  false
  children = getSortedChildren();
                  if ( predicateResults.getsTheLock() )
  wait();
  这里的可重入锁与redisson的有差别,redisson是在锁的value上加1
  阻塞等待
  这里面会创建临时节点
  Thread currentThread = Thread.currentThread();        LockData lockData = threadData.get(currentThread);
  获取节点下,排好序的子节点列表
  threadData.remove(currentThread);
  //移除当前线程的监听器client.removeWatchers();//删除节点路径deleteOurPath(lockPath);
  client.delete().guaranteed().forPath(ourPath);
  lock.acquire();
   
 
 
 
 
  0 条评论
 下一页
  
   
   
   
   
  
  
  
  
  
  
  
  
 