Search in sources :

Example 1 with ConnectionWatchdog

use of io.lettuce.core.protocol.ConnectionWatchdog in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method commandRetriedChannelClosesWhileFlush.

@Test
void commandRetriedChannelClosesWhileFlush() {
    assumeTrue(Version.identify().get("netty-transport").artifactVersion().startsWith("4.0.2"));
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = connection.sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    RedisChannelWriter channelWriter = ConnectionTestUtil.getChannelWriter(connection);
    sync.set(key, "1");
    assertThat(verificationConnection.get(key)).isEqualTo("1");
    final CountDownLatch block = new CountDownLatch(1);
    ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(sync.getStatefulConnection());
    AsyncCommand<String, String, Object> command = getBlockOnEncodeCommand(block);
    channelWriter.write(command);
    connectionWatchdog.setReconnectSuspended(true);
    Channel channel = ConnectionTestUtil.getChannel(sync.getStatefulConnection());
    channel.unsafe().disconnect(channel.newPromise());
    assertThat(channel.isOpen()).isFalse();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isFalse();
    block.countDown();
    assertThat(command.await(2, TimeUnit.SECONDS)).isFalse();
    connectionWatchdog.setReconnectSuspended(false);
    connectionWatchdog.scheduleReconnect();
    assertThat(command.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(command.isCancelled()).isFalse();
    assertThat(command.isDone()).isTrue();
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    assertThat(ConnectionTestUtil.getStack(sync.getStatefulConnection())).isEmpty();
    assertThat(ConnectionTestUtil.getCommandBuffer(sync.getStatefulConnection())).isEmpty();
    sync.getStatefulConnection().close();
    verificationConnection.getStatefulConnection().close();
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) Channel(io.netty.channel.Channel) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionWatchdog

use of io.lettuce.core.protocol.ConnectionWatchdog in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method commandCancelledOverSyncAPIAfterConnectionIsDisconnected.

@Test
void commandCancelledOverSyncAPIAfterConnectionIsDisconnected() {
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> sync = connection.sync();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    sync.set(key, "1");
    ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(sync.getStatefulConnection());
    connectionWatchdog.setListenOnChannelInactive(false);
    sync.quit();
    Wait.untilTrue(() -> !sync.getStatefulConnection().isOpen()).waitOrTimeout();
    try {
        sync.incr(key);
    } catch (RedisException e) {
        assertThat(e).isExactlyInstanceOf(RedisCommandTimeoutException.class);
    }
    assertThat(verificationConnection.get("key")).isEqualTo("1");
    assertThat(ConnectionTestUtil.getDisconnectedBuffer(connection).size()).isGreaterThan(0);
    assertThat(ConnectionTestUtil.getCommandBuffer(connection)).isEmpty();
    connectionWatchdog.setListenOnChannelInactive(true);
    connectionWatchdog.scheduleReconnect();
    while (!ConnectionTestUtil.getCommandBuffer(connection).isEmpty() || !ConnectionTestUtil.getDisconnectedBuffer(connection).isEmpty()) {
        Delay.delay(Duration.ofMillis(10));
    }
    assertThat(sync.get(key)).isEqualTo("1");
    sync.getStatefulConnection().close();
    verificationConnection.getStatefulConnection().close();
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) RedisException(io.lettuce.core.RedisException) RedisCommandTimeoutException(io.lettuce.core.RedisCommandTimeoutException) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionWatchdog

use of io.lettuce.core.protocol.ConnectionWatchdog in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method reconnectIsActiveHandler.

@Test
void reconnectIsActiveHandler() {
    RedisCommands<String, String> connection = client.connect().sync();
    ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(connection.getStatefulConnection());
    assertThat(connectionWatchdog).isNotNull();
    assertThat(connectionWatchdog.isListenOnChannelInactive()).isTrue();
    assertThat(connectionWatchdog.isReconnectSuspended()).isFalse();
    connection.getStatefulConnection().close();
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionWatchdog

use of io.lettuce.core.protocol.ConnectionWatchdog in project lettuce-core by lettuce-io.

the class AtLeastOnceTest method retryAfterConnectionIsDisconnected.

@Test
void retryAfterConnectionIsDisconnected() throws Exception {
    StatefulRedisConnection<String, String> connection = client.connect();
    RedisCommands<String, String> verificationConnection = client.connect().sync();
    connection.sync().set(key, "1");
    ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(connection);
    connectionWatchdog.setListenOnChannelInactive(false);
    connection.async().quit();
    while (connection.isOpen()) {
        Delay.delay(Duration.ofMillis(100));
    }
    assertThat(connection.async().incr(key).await(1, TimeUnit.SECONDS)).isFalse();
    assertThat(verificationConnection.get("key")).isEqualTo("1");
    assertThat(ConnectionTestUtil.getDisconnectedBuffer(connection).size()).isGreaterThan(0);
    assertThat(ConnectionTestUtil.getCommandBuffer(connection)).isEmpty();
    connectionWatchdog.setListenOnChannelInactive(true);
    connectionWatchdog.scheduleReconnect();
    while (!ConnectionTestUtil.getCommandBuffer(connection).isEmpty() || !ConnectionTestUtil.getDisconnectedBuffer(connection).isEmpty()) {
        Delay.delay(Duration.ofMillis(10));
    }
    assertThat(connection.sync().get(key)).isEqualTo("2");
    assertThat(verificationConnection.get(key)).isEqualTo("2");
    connection.close();
    verificationConnection.getStatefulConnection().close();
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) AbstractRedisClientTest(io.lettuce.core.AbstractRedisClientTest) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectionWatchdog

use of io.lettuce.core.protocol.ConnectionWatchdog in project lettuce-core by lettuce-io.

the class AbstractRedisClient method closeResources.

private CompletableFuture<Void> closeResources() {
    List<CompletionStage<Void>> closeFutures = new ArrayList<>();
    List<Closeable> closeableResources = new ArrayList<>(this.closeableResources);
    for (Closeable closeableResource : closeableResources) {
        if (closeableResource instanceof AsyncCloseable) {
            closeFutures.add(((AsyncCloseable) closeableResource).closeAsync());
        } else {
            try {
                closeableResource.close();
            } catch (Exception e) {
                logger.debug("Exception on Close: " + e.getMessage(), e);
            }
        }
        this.closeableResources.remove(closeableResource);
    }
    for (Channel c : channels.toArray(new Channel[0])) {
        if (c == null) {
            continue;
        }
        ChannelPipeline pipeline = c.pipeline();
        ConnectionWatchdog commandHandler = pipeline.get(ConnectionWatchdog.class);
        if (commandHandler != null) {
            commandHandler.setListenOnChannelInactive(false);
        }
    }
    try {
        closeFutures.add(Futures.toCompletionStage(channels.close()));
    } catch (Exception e) {
        logger.debug("Cannot close channels", e);
    }
    return Futures.allOf(closeFutures);
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) Closeable(java.io.Closeable) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) CompletionStage(java.util.concurrent.CompletionStage) CancellationException(java.util.concurrent.CancellationException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

ConnectionWatchdog (io.lettuce.core.protocol.ConnectionWatchdog)7 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)5 Test (org.junit.jupiter.api.Test)5 Channel (io.netty.channel.Channel)3 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RedisCommandTimeoutException (io.lettuce.core.RedisCommandTimeoutException)1 RedisException (io.lettuce.core.RedisException)1 AsyncCloseable (io.lettuce.core.internal.AsyncCloseable)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionStage (java.util.concurrent.CompletionStage)1