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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations