mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
monitor support
This commit is contained in:
@@ -61,7 +61,7 @@ public final class DruidStatManagerFacade {
|
||||
private Set<Object> getDruidDataSourceInstances() {
|
||||
return DruidDataSourceStatManager.getInstances().keySet();
|
||||
}
|
||||
|
||||
|
||||
public Object getDruidDataSourceByName(String name) {
|
||||
for (Object o : this.getDruidDataSourceInstances()) {
|
||||
String itemName = DruidDataSourceUtils.getName(o);
|
||||
@@ -69,7 +69,7 @@ public final class DruidStatManagerFacade {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public final class DruidStatManagerFacade {
|
||||
if (!isResetEnable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
SpringStatManager.getInstance().resetStat();
|
||||
WebAppStatManager.getInstance().resetStat();
|
||||
resetSqlStat();
|
||||
@@ -117,7 +117,7 @@ public final class DruidStatManagerFacade {
|
||||
}
|
||||
|
||||
Object datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource, false);
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer identity) {
|
||||
@@ -134,26 +134,32 @@ public final class DruidStatManagerFacade {
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList(Integer dataSourceId) {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (Object datasource : getDruidDataSourceInstances()) {
|
||||
if (dataSourceId != null && dataSourceId.intValue() != System.identityHashCode(datasource)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Object sqlStat : DruidDataSourceUtils.getSqlStatMap(datasource).values()) {
|
||||
|
||||
Map<String, Object> data = JdbcSqlStatUtils.getData(sqlStat);
|
||||
|
||||
long executeCount = (Long) data.get("ExecuteCount");
|
||||
long runningCount = (Long) data.get("RunningCount");
|
||||
|
||||
if (executeCount == 0 && runningCount == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(data);
|
||||
}
|
||||
return getSqlStatDataList(datasource);
|
||||
}
|
||||
|
||||
return new ArrayList<Map<String, Object>>();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList(Object datasource) {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (Object sqlStat : DruidDataSourceUtils.getSqlStatMap(datasource).values()) {
|
||||
Map<String, Object> data = JdbcSqlStatUtils.getData(sqlStat);
|
||||
|
||||
long executeCount = (Long) data.get("ExecuteCount");
|
||||
long runningCount = (Long) data.get("RunningCount");
|
||||
|
||||
if (executeCount == 0 && runningCount == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -172,9 +178,13 @@ public final class DruidStatManagerFacade {
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getDataSourceStatDataList() {
|
||||
return getDataSourceStatDataList(false);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getDataSourceStatDataList(boolean includeSqlList) {
|
||||
List<Map<String, Object>> datasourceList = new ArrayList<Map<String, Object>>();
|
||||
for (Object dataSource : getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
datasourceList.add(dataSourceToMapData(dataSource, includeSqlList));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
@@ -183,9 +193,9 @@ public final class DruidStatManagerFacade {
|
||||
List<List<String>> traceList = new ArrayList<List<String>>();
|
||||
for (Object dataSource : getDruidDataSourceInstances()) {
|
||||
List<String> stacks = ((DruidDataSource) dataSource).getActiveConnectionStackTrace();
|
||||
if (stacks.size() > 0 ) {
|
||||
traceList.add(stacks);
|
||||
}
|
||||
if (stacks.size() > 0) {
|
||||
traceList.add(stacks);
|
||||
}
|
||||
}
|
||||
return traceList;
|
||||
}
|
||||
@@ -236,8 +246,14 @@ public final class DruidStatManagerFacade {
|
||||
return DruidDataSourceUtils.getActiveConnectionStackTrace(datasource);
|
||||
}
|
||||
|
||||
private Map<String, Object> dataSourceToMapData(Object dataSource) {
|
||||
private Map<String, Object> dataSourceToMapData(Object dataSource, boolean includeSql) {
|
||||
Map<String, Object> map = DruidDataSourceUtils.getStatData(dataSource);
|
||||
|
||||
return DruidDataSourceUtils.getStatData(dataSource);
|
||||
if (includeSql) {
|
||||
List<Map<String, Object>> sqlList = getSqlStatDataList(dataSource);
|
||||
map.put("SQL", sqlList);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.alibaba.druid.support.monitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.stat.DruidStatManagerFacade;
|
||||
import com.alibaba.druid.support.http.stat.WebAppStatManager;
|
||||
import com.alibaba.druid.support.json.JSONUtils;
|
||||
import com.alibaba.druid.support.spring.stat.SpringStatManager;
|
||||
|
||||
public class PushService {
|
||||
|
||||
private final DruidStatManagerFacade statManagerFacade = DruidStatManagerFacade.getInstance();
|
||||
|
||||
private String serverUrl;
|
||||
|
||||
private boolean includeSql = true;
|
||||
|
||||
private boolean includeWebApp = true;
|
||||
private boolean includeWebUri = true;
|
||||
private boolean includeWebSession = true;
|
||||
private boolean includeSpring = true;
|
||||
|
||||
public void collect() {
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
|
||||
if (includeSql) {
|
||||
List<Map<String, Object>> dataSources = statManagerFacade.getDataSourceStatDataList(true);
|
||||
|
||||
data.put("DataSources", dataSources);
|
||||
}
|
||||
|
||||
if (includeWebApp) {
|
||||
List<Map<String, Object>> list = WebAppStatManager.getInstance().getWebAppStatData();
|
||||
data.put("WebApp", list);
|
||||
}
|
||||
|
||||
if (includeWebUri) {
|
||||
List<Map<String, Object>> list = WebAppStatManager.getInstance().getURIStatData();
|
||||
|
||||
data.put("WebURI", list);
|
||||
}
|
||||
|
||||
if (includeWebSession) {
|
||||
List<Map<String, Object>> list = WebAppStatManager.getInstance().getSessionStatData();
|
||||
|
||||
data.put("WebSession", list);
|
||||
}
|
||||
|
||||
if (includeSpring) {
|
||||
List<Map<String, Object>> list = SpringStatManager.getInstance().getMethodStatData();
|
||||
|
||||
data.put("Spring", list);
|
||||
}
|
||||
}
|
||||
|
||||
protected void push(Map<String, Object> data) {
|
||||
String json = JSONUtils.toJSONString(data);
|
||||
//TODO URL Connection Post
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class ConcurrentTest2 extends TestCase {
|
||||
|
||||
private String jdbcUrl;
|
||||
private String user;
|
||||
private String password;
|
||||
private String driverClass;
|
||||
private int minPoolSize = 1;
|
||||
private int maxPoolSize = 8;
|
||||
private int maxActive = 500;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
// jdbcUrl =
|
||||
// "jdbc:mysql://10.20.129.146/dragoon_v25masterdb?useUnicode=true&characterEncoding=UTF-8";
|
||||
// user = "dragoon25";
|
||||
// password = "dragoon25";
|
||||
// driverClass = "com.mysql.jdbc.Driver";
|
||||
|
||||
jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
user = "dragoon25";
|
||||
password = "dragoon25";
|
||||
driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
}
|
||||
|
||||
public void test_concurrent_2() throws Exception {
|
||||
final DruidDataSource dataSource = new DruidDataSource();
|
||||
|
||||
Class.forName("com.alibaba.druid.mock.MockDriver");
|
||||
|
||||
dataSource.setInitialSize(10);
|
||||
dataSource.setMaxActive(maxActive);
|
||||
dataSource.setMinIdle(minPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
dataSource.setMaxWait(1000 * 60 * 1);
|
||||
|
||||
final int THREAD_COUNT = 50;
|
||||
final int LOOP_COUNT = 1000 * 10;
|
||||
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
|
||||
|
||||
final CyclicBarrier barrier = new CyclicBarrier(5, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
dataSource.shrink();
|
||||
}
|
||||
});
|
||||
|
||||
for (int threadIndex = 0; threadIndex < THREAD_COUNT; ++threadIndex) {
|
||||
Thread thread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
startLatch.await();
|
||||
|
||||
for (int i = 0; i < LOOP_COUNT; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
|
||||
barrier.await(10, TimeUnit.SECONDS);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
endLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}
|
||||
|
||||
startLatch.countDown();
|
||||
endLatch.await();
|
||||
System.out.println("connectCount : " + dataSource.getConnectCount());
|
||||
|
||||
// Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getConnectCount());
|
||||
// Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getCloseCount());
|
||||
// Assert.assertEquals(0, dataSource.getConnectErrorCount());
|
||||
// Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user