增强监控

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@333 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-06-30 14:38:59 +00:00
parent c84a036722
commit 89c94b795d
6 changed files with 195 additions and 131 deletions
@@ -68,7 +68,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
public void setConnectionStackTraceEnable(boolean connectionStackTraceEnable) {
this.connectionStackTraceEnable = connectionStackTraceEnable;
}
public JdbcDataSourceStat getDataSourceStat() {
return this.dataSourceStat;
}
@@ -93,41 +93,40 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
}
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
long startNano = System.nanoTime();
long startTime = System.currentTimeMillis();
long nanoSpan = System.nanoTime() - startNano;
long nowTime = System.currentTimeMillis();
connectStat.beforeConnect();
dataSourceStat.getConnectionStat().beforeConnect();
ConnectionProxy connection = null;
{
long startNano = System.nanoTime();
long startTime = System.currentTimeMillis();
try {
connection = super.connection_connect(chain, info);
} catch (SQLException ex) {
connectStat.error(ex);
dataSourceStat.getConnectionStat().error(ex);
throw ex;
long nanoSpan;
long nowTime = System.currentTimeMillis();
connectStat.beforeConnect();
dataSourceStat.getConnectionStat().beforeConnect();
try {
connection = super.connection_connect(chain, info);
nanoSpan = System.nanoTime() - startNano;
} catch (SQLException ex) {
connectStat.connectError(ex);
dataSourceStat.getConnectionStat().connectError(ex);
throw ex;
}
connectStat.afterConnected(nanoSpan);
dataSourceStat.getConnectionStat().afterConnected(nanoSpan);
JdbcConnectionStat.Entry statEntry = getConnectionInfo(connection);
dataSourceStat.getConnections().put(connection.getId(), statEntry);
statEntry.setConnectTime(new Date(startTime));
statEntry.setConnectTimespanNano(nanoSpan);
statEntry.setEstablishNano(System.nanoTime());
statEntry.setEstablishTime(nowTime);
statEntry.setConnectStackTrace(new Exception());
connectStat.setActiveCount(dataSourceStat.getConnections().size());
dataSourceStat.getConnectionStat().setActiveCount(dataSourceStat.getConnections().size());
}
JdbcConnectionStat.Entry statEntry = getConnectionInfo(connection);
dataSourceStat.getConnections().put(connection.getId(), statEntry);
statEntry.setConnectTime(new Date(startTime));
statEntry.setConnectTimespanNano(nanoSpan);
statEntry.setEstablishNano(System.nanoTime());
statEntry.setEstablishTime(nowTime);
statEntry.setConnectStackTrace(new Exception());
connectStat.addConnectionConnectNano(nanoSpan);
dataSourceStat.getConnectionStat().addConnectionConnectNano(nanoSpan);
connectStat.setConcurrentCount(dataSourceStat.getConnections().size());
dataSourceStat.getConnectionStat().setConcurrentCount(dataSourceStat.getConnections().size());
return connection;
}
@@ -513,7 +512,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
@Override
public long getConnectionConnectCount() {
return dataSourceStat.getConnectionStat().getCount();
return dataSourceStat.getConnectionStat().getConnectCount();
}
@Override
@@ -522,8 +521,8 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
}
@Override
public long getConnectionActiveCountMax() {
return dataSourceStat.getConnectionStat().getConcurrentMax();
public long getConnectionConnectingMax() {
return dataSourceStat.getConnectionStat().getConnectingMax();
}
@Override
@@ -538,7 +537,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
@Override
public long getConnectionConnectErrorCount() {
return dataSourceStat.getConnectionStat().getErrorCount();
return dataSourceStat.getConnectionStat().getConnectErrorCount();
}
@Override
@@ -40,7 +40,7 @@ public interface StatFilterMBean {
long getConnectionActiveCount();
long getConnectionActiveCountMax();
long getConnectionConnectingMax();
long getConnectionCommitCount();
@@ -190,26 +190,29 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
OpenType<?>[] indexTypes = new OpenType<?>[] { //
SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, new ArrayType<SimpleType<String>>(SimpleType.STRING, false), SimpleType.DATE, //
SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.INTEGER, SimpleType.STRING //
, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG //
, SimpleType.LONG, SimpleType.INTEGER, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG //
, SimpleType.DATE, SimpleType.LONG, SimpleType.DATE, SimpleType.STRING, SimpleType.STRING //
, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.LONG, SimpleType.INTEGER //
, SimpleType.INTEGER, SimpleType.LONG, SimpleType.LONG, SimpleType.DATE, SimpleType.STRING //
, SimpleType.STRING, SimpleType.LONG, SimpleType.INTEGER, SimpleType.DATE, SimpleType.LONG //
, SimpleType.LONG, SimpleType.INTEGER, SimpleType.INTEGER, SimpleType.LONG, SimpleType.DATE //
, SimpleType.LONG, SimpleType.LONG, SimpleType.DATE, SimpleType.STRING, SimpleType.STRING //
,
, SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.LONG, SimpleType.INTEGER //
, SimpleType.LONG
//
};
String[] indexNames = { "ID", "URL", "Name", "FilterClasses", "CreatedTime", //
"RawUrl", "RawDriverClassName", "RawDriverMajorVersion", "RawDriverMinorVersion", "Properties" //
, "ConnectionOpenningCount", "ConnectionOpenningCountMax", "ConnectionCloseCount", "ConnectionCommitCount", "ConnectionRollbackCount" //
, "ConnectionConnectLastTime", "ConnectionConnectErrorCount", "ConnectLastErrorTime", "ConnectLastErrorMessage", "ConnectLastErrorStackTrace" //
, "ConnectionActiveCount", "ConnectionActiveCountMax", "ConnectionCloseCount", "ConnectionCommitCount", "ConnectionRollbackCount" //
, "ConnectionConnectLastTime", "ConnectionConnectErrorCount", "ConnectionConnectErrorLastTime", "ConnectionConnectErrorLastMessage", "ConnectionConnectErrorLastStackTrace" //
, "StatementCreateCount", "StatementPrepareCount", "StatementPreCallCount", "StatementExecuteCount", "StatementRunningCount" //
, "StatementConcurrentMax", "StatementCloseCount", "StatementErrorCount", "StatementLastErrorTime", "StatementLastErrorMessage" //
, "StatementLastErrorStackTrace", "StatementExecuteMillis", "ConnectingCount", "StatementExecuteLastTime", "ResultSetCloseCount" //
, "StatementLastErrorStackTrace", "StatementExecuteMillis", "ConnectionConnectingCount", "StatementExecuteLastTime", "ResultSetCloseCount" //
, "ResultSetOpenCount", "ResultSetOpenningCount", "ResultSetOpenningMax", "ResultSetFetchRowCount", "ResultSetLastOpenTime" //
, "ResultSetErrorCount", "ResultSetOpenningMillisTotal", "ResultSetLastErrorTime", "ResultSetLastErrorMessage", "ResultSetLastErrorStackTrace"
, "ConnectionConnectCount", "ConnectionErrorLastMessage", "ConnectionErrorLastStackTrace", "ConnectionConnectMillis", "ConnectionConnectingCountMax" //
, "ConnectionConnectMillisMax"
//
};
@@ -246,25 +249,25 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("Properties", getProperties());
if (stat != null) {
map.put("ConnectionOpenningCount", stat.getConnectionActiveCount());
map.put("ConnectionOpenningCountMax", stat.getConnectionStat().getActiveMax());
map.put("ConnectionActiveCount", stat.getConnectionActiveCount());
map.put("ConnectionActiveCountMax", stat.getConnectionStat().getActiveMax());
map.put("ConnectionCloseCount", stat.getConnectionStat().getCloseCount());
map.put("ConnectionCommitCount", stat.getConnectionStat().getCommitCount());
map.put("ConnectionRollbackCount", stat.getConnectionStat().getRollbackCount());
map.put("ConnectionConnectLastTime", stat.getConnectionStat().getLastConnectTime());
map.put("ConnectionConnectErrorCount", stat.getConnectionStat().getErrorCount());
Throwable lastConnectionError = stat.getConnectionStat().getLastError();
if (lastConnectionError != null) {
map.put("ConnectLastErrorTime", stat.getConnectionStat().getLastErrorTime());
map.put("ConnectLastErrorMessage", lastConnectionError.getMessage());
map.put("ConnectionConnectLastTime", stat.getConnectionStat().getConnectLastTime());
map.put("ConnectionConnectErrorCount", stat.getConnectionStat().getConnectErrorCount());
Throwable lastConnectionConnectError = stat.getConnectionStat().getConnectErrorLast();
if (lastConnectionConnectError != null) {
map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime());
map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionError.printStackTrace(new PrintWriter(buf));
map.put("ConnectLastErrorStackTrace", buf.toString());
lastConnectionConnectError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionConnectErrorLastStackTrace", buf.toString());
} else {
map.put("ConnectLastErrorTime", null);
map.put("ConnectLastErrorMessage", null);
map.put("ConnectLastErrorStackTrace", null);
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
map.put("ConnectionConnectErrorLastStackTrace", null);
}
map.put("StatementCreateCount", stat.getStatementStat().getCreateCount());
@@ -292,7 +295,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
}
map.put("StatementExecuteMillis", stat.getStatementStat().getMillisTotal());
map.put("StatementExecuteLastTime", stat.getStatementStat().getExecuteLastTime());
map.put("ConnectingCount", stat.getConnectionStat().getRunningCount());
map.put("ConnectionConnectingCount", stat.getConnectionStat().getConnectingCount());
map.put("ResultSetCloseCount", stat.getResultSetStat().getCloseCount());
map.put("ResultSetOpenCount", stat.getResultSetStat().getOpenCount());
@@ -314,19 +317,33 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("ResultSetLastErrorMessage", null);
map.put("ResultSetLastErrorStackTrace", null);
}
map.put("ConnectionConnectCount", stat.getConnectionStat().getConnectCount());
Throwable lastConnectionError = stat.getConnectionStat().getErrorLast();
if (lastConnectionError != null) {
map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionErrorLastStackTrace", buf.toString());
} else {
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
}
map.put("ConnectionConnectMillis", stat.getConnectionStat().getConnectMillis());
map.put("ConnectionConnectingCountMax", stat.getConnectionStat().getConnectingMax());
map.put("ConnectionConnectMillisMax", stat.getConnectionStat().getConnectMillisMax());
} else {
map.put("ConnectionOpenningCount", null);
map.put("ConnectionOpenningCountMax", null);
map.put("ConnectionActiveCount", null);
map.put("ConnectionActiveCountMax", null);
map.put("ConnectionCloseCount", null);
map.put("ConnectionCommitCount", null);
map.put("ConnectionRollbackCount", null);
map.put("ConnectionConnectLastTime", null);
map.put("ConnectionConnectErrorCount", null);
map.put("ConnectLastErrorTime", null);
map.put("ConnectLastErrorMessage", null);
map.put("ConnectLastErrorStackTrace", null);
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
map.put("ConnectionConnectErrorLastStackTrace", null);
map.put("StatementCreateCount", null);
map.put("StatementPrepareCount", null);
@@ -342,7 +359,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("StatementLastErrorStackTrace", null);
map.put("StatementExecuteMillis", null);
map.put("ConnectingCount", null);
map.put("ConnectionConnectingCount", null);
map.put("StatementExecuteLastTime", null);
map.put("ResultSetCloseCount", null);
@@ -357,6 +374,13 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("ResultSetLastErrorTime", null);
map.put("ResultSetLastErrorMessage", null);
map.put("ResultSetLastErrorStackTrace", null);
map.put("ConnectionConnectCount", null);
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
map.put("ConnectionConnectMillis", null);
map.put("ConnectionConnectingCountMax", null);
map.put("ConnectionConnectMillisMax", null);
}
return new CompositeDataSupport(getCompositeType(), map);
@@ -36,45 +36,54 @@ import com.alibaba.druid.util.JMXUtils;
*/
public class JdbcConnectionStat implements JdbcConnectionStatMBean {
private final AtomicInteger concurrentCount = new AtomicInteger();
private final AtomicInteger concurrentMax = new AtomicInteger();
private final AtomicInteger activeCount = new AtomicInteger();
private final AtomicInteger activeCountMax = new AtomicInteger();
private final AtomicInteger connectingCount = new AtomicInteger();
private final AtomicInteger connectingMax = new AtomicInteger();
private final AtomicLong count = new AtomicLong();
private final AtomicLong errorCount = new AtomicLong();
private final AtomicLong connectCount = new AtomicLong();
private final AtomicLong connectErrorCount = new AtomicLong();
private Throwable connectErrorLast;
private final AtomicLong connectNanoTotal = new AtomicLong(0); // 连接建立消耗时间总和(纳秒)
private final AtomicLong connectNanoMax = new AtomicLong(0); // 连接建立消耗最大时间(纳秒)
private final AtomicLong nanoTotal = new AtomicLong();
private final AtomicLong errorCount = new AtomicLong();
private final AtomicLong nanoTotal = new AtomicLong();
private Throwable lastError;
private long lastErrorTime;
private long lastSampleTime = 0;
private long connectLastTime = 0;
private final AtomicLong closeCount = new AtomicLong(0); // 执行Connection.close的计数
private final AtomicLong commitCount = new AtomicLong(0); // 执行commit的计数
private final AtomicLong rollbackCount = new AtomicLong(0); // 执行rollback的计数
private final AtomicLong connectNanoSpan = new AtomicLong(0); // 连接建立消耗时间总和(纳秒)
private final AtomicLong closeCount = new AtomicLong(0); // 执行Connection.close的计数
private final AtomicLong commitCount = new AtomicLong(0); // 执行commit的计数
private final AtomicLong rollbackCount = new AtomicLong(0); // 执行rollback的计数
public void reset() {
concurrentMax.set(0);
connectingMax.set(0);
connectErrorCount.set(0);
errorCount.set(0);
nanoTotal.set(0);
lastError = null;
lastErrorTime = 0;
lastSampleTime = 0;
connectLastTime = 0;
count.set(0);
connectCount.set(0);
closeCount.set(0);
commitCount.set(0);
rollbackCount.set(0);
connectNanoSpan.set(0);
connectNanoTotal.set(0);
connectNanoMax.set(0);
}
public void beforeConnect() {
int invoking = concurrentCount.incrementAndGet();
int invoking = connectingCount.incrementAndGet();
for (;;) {
int max = concurrentMax.get();
int max = connectingMax.get();
if (invoking > max) {
if (concurrentMax.compareAndSet(max, invoking)) {
if (connectingMax.compareAndSet(max, invoking)) {
break;
} else {
continue;
@@ -84,17 +93,45 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
}
}
count.incrementAndGet();
lastSampleTime = System.currentTimeMillis();
connectCount.incrementAndGet();
connectLastTime = System.currentTimeMillis();
}
public void afterConnected(long delta) {
connectingCount.decrementAndGet();
connectNanoTotal.addAndGet(delta);
for (;;) {
//connectNanoMax
long max = connectNanoMax.get();
if (delta > max) {
if (connectNanoMax.compareAndSet(max, delta)) {
break;
} else {
continue;
}
} else {
break;
}
}
activeCount.incrementAndGet();
}
public long getConnectNanoMax() {
return this.connectNanoMax.get();
}
public long getConnectMillisMax() {
return this.connectNanoMax.get() / (1000 * 1000);
}
public void setConcurrentCount(int concurrentCount) {
this.concurrentCount.set(concurrentCount);
public void setActiveCount(int activeCount) {
this.activeCount.set(activeCount);
for (;;) {
int max = concurrentMax.get();
if (concurrentCount > max) {
if (concurrentMax.compareAndSet(max, concurrentCount)) {
int max = activeCountMax.get();
if (activeCount > max) {
if (activeCountMax.compareAndSet(max, activeCount)) {
break;
} else {
continue;
@@ -104,29 +141,25 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
}
}
}
public int getActiveCount() {
return activeCount.get();
}
public int getAtiveCountMax() {
return this.activeCount.get();
}
public long getErrorCount() {
return errorCount.get();
}
public int getRunningCount() {
return concurrentCount.get();
public int getConnectingCount() {
return connectingCount.get();
}
public int getConcurrentMax() {
return concurrentMax.get();
}
public long getCount() {
return count.get();
}
public Date getLastConnectTime() {
if (lastSampleTime == 0) {
return null;
}
return new Date(lastSampleTime);
public int getConnectingMax() {
return connectingMax.get();
}
public long getNanoTotal() {
@@ -134,16 +167,19 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
}
public void afterClose(long nanoSpan) {
concurrentCount.decrementAndGet();
activeCount.decrementAndGet();
nanoTotal.addAndGet(nanoSpan);
}
public Throwable getLastError() {
public Throwable getErrorLast() {
return lastError;
}
public Date getLastErrorTime() {
public Throwable getConnectErrorLast() {
return this.connectErrorLast;
}
public Date getErrorLastTime() {
if (lastErrorTime <= 0) {
return null;
}
@@ -151,6 +187,15 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
return new Date(lastErrorTime);
}
public void connectError(Throwable error) {
connectErrorCount.incrementAndGet();
connectErrorLast = error;
errorCount.incrementAndGet();
lastError = error;
lastErrorTime = System.currentTimeMillis();
}
public void error(Throwable error) {
errorCount.incrementAndGet();
lastError = error;
@@ -169,17 +214,17 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
@Override
public long getConnectCount() {
return this.getCount();
return connectCount.get();
}
@Override
public long getConnectMillis() {
return connectNanoSpan.get() / (1000 * 1000);
return connectNanoTotal.get() / (1000 * 1000);
}
@Override
public long getActiveMax() {
return this.getConcurrentMax();
public int getActiveMax() {
return this.activeCountMax.get();
}
@Override
@@ -189,17 +234,19 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
@Override
public long getConnectErrorCount() {
return this.getErrorCount();
return connectErrorCount.get();
}
@Override
public Date getConnectLastTime() {
return this.getLastConnectTime();
if (connectLastTime == 0) {
return null;
}
return new Date(connectLastTime);
}
public void addConnectionConnectNano(long delta) {
connectNanoSpan.addAndGet(delta);
}
public void incrementConnectionCloseCount() {
closeCount.incrementAndGet();
@@ -323,8 +370,8 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
return new Date(lastErrorTime);
}
private static String[] indexNames = { "ID", "ConnectTime", "ConnectTimespan", "EstablishTime", "AliveTimespan", "LastSql", "LastError",
"LastErrorTime", "ConnectStatckTrace", "LastStatementStackTrace", "DataSource" };
private static String[] indexNames = { "ID", "ConnectTime", "ConnectTimespan", "EstablishTime", "AliveTimespan", "LastSql", "LastError", "LastErrorTime",
"ConnectStatckTrace", "LastStatementStackTrace", "DataSource" };
private static String[] indexDescriptions = indexNames;
public static CompositeType getCompositeType() throws JMException {
@@ -19,17 +19,11 @@ import java.util.Date;
public interface JdbcConnectionStatMBean {
long getCount();
long getErrorCount();
Date getLastConnectTime();
long getConnectCount();
long getCloseCount();
long getActiveMax();
int getActiveMax();
long getCommitCount();
@@ -24,10 +24,10 @@ public class StatisticTest extends TestCase {
public void test_stat() throws Exception {
JdbcConnectionStat stat = new JdbcConnectionStat();
Assert.assertEquals(null, stat.getLastConnectTime());
stat.setConcurrentCount(1);
Assert.assertEquals(1, stat.getConcurrentMax());
stat.setConcurrentCount(2);
Assert.assertEquals(2, stat.getConcurrentMax());
Assert.assertEquals(null, stat.getConnectLastTime());
stat.setActiveCount(1);
Assert.assertEquals(1, stat.getConnectingMax());
stat.setActiveCount(2);
Assert.assertEquals(2, stat.getConnectingMax());
}
}