Skip to content

Fix liveness checking for unresponsive connections #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public void handleIgnoredMessage() {
handler.onFailure(error);
}

public HandlerHook getBeforeLastHandlerHook() {
return this.beforeLastHandlerHook;
}

public void handleChannelInactive(Throwable cause) {
// report issue if the connection has not been terminated as a result of a graceful shutdown request from its
// parent pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.exceptions.AuthorizationExpiredException;
import org.neo4j.driver.internal.async.connection.AuthorizationStateListener;
import org.neo4j.driver.internal.async.connection.ChannelAttributes;
import org.neo4j.driver.internal.async.inbound.ConnectionReadTimeoutHandler;
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
import org.neo4j.driver.internal.handlers.PingResponseHandler;
import org.neo4j.driver.internal.messaging.request.ResetMessage;
import org.neo4j.driver.internal.util.Clock;
Expand Down Expand Up @@ -117,8 +121,24 @@ private boolean hasBeenIdleForTooLong(Channel channel) {

private Future<Boolean> ping(Channel channel) {
Promise<Boolean> result = channel.eventLoop().newPromise();
messageDispatcher(channel).enqueue(new PingResponseHandler(result, channel, logging));
InboundMessageDispatcher messageDispatcher = messageDispatcher(channel);
messageDispatcher.enqueue(new PingResponseHandler(result, channel, logging));
attachConnectionReadTimeoutHandler(channel, messageDispatcher);
channel.writeAndFlush(ResetMessage.RESET, channel.voidPromise());
return result;
}

private void attachConnectionReadTimeoutHandler(Channel channel, InboundMessageDispatcher messageDispatcher) {
ChannelAttributes.connectionReadTimeout(channel).ifPresent(connectionReadTimeout -> {
ConnectionReadTimeoutHandler connectionReadTimeoutHandler =
new ConnectionReadTimeoutHandler(connectionReadTimeout, TimeUnit.SECONDS);
channel.pipeline().addFirst(connectionReadTimeoutHandler);
log.debug("Added ConnectionReadTimeoutHandler");
messageDispatcher.setBeforeLastHandlerHook((messageType) -> {
channel.pipeline().remove(connectionReadTimeoutHandler);
messageDispatcher.setBeforeLastHandlerHook(null);
log.debug("Removed ConnectionReadTimeoutHandler");
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import static org.hamcrest.junit.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.setConnectionReadTimeout;
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.setCreationTimestamp;
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.setLastUsedTimestamp;
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.setMessageDispatcher;
Expand All @@ -34,6 +38,7 @@
import static org.neo4j.driver.util.TestUtil.await;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.concurrent.Future;
import java.util.Collections;
Expand All @@ -46,6 +51,7 @@
import org.junit.jupiter.api.Test;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.AuthorizationExpiredException;
import org.neo4j.driver.internal.async.inbound.ConnectionReadTimeoutHandler;
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
import org.neo4j.driver.internal.messaging.request.ResetMessage;
import org.neo4j.driver.internal.util.Clock;
Expand Down Expand Up @@ -155,6 +161,65 @@ void shouldKeepIdleConnectionWhenPingSucceeds() {
testPing(true);
}

@Test
void shouldHandlePingWithConnectionReceiveTimeout() {
int idleTimeBeforeConnectionTest = 1000;
long connectionReadTimeout = 60L;
PoolSettings settings = new PoolSettings(
DEFAULT_MAX_CONNECTION_POOL_SIZE,
DEFAULT_CONNECTION_ACQUISITION_TIMEOUT,
NOT_CONFIGURED,
idleTimeBeforeConnectionTest);
Clock clock = Clock.SYSTEM;
NettyChannelHealthChecker healthChecker = newHealthChecker(settings, clock);

setCreationTimestamp(channel, clock.millis());
setConnectionReadTimeout(channel, connectionReadTimeout);
setLastUsedTimestamp(channel, clock.millis() - idleTimeBeforeConnectionTest * 2);

Future<Boolean> healthy = healthChecker.isHealthy(channel);
channel.runPendingTasks();

ChannelHandler firstElementOnPipeline = channel.pipeline().first();
assertInstanceOf(ConnectionReadTimeoutHandler.class, firstElementOnPipeline);
assertNotNull(dispatcher.getBeforeLastHandlerHook());
ConnectionReadTimeoutHandler readTimeoutHandler = (ConnectionReadTimeoutHandler) firstElementOnPipeline;
assertEquals(connectionReadTimeout * 1000L, readTimeoutHandler.getReaderIdleTimeInMillis());
assertEquals(ResetMessage.RESET, single(channel.outboundMessages()));
assertFalse(healthy.isDone());

dispatcher.handleSuccessMessage(Collections.emptyMap());
assertThat(await(healthy), is(true));
assertNull(channel.pipeline().first());
assertNull(dispatcher.getBeforeLastHandlerHook());
}

@Test
void shouldHandlePingWithoutConnectionReceiveTimeout() {
int idleTimeBeforeConnectionTest = 1000;
PoolSettings settings = new PoolSettings(
DEFAULT_MAX_CONNECTION_POOL_SIZE,
DEFAULT_CONNECTION_ACQUISITION_TIMEOUT,
NOT_CONFIGURED,
idleTimeBeforeConnectionTest);
Clock clock = Clock.SYSTEM;
NettyChannelHealthChecker healthChecker = newHealthChecker(settings, clock);

setCreationTimestamp(channel, clock.millis());
setLastUsedTimestamp(channel, clock.millis() - idleTimeBeforeConnectionTest * 2);

Future<Boolean> healthy = healthChecker.isHealthy(channel);
channel.runPendingTasks();

assertNull(channel.pipeline().first());
assertEquals(ResetMessage.RESET, single(channel.outboundMessages()));
assertFalse(healthy.isDone());

dispatcher.handleSuccessMessage(Collections.emptyMap());
assertThat(await(healthy), is(true));
assertNull(channel.pipeline().first());
}

@Test
void shouldDropIdleConnectionWhenPingFails() {
testPing(false);
Expand Down