Skip to content

Commit 84a4351

Browse files
committed
JAVA:613: Added ConnectionPoolStat class with cmd line interface
Clarified in doc that some public classes are not part of the public API Added a few more stats for in use connections: durationMS and threadName
1 parent 70d5c8a commit 84a4351

File tree

9 files changed

+364
-37
lines changed

9 files changed

+364
-37
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private synchronized Response go(OutMessage msg, DBCollection coll, boolean forc
110110
throw new IllegalStateException( "_out shouldn't be null" );
111111

112112
try {
113-
_activeOutMessage = msg;
113+
_activeState = new ActiveState(msg);
114114
msg.prepare();
115115
msg.pipe( _out );
116116

@@ -128,7 +128,7 @@ private synchronized Response go(OutMessage msg, DBCollection coll, boolean forc
128128
throw ioe;
129129
}
130130
finally {
131-
_activeOutMessage = null;
131+
_activeState = null;
132132
_processingResponse = false;
133133
}
134134
}
@@ -145,7 +145,7 @@ synchronized private Response findOne( DB db , String coll , DBObject q ) throws
145145
}
146146

147147
synchronized CommandResult runCommand( DB db , DBObject cmd ) throws IOException {
148-
Response res = findOne( db , "$cmd" , cmd );
148+
Response res = findOne(db, "$cmd", cmd);
149149
return convertToCommandResult(cmd, res);
150150
}
151151

@@ -168,7 +168,7 @@ synchronized CommandResult tryGetLastError( DB db , long last, WriteConcern conc
168168
if ( last != _calls )
169169
return null;
170170

171-
return getLastError( db , concern );
171+
return getLastError(db, concern);
172172
}
173173

174174
/**
@@ -265,8 +265,8 @@ protected void finalize() throws Throwable{
265265
close();
266266
}
267267

268-
OutMessage getOutMessageBeingProcessed() {
269-
return _activeOutMessage;
268+
ActiveState getActiveState() {
269+
return _activeState;
270270
}
271271

272272
int getLocalPort() {
@@ -338,7 +338,18 @@ public DBPortPool getPool() {
338338
private Map<DB,Boolean> _authed = new ConcurrentHashMap<DB, Boolean>( );
339339
int _lastThread;
340340
long _calls = 0;
341-
private volatile OutMessage _activeOutMessage;
341+
private volatile ActiveState _activeState;
342+
343+
class ActiveState {
344+
ActiveState(final OutMessage outMessage) {
345+
this.outMessage = outMessage;
346+
this.startTime = System.nanoTime();
347+
this.threadName = Thread.currentThread().getName();
348+
}
349+
final OutMessage outMessage;
350+
final long startTime;
351+
final String threadName;
352+
}
342353

343354
private static Logger _rootLogger = Logger.getLogger( "com.mongodb.port" );
344355
}

src/main/com/mongodb/DBPortPool.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,21 @@
3131
import java.util.concurrent.atomic.AtomicInteger;
3232
import java.util.logging.Level;
3333

34+
/**
35+
* This class is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
36+
*/
3437
public class DBPortPool extends SimplePool<DBPort> implements MongoConnectionPoolMXBean {
3538

36-
public MongoConnection[] getInUseConnections() {
37-
List<MongoConnection> connectionList = new ArrayList<MongoConnection>();
39+
public InUseConnectionInfo[] getInUseConnections() {
40+
List<InUseConnectionInfo> inUseConnectionInfoList = new ArrayList<InUseConnectionInfo>();
3841
synchronized (_avail) {
3942
for (DBPort port : _all) {
4043
if (!_avail.contains(port)) {
41-
OutMessage curOutMessage = port.getOutMessageBeingProcessed();
42-
if (curOutMessage != null) {
43-
connectionList.add(new MongoConnection(port.getLocalPort(), curOutMessage.getNamespace(),
44-
curOutMessage.getOpCode(),
45-
curOutMessage.getQuery() != null ? curOutMessage.getQuery().toString() : null));
46-
} else {
47-
connectionList.add(new MongoConnection(port.getLocalPort()));
48-
}
44+
inUseConnectionInfoList.add(new InUseConnectionInfo(port));
4945
}
5046
}
5147
}
52-
return connectionList.toArray(new MongoConnection[connectionList.size()]);
48+
return inUseConnectionInfoList.toArray(new InUseConnectionInfo[inUseConnectionInfoList.size()]);
5349
}
5450

5551
@Override

src/main/com/mongodb/MongoConnection.java renamed to src/main/com/mongodb/InUseConnectionInfo.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,28 @@
1515
*/
1616
package com.mongodb;
1717

18-
public class MongoConnection {
19-
20-
public MongoConnection(final int localPort, final String namespace, final OutMessage.OpCode opCode, final String query) {
21-
this.localPort = localPort;
22-
this.namespace = namespace;
23-
this.opCode = opCode;
24-
this.query = query;
25-
}
18+
/**
19+
* This class is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
20+
*/
21+
public class InUseConnectionInfo {
2622

27-
public MongoConnection(final int localPort) {
28-
this(localPort, null, null, null);
23+
InUseConnectionInfo(final DBPort port) {
24+
DBPort.ActiveState activeState = port.getActiveState();
25+
if (activeState == null) {
26+
durationMS = 0;
27+
namespace = null;
28+
opCode = null;
29+
query = null;
30+
threadName = null;
31+
}
32+
else {
33+
durationMS = (System.nanoTime() - activeState.startTime) / 1000000;
34+
namespace = activeState.outMessage.getNamespace();
35+
opCode = activeState.outMessage.getOpCode();
36+
query = activeState.outMessage.getQuery() != null ? activeState.outMessage.getQuery().toString() : null;
37+
threadName = activeState.threadName;
38+
}
39+
localPort = port.getLocalPort();
2940
}
3041

3142
public String getNamespace() {
@@ -44,9 +55,18 @@ public int getLocalPort() {
4455
return localPort;
4556
}
4657

58+
public long getDurationMS() {
59+
return durationMS;
60+
}
61+
62+
public String getThreadName() {
63+
return threadName;
64+
}
4765

4866
private final String namespace;
4967
private final OutMessage.OpCode opCode;
5068
private final String query;
5169
private final int localPort;
70+
private final long durationMS;
71+
private final String threadName;
5272
}

src/main/com/mongodb/MongoConnectionPoolMXBean.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import com.mongodb.util.SimplePoolMXBean;
2020

21+
/**
22+
* This interface is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
23+
*/
2124
public interface MongoConnectionPoolMXBean extends SimplePoolMXBean {
22-
public MongoConnection[] getInUseConnections();
25+
public InUseConnectionInfo[] getInUseConnections();
2326
public String getHost();
2427
public int getPort();
2528
}

0 commit comments

Comments
 (0)