Check that RLock still belongs to owner during renewal expiration. #597

pull/614/head
Nikita 9 years ago
parent 42bc256cc7
commit e76d28a768

@ -148,7 +148,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return get(tryAcquireAsync(leaseTime, unit, Thread.currentThread().getId())); return get(tryAcquireAsync(leaseTime, unit, Thread.currentThread().getId()));
} }
private RFuture<Boolean> tryAcquireOnceAsync(long leaseTime, TimeUnit unit, long threadId) { private RFuture<Boolean> tryAcquireOnceAsync(long leaseTime, TimeUnit unit, final long threadId) {
if (leaseTime != -1) { if (leaseTime != -1) {
return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_NULL_BOOLEAN); return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_NULL_BOOLEAN);
} }
@ -163,14 +163,14 @@ public class RedissonLock extends RedissonExpirable implements RLock {
Boolean ttlRemaining = future.getNow(); Boolean ttlRemaining = future.getNow();
// lock acquired // lock acquired
if (ttlRemaining) { if (ttlRemaining) {
scheduleExpirationRenewal(); scheduleExpirationRenewal(threadId);
} }
} }
}); });
return ttlRemainingFuture; return ttlRemainingFuture;
} }
private <T> RFuture<Long> tryAcquireAsync(long leaseTime, TimeUnit unit, long threadId) { private <T> RFuture<Long> tryAcquireAsync(long leaseTime, TimeUnit unit, final long threadId) {
if (leaseTime != -1) { if (leaseTime != -1) {
return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_LONG); return tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_LONG);
} }
@ -185,7 +185,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
Long ttlRemaining = future.getNow(); Long ttlRemaining = future.getNow();
// lock acquired // lock acquired
if (ttlRemaining == null) { if (ttlRemaining == null) {
scheduleExpirationRenewal(); scheduleExpirationRenewal(threadId);
} }
} }
}); });
@ -197,7 +197,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return get(tryLockAsync()); return get(tryLockAsync());
} }
private void scheduleExpirationRenewal() { private void scheduleExpirationRenewal(final long threadId) {
if (expirationRenewalMap.containsKey(getEntryName())) { if (expirationRenewalMap.containsKey(getEntryName())) {
return; return;
} }
@ -205,7 +205,15 @@ public class RedissonLock extends RedissonExpirable implements RLock {
Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() { Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override @Override
public void run(Timeout timeout) throws Exception { public void run(Timeout timeout) throws Exception {
RFuture<Boolean> future = expireAsync(internalLockLeaseTime, TimeUnit.MILLISECONDS);
RFuture<Boolean> future = commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
"redis.call('pexpire', KEYS[1], ARGV[1]); " +
"return 1; " +
"end; " +
"return 0;",
Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId));
future.addListener(new FutureListener<Boolean>() { future.addListener(new FutureListener<Boolean>() {
@Override @Override
public void operationComplete(Future<Boolean> future) throws Exception { public void operationComplete(Future<Boolean> future) throws Exception {
@ -217,7 +225,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
if (future.getNow()) { if (future.getNow()) {
// reschedule itself // reschedule itself
scheduleExpirationRenewal(); scheduleExpirationRenewal(threadId);
} }
} }
}); });

Loading…
Cancel
Save