在阿里云Redis环境下 采用SingleServerConfig配置 使用RLock锁时,会出一定机率的解锁问题: 多线程模式下, 对同一个key进行加锁, A线程加锁成功, B线程等待, A线程解锁, B线程继续等待直到30秒默认时间才能加锁成功.
public class RLockTest extends BaseTest {
@Autowired
private RedissonClient redissonClient;
private List<String> lockKeys = new ArrayList<>();
private Logger log= LoggerFactory.getLogger(getClass());
@Before
public void init() {
for (int i = 0; i < 10; i++) {
lockKeys.add("lock_key_" + i);
}
}
@Test
public void test() {
List<Thread> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int finalI = i;
list.add(new Thread(() -> {
for (int j = 0; j < 10; j++) {
LogRequestIdPlugin.setRequestId(LogRequestIdPlugin.newRequestId());
doLock(finalI);
LogRequestIdPlugin.setRequestId(null);
}
}));
}
for (Thread t : list) {
t.start();
}
for (Thread t : list) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void doLock(int i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
RLock lock = redissonClient.getLock(lockKeys.get(((int) (Math.random() * 10000) % lockKeys.size())));
try {
log.info("thread:" + i + " start locking");
long s = System.currentTimeMillis();
lock.lock();
if (System.currentTimeMillis() - s > 10000) {
log.error("加锁异常!!!");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("thread:" + i + " locked");
} finally {
lock.unlock();
}
}
}
15:15:27.923 [B5oYGRb11f8524b] [Thread-29] INFO cn.com.sjfx.account.RLockTest - thread:3 start locking 15:15:27.926 [B5oYGRb11f8524b] [Thread-29] DEBUG org.redisson.command.CommandAsyncService - acquired connection for command (EVAL) and params [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pe..., 1, lock_key_5, 30000, 30e213ca-fbb8-49b7-a6a2-45fe94e15ec7:292] from slot NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=0, freeSubscribeConnectionsCounter=value:49:queue:0, freeConnectionsAmount=4, freeConnectionsCounter=value:58:queue:0, freezed=false, freezeReason=null, client=[addr=redis://192.168.1.26:6379], nodeType=MASTER, firstFail=0]]] using node 192.168.1.26/192.168.1.26:6379... RedisConnection@154358690 [redisClient=[addr=redis://192.168.1.26:6379], channel=[id: 0xcaa97c72, L:/192.168.1.26:37308 - R:192.168.1.26/192.168.1.26:6379], command=null] 15:15:28.014 [B5oYGRb11f8524b] [Thread-29] DEBUG org.redisson.command.CommandAsyncService - acquired connection for command (EVAL) and params [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pe..., 1, lock_key_5, 30000, 30e213ca-fbb8-49b7-a6a2-45fe94e15ec7:292] from slot NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=0, freeSubscribeConnectionsCounter=value:49:queue:0, freeConnectionsAmount=8, freeConnectionsCounter=value:62:queue:0, freezed=false, freezeReason=null, client=[addr=redis://192.168.1.26:6379], nodeType=MASTER, firstFail=0]]] using node 192.168.1.26/192.168.1.26:6379... RedisConnection@792711111 [redisClient=[addr=redis://192.168.1.26:6379], channel=[id: 0x00f6d3b9, L:/192.168.1.26:37324 - R:192.168.1.26/192.168.1.26:6379], command=null]
15:15:57.966 [B5oYGRb11f8524b] [Thread-29] DEBUG org.redisson.command.CommandAsyncService - acquired connection for command (EVAL) and params [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pe..., 1, lock_key_5, 30000, 30e213ca-fbb8-49b7-a6a2-45fe94e15ec7:292] from slot NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=0, freeSubscribeConnectionsCounter=value:49:queue:0, freeConnectionsAmount=9, freeConnectionsCounter=value:63:queue:0, freezed=false, freezeReason=null, client=[addr=redis://192.168.1.26:6379], nodeType=MASTER, firstFail=0]]] using node 192.168.1.26/192.168.1.26:6379... RedisConnection@792711111 [redisClient=[addr=redis://192.168.1.26:6379], channel=[id: 0x00f6d3b9, L:/192.168.1.26:37324 - R:192.168.1.26/192.168.1.26:6379], command=null]
15:15:58.008 [B5oYGRb11f8524b] [Thread-29] ERROR cn.com.sjfx.account.RLockTest - 加锁异常!!!