Skip to content

Commit 7b4e4f7

Browse files
authored
Corrected the logging behavior when a channel is marked inactive due to a graceful connection pool shutdown. (#826)
1 parent 4c053ca commit 7b4e4f7

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/inbound/ChannelErrorHandler.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@ public void channelInactive( ChannelHandlerContext ctx )
6767
{
6868
log.debug( "Channel is inactive" );
6969

70+
String terminationReason = terminationReason( ctx.channel() );
71+
Throwable error = ErrorUtil.newConnectionTerminatedError( terminationReason );
72+
7073
if ( !failed )
7174
{
7275
// channel became inactive not because of a fatal exception that came from exceptionCaught
7376
// it is most likely inactive because actual network connection broke or was explicitly closed by the driver
7477

75-
String terminationReason = terminationReason( ctx.channel() );
76-
ServiceUnavailableException error = ErrorUtil.newConnectionTerminatedError( terminationReason );
77-
fail( ctx, error );
78+
messageDispatcher.handleChannelInactive( error );
79+
ctx.channel().close();
80+
}
81+
else
82+
{
83+
fail( error );
7884
}
7985
}
8086

@@ -89,16 +95,14 @@ public void exceptionCaught( ChannelHandlerContext ctx, Throwable error )
8995
{
9096
failed = true;
9197
log.warn( "Fatal error occurred in the pipeline", error );
92-
fail( ctx, error );
98+
fail( error );
9399
}
94100
}
95101

96-
private void fail( ChannelHandlerContext ctx, Throwable error )
102+
private void fail( Throwable error )
97103
{
98104
Throwable cause = transformError( error );
99105
messageDispatcher.handleChannelError( cause );
100-
log.debug( "Closing channel because of a failure '%s'", error );
101-
ctx.close();
102106
}
103107

104108
private static Throwable transformError( Throwable error )

driver/src/main/java/org/neo4j/driver/internal/async/inbound/InboundMessageDispatcher.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Queue;
2727

28+
import org.neo4j.driver.exceptions.ServiceUnavailableException;
2829
import org.neo4j.driver.internal.handlers.ResetResponseHandler;
2930
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
3031
import org.neo4j.driver.internal.messaging.ResponseMessageHandler;
@@ -45,6 +46,7 @@ public class InboundMessageDispatcher implements ResponseMessageHandler
4546
private final Queue<ResponseHandler> handlers = new LinkedList<>();
4647
private final Logger log;
4748

49+
private volatile boolean gracefullyClosed;
4850
private Throwable currentError;
4951
private boolean fatalErrorOccurred;
5052

@@ -142,6 +144,20 @@ public void handleIgnoredMessage()
142144
handler.onFailure( error );
143145
}
144146

147+
public void handleChannelInactive( Throwable cause )
148+
{
149+
// report issue if the connection has not been terminated as a result of a graceful shutdown request from its
150+
// parent pool
151+
if ( !gracefullyClosed )
152+
{
153+
handleChannelError( cause );
154+
}
155+
else
156+
{
157+
channel.close();
158+
}
159+
}
160+
145161
public void handleChannelError( Throwable error )
146162
{
147163
if ( currentError != null )
@@ -160,6 +176,9 @@ public void handleChannelError( Throwable error )
160176
ResponseHandler handler = removeHandler();
161177
handler.onFailure( currentError );
162178
}
179+
180+
log.debug( "Closing channel because of a failure '%s'", error );
181+
channel.close();
163182
}
164183

165184
public void clearCurrentError()
@@ -177,6 +196,11 @@ public boolean fatalErrorOccurred()
177196
return fatalErrorOccurred;
178197
}
179198

199+
public void prepareToCloseChannel( )
200+
{
201+
this.gracefullyClosed = true;
202+
}
203+
180204
/**
181205
* <b>Visible for testing</b>
182206
*/

driver/src/main/java/org/neo4j/driver/internal/messaging/v3/BoltProtocolV3.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.neo4j.driver.internal.BookmarkHolder;
3434
import org.neo4j.driver.internal.DatabaseName;
3535
import org.neo4j.driver.internal.async.UnmanagedTransaction;
36+
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
3637
import org.neo4j.driver.internal.cluster.RoutingContext;
3738
import org.neo4j.driver.internal.cursor.AsyncResultCursorOnlyFactory;
3839
import org.neo4j.driver.internal.cursor.ResultCursorFactory;
@@ -101,9 +102,13 @@ public void initializeChannel( String userAgent, AuthToken authToken, RoutingCon
101102
@Override
102103
public void prepareToCloseChannel( Channel channel )
103104
{
105+
InboundMessageDispatcher messageDispatcher = messageDispatcher( channel );
106+
104107
GoodbyeMessage message = GoodbyeMessage.GOODBYE;
105-
messageDispatcher( channel ).enqueue( NoOpResponseHandler.INSTANCE );
108+
messageDispatcher.enqueue( NoOpResponseHandler.INSTANCE );
106109
channel.writeAndFlush( message, channel.voidPromise() );
110+
111+
messageDispatcher.prepareToCloseChannel( );
107112
}
108113

109114
@Override

0 commit comments

Comments
 (0)