原文鏈接:Redisson分佈式鎖學習總結:公平鎖 RedissonFairLock#lock 獲取鎖源碼分析
一、RedissonFairLock#lock 源碼分析
public class RedissonFairLockDemo {
public static void main(String[] args) {
RedissonClient client = RedissonClientUtil.getClient("");
RLock fairLock = client.getFairLock("myLock");
// 最常見的使用方法
try {
fairLock.lock();
}catch (Exception e){
e.printStackTrace();
}finally {
fairLock.unlock();
}
}
}
1、根據鎖key計算出 slot,一個slot對應的是redis集羣的一個節點
RedissonFairLock 其實是 RedissonLock 的子類,它主要是基於 RedissonLock 做的擴展,主要擴展在於加鎖和釋放鎖的地方,其他的邏輯都直接複用 RedissonLock:例如加鎖前計算slot、watchdog機制等等。
2、RedissonFairLock 之 lua 腳本加鎖
RedissonFairLock#tryLockInnerAsync:裏面有兩段 lua 腳本,我們現在只需要關注第二段即可。
if (command == RedisCommands.EVAL_LONG) {
return evalWriteAsync(getName(), LongCodec.INSTANCE, command,
// remove stale threads
"while true do " +
"local firstThreadId2 = redis.call('lindex', KEYS[2], 0);" +
"if firstThreadId2 == false then " +
"break;" +
"end;" +
"local timeout = tonumber(redis.call('zscore', KEYS[3], firstThreadId2));" +
"if timeout <= tonumber(ARGV[4]) then " +
// remove the item from the queue and timeout set
// NOTE we do not alter any other timeout
"redis.call('zrem', KEYS[3], firstThreadId2);" +
"redis.call('lpop', KEYS[2]);" +
"else " +
"break;" +
"end;" +
"end;" +
// check if the lock can be acquired now
"if (redis.call('exists', KEYS[1]) == 0) " +
"and ((redis.call('exists', KEYS[2]) == 0) " +
"or (redis.call('lindex', KEYS[2], 0) == ARGV[2])) then " +
// remove this thread from the queue and timeout set
"redis.call('lpop', KEYS[2]);" +
"redis.call('zrem', KEYS[3], ARGV[2]);" +
// decrease timeouts for all waiting in the queue
"local keys = redis.call('zrange', KEYS[3], 0, -1);" +
"for i = 1, #keys, 1 do " +
"redis.call('zincrby', KEYS[3], -tonumber(ARGV[3]), keys[i]);" +
"end;" +
// acquire the lock and set the TTL for the lease
"redis.call('hset', KEYS[1], ARGV[2], 1);" +
"redis.call('pexpire', KEYS[1], ARGV[1]);" +
"return nil;" +
"end;" +
// check if the lock is already held, and this is a re-entry
"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;" +
// the lock cannot be acquired
// check if the thread is already in the queue
"local timeout = redis.call('zscore', KEYS[3], ARGV[2]);" +
"if timeout ~= false then " +
// the real timeout is the timeout of the prior thread
// in the queue, but this is approximately correct, and
// avoids having to traverse the queue
"return timeout - tonumber(ARGV[3]) - tonumber(ARGV[4]);" +
"end;" +
// add the thread to the queue at the end, and set its timeout in the timeout set to the timeout of
// the prior thread in the queue (or the timeout of the lock if the queue is empty) plus the
// threadWaitTime
"local lastThreadId = redis.call('lindex', KEYS[2], -1);" +
"local ttl;" +
"if lastThreadId ~= false and lastThreadId ~= ARGV[2] then " +
"ttl = tonumber(redis.call('zscore', KEYS[3], lastThreadId)) - tonumber(ARGV[4]);" +
"else " +
"ttl = redis.call('pttl', KEYS[1]);" +
"end;" +
"local timeout = ttl + tonumber(ARGV[3]) + tonumber(ARGV[4]);" +
"if redis.call('zadd', KEYS[3], timeout, ARGV[2]) == 1 then " +
"redis.call('rpush', KEYS[2], ARGV[2]);" +
"end;" +
"return ttl;",
Arrays.asList(getName(), threadsQueueName, timeoutSetName),
internalLockLeaseTime, getLockName(threadId), wait, currentTime);
}
lua 腳本雖然很長,但其實作者給的註釋也是非常的清晰,讓我們知道lua腳本每一步的含義,所以下面我將講解每一個分支究竟利用redis命令做了什麼。
2.1、KEYS
Arrays.asList(getName(), threadsQueueName, timeoutSetName):
- getName(): 鎖key
- threadsQueueName:prefixName("redisson_lock_queue", name),用於鎖排隊
- timeoutSetName:prefixName("redisson_lock_timeout", name),用於隊列中每個客戶端的等待超時時間
KEYS:["myLock","redisson_lock_queue:{myLock}","redisson_lock_timeout:{myLock}"]
2.2、ARGVS
internalLockLeaseTime, getLockName(threadId), wait, currentTime:
- internalLockLeaseTime:其實就是 watchdog 的超時時間,默認是30000毫秒,可看 Config#lockWatchdogTimeout。
private long lockWatchdogTimeout = 30 * 1000;
- getLockName(threadId):return id + ":" + threadId,客戶端ID(UUID):線程ID(threadId)
- wait:就是 threadWaitTime,默認30_0000毫秒
public RedissonFairLock(CommandAsyncExecutor commandExecutor, String name) { this(commandExecutor, name, 60000*5); } public RedissonFairLock(CommandAsyncExecutor commandExecutor, String name, long threadWaitTime) { super(commandExecutor, name); this.commandExecutor = commandExecutor; this.threadWaitTime = threadWaitTime; threadsQueueName = prefixName("redisson_lock_queue", name); timeoutSetName = prefixName("redisson_lock_timeout", name); }
- currentTime:當前時間時間戳
ARGVS:[30_000毫秒,"UUID:threadId",30_0000毫秒,當前時間戳]
2.3、lua 腳本分析
1、分支一:清理過期的等待線程
場景:
這個死循環的作用主要用於清理過期的等待線程,主要避免下面場景,避免無效客戶端佔用等待隊列資源
- 獲取鎖失敗,然後進入等待隊列,但是網絡出現問題,那麼後續很有可能就不能繼續正常獲取鎖了。
- 獲取鎖失敗,然後進入等待隊列,但是之後客戶端所在服務器宕機了。
"while true do " +
"local firstThreadId2 = redis.call('lindex', KEYS[2], 0);" +
"if firstThreadId2 == false then " +
"break;" +
"end;" +
"local timeout = tonumber(redis.call('zscore', KEYS[3], firstThreadId2));" +
"if timeout <= tonumber(ARGV[4]) then " +
// remove the item from the queue and timeout set
// NOTE we do not alter any other timeout
"redis.call('zrem', KEYS[3], firstThreadId2);" +
"redis.call('lpop', KEYS[2]);" +
"else " +
"break;" +
"end;" +
"end;" +
-
開啓死循環
-
利用 lindex 命令判斷等待隊列中第一個元素是否存在,如果存在,直接跳出循環
lidex redisson_lock_queue:{myLock} 0
-
如果等待隊列中第一個元素不爲空(例如返回了LockName,即客戶端UUID拼接線程ID),利用 zscore 在 超時記錄集合(sorted set) 中獲取對應的超時時間
zscore redisson_lock_timeout:{myLock} UUID:threadId
-
如果超時時間已經小於當前時間,那麼首先從超時集合中移除該節點,接着也在等待隊列中彈出第一個節點
zrem redisson_lock_timeout:{myLock} UUID:threadId lpop redisson_lock_queue:{myLock}
-
如果等待隊列中的第一個元素還未超時,直接退出死循環
2、分支二:檢查是否可成功獲取鎖
場景:
- 其他客戶端剛釋放鎖,並且等待隊列爲空
- 其他客戶端剛釋放鎖,並且等待隊列中的第一個元素就是當前客戶端當前線程
// check if the lock can be acquired now
"if (redis.call('exists', KEYS[1]) == 0) " +
"and ((redis.call('exists', KEYS[2]) == 0) " +
"or (redis.call('lindex', KEYS[2], 0) == ARGV[2])) then " +
// remove this thread from the queue and timeout set
"redis.call('lpop', KEYS[2]);" +
"redis.call('zrem', KEYS[3], ARGV[2]);" +
// decrease timeouts for all waiting in the queue
"local keys = redis.call('zrange', KEYS[3], 0, -1);" +
"for i = 1, #keys, 1 do " +
"redis.call('zincrby', KEYS[3], -tonumber(ARGV[3]), keys[i]);" +
"end;" +
// acquire the lock and set the TTL for the lease
"redis.call('hset', KEYS[1], ARGV[2], 1);" +
"redis.call('pexpire', KEYS[1], ARGV[1]);" +
"return nil;" +
"end;" +
-
當前鎖還未被獲取 and(等待隊列不存在 or 等待隊列的第一個元素是當前客戶端當前線程)
exists myLock:判斷鎖是否存在 exists redisson_lock_queue:{myLock}:判斷等待隊列是否爲空 lindex redisson_lock_timeout:{myLock} 0:獲取等待隊列中的第一個元素,用於判斷是否等於當前客戶端當前線程
-
如果步驟1滿足,從等待隊列和超時集合中移除當前線程
lpop redisson_lock_queue:{myLock}:彈出等待隊列中的第一個元素,即當前線程 zrem redisson_lock_timeout:{myLock} UUID:threadId:從超時集合中移除當前客戶端當前線程
-
刷新超時集合中,其他元素的超時時間,即更新他們得分數
zrange redisson_lock_timeout:{myLock} 0 -1:從超時集合中獲取所有的元素
遍歷,然後執行下面命令更新分數,即超時時間:
zincrby redisson_lock_timeout:{myLock} -30w毫秒 keys[i]
因爲這裏的客戶端都是調用 lock()方法,就是等待直到最後獲取到鎖;所以某個客戶端可以成功獲取鎖的時候,要幫其他等待的客戶端刷新一下等待時間,不然在分支一的死循環中就被幹掉了。
-
最後,往加鎖集合(map) myLock 中加入當前客戶端當前線程,加鎖次數爲1,然後刷新 myLock 的過期時間,返回nil
hset myLock UUID:threadId 1:將當前線程加入加鎖記錄中。 espire myLock 3w毫秒:重置鎖的過期時間。
加入此節點後,map集合如下:
myLock:{ "UUID:threadId":1 }
使用這個map記錄加鎖次數,主要用於支持可重入加鎖。
3、分支三:當前線程曾經獲取鎖,重複獲取鎖。
場景:
- 當前線程已經成功獲取過鎖,現在重新再次獲取鎖。
- 即:Redisson 的公平鎖是支持可重入的。
"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;" +
-
利用 hexists 命令判斷加鎖記錄集合中,是否存在當前客戶端當前線程
hexists myLock UUID:threadId
-
如果存在,那麼增加加鎖次數,並且刷新鎖的過期時間
hincrby myLock UUID:threadId 1:增加加鎖次數 pexpire myLock 30000毫秒:刷新鎖key的過期時間
4、分支四:當前線程本就在等待隊列中,返回等待時間
"local timeout = redis.call('zscore', KEYS[3], ARGV[2]);" +
"if timeout ~= false then " +
// the real timeout is the timeout of the prior thread
// in the queue, but this is approximately correct, and
// avoids having to traverse the queue
"return timeout - tonumber(ARGV[3]) - tonumber(ARGV[4]);" +
"end;" +
-
利用 zscore 獲取當前線程在超時集合中的超時時間
zscore redisson_lock_timeout:{myLock} UUID:threadId
-
返回實際的等待時間爲:超時集合裏的時間戳-30w毫秒-當前時間戳
5、分支五:當前線程首次嘗試獲取鎖,將當前線程加入到超時集合中,同時放入等待隊列中
"local lastThreadId = redis.call('lindex', KEYS[2], -1);" +
"local ttl;" +
"if lastThreadId ~= false and lastThreadId ~= ARGV[2] then " +
"ttl = tonumber(redis.call('zscore', KEYS[3], lastThreadId)) - tonumber(ARGV[4]);" +
"else " +
"ttl = redis.call('pttl', KEYS[1]);" +
"end;" +
"local timeout = ttl + tonumber(ARGV[3]) + tonumber(ARGV[4]);" +
"if redis.call('zadd', KEYS[3], timeout, ARGV[2]) == 1 then " +
"redis.call('rpush', KEYS[2], ARGV[2]);" +
"end;" +
"return ttl;",
-
利用 lindex 命令獲取等待隊列中排在最後的線程
lindex redisson_lock_queue:{myLock} -1
-
計算 ttl
- 如果等待隊列中最後的線程不爲空且不是當前線程,根據此線程計算出ttl
zscore redisson_lock_timeout:{myLock} lastThreadId:獲取等待隊列中最後的線程得過期時間 ttl = timeout - 當前時間戳
- 如果等待隊列中不存在其他的等待線程,直接返回鎖key的過期時間
ttl = pttl myLock
-
計算timeout,並將當前線程放入超時集合和等待隊列中
timeout = ttl + 30w毫秒 + 當前時間戳 zadd redisson_lock_timeout:{myLock} timeout UUID:threadId:放入超時集合 rpush redisson_lock_queue:{myLock} UUID:threadId:如果成功放入超市集合,同時放入等待隊列
-
最後返回ttl
3、watchdog 不斷爲鎖續命
因爲 RedissonFairLock 是基於 RedissonLock 做的,所以 watchdog 還是 RedissonLock 那一套。
4、死循環獲取鎖
因爲 RedissonFairLock 是基於 RedissonLock 做的,所以死循環獲取鎖也還是 RedissonLock 那一套。
5、其他的加鎖方式
如果我們需要指定獲取鎖成功後持有鎖的時長,可以執行下面方法,指定 leaseTime
lock.lock(10, TimeUnit.SECONDS);
如果指定了 leaseTime,watchdog就不會再啓用了。
如果不但需要指定持有鎖的時長,還想避免鎖獲取失敗時的死循環,可以同時指定 leaseTime 和 waitTime
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
如果指定了 waitTime,只會在 waitTime 時間內循環嘗試獲取鎖,超過 waitTime 如果還是獲取失敗,直接返回false。