线上分布式锁问题排查
最近在开发企微数据统计需求,需要去定时拉取企微业务数据,但是企微接口有并发量限制,所以根据业务添加了分布式锁,在线上抛出了java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node异常,所以根据报错排查并解决。
项目代码如下
//加锁,防止出现调用企微接口并发报错限制
Mutex lock = null;
try {
lock = lockService.lock(tableName, wcUserId,2);
resp = wecomService.getUserBehaviorStatic(cropId, request);
log.info("调用企微成员数据统计接口返回参数:{}", JSON.toJSONString(resp));
}catch (Exception e){
throw new RuntimeException("企微接口调用失败,请联系管理员");
}
finally {
if(null != lock ){
lockService.unlock(lock);
}
}
Redission分布式锁进行unlock操作时,有个异常源码如下:
public void unlock() {
try {
this.get(this.unlockAsync(Thread.currentThread().getId()));
} catch (RedisException var2) {
if (var2.getCause() instanceof IllegalMonitorStateException) {
throw (IllegalMonitorStateException)var2.getCause();
} else {
throw var2;
}
}
}
public RFuture<Void> unlockAsync(long threadId) {
RFuture<Boolean> future = this.unlockInnerAsync(threadId);
CompletionStage<Void> f = future.handle((opStatus, e) -> {
this.cancelExpirationRenewal(threadId);
if (e != null) {
throw new CompletionException(e);
} else if (opStatus == null) {
IllegalMonitorStateException cause = new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: " + this.id + " thread-id: " + threadId);
throw new CompletionException(cause);
} else {
return null;
}
});
return new CompletableFutureWrapper(f);
}
为什么会有这样的问题,主要可能原因有两个
1.由于在进行lock操作时,会设置一个时间,当你在完成lock后,里面的业务代码执行时间大于lock时间时,进行unlock,会抛出该异常。
2.多线程竞争的问题,当第一个线程完成lock,此时并未 unlock,如此,第二个线程尝试获取锁,并进行lock操作,会抛出该异常。
解决办法:
在lock或unlock前,判断下状态合法性即可,而非直接进行加锁解锁操作。
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}