Skip to content

Commit 6aded83

Browse files
committed
Added trace logging around connection pool
1 parent 1240e48 commit 6aded83

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

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

Lines changed: 22 additions & 11 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. Connection will be disposed" );
7371
pooledConnection.dispose();
7472
}
7573
if (isTerminating.get()) {
@@ -93,8 +91,13 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
9391
PooledConnection connection = queue.poll();
9492
if ( connection == null )
9593
{
94+
trace( "No Idle connections. Creating new connection." );
9695
connection = supplier.get();
9796
}
97+
else
98+
{
99+
trace( "Acquiring connection." );
100+
}
98101
acquiredConnections.add( connection );
99102

100103
if (isTerminating.get()) {
@@ -105,11 +108,6 @@ public PooledConnection acquire( Supplier<PooledConnection> supplier )
105108
return connection;
106109
}
107110

108-
public List<PooledConnection> toList()
109-
{
110-
return new ArrayList<>( queue );
111-
}
112-
113111
public boolean isEmpty()
114112
{
115113
return queue.isEmpty();
@@ -140,6 +138,8 @@ public void terminate()
140138
{
141139
if ( isTerminating.compareAndSet( false, true ) )
142140
{
141+
trace( "Initiating connection queue termination." );
142+
143143
while ( !queue.isEmpty() )
144144
{
145145
PooledConnection idleConnection = queue.poll();
@@ -170,6 +170,17 @@ private void disposeSafely( PooledConnection connection )
170170

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

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 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)