Skip to content

Commit 88dd1f5

Browse files
committed
Added trace logging around connection pool
1 parent 1240e48 commit 88dd1f5

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

driver/src/main/java/org/neo4j/driver/internal/net/pooling/BlockingPooledConnectionQueue.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
package org.neo4j.driver.internal.net.pooling;
2020

21-
import java.util.ArrayList;
2221
import java.util.Collections;
23-
import java.util.List;
2422
import java.util.Set;
2523
import java.util.concurrent.BlockingQueue;
2624
import java.util.concurrent.ConcurrentHashMap;
@@ -40,8 +38,6 @@
4038
*/
4139
public class BlockingPooledConnectionQueue
4240
{
43-
public static final String LOG_NAME = "ConnectionQueue";
44-
4541
/** The backing queue, keeps track of connections currently in queue */
4642
private final BlockingQueue<PooledConnection> queue;
4743
private final Logger logger;
@@ -69,7 +65,9 @@ public boolean offer( PooledConnection pooledConnection )
6965
acquiredConnections.remove( pooledConnection );
7066
boolean offer = queue.offer( pooledConnection );
7167
// not added back to the queue, dispose of the connection
72-
if (!offer) {
68+
if ( !offer )
69+
{
70+
trace( "Queue is at capacity. Offered connection will be disposed." );
7371
pooledConnection.dispose();
7472
}
7573
if (isTerminating.get()) {
@@ -89,12 +87,16 @@ public boolean offer( PooledConnection pooledConnection )
8987
*/
9088
public PooledConnection acquire( Supplier<PooledConnection> supplier )
9189
{
92-
9390
PooledConnection connection = queue.poll();
9491
if ( connection == null )
9592
{
93+
trace( "No idle connections. Creating new connection." );
9694
connection = supplier.get();
9795
}
96+
else
97+
{
98+
trace( "Acquired and idle connection." );
99+
}
98100
acquiredConnections.add( connection );
99101

100102
if (isTerminating.get()) {
@@ -105,11 +107,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105107
return connection;
106108
}
107109

108-
public List<PooledConnection> toList()
109-
{
110-
return new ArrayList<>( queue );
111-
}
112-
113110
public boolean isEmpty()
114111
{
115112
return queue.isEmpty();
@@ -140,6 +137,8 @@ public void terminate()
140137
{
141138
if ( isTerminating.compareAndSet( false, true ) )
142139
{
140+
trace( "Initiating connection queue termination." );
141+
143142
while ( !queue.isEmpty() )
144143
{
145144
PooledConnection idleConnection = queue.poll();
@@ -170,6 +169,17 @@ private void disposeSafely( PooledConnection connection )
170169

171170
private static Logger createLogger( BoltServerAddress address, Logging logging )
172171
{
173-
return new DelegatingLogger( logging.getLog( LOG_NAME ), address.toString() );
172+
Logger log = logging.getLog( BlockingPooledConnectionQueue.class.getSimpleName() );
173+
return new DelegatingLogger( log, address.toString() );
174+
}
175+
176+
private void trace( String message )
177+
{
178+
// Call to activeConnections is costly. This if block is to avoid that.
179+
if ( logger.isTraceEnabled() )
180+
{
181+
logger.trace( "%s ActiveConnections %s IdleConnections %s",
182+
message, activeConnections(), queue.size() );
183+
}
174184
}
175185
}

driver/src/main/java/org/neo4j/driver/internal/net/pooling/SocketConnectionPool.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.driver.internal.spi.PooledConnection;
3131
import org.neo4j.driver.internal.util.Clock;
3232
import org.neo4j.driver.internal.util.Supplier;
33+
import org.neo4j.driver.v1.Logger;
3334
import org.neo4j.driver.v1.Logging;
3435

3536
/**
@@ -58,6 +59,7 @@ public class SocketConnectionPool implements ConnectionPool
5859
private final ConnectionValidator<PooledConnection> connectionValidator;
5960
private final Clock clock;
6061
private final Logging logging;
62+
private final Logger logger;
6163

6264
public SocketConnectionPool( PoolSettings poolSettings, Connector connector, Clock clock, Logging logging )
6365
{
@@ -66,6 +68,7 @@ public SocketConnectionPool( PoolSettings poolSettings, Connector connector, Clo
6668
this.connectionValidator = new PooledConnectionValidator( this );
6769
this.clock = clock;
6870
this.logging = logging;
71+
this.logger = logging.getLog( SocketConnectionPool.class.getSimpleName() );
6972
}
7073

7174
@Override
@@ -85,6 +88,7 @@ public void purge( BoltServerAddress address )
8588
BlockingPooledConnectionQueue connections = pools.remove( address );
8689
if ( connections != null )
8790
{
91+
logger.trace( "Purging pool for address %s", address );
8892
connections.terminate();
8993
}
9094
}
@@ -107,6 +111,7 @@ public void close()
107111
{
108112
if ( closed.compareAndSet( false, true ) )
109113
{
114+
logger.trace( "Initiating connection pool termination" );
110115
for ( BlockingPooledConnectionQueue pool : pools.values() )
111116
{
112117
pool.terminate();

0 commit comments

Comments
 (0)