RedisLockRegistry分布式锁的基本原理与超时释放问题的探索


前言

本文主要介绍Spring的RedisLockRegistry的基本原理,并提出一种针对其超时释放锁问题的解决办法
注意该方法未经线上环境验证,仅供参考


一、RedisLockRegistry

RedisLockRegistry是spring提供的一种分布式锁的工具类,通过该类可以实现分布式环境下对同一个key值加锁,控制并发

二、使用步骤

1.引入依赖

pom:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
      <groupId>org.springframework.integration</groupId>
      <artifactId>spring-integration-redis</artifactId>
</dependency>

2.基本原理

1.配置Configuration,实例化RedisLockRegistry
2.新增注解和切面,切面内使用RedisLockRegistry.obtain(lockKey),然后加锁,成功继续执行,失败则报错阻断
3.obtain的默认是RedisSpinLock,大部分方法都使用其父类RedisLock里的,这俩都是RedisLockRegistry的私有内部类.
RedisLock内部持有一个本地锁ReentrantLock,lock时先调用本地锁的lock,本地锁加锁成功才执行redis脚本获取分布式锁
4.加锁脚本的含义就是通过lockKey取redis拿值,并与当前RedisLockRegistry里的clientId进行比较,如果等于则刷新过期时间并返回true,如果为空则设值并设置过期时间并返回true,否则返回false
5.解锁时先判断当前线程是否持有localLock的锁,然后再判断是否是重入,重入的话只解锁localLock;
当localLock完全解锁时还会顺带校验下redis里的lockKey的value是不是当前的clientId,如果为null或者其他值则抛出IllegalStateException,代表超时释放锁了

3.超时释放及解决办法

由于RedisLockRegistry实例化时即设置了锁的过期时间,默认60s,当执行代码过慢时,redis里的值自动过期了,导致其他线程也能成功获取锁,并且原先获取锁的还会抛异常,这种一般不是我们想要的,在参考了Redisson的刷新机制后,对RedisLockRegistry做了些简单封装,可实现类似功能,以下是代码改造

@Slf4j
public class RedisLockRegistryWrapper implements ExpirableLockRegistry {

    private final RedisLockRegistry redisLockRegistry;

    private final StringRedisTemplate stringRedisTemplate;

    private final String clientId;
    private final String registryKey;

    private final long expireAfter;

    private final TaskScheduler taskScheduler;

    private final Map<String, ScheduledFuture<?>> scheduledFuturesMap = new ConcurrentHashMap<>();


    public RedisLockRegistryWrapper(RedisLockRegistry redisLockRegistry,
                                    TaskScheduler taskScheduler,
                                    StringRedisTemplate stringRedisTemplate,
                                    String registryKey,
                                    @Nullable Long expireAfter) {
        Assert.notNull(registryKey, "registryKey must not be null");
        this.redisLockRegistry = redisLockRegistry;
        this.taskScheduler = taskScheduler;
        this.stringRedisTemplate = stringRedisTemplate;
        this.clientId = (String) getValueReflective(redisLockRegistry, "clientId");
        this.registryKey = registryKey;
        this.expireAfter = expireAfter == null ? (Long) getValueReflective(redisLockRegistry, "expireAfter") : expireAfter;
    }

    @Override
    public void expireUnusedOlderThan(long age) {
        redisLockRegistry.expireUnusedOlderThan(age);
    }

    @Override
    public Lock obtain(Object lockKey) {
        Lock lock = redisLockRegistry.obtain(lockKey);
        return new LockWrapper(lock, lockKey);
    }


    private static Object getValueReflective(Object targetObj, String fieldName) {
        return getValueReflective(targetObj, fieldName, null);
    }

    private static Object getValueReflective(Object targetObj, String fieldName, @Nullable Class<?> superClass) {
        Class<?> clazzToUse = superClass == null ? targetObj.getClass() : superClass;
        try {
            Field field = clazzToUse.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(targetObj);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new CustomException("请求失败,请联系管理员", e);
        }
    }


    private class LockWrapper implements Lock, Runnable {

        private final Lock lock;

        private final String lockKey;

        private final ReentrantLock localLock;

        private final String script = "local lockClientId = redis.call('GET', KEYS[1]) " +
                "if lockClientId == ARGV[1] then " +
                "  redis.call('PEXPIRE', KEYS[1], ARGV[2]) " +
                "  return true " +
                "end " +
                "return false";

        private final RedisScript<Boolean> renewLockScript =
                new DefaultRedisScript<>(script, Boolean.class);

        public LockWrapper(Lock lock, Object lockKey) {
            this.lock = lock;
            this.lockKey = registryKey + ":" + lockKey;
            this.localLock = (ReentrantLock) getValueReflective(lock, "localLock", lock.getClass().getSuperclass());
        }

        @Override
        public void lock() {
            lock.lock();
            scheduleRenew();
        }

        @Override
        public void lockInterruptibly() throws InterruptedException {
            lock.lockInterruptibly();
            scheduleRenew();
        }

        @Override
        public boolean tryLock() {
            boolean locked = lock.tryLock();
            if (locked) {
                scheduleRenew();
            }
            return locked;
        }

        @Override
        public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
            boolean locked = lock.tryLock(time, unit);
            if (locked) {
                scheduleRenew();
            }
            return locked;
        }

        @Override
        public void unlock() {
            lock.unlock();
            cancelRenew();
        }

        @Override
        public Condition newCondition() {
            return lock.newCondition();
        }

        public void scheduleRenew() {
            scheduledFuturesMap.computeIfAbsent(lockKey, key -> taskScheduler.scheduleAtFixedRate(this, Instant.now(), Duration.ofMillis(expireAfter / 3)));
        }

        public void cancelRenew() {
            if (this.localLock.getHoldCount() < 1) {
                ScheduledFuture<?> scheduledFuture = scheduledFuturesMap.remove(lockKey);
                scheduledFuture.cancel(false);
                log.info("cancel renew key {}", this.lockKey);
            }
        }


        @Override
        public void run() {
            log.info("renew key {}", this.lockKey);
            Boolean success = stringRedisTemplate
                    .execute(renewLockScript, Collections.singletonList(this.lockKey),
                            clientId,
                            String.valueOf(expireAfter));
            if (success == null || !success) {
                cancelRenew();
            }
        }
    }
}

实例化BEAN

@Configuration
public class RedisLockRegistryConfig {

    @Bean("redisLockRegistry")
    public ExpirableLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory, TaskScheduler taskScheduler) {
        String registryKey = "lock";
        long expireAfter = 15000;
        RedisLockRegistry registry = new RedisLockRegistry(redisConnectionFactory, registryKey, expireAfter);
        return new RedisLockRegistryWrapper(registry, taskScheduler, new StringRedisTemplate(redisConnectionFactory), registryKey, expireAfter);
    }

}

外部注入使用

@Resource
private ExpirableLockRegistry redisLockRegistry;

Lock lock = redisLockRegistry.obtain(key)
lock.lock();
//do someting
lock.unlock();

还和以前一样使用对应方法加锁,主要改造点在于lock时根据状态来定时调用一个刷新过期时间的redis脚本,该脚本也是通过加锁脚本精简而来,经简单测试,可以做到定时刷新过期时间的功能

总结

以上方法也是参考了Redisson的看门狗机制,并简单的复刻其功能,旨在提供一种解决问题的思路,当然也更推荐使用Redisson来代替redisLockRegistry实现分布式锁的功能
欢迎大家一起探讨学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值