redis分佈式鎖RedissonLock的實現細節解析
redis分佈式鎖RedissonLock
簡單使用
String key = "key-lock"; RLock lock = redisson.getLock(key); lock.lock(); try { // TODO } catch (Exception e){ log.error(e.getMessage(), e); } finally { lock.unlock(); }
String key = "key-tryLock"; long maxWaitTime = 3_000; RLock lock = redisson.getLock(key); if (lock.tryLock(maxWaitTime, TimeUnit.MILLISECONDS)){ try { // TODO } catch (Exception e){ log.error(e.getMessage(), e); } finally { lock.unlock(); } } else { log.debug("redis鎖競爭失敗"); }
流程圖
多個線程節點鎖競爭的正常流程如下圖:
多個線程節點鎖競爭,並出現節點下線的異常流程如下圖:
源碼解析
RedissonLock是可重入鎖,使用redis的hash結構作為鎖的標識存儲,鎖的名稱作為hash的key,UUID + 線程ID作為hash的field,鎖被重入的次數作為hash的value。如圖所示:
private void lock(long leaseTime, TimeUnit unit, boolean interruptibly) throws InterruptedException { long threadId = Thread.currentThread().getId(); // 嘗試獲取鎖,鎖獲取成功則ttl為null;獲取失敗則返回鎖的剩餘過期時間 Long ttl = tryAcquire(leaseTime, unit, threadId); if (ttl == null) { return; } // 鎖被其他線程占用而索取失敗,使用線程通知而非自旋的方式等待鎖 // 使用redis的發佈訂閱pub/sub功能來等待鎖的釋放通知 RFuture<RedissonLockEntry> future = subscribe(threadId); commandExecutor.syncSubscription(future); try { while (true) { ttl = tryAcquire(leaseTime, unit, threadId); // 嘗試獲取鎖,鎖獲取成功則ttl為null;獲取失敗則返回鎖的剩餘過期時間 if (ttl == null) { break; } if (ttl >= 0) { // 使用LockSupport.parkNanos方法線程休眠 try { getEntry(threadId).getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { if (interruptibly) { throw e; } getEntry(threadId).getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } } else { if (interruptibly) { getEntry(threadId).getLatch().acquire(); } else { getEntry(threadId).getLatch().acquireUninterruptibly(); } } } } finally { // 退出鎖競爭(鎖獲取成功或者放棄獲取鎖),則取消鎖的釋放訂閱 unsubscribe(future, threadId); } }
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { long time = unit.toMillis(waitTime); long current = System.currentTimeMillis(); long threadId = Thread.currentThread().getId(); Long ttl = tryAcquire(leaseTime, unit, threadId); if (ttl == null) { return true; } time -= System.currentTimeMillis() - current; if (time <= 0) { acquireFailed(threadId); return false; } current = System.currentTimeMillis(); RFuture<RedissonLockEntry> subscribeFuture = subscribe(threadId); if (!await(subscribeFuture, time, TimeUnit.MILLISECONDS)) { if (!subscribeFuture.cancel(false)) { subscribeFuture.onComplete((res, e) -> { if (e == null) { unsubscribe(subscribeFuture, threadId); } }); } acquireFailed(threadId); return false; } try { time -= System.currentTimeMillis() - current; if (time <= 0) { acquireFailed(threadId); return false; } while (true) { long currentTime = System.currentTimeMillis(); ttl = tryAcquire(leaseTime, unit, threadId); // lock acquired if (ttl == null) { return true; } time -= System.currentTimeMillis() - currentTime; if (time <= 0) { acquireFailed(threadId); return false; } currentTime = System.currentTimeMillis(); if (ttl >= 0 && ttl < time) { getEntry(threadId).getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } else { getEntry(threadId).getLatch().tryAcquire(time, TimeUnit.MILLISECONDS); } time -= System.currentTimeMillis() - currentTime; if (time <= 0) { acquireFailed(threadId); return false; } } } finally { unsubscribe(subscribeFuture, threadId); } }
RedissonLock實現的是可重入鎖,通過redis的hash結構實現,而非加單的set nx ex。為瞭實現原子性的復雜的加鎖邏輯,而通過lua腳本實現。獲取鎖會有如下三種狀態:
1、鎖未被任何線程占用,則鎖獲取成功,返回null
2、鎖被當前線程占用,則鎖獲取成功並進行鎖的重入,對鎖的重入計數+1,返回null
3、鎖被其他線程占用,則鎖獲取失敗,返回該鎖的自動過期時間ttl
<T> RFuture<T> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) { internalLockLeaseTime = unit.toMillis(leaseTime); return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, command, "if (redis.call('exists', KEYS[1]) == 0) then " + "redis.call('hset', KEYS[1], ARGV[2], 1); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return nil; " + "end; " + "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " + "redis.call('hincrby', KEYS[1], ARGV[2], 1); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return nil; " + "end; " + "return redis.call('pttl', KEYS[1]);", Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId)); }
當鎖因為被其他線程占用而 使用redis的發佈訂閱pub/sub功能,通過監聽鎖的釋放通知(在其他線程通過RedissonLock釋放鎖時,會通過發佈訂閱pub/sub功能發起通知),等待鎖被其他線程釋放。通過如此的線程喚醒而非自旋的操作,提高瞭鎖的效率。
public RFuture<E> subscribe(String entryName, String channelName) { AtomicReference<Runnable> listenerHolder = new AtomicReference<Runnable>(); AsyncSemaphore semaphore = service.getSemaphore(new ChannelName(channelName)); RPromise<E> newPromise = new RedissonPromise<E>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { return semaphore.remove(listenerHolder.get()); } }; Runnable listener = new Runnable() { @Override public void run() { E entry = entries.get(entryName); if (entry != null) { entry.aquire(); semaphore.release(); entry.getPromise().onComplete(new TransferListener<E>(newPromise)); return; } E value = createEntry(newPromise); value.aquire(); E oldValue = entries.putIfAbsent(entryName, value); if (oldValue != null) { oldValue.aquire(); semaphore.release(); oldValue.getPromise().onComplete(new TransferListener<E>(newPromise)); return; } RedisPubSubListener<Object> listener = createListener(channelName, value); service.subscribe(LongCodec.INSTANCE, channelName, semaphore, listener); } }; semaphore.acquire(listener); listenerHolder.set(listener); return newPromise; }
由於是可重入鎖則需要在釋放鎖的時候做訂閱通知,因此釋放鎖的操作同樣是lua腳本實現。鎖的釋放會有如下三個狀態:
1、等待釋放的鎖不存在或者不是當前線程持有,返回null
2、等待釋放的鎖被當前線程持有,且該鎖當前被重入多次,則鎖的重入計數-1,返回0
3、等待釋放的鎖被當前線程持有,且該鎖當前未被重入,則鎖的刪除並發佈該鎖釋放的訂閱通知,返回1
protected RFuture<Boolean> unlockInnerAsync(long threadId) { return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then " + "return nil;" + "end; " + "local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); " + "if (counter > 0) then " + "redis.call('pexpire', KEYS[1], ARGV[2]); " + "return 0; " + "else " + "redis.call('del', KEYS[1]); " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1; "+ "end; " + "return nil;", Arrays.<Object>asList(getName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName(threadId)); }
Watchdog
RedissonLock為瞭避免應用獲取鎖後宕機,因為沒人來釋放鎖而導致死鎖情況的出現,默認每次鎖的占用隻有30秒的時間(org.redisson.config.Config#lockWatchdogTimeout = 30 * 1000)。
於是便有瞭Watchdog設計,由獨立的線程定時給未釋放的鎖續期,默認鎖有效期的三分之一的時長即每10秒給鎖自動續期。
private void renewExpiration() { ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName()); if (ee == null) { return; } // 默認10秒鐘後執行鎖續期任務 Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName()); if (ent == null) { return; } Long threadId = ent.getFirstThreadId(); if (threadId == null) { return; } RFuture<Boolean> future = renewExpirationAsync(threadId); future.onComplete((res, e) -> { if (e != null) { log.error("Can't update lock " + getName() + " expiration", e); return; } // 如果鎖續期成功,則10秒鐘後再次續期 if (res) { renewExpiration(); } }); } }, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS); ee.setTimeout(task); } protected RFuture<Boolean> renewExpirationAsync(long threadId) { return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return 1; " + "end; " + "return 0;", Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId)); }
Redisson 幾種鎖
1. 可重入鎖(Reentrant Lock)
Redisson的分佈式可重入鎖RLock Java對象實現瞭java.util.concurrent.locks.Lock接口,同時還支持自動過期解鎖。
public void testReentrantLock(RedissonClient redisson){ RLock lock = redisson.getLock("anyLock"); try{ // 1. 最常見的使用方法 //lock.lock(); // 2. 支持過期解鎖功能,10秒鐘以後自動解鎖, 無需調用unlock方法手動解鎖 //lock.lock(10, TimeUnit.SECONDS); // 3. 嘗試加鎖,最多等待3秒,上鎖以後10秒自動解鎖 boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS); if(res){ //成功 // do your business } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
Redisson同時還為分佈式鎖提供瞭異步執行的相關方法:
public void testAsyncReentrantLock(RedissonClient redisson){ RLock lock = redisson.getLock("anyLock"); try{ lock.lockAsync(); lock.lockAsync(10, TimeUnit.SECONDS); Future<Boolean> res = lock.tryLockAsync(3, 10, TimeUnit.SECONDS); if(res.get()){ // do your business } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } finally { lock.unlock(); } }
2. 公平鎖(Fair Lock)
Redisson分佈式可重入公平鎖也是實現瞭java.util.concurrent.locks.Lock接口的一種RLock對象。在提供瞭自動過期解鎖功能的同時,保證瞭當多個Redisson客戶端線程同時請求加鎖時,優先分配給先發出請求的線程。
public void testFairLock(RedissonClient redisson){ RLock fairLock = redisson.getFairLock("anyLock"); try{ // 最常見的使用方法 fairLock.lock(); // 支持過期解鎖功能, 10秒鐘以後自動解鎖,無需調用unlock方法手動解鎖 fairLock.lock(10, TimeUnit.SECONDS); // 嘗試加鎖,最多等待100秒,上鎖以後10秒自動解鎖 boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } finally { fairLock.unlock(); } }
Redisson同時還為分佈式可重入公平鎖提供瞭異步執行的相關方法:
RLock fairLock = redisson.getFairLock("anyLock"); fairLock.lockAsync(); fairLock.lockAsync(10, TimeUnit.SECONDS); Future<Boolean> res = fairLock.tryLockAsync(100, 10, TimeUnit.SECONDS);
3. 聯鎖(MultiLock)
Redisson的RedissonMultiLock對象可以將多個RLock對象關聯為一個聯鎖,每個RLock對象實例可以來自於不同的Redisson實例。
public void testMultiLock(RedissonClient redisson1, RedissonClient redisson2, RedissonClient redisson3){ RLock lock1 = redisson1.getLock("lock1"); RLock lock2 = redisson2.getLock("lock2"); RLock lock3 = redisson3.getLock("lock3"); RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3); try { // 同時加鎖:lock1 lock2 lock3, 所有的鎖都上鎖成功才算成功。 lock.lock(); // 嘗試加鎖,最多等待100秒,上鎖以後10秒自動解鎖 boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
4. 紅鎖(RedLock)
Redisson的RedissonRedLock對象實現瞭Redlock介紹的加鎖算法。該對象也可以用來將多個RLock
對象關聯為一個紅鎖,每個RLock對象實例可以來自於不同的Redisson實例。
public void testRedLock(RedissonClient redisson1, RedissonClient redisson2, RedissonClient redisson3){ RLock lock1 = redisson1.getLock("lock1"); RLock lock2 = redisson2.getLock("lock2"); RLock lock3 = redisson3.getLock("lock3"); RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3); try { // 同時加鎖:lock1 lock2 lock3, 紅鎖在大部分節點上加鎖成功就算成功。 lock.lock(); // 嘗試加鎖,最多等待100秒,上鎖以後10秒自動解鎖 boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }
5. 讀寫鎖(ReadWriteLock)
Redisson的分佈式可重入讀寫鎖RReadWriteLock Java對象實現瞭java.util.concurrent.locks.ReadWriteLock接口。同時還支持自動過期解鎖。該對象允許同時有多個讀取鎖,但是最多隻能有一個寫入鎖。
RReadWriteLock rwlock = redisson.getLock("anyRWLock"); // 最常見的使用方法 rwlock.readLock().lock(); // 或 rwlock.writeLock().lock(); // 支持過期解鎖功能 // 10秒鐘以後自動解鎖 // 無需調用unlock方法手動解鎖 rwlock.readLock().lock(10, TimeUnit.SECONDS); // 或 rwlock.writeLock().lock(10, TimeUnit.SECONDS); // 嘗試加鎖,最多等待100秒,上鎖以後10秒自動解鎖 boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS); // 或 boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS); ... lock.unlock();
6. 信號量(Semaphore)
Redisson的分佈式信號量(Semaphore)Java對象RSemaphore采用瞭與java.util.concurrent.Semaphore相似的接口和用法。
RSemaphore semaphore = redisson.getSemaphore("semaphore"); semaphore.acquire(); //或 semaphore.acquireAsync(); semaphore.acquire(23); semaphore.tryAcquire(); //或 semaphore.tryAcquireAsync(); semaphore.tryAcquire(23, TimeUnit.SECONDS); //或 semaphore.tryAcquireAsync(23, TimeUnit.SECONDS); semaphore.release(10); semaphore.release(); //或 semaphore.releaseAsync();
7. 可過期性信號量(PermitExpirableSemaphore)
Redisson的可過期性信號量(PermitExpirableSemaphore)實在RSemaphore對象的基礎上,為每個信號增加瞭一個過期時間。每個信號可以通過獨立的ID來辨識,釋放時隻能通過提交這個ID才能釋放。
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore"); String permitId = semaphore.acquire(); // 獲取一個信號,有效期隻有2秒鐘。 String permitId = semaphore.acquire(2, TimeUnit.SECONDS); // ... semaphore.release(permitId);
8. 閉鎖(CountDownLatch)
Redisson的分佈式閉鎖(CountDownLatch)Java對象RCountDownLatch采用瞭與java.util.concurrent.CountDownLatch相似的接口和用法。
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Redis分佈式鎖Redlock的實現
- redis分佈式鎖的8大坑總結梳理
- Redis中Redisson紅鎖(Redlock)使用原理
- Redisson可重入鎖解鎖邏輯詳細講解
- Redis分佈式鎖如何自動續期的實現