36
36
import org .neo4j .driver .internal .BoltServerAddress ;
37
37
import org .neo4j .driver .internal .async .ChannelConnector ;
38
38
import org .neo4j .driver .internal .async .NettyConnection ;
39
+ import org .neo4j .driver .internal .metrics .DriverMetricsHandler ;
40
+ import org .neo4j .driver .internal .metrics .ListenerEvent ;
41
+ import org .neo4j .driver .internal .metrics .SimpleTimerListenerEvent ;
39
42
import org .neo4j .driver .internal .spi .Connection ;
40
43
import org .neo4j .driver .internal .spi .ConnectionPool ;
41
44
import org .neo4j .driver .internal .util .Clock ;
@@ -48,29 +51,31 @@ public class ConnectionPoolImpl implements ConnectionPool
48
51
{
49
52
private final ChannelConnector connector ;
50
53
private final Bootstrap bootstrap ;
51
- private final ActiveChannelTracker activeChannelTracker ;
54
+ private final NettyChannelTracker nettyChannelTracker ;
52
55
private final NettyChannelHealthChecker channelHealthChecker ;
53
56
private final PoolSettings settings ;
54
57
private final Clock clock ;
55
58
private final Logger log ;
59
+ private DriverMetricsHandler driverMetrics ;
56
60
57
61
private final ConcurrentMap <BoltServerAddress ,ChannelPool > pools = new ConcurrentHashMap <>();
58
62
private final AtomicBoolean closed = new AtomicBoolean ();
59
63
60
64
public ConnectionPoolImpl ( ChannelConnector connector , Bootstrap bootstrap , PoolSettings settings ,
61
- Logging logging , Clock clock )
65
+ DriverMetricsHandler metricsHandler , Logging logging , Clock clock )
62
66
{
63
- this ( connector , bootstrap , new ActiveChannelTracker ( logging ), settings , logging , clock );
67
+ this ( connector , bootstrap , new NettyChannelTracker ( metricsHandler , logging ), settings , metricsHandler , logging , clock );
64
68
}
65
69
66
- ConnectionPoolImpl ( ChannelConnector connector , Bootstrap bootstrap , ActiveChannelTracker activeChannelTracker ,
67
- PoolSettings settings , Logging logging , Clock clock )
70
+ ConnectionPoolImpl ( ChannelConnector connector , Bootstrap bootstrap , NettyChannelTracker nettyChannelTracker ,
71
+ PoolSettings settings , DriverMetricsHandler driverMetrics , Logging logging , Clock clock )
68
72
{
69
73
this .connector = connector ;
70
74
this .bootstrap = bootstrap ;
71
- this .activeChannelTracker = activeChannelTracker ;
75
+ this .nettyChannelTracker = nettyChannelTracker ;
72
76
this .channelHealthChecker = new NettyChannelHealthChecker ( settings , clock , logging );
73
77
this .settings = settings ;
78
+ this .driverMetrics = driverMetrics ;
74
79
this .clock = clock ;
75
80
this .log = logging .getLog ( ConnectionPool .class .getSimpleName () );
76
81
}
@@ -80,15 +85,26 @@ public CompletionStage<Connection> acquire( BoltServerAddress address )
80
85
{
81
86
log .trace ( "Acquiring a connection from pool towards %s" , address );
82
87
88
+ // TODO no need to init if no driver metrics
89
+ ListenerEvent acquireEvent = new SimpleTimerListenerEvent ();
90
+
83
91
assertNotClosed ();
84
92
ChannelPool pool = getOrCreatePool ( address );
93
+ driverMetrics .beforeAcquiring ( address , acquireEvent );
85
94
Future <Channel > connectionFuture = pool .acquire ();
86
95
87
96
return Futures .asCompletionStage ( connectionFuture ).handle ( ( channel , error ) ->
88
97
{
89
- processAcquisitionError ( error );
90
- assertNotClosed ( address , channel , pool );
91
- return new NettyConnection ( channel , pool , clock );
98
+ try
99
+ {
100
+ processAcquisitionError ( error );
101
+ assertNotClosed ( address , channel , pool );
102
+ return new NettyConnection ( channel , pool , clock );
103
+ }
104
+ finally
105
+ {
106
+ driverMetrics .afterAcquired ( address , acquireEvent );
107
+ }
92
108
} );
93
109
}
94
110
@@ -99,7 +115,7 @@ public void retainAll( Set<BoltServerAddress> addressesToRetain )
99
115
{
100
116
if ( !addressesToRetain .contains ( address ) )
101
117
{
102
- int activeChannels = activeChannelTracker . activeChannelCount ( address );
118
+ int activeChannels = nettyChannelTracker . inUseChannelCount ( address );
103
119
if ( activeChannels == 0 )
104
120
{
105
121
// address is not present in updated routing table and has no active connections
@@ -118,9 +134,15 @@ public void retainAll( Set<BoltServerAddress> addressesToRetain )
118
134
}
119
135
120
136
@ Override
121
- public int activeConnections ( BoltServerAddress address )
137
+ public int inUseConnections ( BoltServerAddress address )
138
+ {
139
+ return nettyChannelTracker .inUseChannelCount ( address );
140
+ }
141
+
142
+ @ Override
143
+ public int idleConnections ( BoltServerAddress address )
122
144
{
123
- return activeChannelTracker . activeChannelCount ( address );
145
+ return nettyChannelTracker . idleChannelCount ( address );
124
146
}
125
147
126
148
@ Override
@@ -150,6 +172,12 @@ public CompletionStage<Void> close()
150
172
.thenApply ( ignore -> null );
151
173
}
152
174
175
+ @ Override
176
+ public boolean isOpen ()
177
+ {
178
+ return !closed .get ();
179
+ }
180
+
153
181
private ChannelPool getOrCreatePool ( BoltServerAddress address )
154
182
{
155
183
ChannelPool pool = pools .get ( address );
@@ -163,13 +191,19 @@ private ChannelPool getOrCreatePool( BoltServerAddress address )
163
191
pool .close ();
164
192
return getOrCreatePool ( address );
165
193
}
194
+ else
195
+ {
196
+ // We added a new pool as a result we add a new metrics for the pool too
197
+ driverMetrics .addPoolMetrics ( address , this );
198
+ }
166
199
}
200
+
167
201
return pool ;
168
202
}
169
203
170
204
ChannelPool newPool ( BoltServerAddress address )
171
205
{
172
- return new NettyChannelPool ( address , connector , bootstrap , activeChannelTracker , channelHealthChecker ,
206
+ return new NettyChannelPool ( address , connector , bootstrap , nettyChannelTracker , channelHealthChecker ,
173
207
settings .connectionAcquisitionTimeout (), settings .maxConnectionPoolSize () );
174
208
}
175
209
@@ -217,4 +251,10 @@ private void assertNotClosed( BoltServerAddress address, Channel channel, Channe
217
251
assertNotClosed ();
218
252
}
219
253
}
254
+
255
+ @ Override
256
+ public String toString ()
257
+ {
258
+ return "ConnectionPoolImpl{" + "pools=" + pools + '}' ;
259
+ }
220
260
}
0 commit comments