聊聊redisson的分散式鎖

go4it發表於2019-03-04

本文主要研究一下redisson的分散式鎖

maven

		<dependency>
			<groupId>org.redisson</groupId>
			<artifactId>redisson</artifactId>
			<version>3.8.1</version>
		</dependency>
複製程式碼

例項

    @Test
    public void testDistributedLock(){
        Config config = new Config();
//        config.setTransportMode(TransportMode.EPOLL);
        config.useSingleServer()
                .setAddress("redis://192.168.99.100:6379");
        RedissonClient redisson = Redisson.create(config);


        IntStream.rangeClosed(1,5)
                .parallel()
                .forEach(i -> {
                    executeLock(redisson);
                });

        executeLock(redisson);
    }

    public void executeLock(RedissonClient redisson){
        RLock lock = redisson.getLock("myLock");
        boolean locked = false;
        try{
            LOGGER.info("try lock");
            locked = lock.tryLock();
//            locked = lock.tryLock(1,2,TimeUnit.MINUTES);
            LOGGER.info("get lock result:{}",locked);
            if(locked){
                TimeUnit.HOURS.sleep(1);
                LOGGER.info("get lock and finish");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            LOGGER.info("enter unlock");
            if(locked){
                lock.unlock();
            }
        }
    }
複製程式碼

原始碼解析

RedissonLock.tryLock

redisson-3.8.1-sources.jar!/org/redisson/RedissonLock.java

    @Override
    public boolean tryLock() {
        return get(tryLockAsync());
    }

    @Override
    public RFuture<Boolean> tryLockAsync() {
        return tryLockAsync(Thread.currentThread().getId());
    }

    @Override
    public RFuture<Boolean> tryLockAsync(long threadId) {
        return tryAcquireOnceAsync(-1, null, threadId);
    }

    private RFuture<Boolean> tryAcquireOnceAsync(long leaseTime, TimeUnit unit, final long threadId) {
        if (leaseTime != -1) {
            return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_NULL_BOOLEAN);
        }
        RFuture<Boolean> ttlRemainingFuture = tryLockInnerAsync(commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_NULL_BOOLEAN);
        ttlRemainingFuture.addListener(new FutureListener<Boolean>() {
            @Override
            public void operationComplete(Future<Boolean> future) throws Exception {
                if (!future.isSuccess()) {
                    return;
                }

                Boolean ttlRemaining = future.getNow();
                // lock acquired
                if (ttlRemaining) {
                    scheduleExpirationRenewal(threadId);
                }
            }
        });
        return ttlRemainingFuture;
    }

    <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));
    }

    protected String getLockName(long threadId) {
        return id + ":" + threadId;
    }

    private void scheduleExpirationRenewal(final long threadId) {
        if (expirationRenewalMap.containsKey(getEntryName())) {
            return;
        }

        Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                
                RFuture<Boolean> future = renewExpirationAsync(threadId);
                
                future.addListener(new FutureListener<Boolean>() {
                    @Override
                    public void operationComplete(Future<Boolean> future) throws Exception {
                        expirationRenewalMap.remove(getEntryName());
                        if (!future.isSuccess()) {
                            log.error("Can`t update lock " + getName() + " expiration", future.cause());
                            return;
                        }
                        
                        if (future.getNow()) {
                            // reschedule itself
                            scheduleExpirationRenewal(threadId);
                        }
                    }
                });
            }

        }, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);

        if (expirationRenewalMap.putIfAbsent(getEntryName(), new ExpirationEntry(threadId, task)) != null) {
            task.cancel();
        }
    }

    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));
    }

    private <T> RFuture<Long> tryAcquireAsync(long leaseTime, TimeUnit unit, final long threadId) {
        if (leaseTime != -1) {
            return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_LONG);
        }
        RFuture<Long> ttlRemainingFuture = tryLockInnerAsync(commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG);
        ttlRemainingFuture.addListener(new FutureListener<Long>() {
            @Override
            public void operationComplete(Future<Long> future) throws Exception {
                if (!future.isSuccess()) {
                    return;
                }

                Long ttlRemaining = future.getNow();
                // lock acquired
                if (ttlRemaining == null) {
                    scheduleExpirationRenewal(threadId);
                }
            }
        });
        return ttlRemainingFuture;
    }
複製程式碼
  • 這裡leaseTime沒有設定的話,預設是-1,使用的是commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(),預設為30秒
  • tryLockInnerAsync使用的是一段lua指令碼,該指令碼有3個引數,第一個引數為KEYS陣列,後面幾個引數為ARGV陣列的元素
  • 這裡key的值為呼叫方指定的這個redissonLock的名稱,兩個變數,第一個為leaseTime,第二個為鎖的名稱,使用redissonLock的id+執行緒id
  • lua指令碼第一個方法判斷redissonLock的hashmap是否存在,如果不存在則建立,該hashmap有一個entry的key為鎖名稱,valude為1,之後設定該hashmap失效時間為leaseTime
  • lua指令碼第二個方法是在redissonLock的hashmap存在的情況下,將該鎖名的value增1,同時設定失效時間為leaseTime
  • 最後返回該redissonLock名稱的key的ttl
  • 執行成功之後判斷ttl是否還有值,有的話則呼叫scheduleExpirationRenewal,防止lock未執行完就失效
  • scheduleExpirationRenewal是註冊一個延時任務,在internalLockLeaseTime / 3的時候觸發,執行的方法是renewExpirationAsync,將該鎖失效時間重置回internalLockLeaseTime
  • scheduleExpirationRenewal裡頭給scheduleExpirationRenewal任務增加listener,如果設定成功之後還會再次遞迴呼叫scheduleExpirationRenewal重新註冊延時任務
  • tryAcquireAsync(long leaseTime, TimeUnit unit, final long threadId)方法是指定自動解鎖時間時呼叫的方法,它與tryAcquireOnceAsync的區別在於,它對ttl的方回值採用long值來判斷,如果是null,才執行延長失效時間的定時任務,而tryAcquireOnceAsync方法採用的是BooleanNullReplayConvertor,只要返回不是null,則返回true

RedissonLock.unlock

redisson-3.8.1-sources.jar!/org/redisson/RedissonLock.java

    @Override
    public void unlock() {
        try {
            get(unlockAsync(Thread.currentThread().getId()));
        } catch (RedisException e) {
            if (e.getCause() instanceof IllegalMonitorStateException) {
                throw (IllegalMonitorStateException)e.getCause();
            } else {
                throw e;
            }
        }
        
//        Future<Void> future = unlockAsync();
//        future.awaitUninterruptibly();
//        if (future.isSuccess()) {
//            return;
//        }
//        if (future.cause() instanceof IllegalMonitorStateException) {
//            throw (IllegalMonitorStateException)future.cause();
//        }
//        throw commandExecutor.convertException(future);
    }

    @Override
    public RFuture<Void> unlockAsync(final long threadId) {
        final RPromise<Void> result = new RedissonPromise<Void>();
        RFuture<Boolean> future = unlockInnerAsync(threadId);

        future.addListener(new FutureListener<Boolean>() {
            @Override
            public void operationComplete(Future<Boolean> future) throws Exception {
                if (!future.isSuccess()) {
                    cancelExpirationRenewal(threadId);
                    result.tryFailure(future.cause());
                    return;
                }

                Boolean opStatus = future.getNow();
                if (opStatus == null) {
                    IllegalMonitorStateException cause = new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: "
                            + id + " thread-id: " + threadId);
                    result.tryFailure(cause);
                    return;
                }
                if (opStatus) {
                    cancelExpirationRenewal(null);
                }
                result.trySuccess(null);
            }
        });

        return result;
    }

    protected RFuture<Boolean> unlockInnerAsync(long threadId) {
        return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
                "if (redis.call(`exists`, KEYS[1]) == 0) then " +
                    "redis.call(`publish`, KEYS[2], ARGV[1]); " +
                    "return 1; " +
                "end;" +
                "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.unlockMessage, internalLockLeaseTime, getLockName(threadId));

    }

    String getChannelName() {
        return prefixName("redisson_lock__channel", getName());
    }

    void cancelExpirationRenewal(Long threadId) {
        ExpirationEntry task = expirationRenewalMap.get(getEntryName());
        if (task != null && (threadId == null || task.getThreadId() == threadId)) {
            expirationRenewalMap.remove(getEntryName());
            task.getTimeout().cancel();
        }
    }
複製程式碼
  • unlockInnerAsync通過lua指令碼來釋放鎖,該lua使用兩個key,一個是redissonLock名稱,一個是channelName
  • 該lua使用的變數有三個,一個是pubSub的unlockMessage,預設為0,一個是internalLockLeaseTime,預設為commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(),一個是鎖名稱
  • 如果該redissonLock不存在,則直接釋出unlock訊息返回1;如果該鎖不存在則返回nil;
  • 如果該鎖存在則將其計數-1,如果counter大於0,則重置下失效時間,返回0;如果counter不大於0,則刪除該redissonLock鎖,釋出unlockMessage,返回1;如果上面條件都沒有命中返回nil
  • unlockAsync裡頭對unlockInnerAsync註冊了FutureListener,主要是呼叫cancelExpirationRenewal,取消掉scheduleExpirationRenewal任務

LockPubSub

redisson-3.8.1-sources.jar!/org/redisson/pubsub/LockPubSub.java

public class LockPubSub extends PublishSubscribe<RedissonLockEntry> {

    public static final Long unlockMessage = 0L;

    @Override
    protected RedissonLockEntry createEntry(RPromise<RedissonLockEntry> newPromise) {
        return new RedissonLockEntry(newPromise);
    }

    @Override
    protected void onMessage(RedissonLockEntry value, Long message) {
        if (message.equals(unlockMessage)) {
            Runnable runnableToExecute = value.getListeners().poll();
            if (runnableToExecute != null) {
                runnableToExecute.run();
            }

            value.getLatch().release();
        }
    }

}
複製程式碼
  • 接收到unlockMessage的時候,會呼叫RedissonLockEntry的listener,然後觸發latch的release
  • tryAcquireOnceAsync這個方法預設沒有建立LockPubSub,而且沒有指定自動解鎖時間,則定時任務會一直延長失效時間,這個可能存在鎖一直沒釋放的風險

小結

加鎖有如下注意事項:

  • 加鎖需要設定超時時間,防止出現死鎖
  • 加鎖以及設定超時時間的時候,需要保證兩個操作的原子性,因而最好使用lua指令碼或者使用支援NX以及EX的set方法
  • 加鎖的時候需要把加鎖的呼叫方資訊,比如執行緒id給記錄下來,這個在解鎖的時候需要使用
  • 對於加鎖時長不確定的任務,為防止任務未執行完導致超時被釋放,需要對尚未執行完的任務延長失效時間

解鎖有如下注意事項:

  • 解鎖一系列操作(判斷key是否存在,存在的話刪除key等)需要保證原子性,因而最好使用lua指令碼
  • 解鎖需要判斷呼叫方是否與加鎖時記錄的是否一致,防止鎖被誤刪
  • 如果有延續失效時間的延時任務,在解鎖的時候,需要終止掉該任務

doc

相關文章