分布式锁-Zookeeper锁
前言
上篇我们说到分布式锁的大概与Redis锁的实现与原理,接下来我们介绍下Zookeeper锁。
Zookeeper锁
我们在实现zookeeper锁时也使用了别人撸好的轮子稍加封装,使用的是apache的curator。
-
首先,我们实例化一个Curator的客户端实例
@ConditionalOnClass(DistributedLock.class) @AutoConfigureAfter(DistributedLockProperties.class) public class ZookeeperLockAutoConfiguration { private final DistributedLockProperties properties; public ZookeeperLockAutoConfiguration(DistributedLockProperties properties) { this.properties = properties; } @Bean(name = "zookeeperLock", destroyMethod = "close") public DistributedLock zookeeperLock() { return new ZookeeperLock(curator()); } private CuratorFramework curator() { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(properties.getZookeeper().getConfigCenterHost(), retryPolicy); client.start(); return client; } }
-
配置完成后,我们可以使用api实现一个简单的逻辑封装
private final ConcurrentMap<String, InterProcessMutex> lockData = Maps.newConcurrentMap(); public <T> T lock(long waitTime, long releaseTime, Mutex<T> mutex) { final String lockPath = "/" + LOCK_PATH_PREFIX + "/" + mutex.lockPath(); InterProcessMutex lock = lockData.get(lockPath); if (lock == null){ lock = new InterProcessMutex(client, lockPath); lockData.put(lockPath, lock); } try { // 尝试获取锁 if (lock.acquire(waitTime, TimeUnit.MILLISECONDS)) { // 获取成功并执行相关逻辑 return mutex.execute(); } else { // 否则抛出获取异常 throw new LockException(String.format("Lock[path=%s] failed", lockPath)); } } catch (Exception e) { throw new LockException(String.format("Lock[path=%s] failed", lockPath)); } finally { try { // 判断是否是当前线程获取到锁 if (lock.isOwnedByCurrentThread()) { // 若是,则释放锁 lock.release(); } } catch (Exception e) { log.error(String.format("An error happened when try to release lock[path=%s]", lockPath), e); } } }
- 使用了acquire尝试获取锁
- 在finally中释放锁
原理分析
-
我们知道Zookeeper中的节点分为永久节点与临时节点两大类,在这两大类基础上还有SEQUENTIAL属性,该属性表示节点具有有序性。如果指定该属性,那么在这个节点创建时,Zookeeper会自动在其节点名称后面追加一个由父节点维护的递增数字
-
因为Zookeeper中节点路径是不可能重复的,所以可以利用这个机制加上有序的临时节点完成分布式锁的实现。
-
如果不是很了解Zookeeper的相关基础知识,可以参考 https://www.sohu.com/a/323031234_120104204
加锁
@Override
public boolean acquire(long time, TimeUnit unit) throws Exception
{
return internalLock(time, unit);
}
private final LockInternals internals;
private final String basePath;
private final ConcurrentMap<Thread, LockData> threadData = Maps.newConcurrentMap();
private boolean internalLock(long time, TimeUnit unit) throws Exception
{
/*
Note on concurrency: a given lockData instance
can be only acted on by a single thread so locking isn't necessary
*/
// 获取当前线程
Thread currentThread = Thread.currentThread();
// 在缓存中get当前线程是否已经获取到锁
LockData lockData = threadData.get(currentThread);
// 缓存中包含当前线程的锁
if ( lockData != null )
{
// re-entering
// 可重入次数 +1 并且get返回
lockData.lockCount.incrementAndGet();
// 返回true 结束锁的获取
return true;
}
// 尝试新增锁定当前path
String lockPath = internals.attemptLock(time, unit, getLockNodeBytes());
// 若新增节点成功,即锁定成功
if ( lockPath != null )
{
// 初始化lockData对象,并存入当前缓存中,可以看到LockData类中存在AtomicInteger用来记录锁的重入次数
LockData newLockData = new LockData(currentThread, lockPath);
threadData.put(currentThread, newLockData);
return true;
}
return false;
}
private static class LockData
{
final Thread owningThread;
final String lockPath;
final AtomicInteger lockCount = new AtomicInteger(1);
private LockData(Thread owningThread, String lockPath)
{
this.owningThread = owningThread;
this.lockPath = lockPath;
}
}
- 以上代码是InterProcessMutex类的acquire方法与internalLock方法
/**
* 尝试加锁
*/
String attemptLock(long time, TimeUnit unit, byte[] lockNodeBytes) throws Exception
{
final long startMillis = System.currentTimeMillis();
final Long millisToWait = (unit != null) ? unit.toMillis(time) : null;
final byte[] localLockNodeBytes = (revocable.get() != null) ? new byte[0] : lockNodeBytes;
int retryCount = 0;
String ourPath = null;
boolean hasTheLock = false;
boolean isDone = false;
while ( !isDone )
{
// 此处while循环默认只会执行一次,只有抛出NoNodeException才会将isDone置为false,并重新进入循环
isDone = true;
try
{
// 创建节点
ourPath = driver.createsTheLock(client, path, localLockNodeBytes);
// 判断是否是第一个节点,如果是则返回,否则等待前一个锁释放
// 解析可查看下面internalLockLoop方法的解析
hasTheLock = internalLockLoop(startMillis, millisToWait, ourPath);
}
catch ( KeeperException.NoNodeException e )
{
// gets thrown by StandardLockInternalsDriver when it can't find the lock node
// this can happen when the session expires, etc. So, if the retry allows, just try it all again
// 上面官方注释为,这里的异常只会发生在session过期的时候,所以增加了重试
if ( client.getZookeeperClient().getRetryPolicy().allowRetry(retryCount++, System.currentTimeMillis() - startMillis, RetryLoop.getDefaultRetrySleeper()) )
{
isDone = false;
}
else
{
throw e;
}
}
}
// 如果获得了锁
if ( hasTheLock )
{
// 返回节点路径
return ourPath;
}
return null;
}
/**
* 创建锁节点
*
* CreateMode.EPHEMERAL_SEQUENTIAL 临时有序节点
*/
public String createsTheLock(CuratorFramework client, String path, byte[] lockNodeBytes) throws Exception
{
String ourPath;
if ( lockNodeBytes != null )
{
ourPath = client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, lockNodeBytes);
}
else
{
ourPath = client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path);
}
return ourPath;
}
// 判断是否是第一个节点,如果是则返回,否则等待前一个锁释放
private boolean internalLockLoop(long startMillis, Long millisToWait, String ourPath) throws Exception
{
boolean haveTheLock = false;
boolean doDelete = false;
try
{
if ( revocable.get() != null )
{
client.getData().usingWatcher(revocableWatcher).forPath(ourPath);
}
// 如果没有获得当前锁
while ( (client.getState() == CuratorFrameworkState.STARTED) && !haveTheLock )
{
// 获取所有的排序过的子节点
List<String> children = getSortedChildren();
// 获取节点路径
String sequenceNodeName = ourPath.substring(basePath.length() + 1); // +1 to include the slash
// 判断当前节点是否是最小节点,如果不是则返回前一节点的路径
// 解析可看下面getsTheLock方法的源码
PredicateResults predicateResults = driver.getsTheLock(client, children, sequenceNodeName, maxLeases);
// 判断当前是否是最小节点
if ( predicateResults.getsTheLock() )
{
// 如果是,则确定已经获取到锁
haveTheLock = true;
}
else
{
// 拼接过去上一节点的完整路径
String previousSequencePath = basePath + "/" + predicateResults.getPathToWatch();
// 加锁
synchronized(this)
{
try
{
// use getData() instead of exists() to avoid leaving unneeded watchers which is a type of resource leak
// 注意上面的官方注释,之所以使用getData替代exists判断,是因为exists判断后,不管节点是否存在都会添加监听器,这样属于资源浪费
// 设置上一节点的监听器
client.getData().usingWatcher(watcher).forPath(previousSequencePath);
// 如果超时时间设置的不是空
if ( millisToWait != null )
{
// 超时时间减去现在已经使用过的时间
millisToWait -= (System.currentTimeMillis() - startMillis);
// 初始化开始时间
startMillis = System.currentTimeMillis();
// 如果超时时间已经结束
if ( millisToWait <= 0 )
{
// 删除当前节点标记设置为true
doDelete = true; // timed out - delete our node
// 跳出while循环
break;
}
// 等待剩余的等待时间
wait(millisToWait);
}
else
{
// 如果没有设置超时时间则一直等待
wait();
}
}
catch ( KeeperException.NoNodeException e )
{
// 如果报错则再次尝试
// it has been deleted (i.e. lock released). Try to acquire again
}
}
}
}
}
catch ( Exception e )
{
ThreadUtils.checkInterrupted(e);
doDelete = true;
throw e;
}
finally
{
if ( doDelete )
{
// 删除当前节点(无用)
deleteOurPath(ourPath);
}
}
// 返回获取锁的结果
return haveTheLock;
}
// 比对判断当前节点是否是最小节点
// maxLeases == 1 可以往上追溯到
public PredicateResults getsTheLock(CuratorFramework client, List<String> children, String sequenceNodeName, int maxLeases) throws Exception
{
// 获取当前节点在所有子节点的位置
int ourIndex = children.indexOf(sequenceNodeName);
validateOurIndex(sequenceNodeName, ourIndex);
// 若果当前子节点的位置小于1
boolean getsTheLock = ourIndex < maxLeases;
// 如果是则代表当前节点是最小的节点,如果不是则返回前一节点的路径
String pathToWatch = getsTheLock ? null : children.get(ourIndex - maxLeases);
return new PredicateResults(pathToWatch, getsTheLock);
}
- 以上代码为获取锁的代码,主要是添加节点 -> 判断是否最小节点 -> 不是则添加上一节点的监听器
释放锁
public void release() throws Exception
{
/*
Note on concurrency: a given lockData instance
can be only acted on by a single thread so locking isn't necessary
*/
// 获取当前线程
Thread currentThread = Thread.currentThread();
// 获取当前线程是否持有锁
LockData lockData = threadData.get(currentThread);
if ( lockData == null )
{
// 如果没有锁,则抛出异常
throw new IllegalMonitorStateException("You do not own the lock: " + basePath);
}
// 减少当前锁的重入次数
int newLockCount = lockData.lockCount.decrementAndGet();
// 如果重入次数大于0,则返回
if ( newLockCount > 0 )
{
return;
}
// 如果重入次数小于0,则抛出异常
if ( newLockCount < 0 )
{
throw new IllegalMonitorStateException("Lock count has gone negative for lock: " + basePath);
}
try
{
// 释放锁资源
internals.releaseLock(lockData.lockPath);
}
finally
{
// 删除缓存中当前线程的锁数据
threadData.remove(currentThread);
}
}
final void releaseLock(String lockPath) throws Exception
{
// 移除监听器
client.removeWatchers();
// 还没找到干啥的
revocable.set(null);
// 删除当前节点
deleteOurPath(lockPath);
}
五. 项目案例源码地址
针对redisson的redis锁与curator的zookeeper锁做了简单的封装,github项目地址如下:
时在中春,阳和方起