Search in sources :

Example 6 with RedisTimeoutException

use of org.redisson.client.RedisTimeoutException in project redisson by redisson.

the class RedisClientEntry method pingAsync.

@Override
public RFuture<Boolean> pingAsync(long timeout, TimeUnit timeUnit) {
    RFuture<Boolean> f = commandExecutor.readAsync(client, null, RedisCommands.PING_BOOL);
    CompletableFuture<Boolean> s = f.toCompletableFuture().handle((res, e) -> {
        if (e != null) {
            return false;
        }
        return res;
    });
    commandExecutor.getConnectionManager().newTimeout(t -> {
        RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for command: PING, Redis client: " + client);
        s.completeExceptionally(ex);
    }, timeout, timeUnit);
    return new CompletableFutureWrapper<>(s);
}
Also used : RedisTimeoutException(org.redisson.client.RedisTimeoutException) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 7 with RedisTimeoutException

use of org.redisson.client.RedisTimeoutException in project redisson by redisson.

the class RedisNode method pingAsync.

@Override
public RFuture<Boolean> pingAsync(long timeout, TimeUnit timeUnit) {
    RFuture<Boolean> f = commandExecutor.readAsync(client, null, RedisCommands.PING_BOOL);
    CompletionStage<Boolean> s = f.exceptionally(e -> false);
    commandExecutor.getConnectionManager().newTimeout(t -> {
        RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout (" + timeUnit.toMillis(timeout) + "ms) for command: PING, Redis client: " + client);
        s.toCompletableFuture().completeExceptionally(ex);
    }, timeout, timeUnit);
    return new CompletableFutureWrapper<>(s);
}
Also used : RedisTimeoutException(org.redisson.client.RedisTimeoutException) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 8 with RedisTimeoutException

use of org.redisson.client.RedisTimeoutException in project redisson by redisson.

the class PublishSubscribe method subscribe.

public CompletableFuture<E> subscribe(String entryName, String channelName) {
    AsyncSemaphore semaphore = service.getSemaphore(new ChannelName(channelName));
    CompletableFuture<E> newPromise = new CompletableFuture<>();
    int timeout = service.getConnectionManager().getConfig().getTimeout();
    Timeout lockTimeout = service.getConnectionManager().newTimeout(t -> {
        newPromise.completeExceptionally(new RedisTimeoutException("Unable to acquire subscription lock after " + timeout + "ms. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
    }, timeout, TimeUnit.MILLISECONDS);
    semaphore.acquire(() -> {
        if (!lockTimeout.cancel()) {
            semaphore.release();
            return;
        }
        E entry = entries.get(entryName);
        if (entry != null) {
            entry.acquire();
            semaphore.release();
            entry.getPromise().whenComplete((r, e) -> {
                if (e != null) {
                    newPromise.completeExceptionally(e);
                    return;
                }
                newPromise.complete(r);
            });
            return;
        }
        E value = createEntry(newPromise);
        value.acquire();
        E oldValue = entries.putIfAbsent(entryName, value);
        if (oldValue != null) {
            oldValue.acquire();
            semaphore.release();
            oldValue.getPromise().whenComplete((r, e) -> {
                if (e != null) {
                    newPromise.completeExceptionally(e);
                    return;
                }
                newPromise.complete(r);
            });
            return;
        }
        RedisPubSubListener<Object> listener = createListener(channelName, value);
        CompletableFuture<PubSubConnectionEntry> s = service.subscribe(LongCodec.INSTANCE, channelName, semaphore, listener);
        s.whenComplete((r, e) -> {
            if (e != null) {
                value.getPromise().completeExceptionally(e);
                return;
            }
            value.getPromise().complete(value);
        });
    });
    return newPromise;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ChannelName(org.redisson.client.ChannelName) RedisTimeoutException(org.redisson.client.RedisTimeoutException) Timeout(io.netty.util.Timeout)

Example 9 with RedisTimeoutException

use of org.redisson.client.RedisTimeoutException in project redisson by redisson.

the class RedissonTopicTest method testResubscriptionAfterFailover.

@Test
public void testResubscriptionAfterFailover() throws Exception {
    RedisRunner.RedisProcess master = new RedisRunner().nosave().randomDir().port(6400).run();
    RedisRunner.RedisProcess slave1 = new RedisRunner().port(6380).nosave().randomDir().slaveof("127.0.0.1", 6400).run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner().nosave().randomDir().port(26379).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6400, 2).sentinelDownAfterMilliseconds("myMaster", 750).sentinelFailoverTimeout("myMaster", 1250).run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner().nosave().randomDir().port(26380).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6400, 2).sentinelDownAfterMilliseconds("myMaster", 750).sentinelFailoverTimeout("myMaster", 1250).run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner().nosave().randomDir().port(26381).sentinel().sentinelMonitor("myMaster", "127.0.0.1", 6400, 2).sentinelDownAfterMilliseconds("myMaster", 750).sentinelFailoverTimeout("myMaster", 1250).run();
    Thread.sleep(1000);
    Config config = new Config();
    config.useSentinelServers().addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster").setSubscriptionsPerConnection(20).setSubscriptionConnectionPoolSize(200);
    RedissonClient redisson = Redisson.create(config);
    ScheduledExecutorService executor1 = Executors.newScheduledThreadPool(5);
    AtomicBoolean exceptionDetected = new AtomicBoolean(false);
    Deque<String> status = new ConcurrentLinkedDeque<>();
    Runnable rLockPayload = () -> {
        try {
            Integer randomLock = ThreadLocalRandom.current().nextInt(100);
            RLock lock = redisson.getLock(randomLock.toString());
            lock.lock(10, TimeUnit.SECONDS);
            lock.unlock();
            status.add("ok");
        } catch (Exception e) {
            status.add("failed");
            if (e.getCause().getMessage().contains("slaves were synced")) {
                return;
            }
            e.printStackTrace();
            exceptionDetected.set(true);
        }
    };
    executor1.scheduleAtFixedRate(rLockPayload, 100, 50, TimeUnit.MILLISECONDS);
    executor1.scheduleAtFixedRate(rLockPayload, 100, 50, TimeUnit.MILLISECONDS);
    executor1.scheduleAtFixedRate(rLockPayload, 100, 50, TimeUnit.MILLISECONDS);
    executor1.scheduleAtFixedRate(rLockPayload, 100, 50, TimeUnit.MILLISECONDS);
    executor1.scheduleAtFixedRate(rLockPayload, 100, 50, TimeUnit.MILLISECONDS);
    Thread.sleep(java.time.Duration.ofSeconds(10).toMillis());
    RedisClientConfig masterConfig = new RedisClientConfig().setAddress(master.getRedisServerAddressAndPort());
    // Failover from master to slave
    try {
        RedisClient.create(masterConfig).connect().sync(new RedisStrictCommand<Void>("DEBUG", "SEGFAULT"));
    } catch (RedisTimeoutException e) {
    // node goes down, so this command times out waiting for the response
    }
    // Re-introduce master as slave like kubernetes would do
    RedisRunner.RedisProcess slave2 = new RedisRunner().port(6381).nosave().randomDir().slaveof("127.0.0.1", 6380).run();
    System.out.println("Failover Finished, start to see Subscribe timeouts now. Can't recover this without a refresh of redison client ");
    Thread.sleep(java.time.Duration.ofSeconds(10).toMillis());
    assertThat(exceptionDetected.get()).isFalse();
    assertThat(status.peekLast()).isEqualTo("ok");
    executor1.shutdown();
    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();
}
Also used : Config(org.redisson.config.Config) RedisClientConfig(org.redisson.client.RedisClientConfig) IOException(java.io.IOException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisClientConfig(org.redisson.client.RedisClientConfig)

Example 10 with RedisTimeoutException

use of org.redisson.client.RedisTimeoutException in project redisson by redisson.

the class RedissonTopicTest method failover.

private void failover(ClusterProcesses processes, RedisRunner master, RedisRunner slave) throws InterruptedException {
    final RedisClient masterClient = connect(processes, master);
    try {
        masterClient.connect().sync(new RedisStrictCommand<Void>("DEBUG", "SEGFAULT"));
    } catch (RedisTimeoutException e) {
    // node goes down, so this command times out waiting for the response
    }
    Thread.sleep(java.time.Duration.ofSeconds(25).toMillis());
    final RedisClient slaveClient = connect(processes, slave);
    slaveClient.connect().sync(new RedisStrictCommand<Void>("CLUSTER", "FAILOVER"), "TAKEOVER");
    Thread.sleep(java.time.Duration.ofSeconds(25).toMillis());
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisTimeoutException(org.redisson.client.RedisTimeoutException)

Aggregations

RedisTimeoutException (org.redisson.client.RedisTimeoutException)13 Timeout (io.netty.util.Timeout)6 TimerTask (io.netty.util.TimerTask)5 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)4 ChannelFuture (io.netty.channel.ChannelFuture)3 RedisConnection (org.redisson.client.RedisConnection)3 MasterSlaveServersConfig (org.redisson.config.MasterSlaveServersConfig)3 CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RedisAskException (org.redisson.client.RedisAskException)2 RedisLoadingException (org.redisson.client.RedisLoadingException)2 RedisMovedException (org.redisson.client.RedisMovedException)2 RedisTryAgainException (org.redisson.client.RedisTryAgainException)2 CommandData (org.redisson.client.protocol.CommandData)2 NodeSource (org.redisson.connection.NodeSource)2 RPromise (org.redisson.misc.RPromise)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)1 IOException (java.io.IOException)1