Skip to content

Commit b0fba93

Browse files
author
Zhen
committed
The metrics registration must happen before the pool creation to avoid accessing a metrics of a pool before the metrics is fully created.
1 parent 2a1c771 commit b0fba93

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/pool/ConnectionPoolImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,23 @@ public boolean isOpen()
179179
private ChannelPool getOrCreatePool( BoltServerAddress address )
180180
{
181181
ChannelPool pool = pools.get( address );
182-
if ( pool == null )
182+
if ( pool != null )
183183
{
184-
pool = newPool( address );
184+
return pool;
185+
}
185186

186-
if ( pools.putIfAbsent( address, pool ) != null )
187-
{
188-
// We lost a race to create the pool, dispose of the one we created, and recurse
189-
pool.close();
190-
return getOrCreatePool( address );
191-
}
192-
else
187+
synchronized ( this )
188+
{
189+
pool = pools.get( address );
190+
if ( pool != null )
193191
{
194-
// We added a new pool as a result we add a new metrics for the pool too
195-
driverMetricsListener.addMetrics( address, this );
192+
return pool;
196193
}
197-
}
198194

195+
driverMetricsListener.addMetrics( address, this );
196+
pool = newPool( address );
197+
pools.put( address, pool );
198+
}
199199
return pool;
200200
}
201201

driver/src/main/java/org/neo4j/driver/internal/metrics/InternalConnectionMetrics.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.neo4j.driver.internal.metrics;
2020

2121
import java.time.Duration;
22+
import java.util.Objects;
2223

2324
import org.neo4j.driver.internal.BoltServerAddress;
2425
import org.neo4j.driver.internal.metrics.spi.ConnectionMetrics;
@@ -33,6 +34,7 @@ public class InternalConnectionMetrics implements ConnectionMetrics, ConnectionM
3334

3435
public InternalConnectionMetrics( BoltServerAddress serverAddress, int connectionTimeoutMillis )
3536
{
37+
Objects.requireNonNull( serverAddress );
3638
this.serverAddress = serverAddress;
3739
connHistogram = new InternalHistogram( Duration.ofMillis( connectionTimeoutMillis ).toNanos() );
3840
inUseHistogram = new InternalHistogram();

driver/src/main/java/org/neo4j/driver/internal/metrics/InternalConnectionPoolMetrics.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.neo4j.driver.internal.metrics;
2121

2222
import java.time.Duration;
23+
import java.util.Objects;
2324
import java.util.concurrent.atomic.AtomicInteger;
2425
import java.util.concurrent.atomic.AtomicLong;
2526

@@ -45,6 +46,9 @@ public class InternalConnectionPoolMetrics implements ConnectionPoolMetrics, Con
4546

4647
public InternalConnectionPoolMetrics( BoltServerAddress address, ConnectionPool pool, long connAcquisitionTimeoutMs )
4748
{
49+
Objects.requireNonNull( address );
50+
Objects.requireNonNull( pool );
51+
4852
this.address = address;
4953
this.pool = pool;
5054
this.acquisitionTimeHistogram = new InternalHistogram( Duration.ofMillis( connAcquisitionTimeoutMs ).toNanos() );

driver/src/main/java/org/neo4j/driver/internal/metrics/InternalDriverMetrics.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.neo4j.driver.internal.metrics;
2020

2121
import java.util.Map;
22+
import java.util.Objects;
2223
import java.util.concurrent.ConcurrentHashMap;
2324

2425
import org.neo4j.driver.internal.BoltServerAddress;
@@ -41,6 +42,7 @@ public class InternalDriverMetrics extends InternalAbstractDriverMetrics
4142

4243
public InternalDriverMetrics( Config config )
4344
{
45+
Objects.requireNonNull( config );
4446
this.config = config;
4547
this.connectionPoolMetrics = new ConcurrentHashMap<>();
4648
this.connectionMetrics = new ConcurrentHashMap<>();

0 commit comments

Comments
 (0)