Search in sources :

Example 31 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class RedissonPermitExpirableSemaphore method tryAcquireAsync.

private void tryAcquireAsync(AtomicLong time, int permits, RedissonLockEntry entry, CompletableFuture<String> result, long ttl, TimeUnit timeUnit) {
    if (result.isDone()) {
        unsubscribe(entry);
        return;
    }
    if (time.get() <= 0) {
        unsubscribe(entry);
        result.complete(null);
        return;
    }
    long timeoutDate = calcTimeout(ttl, timeUnit);
    long curr = System.currentTimeMillis();
    RFuture<String> tryAcquireFuture = tryAcquireAsync(permits, timeoutDate);
    tryAcquireFuture.whenComplete((permitId, e) -> {
        if (e != null) {
            unsubscribe(entry);
            result.completeExceptionally(e);
            return;
        }
        Long nearestTimeout;
        if (permitId != null) {
            if (!permitId.startsWith(":")) {
                unsubscribe(entry);
                if (!result.complete(permitId)) {
                    releaseAsync(permitId);
                }
                return;
            } else {
                nearestTimeout = Long.valueOf(permitId.substring(1)) - System.currentTimeMillis();
            }
        } else {
            nearestTimeout = null;
        }
        long el = System.currentTimeMillis() - curr;
        time.addAndGet(-el);
        if (time.get() <= 0) {
            unsubscribe(entry);
            result.complete(null);
            return;
        }
        // waiting for message
        long current = System.currentTimeMillis();
        if (entry.getLatch().tryAcquire()) {
            tryAcquireAsync(time, permits, entry, result, ttl, timeUnit);
        } else {
            AtomicReference<Timeout> waitTimeoutFutureRef = new AtomicReference<Timeout>();
            Timeout scheduledFuture;
            if (nearestTimeout != null) {
                scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {

                    @Override
                    public void run(Timeout timeout) throws Exception {
                        if (waitTimeoutFutureRef.get() != null && !waitTimeoutFutureRef.get().cancel()) {
                            return;
                        }
                        long elapsed = System.currentTimeMillis() - current;
                        time.addAndGet(-elapsed);
                        tryAcquireAsync(time, permits, entry, result, ttl, timeUnit);
                    }
                }, nearestTimeout, TimeUnit.MILLISECONDS);
            } else {
                scheduledFuture = null;
            }
            Runnable listener = () -> {
                if (waitTimeoutFutureRef.get() != null && !waitTimeoutFutureRef.get().cancel()) {
                    entry.getLatch().release();
                    return;
                }
                if (scheduledFuture != null && !scheduledFuture.cancel()) {
                    entry.getLatch().release();
                    return;
                }
                long elapsed = System.currentTimeMillis() - current;
                time.addAndGet(-elapsed);
                tryAcquireAsync(time, permits, entry, result, ttl, timeUnit);
            };
            entry.addListener(listener);
            long t = time.get();
            Timeout waitTimeoutFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {

                @Override
                public void run(Timeout timeout) throws Exception {
                    if (scheduledFuture != null && !scheduledFuture.cancel()) {
                        return;
                    }
                    if (entry.removeListener(listener)) {
                        long elapsed = System.currentTimeMillis() - current;
                        time.addAndGet(-elapsed);
                        tryAcquireAsync(time, permits, entry, result, ttl, timeUnit);
                    }
                }
            }, t, TimeUnit.MILLISECONDS);
            waitTimeoutFutureRef.set(waitTimeoutFuture);
        }
    });
}
Also used : TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 32 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class RedissonPermitExpirableSemaphore method tryAcquireAsync.

private RFuture<String> tryAcquireAsync(int permits, long waitTime, long ttl, TimeUnit timeUnit) {
    CompletableFuture<String> result = new CompletableFuture<>();
    AtomicLong time = new AtomicLong(timeUnit.toMillis(waitTime));
    long curr = System.currentTimeMillis();
    long timeoutDate = calcTimeout(ttl, timeUnit);
    RFuture<String> tryAcquireFuture = tryAcquireAsync(permits, timeoutDate);
    tryAcquireFuture.whenComplete((permitId, e) -> {
        if (e != null) {
            result.completeExceptionally(e);
            return;
        }
        if (permitId != null && !permitId.startsWith(":")) {
            if (!result.complete(permitId)) {
                releaseAsync(permitId);
            }
            return;
        }
        long el = System.currentTimeMillis() - curr;
        time.addAndGet(-el);
        if (time.get() <= 0) {
            result.complete(null);
            return;
        }
        long current = System.currentTimeMillis();
        AtomicReference<Timeout> futureRef = new AtomicReference<Timeout>();
        CompletableFuture<RedissonLockEntry> subscribeFuture = subscribe();
        subscribeFuture.whenComplete((r, ex) -> {
            if (ex != null) {
                result.completeExceptionally(ex);
                return;
            }
            if (futureRef.get() != null) {
                futureRef.get().cancel();
            }
            long elapsed = System.currentTimeMillis() - current;
            time.addAndGet(-elapsed);
            tryAcquireAsync(time, permits, r, result, ttl, timeUnit);
        });
        if (!subscribeFuture.isDone()) {
            Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {

                @Override
                public void run(Timeout timeout) throws Exception {
                    if (!subscribeFuture.isDone()) {
                        result.complete(null);
                    }
                }
            }, time.get(), TimeUnit.MILLISECONDS);
            futureRef.set(scheduledFuture);
        }
    });
    return new CompletableFutureWrapper<>(result);
}
Also used : Timeout(io.netty.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(io.netty.util.TimerTask) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 33 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class RedissonLock method tryLockAsync.

@Override
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long currentThreadId) {
    CompletableFuture<Boolean> result = new CompletableFuture<>();
    AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
    long currentTime = System.currentTimeMillis();
    RFuture<Long> ttlFuture = tryAcquireAsync(waitTime, leaseTime, unit, currentThreadId);
    ttlFuture.whenComplete((ttl, e) -> {
        if (e != null) {
            result.completeExceptionally(e);
            return;
        }
        // lock acquired
        if (ttl == null) {
            if (!result.complete(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<Timeout> futureRef = new AtomicReference<Timeout>();
        CompletableFuture<RedissonLockEntry> subscribeFuture = subscribe(currentThreadId);
        subscribeFuture.whenComplete((r, ex) -> {
            if (ex != null) {
                result.completeExceptionally(ex);
                return;
            }
            if (futureRef.get() != null) {
                futureRef.get().cancel();
            }
            long elapsed = System.currentTimeMillis() - current;
            time.addAndGet(-elapsed);
            tryLockAsync(time, waitTime, leaseTime, unit, r, 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 new CompletableFutureWrapper<>(result);
}
Also used : Timeout(io.netty.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) RedisException(org.redisson.client.RedisException) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(io.netty.util.TimerTask) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 34 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class RedissonSemaphore method tryAcquireAsync.

private void tryAcquireAsync(AtomicLong time, int permits, RedissonLockEntry entry, CompletableFuture<Boolean> result) {
    if (result.isDone()) {
        unsubscribe(entry);
        return;
    }
    if (time.get() <= 0) {
        unsubscribe(entry);
        result.complete(false);
        return;
    }
    long curr = System.currentTimeMillis();
    RFuture<Boolean> tryAcquireFuture = tryAcquireAsync(permits);
    tryAcquireFuture.whenComplete((res, e) -> {
        if (e != null) {
            unsubscribe(entry);
            result.completeExceptionally(e);
            return;
        }
        if (res) {
            unsubscribe(entry);
            if (!result.complete(true)) {
                releaseAsync(permits);
            }
            return;
        }
        long el = System.currentTimeMillis() - curr;
        time.addAndGet(-el);
        if (time.get() <= 0) {
            unsubscribe(entry);
            result.complete(false);
            return;
        }
        // waiting for message
        long current = System.currentTimeMillis();
        if (entry.getLatch().tryAcquire()) {
            tryAcquireAsync(time, permits, entry, result);
        } else {
            AtomicBoolean executed = new AtomicBoolean();
            AtomicReference<Timeout> futureRef = new AtomicReference<>();
            Runnable listener = () -> {
                executed.set(true);
                if (futureRef.get() != null && !futureRef.get().cancel()) {
                    entry.getLatch().release();
                    return;
                }
                long elapsed = System.currentTimeMillis() - current;
                time.addAndGet(-elapsed);
                tryAcquireAsync(time, permits, entry, result);
            };
            entry.addListener(listener);
            long t = time.get();
            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);
                            tryAcquireAsync(time, permits, entry, result);
                        }
                    }
                }, t, TimeUnit.MILLISECONDS);
                futureRef.set(scheduledFuture);
            }
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 35 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class RedissonSemaphore method tryAcquireAsync.

@Override
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit) {
    CompletableFuture<Boolean> result = new CompletableFuture<>();
    AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
    long curr = System.currentTimeMillis();
    RFuture<Boolean> tryAcquireFuture = tryAcquireAsync(permits);
    tryAcquireFuture.whenComplete((res, e) -> {
        if (e != null) {
            result.completeExceptionally(e);
            return;
        }
        if (res) {
            if (!result.complete(true)) {
                releaseAsync(permits);
            }
            return;
        }
        long elap = System.currentTimeMillis() - curr;
        time.addAndGet(-elap);
        if (time.get() <= 0) {
            result.complete(false);
            return;
        }
        long current = System.currentTimeMillis();
        AtomicReference<Timeout> futureRef = new AtomicReference<Timeout>();
        CompletableFuture<RedissonLockEntry> subscribeFuture = subscribe();
        subscribeFuture.whenComplete((r, ex) -> {
            if (ex != null) {
                result.completeExceptionally(ex);
                return;
            }
            if (futureRef.get() != null) {
                futureRef.get().cancel();
            }
            long elapsed = System.currentTimeMillis() - current;
            time.addAndGet(-elapsed);
            if (time.get() < 0) {
                unsubscribe(r);
                result.complete(false);
                return;
            }
            tryAcquireAsync(time, permits, r, result);
        });
        if (!subscribeFuture.isDone()) {
            Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {

                @Override
                public void run(Timeout timeout) throws Exception {
                    if (!subscribeFuture.isDone()) {
                        result.complete(false);
                    }
                }
            }, time.get(), TimeUnit.MILLISECONDS);
            futureRef.set(scheduledFuture);
        }
    });
    return new CompletableFutureWrapper<>(result);
}
Also used : Timeout(io.netty.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(io.netty.util.TimerTask) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

Timeout (io.netty.util.Timeout)38 TimerTask (io.netty.util.TimerTask)38 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 RFuture (org.redisson.api.RFuture)8 RedisTimeoutException (org.redisson.client.RedisTimeoutException)7 FutureListener (io.netty.util.concurrent.FutureListener)6 ByteBuf (io.netty.buffer.ByteBuf)5 ChannelFuture (io.netty.channel.ChannelFuture)5 Future (io.netty.util.concurrent.Future)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 RedisException (org.redisson.client.RedisException)5 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)5 CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)5 RedisConnection (org.redisson.client.RedisConnection)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 IOException (java.io.IOException)3