未验证 提交 68ad0add 编写于 作者: N Nikita Koksharov 提交者: GitHub

Revert "Avoid redisson fair lock blocking if there are lots of registered thr…"

上级 df0c74a2
......@@ -201,22 +201,6 @@ public class RedissonFairLock extends RedissonLock implements RLock {
"return nil;" +
"end;" +
// check if the lock is not held, and other queues are not used
"while true do " +
"local firstThreadId = redis.call('lindex', KEYS[2], 0);" +
"if (firstThreadId == false) or (firstThreadId == ARGV[2]) then " +
"break;"+
"end;" +
"local timeout = tonumber(redis.call('zscore', KEYS[3], firstThreadId));" +
"if (timeout > tonumber(ARGV[4])) and (redis.call('exists', KEYS[1]) == 0) then " +
"redis.call('lpop', KEYS[2]);" +
"redis.call('zrem', KEYS[3], firstThreadId);" +
"else " +
"break;" +
"end;" +
"end;" +
// the lock cannot be acquired
// check if the thread is already in the queue
"local timeout = redis.call('zscore', KEYS[3], ARGV[2]);" +
......
......@@ -933,62 +933,5 @@ public class RedissonFairLockTest extends BaseConcurrentTest {
await().atMost(30, TimeUnit.SECONDS).until(() -> lockedCounter.get() == totalThreads);
}
@Test
public void testLockBlock() throws InterruptedException{
Config cfg = createConfig();
cfg.setLockWatchdogTimeout(30000);
RedissonClient redisson = Redisson.create(cfg);
int totalExecutorCount = 5;
int totalThreadCount = 100;
int interval = 1000;
Lock lock = redisson.getFairLock("testLockBlock");
for (int count = 0; count < totalExecutorCount; count++) {
ExecutorService executor = Executors.newFixedThreadPool(totalThreadCount);
for (int i = 0; i < totalThreadCount; i++) {
final int finalI = i;
executor.submit(() -> {
log.info("running " + finalI + " in thread " + Thread.currentThread().getId());
try {
lock.lock();
log.info("Thread " + finalI + " got lock");
} catch (Exception ex) {
log.error("Failed to get lock");
} finally {
lock.unlock();
}
});
}
executor.shutdownNow();
}
redisson.shutdown();
// In case connection closed
redisson = Redisson.create(cfg);
long timeOut = redisson.getConfig().getLockWatchdogTimeout() + interval;
ExecutorService lockExecutor = Executors.newFixedThreadPool(1);
Lock lockSecond = redisson.getFairLock("testLockBlock");
Future<Boolean> future = lockExecutor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
// check if this lock can be acquired in short time
Thread.sleep(timeOut);
lockSecond.lock();
return Boolean.TRUE;
}
});
Boolean got = Boolean.FALSE;
try{
got = future.get(timeOut + interval, TimeUnit.MILLISECONDS);
if (got) {
log.info("Got lock immediately after startup");
}else{
log.info("Failed to get lock due to blocked");
}
}catch (Exception e){
log.info("Failed to get lock due to blocked");
}
Assert.assertTrue(got);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册