美文网首页Redisson源码剖析
2. Redisson源码剖析-公平锁的可重入加锁的源码剖析

2. Redisson源码剖析-公平锁的可重入加锁的源码剖析

作者: T_log | 来源:发表于2020-10-24 16:56 被阅读0次
    1. 公平锁加锁的源码在RedissonFairLock
    KEYS = Arrays.<Object>asList(getName(), threadsQueueName, timeoutSetName)
    KEYS[1] = getName() = 锁的名字,“anyLock”
    KEYS[2] = threadsQueueName = redisson_lock_queue:{anyLock},基于redis的数据结构实现的一个队列
    KEYS[3] = timeoutSetName = redisson_lock_timeout:{anyLock},基于redis的数据结构实现的一个Set数据集合,有序集合,可以自动按照你给每个数据指定的一个分数(score)来进行排序
    
    ARGV =  internalLockLeaseTime, getLockName(threadId), currentTime + threadWaitTime, currentTime
    ARGV[1] = 30000毫秒
    ARGV[2] = UUID:threadId
    ARGV[3] = 当前时间(10:00:00) + 5000毫秒 = 10:00:05
    ARGV[4] = 当前时间(10:00:00)
    
    

    代码片段一、

    @Override
    <T> RFuture<T> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
        internalLockLeaseTime = unit.toMillis(leaseTime);
    
    
        long currentTime = System.currentTimeMillis();
        if (command == RedisCommands.EVAL_NULL_BOOLEAN) {
            return commandExecutor.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[3]) then "
                        + "redis.call('zrem', KEYS[3], firstThreadId2); "
                        + "redis.call('lpop', KEYS[2]); "
                    + "else "
                        + "break;"
                    + "end; "
                  + "end;"
                    + 
                    
                    "if (redis.call('exists', KEYS[1]) == 0) and ((redis.call('exists', KEYS[2]) == 0) "
                            + "or (redis.call('lindex', KEYS[2], 0) == ARGV[2])) then " +
                            "redis.call('lpop', KEYS[2]); " +
                            "redis.call('zrem', KEYS[3], ARGV[2]); " +
                            "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 1;", 
                    Arrays.<Object>asList(getName(), threadsQueueName, timeoutSetName), 
                    internalLockLeaseTime, getLockName(threadId), currentTime);
        }
        
        if (command == RedisCommands.EVAL_LONG) {
            return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, command,
                    // remove stale threads
                    // 1. 首先会进入到这里,lindex redisson_lock_queue:{anyLock} 0,获取队列中的第一个元素
                    "while true do "
                    + "local firstThreadId2 = redis.call('lindex', KEYS[2], 0);”
                    + "if firstThreadId2 == false then "
                        + "break;"
                    + "end; “
                    // 判断他的timeout时间是否大于当前时间,假设当前时间为10:00:08,不成立,直接跳出死循环
                    + "local timeout = tonumber(redis.call('zscore', KEYS[3], firstThreadId2));"
                    + "if timeout <= tonumber(ARGV[4]) then "
                        + "redis.call('zrem', KEYS[3], firstThreadId2); "
                        + "redis.call('lpop', KEYS[2]); "
                    + "else "
                        + "break;"
                    + "end; "
                  + "end;"
                        // 1. Exists anyLock,肯定是存在的,所以条件不成立
                      + "if (redis.call('exists', KEYS[1]) == 0) and ((redis.call('exists', KEYS[2]) == 0) "
                            + "or (redis.call('lindex', KEYS[2], 0) == ARGV[2])) then " +
                            "redis.call('lpop', KEYS[2]); " +
                            "redis.call('zrem', KEYS[3], ARGV[2]); " +
                            "redis.call('hset', KEYS[1], ARGV[2], 1); " +
                            "redis.call('pexpire', KEYS[1], ARGV[1]); " +
                            "return nil; " +
                        "end; “ +
                        // 1. Hexists anyLock UUID_01:thread_01 ,判断anyLock的hash中是否存在一个UUID_01:threadId_01的key,当前这个锁是否是客户端A加的锁,条件是成立的
                        "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then “ +
                            // hincrby  anyLock UUID_01:threadId_01 累加1
                            "redis.call('hincrby', KEYS[1], ARGV[2], 1); “ +
                            // 更新anyLock的生存周期为30000毫秒
                            "redis.call('pexpire', KEYS[1], ARGV[1]); “ +
                            // 返回nil
                            "return nil; " +
                        "end; " +
                            
                        "local firstThreadId = redis.call('lindex', KEYS[2], 0); " +
                        "local ttl; " + 
                        "if firstThreadId ~= false and firstThreadId ~= ARGV[2] then " + 
                            "ttl = tonumber(redis.call('zscore', KEYS[3], firstThreadId)) - tonumber(ARGV[4]);" + 
                        "else "
                          + "ttl = redis.call('pttl', KEYS[1]);" + 
                        "end; " + 
                            
                        "local timeout = ttl + tonumber(ARGV[3]);" + 
                        "if redis.call('zadd', KEYS[3], timeout, ARGV[2]) == 1 then " +
                            "redis.call('rpush', KEYS[2], ARGV[2]);" +
                        "end; " +
                        "return ttl;", 
                        Arrays.<Object>asList(getName(), threadsQueueName, timeoutSetName), 
                                    internalLockLeaseTime, getLockName(threadId), currentTime + threadWaitTime, currentTime);
        }
        
        throw new IllegalArgumentException();
    }
    
    

    相关文章

      网友评论

        本文标题:2. Redisson源码剖析-公平锁的可重入加锁的源码剖析

        本文链接:https://www.haomeiwen.com/subject/rvfwmktx.html