-
Notifications
You must be signed in to change notification settings - Fork 138
/
ConnectionPoolEmitterImpl.java
440 lines (402 loc) · 17.9 KB
/
ConnectionPoolEmitterImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.resource.pool.monitor;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.enterprise.config.serverbeans.ResourcePool;
import com.sun.enterprise.connectors.ConnectorRuntime;
import com.sun.enterprise.resource.listener.PoolLifeCycleListener;
import com.sun.logging.LogDomains;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.glassfish.connectors.config.ConnectorConnectionPool;
import org.glassfish.external.probe.provider.PluginPoint;
import org.glassfish.external.probe.provider.StatsProviderManager;
import org.glassfish.resourcebase.resources.api.PoolInfo;
import static com.sun.appserv.connectors.internal.api.ConnectorsUtil.escapeResourceNameForMonitoring;
import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_APP;
/**
* Implementation of PoolLifeCycleListener interface to listen to events related
* to jdbc monitoring. The methods invoke the probe providers internally to
* provide the monitoring related information.
*
* @author Shalini M
*/
public class ConnectionPoolEmitterImpl implements PoolLifeCycleListener {
private final PoolInfo poolInfo;
private final ConnectionPoolProbeProvider poolProbeProvider;
/** Map of app names and respective emitters for a pool. */
private final Map<PoolInfo, Map<String, ConnectionPoolAppEmitterImpl>> appStatsMap;
/** Map of app names for a resource handle id */
private final Map<Long, String> resourceAppAssociationMap;
private static Logger _logger = LogDomains.getLogger(ConnectionPoolEmitterImpl.class, LogDomains.RSR_LOGGER);
private final List<ConnectorConnPoolAppStatsProvider> ccPoolAppStatsProviders;
private final ConnectorRuntime runtime;
// keep a static reference to InitialContext so as to avoid performance issues.
private volatile static InitialContext ic;
/**
* Constructor
*
* @param poolInfo connection pool on whose behalf this emitter emits pool related
* probe events
* @param provider
*/
public ConnectionPoolEmitterImpl(PoolInfo poolInfo, ConnectionPoolProbeProvider provider) {
this.poolInfo = poolInfo;
this.poolProbeProvider = provider;
this.ccPoolAppStatsProviders = new ArrayList<>();
this.appStatsMap = new HashMap<>();
this.resourceAppAssociationMap = new ConcurrentHashMap<>();
runtime = ConnectorRuntime.getRuntime();
if (ic == null) {
synchronized (ConnectionPoolEmitterImpl.class) {
if (ic == null) {
try {
ic = new InitialContext();
} catch (NamingException e) {
// ignore
}
}
}
}
}
/**
* Fires probe event that a stack trace is to be printed on the server.log.
* The stack trace is mainly related to connection leak tracing for the
* given jdbc connection pool.
* @param stackTrace
*/
@Override
public void toString(StringBuffer stackTrace) {
stackTrace.append("\n Monitoring Statistics for \n" + poolInfo);
poolProbeProvider.toString(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName(), stackTrace);
}
/**
* Fires probe event that a connection has been acquired by the application
* for the given jdbc connection pool.
*/
@Override
public void connectionAcquired(long resourceHandleId) {
ConnectionPoolAppEmitterImpl appEmitter =
detectAppBasedProviders(getAppName(resourceHandleId));
poolProbeProvider.connectionAcquiredEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
if(appEmitter != null) {
appEmitter.connectionAcquired();
}
}
/**
* Fires probe event related to the fact that a connection request is served
* in the time <code>timeTakenInMillis</code> for the given jdbc connection
* pool.
*
* @param timeTakenInMillis time taken to serve a connection
*/
@Override
public void connectionRequestServed(long timeTakenInMillis) {
poolProbeProvider.connectionRequestServedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName(), timeTakenInMillis);
}
/**
* Fires probe event related to the fact that the given jdbc connection pool
* has got a connection timed-out event.
*/
@Override
public void connectionTimedOut() {
poolProbeProvider.connectionTimedOutEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event that a connection under test does not match the
* current request for the given jdbc connection pool.
*/
@Override
public void connectionNotMatched() {
poolProbeProvider.connectionNotMatchedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event that a connection under test matches the current
* request for the given jdbc connection pool.
*/
@Override
public void connectionMatched() {
poolProbeProvider.connectionMatchedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event that a connection is destroyed for the
* given jdbc connection pool.
*/
@Override
public void connectionDestroyed(long resourceHandleId) {
poolProbeProvider.connectionDestroyedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
// Clearing the resource handle id appName mappings stored
// This is useful in cases where connection-leak-reclaim is ON where we destroy
// the connection. In this case, connection-release would not have happened.
resourceAppAssociationMap.remove(resourceHandleId);
}
/**
* Fires probe event that a connection is released for the given jdbc
* connection pool.
*/
@Override
public void connectionReleased(long resourceHandleId) {
ConnectionPoolAppEmitterImpl appEmitter =
detectAppBasedProviders(getAppName(resourceHandleId));
poolProbeProvider.connectionReleasedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
if(appEmitter != null) {
appEmitter.connectionReleased();
}
// Clearing the resource handle id appName mappings stored
resourceAppAssociationMap.remove(resourceHandleId);
}
/**
* Fires probe event that a connection is created for the given jdbc
* connection pool.
*/
@Override
public void connectionCreated() {
poolProbeProvider.connectionCreatedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event related to the fact that the given jdbc connection pool
* has got a connection leak event.
*
*/
@Override
public void foundPotentialConnectionLeak() {
poolProbeProvider.potentialConnLeakEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a connection validation failed event.
*
* @param count number of times the validation failed
*/
@Override
public void connectionValidationFailed(int count) {
poolProbeProvider.connectionValidationFailedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName(), count);
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a connection used event.
*/
@Override
public void connectionUsed(long resourceHandleId) {
ConnectionPoolAppEmitterImpl appEmitter =
detectAppBasedProviders(getAppName(resourceHandleId));
poolProbeProvider.connectionUsedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
if (appEmitter != null) {
appEmitter.connectionUsed();
}
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a connection freed event.
*
* @param count number of connections freed to pool
*/
@Override
public void connectionsFreed(int count) {
poolProbeProvider.connectionsFreedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName(), count);
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a decrement connection used event.
*
*/
@Override
public void decrementConnectionUsed(long resourceHandleId) {
ConnectionPoolAppEmitterImpl appEmitter =
detectAppBasedProviders(getAppName(resourceHandleId));
poolProbeProvider.decrementConnectionUsedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
if(appEmitter != null) {
appEmitter.decrementConnectionUsed();
}
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a decrement free connections size event.
*
*/
@Override
public void decrementNumConnFree() {
poolProbeProvider.decrementNumConnFreeEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event related to the fact the given jdbc connection pool has
* got a decrement free connections size event.
*
* @param beingDestroyed if the connection is destroyed due to error
* @param steadyPoolSize
*/
@Override
public void incrementNumConnFree(boolean beingDestroyed, int steadyPoolSize) {
poolProbeProvider.incrementNumConnFreeEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName(), beingDestroyed, steadyPoolSize);
}
/**
* Fires probe event related to the fact the given jdbc connection pool's
* wait queue length has been incremented
*
*/
@Override
public void connectionRequestQueued() {
poolProbeProvider.connectionRequestQueuedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
/**
* Fires probe event related to the fact the given jdbc connection pool's
* wait queue length has been decremented.
*
*/
@Override
public void connectionRequestDequeued() {
poolProbeProvider.connectionRequestDequeuedEvent(poolInfo.getName().toString(), poolInfo.getApplicationName(), poolInfo.getModuleName());
}
private String getAppName(long resourceHandleId) {
// if monitoring is disabled, avoid sending events
// as we need to do "java:app/AppName" to get applicationName for each
// acquire/return connection call which is a performance bottleneck.
if(!runtime.isJdbcPoolMonitoringEnabled() && !runtime.isConnectorPoolMonitoringEnabled()){
return null;
}
String appName = resourceAppAssociationMap.get(resourceHandleId);
if(appName == null){
try {
if(ic == null){
synchronized(ConnectionPoolEmitterImpl.class) {
if(ic == null) {
ic = new InitialContext();
}
}
}
appName = (String) ic.lookup(JNDI_CTX_JAVA_APP + "AppName");
resourceAppAssociationMap.put(resourceHandleId, appName);
} catch (NamingException ex) {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Unable to get application name using "
+ "java:app/AppName method");
}
}
}
return appName;
}
/**
* Detect if a Stats Provider has already been registered to the
* monitoring framework for this appName and if so, return the specific
* emitter. If not already registered, create and register the
* Stats Provider object to the monitoring framework and add to the list
* of emitters.
*
* @param appName
* @return
*/
private ConnectionPoolAppEmitterImpl detectAppBasedProviders(String appName) {
ConnectionPoolAppProbeProvider probeAppProvider = null;
ConnectionPoolAppEmitterImpl connPoolAppEmitter = null;
if (appName == null) {
//Case when appname cannot be detected. Emitter cannot exist for
//a null appName for any pool.
return null;
}
if (appStatsMap.containsKey(poolInfo)) {
//Some apps have been registered for this pool.
//Find if this appName is already registered.
//All appEmitters for this pool
Map<String, ConnectionPoolAppEmitterImpl> appEmitters = appStatsMap.get(poolInfo);
//Check if the appEmitters list has an emitter for the appName.
ConnectionPoolAppEmitterImpl emitter = appEmitters.get(appName);
if (emitter != null) {
// This appName has already been registered to StatsProviderManager
return emitter;
}
if (!ConnectorsUtil.isApplicationScopedResource(poolInfo)) {
// register to the StatsProviderManager and add to the list.
probeAppProvider = registerConnectionPool(appName);
connPoolAppEmitter = addToList(appName, probeAppProvider, appEmitters);
}
} else if (!ConnectorsUtil.isApplicationScopedResource(poolInfo)) {
// Does not contain any app providers associated with this poolname
// Create a map of app emitters for the appName and add them to the
// appStatsMap
probeAppProvider = registerConnectionPool(appName);
Map<String, ConnectionPoolAppEmitterImpl> appEmitters = new HashMap<>();
connPoolAppEmitter = addToList(appName, probeAppProvider, appEmitters);
}
return connPoolAppEmitter;
}
/**
* Register the jdbc/connector connection pool Stats Provider object to the
* monitoring framework under the specific application name monitoring
* sub tree.
*
* @param appName
* @return
*/
private ConnectionPoolAppProbeProvider registerConnectionPool(String appName) {
ResourcePool pool = runtime.getConnectionPoolConfig(poolInfo);
ConnectionPoolAppProbeProvider probeAppProvider =
runtime.getProbeProviderUtil().getConnPoolBootstrap().registerPool(poolInfo, appName);
if (pool instanceof ConnectorConnectionPool) {
probeAppProvider = new ConnectorConnPoolAppProbeProvider();
ConnectorConnPoolAppStatsProvider ccPoolAppStatsProvider =
new ConnectorConnPoolAppStatsProvider(poolInfo, appName);
StatsProviderManager.register(
"connector-connection-pool",
PluginPoint.SERVER,
"resources/" + escapeResourceNameForMonitoring(poolInfo.getName()) + "/" + appName,
ccPoolAppStatsProvider);
ccPoolAppStatsProviders.add(ccPoolAppStatsProvider);
}
return probeAppProvider;
}
/**
* Add to the pool emitters list. the connection pool application emitter
* for the specific poolInfo and appName.
* @param appName
* @param probeAppProvider
* @param appEmitters
* @return
*/
private ConnectionPoolAppEmitterImpl addToList(String appName,
ConnectionPoolAppProbeProvider probeAppProvider,
Map<String, ConnectionPoolAppEmitterImpl> appEmitters) {
final ConnectionPoolAppEmitterImpl connPoolAppEmitter;
if (probeAppProvider == null) {
connPoolAppEmitter = null;
} else {
//Add the newly created probe provider to the list.
connPoolAppEmitter = new ConnectionPoolAppEmitterImpl(poolInfo.getName(), appName, probeAppProvider);
// NOTE : this appName here is different from "appName" instance variable.
appEmitters.put(appName, connPoolAppEmitter);
appStatsMap.put(poolInfo, appEmitters);
}
runtime.getProbeProviderUtil().getConnPoolBootstrap().addToPoolEmitters(poolInfo, this);
return connPoolAppEmitter;
}
/**
* Unregister the AppStatsProviders registered for this connection pool.
*/
public void unregisterAppStatsProviders() {
runtime.getProbeProviderUtil().getConnPoolBootstrap().unRegisterPool();
for (ConnectorConnPoolAppStatsProvider ccPoolAppStatsProvider : ccPoolAppStatsProviders) {
StatsProviderManager.unregister(ccPoolAppStatsProvider);
}
ccPoolAppStatsProviders.clear();
}
}