All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.RedissonLock Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.43.0
Show newest version
/**
 * Copyright (c) 2013-2020 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import org.redisson.api.RFuture;
import org.redisson.client.RedisException;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.pubsub.LockPubSub;

import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Distributed implementation of {@link java.util.concurrent.locks.Lock}
 * Implements reentrant lock.
* Lock will be removed automatically if client disconnects. *

* Implements a non-fair locking so doesn't guarantees an acquire order. * * @author Nikita Koksharov * */ public class RedissonLock extends RedissonBaseLock { protected long internalLockLeaseTime; protected final LockPubSub pubSub; final CommandAsyncExecutor commandExecutor; public RedissonLock(CommandAsyncExecutor commandExecutor, String name) { super(commandExecutor, name); this.commandExecutor = commandExecutor; this.internalLockLeaseTime = commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(); this.pubSub = commandExecutor.getConnectionManager().getSubscribeService().getLockPubSub(); } String getChannelName() { return prefixName("redisson_lock__channel", getName()); } @Override public void lock() { try { lock(-1, null, false); } catch (InterruptedException e) { throw new IllegalStateException(); } } @Override public void lock(long leaseTime, TimeUnit unit) { try { lock(leaseTime, unit, false); } catch (InterruptedException e) { throw new IllegalStateException(); } } @Override public void lockInterruptibly() throws InterruptedException { lock(-1, null, true); } @Override public void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException { lock(leaseTime, unit, true); } private void lock(long leaseTime, TimeUnit unit, boolean interruptibly) throws InterruptedException { long threadId = Thread.currentThread().getId(); Long ttl = tryAcquire(-1, leaseTime, unit, threadId); // lock acquired if (ttl == null) { return; } RFuture future = subscribe(threadId); if (interruptibly) { commandExecutor.syncSubscriptionInterrupted(future); } else { commandExecutor.syncSubscription(future); } try { while (true) { ttl = tryAcquire(-1, leaseTime, unit, threadId); // lock acquired if (ttl == null) { break; } // waiting for message if (ttl >= 0) { try { future.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { if (interruptibly) { throw e; } future.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } } else { if (interruptibly) { future.getNow().getLatch().acquire(); } else { future.getNow().getLatch().acquireUninterruptibly(); } } } } finally { unsubscribe(future, threadId); } // get(lockAsync(leaseTime, unit)); } private Long tryAcquire(long waitTime, long leaseTime, TimeUnit unit, long threadId) { return get(tryAcquireAsync(waitTime, leaseTime, unit, threadId)); } private RFuture tryAcquireOnceAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId) { if (leaseTime != -1) { return tryLockInnerAsync(waitTime, leaseTime, unit, threadId, RedisCommands.EVAL_NULL_BOOLEAN); } RFuture ttlRemainingFuture = tryLockInnerAsync(waitTime, internalLockLeaseTime, TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_NULL_BOOLEAN); ttlRemainingFuture.onComplete((ttlRemaining, e) -> { if (e != null) { return; } // lock acquired if (ttlRemaining) { scheduleExpirationRenewal(threadId); } }); return ttlRemainingFuture; } private RFuture tryAcquireAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId) { if (leaseTime != -1) { return tryLockInnerAsync(waitTime, leaseTime, unit, threadId, RedisCommands.EVAL_LONG); } RFuture ttlRemainingFuture = tryLockInnerAsync(waitTime, internalLockLeaseTime, TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG); ttlRemainingFuture.onComplete((ttlRemaining, e) -> { if (e != null) { return; } // lock acquired if (ttlRemaining == null) { scheduleExpirationRenewal(threadId); } }); return ttlRemainingFuture; } @Override public boolean tryLock() { return get(tryLockAsync()); } RFuture tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand command) { internalLockLeaseTime = unit.toMillis(leaseTime); return evalWriteAsync(getName(), LongCodec.INSTANCE, command, "if (redis.call('exists', KEYS[1]) == 0) then " + "redis.call('hincrby', KEYS[1], ARGV[2], 1); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return nil; " + "end; " + "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " + "redis.call('hincrby', KEYS[1], ARGV[2], 1); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return nil; " + "end; " + "return redis.call('pttl', KEYS[1]);", Collections.singletonList(getName()), internalLockLeaseTime, getLockName(threadId)); } @Override public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { long time = unit.toMillis(waitTime); long current = System.currentTimeMillis(); long threadId = Thread.currentThread().getId(); Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId); // lock acquired if (ttl == null) { return true; } time -= System.currentTimeMillis() - current; if (time <= 0) { acquireFailed(waitTime, unit, threadId); return false; } current = System.currentTimeMillis(); RFuture subscribeFuture = subscribe(threadId); if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) { if (!subscribeFuture.cancel(false)) { subscribeFuture.onComplete((res, e) -> { if (e == null) { unsubscribe(subscribeFuture, threadId); } }); } acquireFailed(waitTime, unit, threadId); return false; } try { time -= System.currentTimeMillis() - current; if (time <= 0) { acquireFailed(waitTime, unit, threadId); return false; } while (true) { long currentTime = System.currentTimeMillis(); ttl = tryAcquire(waitTime, leaseTime, unit, threadId); // lock acquired if (ttl == null) { return true; } time -= System.currentTimeMillis() - currentTime; if (time <= 0) { acquireFailed(waitTime, unit, threadId); return false; } // waiting for message currentTime = System.currentTimeMillis(); if (ttl >= 0 && ttl < time) { subscribeFuture.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS); } else { subscribeFuture.getNow().getLatch().tryAcquire(time, TimeUnit.MILLISECONDS); } time -= System.currentTimeMillis() - currentTime; if (time <= 0) { acquireFailed(waitTime, unit, threadId); return false; } } } finally { unsubscribe(subscribeFuture, threadId); } // return get(tryLockAsync(waitTime, leaseTime, unit)); } protected RFuture subscribe(long threadId) { return pubSub.subscribe(getEntryName(), getChannelName()); } protected void unsubscribe(RFuture future, long threadId) { pubSub.unsubscribe(future.getNow(), getEntryName(), getChannelName()); } @Override public boolean tryLock(long waitTime, TimeUnit unit) throws InterruptedException { return tryLock(waitTime, -1, unit); } @Override public void unlock() { try { get(unlockAsync(Thread.currentThread().getId())); } catch (RedisException e) { if (e.getCause() instanceof IllegalMonitorStateException) { throw (IllegalMonitorStateException) e.getCause(); } else { throw e; } } // Future future = unlockAsync(); // future.awaitUninterruptibly(); // if (future.isSuccess()) { // return; // } // if (future.cause() instanceof IllegalMonitorStateException) { // throw (IllegalMonitorStateException)future.cause(); // } // throw commandExecutor.convertException(future); } @Override public boolean forceUnlock() { return get(forceUnlockAsync()); } @Override public RFuture forceUnlockAsync() { cancelExpirationRenewal(null); return evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('del', KEYS[1]) == 1) then " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1 " + "else " + "return 0 " + "end", Arrays.asList(getName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE); } protected RFuture unlockInnerAsync(long threadId) { return evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then " + "return nil;" + "end; " + "local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); " + "if (counter > 0) then " + "redis.call('pexpire', KEYS[1], ARGV[2]); " + "return 0; " + "else " + "redis.call('del', KEYS[1]); " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1; " + "end; " + "return nil;", Arrays.asList(getName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName(threadId)); } @Override public RFuture lockAsync() { return lockAsync(-1, null); } @Override public RFuture lockAsync(long leaseTime, TimeUnit unit) { long currentThreadId = Thread.currentThread().getId(); return lockAsync(leaseTime, unit, currentThreadId); } @Override public RFuture lockAsync(long currentThreadId) { return lockAsync(-1, null, currentThreadId); } @Override public RFuture lockAsync(long leaseTime, TimeUnit unit, long currentThreadId) { RPromise result = new RedissonPromise(); RFuture ttlFuture = tryAcquireAsync(-1, leaseTime, unit, currentThreadId); ttlFuture.onComplete((ttl, e) -> { if (e != null) { result.tryFailure(e); return; } // lock acquired if (ttl == null) { if (!result.trySuccess(null)) { unlockAsync(currentThreadId); } return; } RFuture subscribeFuture = subscribe(currentThreadId); subscribeFuture.onComplete((res, ex) -> { if (ex != null) { result.tryFailure(ex); return; } lockAsync(leaseTime, unit, subscribeFuture, result, currentThreadId); }); }); return result; } private void lockAsync(long leaseTime, TimeUnit unit, RFuture subscribeFuture, RPromise result, long currentThreadId) { RFuture ttlFuture = tryAcquireAsync(-1, leaseTime, unit, currentThreadId); ttlFuture.onComplete((ttl, e) -> { if (e != null) { unsubscribe(subscribeFuture, currentThreadId); result.tryFailure(e); return; } // lock acquired if (ttl == null) { unsubscribe(subscribeFuture, currentThreadId); if (!result.trySuccess(null)) { unlockAsync(currentThreadId); } return; } RedissonLockEntry entry = subscribeFuture.getNow(); if (entry.getLatch().tryAcquire()) { lockAsync(leaseTime, unit, subscribeFuture, result, currentThreadId); } else { // waiting for message AtomicReference futureRef = new AtomicReference(); Runnable listener = () -> { if (futureRef.get() != null) { futureRef.get().cancel(); } lockAsync(leaseTime, unit, subscribeFuture, result, currentThreadId); }; entry.addListener(listener); if (ttl >= 0) { Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (entry.removeListener(listener)) { lockAsync(leaseTime, unit, subscribeFuture, result, currentThreadId); } } }, ttl, TimeUnit.MILLISECONDS); futureRef.set(scheduledFuture); } } }); } @Override public RFuture tryLockAsync() { return tryLockAsync(Thread.currentThread().getId()); } @Override public RFuture tryLockAsync(long threadId) { return tryAcquireOnceAsync(-1, -1, null, threadId); } @Override public RFuture tryLockAsync(long waitTime, TimeUnit unit) { return tryLockAsync(waitTime, -1, unit); } @Override public RFuture tryLockAsync(long waitTime, long leaseTime, TimeUnit unit) { long currentThreadId = Thread.currentThread().getId(); return tryLockAsync(waitTime, leaseTime, unit, currentThreadId); } @Override public RFuture tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long currentThreadId) { RPromise result = new RedissonPromise(); AtomicLong time = new AtomicLong(unit.toMillis(waitTime)); long currentTime = System.currentTimeMillis(); RFuture ttlFuture = tryAcquireAsync(waitTime, leaseTime, unit, currentThreadId); ttlFuture.onComplete((ttl, e) -> { if (e != null) { result.tryFailure(e); return; } // lock acquired if (ttl == null) { if (!result.trySuccess(true)) { unlockAsync(currentThreadId); } return; } long el = System.currentTimeMillis() - currentTime; time.addAndGet(-el); if (time.get() <= 0) { trySuccessFalse(currentThreadId, result); return; } long current = System.currentTimeMillis(); AtomicReference futureRef = new AtomicReference(); RFuture subscribeFuture = subscribe(currentThreadId); subscribeFuture.onComplete((r, ex) -> { if (ex != null) { result.tryFailure(ex); return; } if (futureRef.get() != null) { futureRef.get().cancel(); } long elapsed = System.currentTimeMillis() - current; time.addAndGet(-elapsed); tryLockAsync(time, waitTime, leaseTime, unit, subscribeFuture, result, currentThreadId); }); if (!subscribeFuture.isDone()) { Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (!subscribeFuture.isDone()) { subscribeFuture.cancel(false); trySuccessFalse(currentThreadId, result); } } }, time.get(), TimeUnit.MILLISECONDS); futureRef.set(scheduledFuture); } }); return result; } private void trySuccessFalse(long currentThreadId, RPromise result) { acquireFailedAsync(-1, null, currentThreadId).onComplete((res, e) -> { if (e == null) { result.trySuccess(false); } else { result.tryFailure(e); } }); } private void tryLockAsync(AtomicLong time, long waitTime, long leaseTime, TimeUnit unit, RFuture subscribeFuture, RPromise result, long currentThreadId) { if (result.isDone()) { unsubscribe(subscribeFuture, currentThreadId); return; } if (time.get() <= 0) { unsubscribe(subscribeFuture, currentThreadId); trySuccessFalse(currentThreadId, result); return; } long curr = System.currentTimeMillis(); RFuture ttlFuture = tryAcquireAsync(waitTime, leaseTime, unit, currentThreadId); ttlFuture.onComplete((ttl, e) -> { if (e != null) { unsubscribe(subscribeFuture, currentThreadId); result.tryFailure(e); return; } // lock acquired if (ttl == null) { unsubscribe(subscribeFuture, currentThreadId); if (!result.trySuccess(true)) { unlockAsync(currentThreadId); } return; } long el = System.currentTimeMillis() - curr; time.addAndGet(-el); if (time.get() <= 0) { unsubscribe(subscribeFuture, currentThreadId); trySuccessFalse(currentThreadId, result); return; } // waiting for message long current = System.currentTimeMillis(); RedissonLockEntry entry = subscribeFuture.getNow(); if (entry.getLatch().tryAcquire()) { tryLockAsync(time, waitTime, leaseTime, unit, subscribeFuture, result, currentThreadId); } else { AtomicBoolean executed = new AtomicBoolean(); AtomicReference futureRef = new AtomicReference(); Runnable listener = () -> { executed.set(true); if (futureRef.get() != null) { futureRef.get().cancel(); } long elapsed = System.currentTimeMillis() - current; time.addAndGet(-elapsed); tryLockAsync(time, waitTime, leaseTime, unit, subscribeFuture, result, currentThreadId); }; entry.addListener(listener); long t = time.get(); if (ttl >= 0 && ttl < time.get()) { t = ttl; } if (!executed.get()) { Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (entry.removeListener(listener)) { long elapsed = System.currentTimeMillis() - current; time.addAndGet(-elapsed); tryLockAsync(time, waitTime, leaseTime, unit, subscribeFuture, result, currentThreadId); } } }, t, TimeUnit.MILLISECONDS); futureRef.set(scheduledFuture); } } }); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy