mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
remove unused code
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
import com.alibaba.druid.stat.service.dto.SqlInfo;
|
||||
import com.alibaba.druid.stat.service.impl.DruidStatMemoryStore;
|
||||
|
||||
public class DruidStatService {
|
||||
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
private ScheduledFuture<?> collectPlan;
|
||||
|
||||
private int collectPeriodSeconds = 60 * 5;
|
||||
|
||||
private DruidStatStore store;
|
||||
|
||||
public int getCollectPeriodSeconds() {
|
||||
return collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public void setCollectPeriodSeconds(int collectPeriodSeconds) {
|
||||
this.collectPeriodSeconds = collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
if (store != null) {
|
||||
store = new DruidStatMemoryStore();
|
||||
}
|
||||
|
||||
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
collectPlan = scheduler.scheduleAtFixedRate(new CollectTask(), collectPeriodSeconds, collectPeriodSeconds,
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
if (collectPlan == null) {
|
||||
collectPlan.cancel(true);
|
||||
}
|
||||
if (scheduler != null) {
|
||||
scheduler.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void collect() {
|
||||
List<DataSourceInfo> dataSourceStatList = new ArrayList<DataSourceInfo>();
|
||||
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
|
||||
DataSourceInfo dataSourceStat = new DataSourceInfo();
|
||||
|
||||
dataSourceStat.setCollectTimeMillis(System.currentTimeMillis());
|
||||
|
||||
dataSourceStat.setId(dataSource.getID());
|
||||
dataSourceStat.setUrl(dataSource.getUrl());
|
||||
dataSourceStat.setDbType(dataSource.getDbType());
|
||||
|
||||
dataSourceStat.setActiveCount(dataSource.getActiveCount());
|
||||
dataSourceStat.setActivePeak(dataSource.getActivePeak());
|
||||
dataSourceStat.setCloseCount((int) dataSource.getCloseCount());
|
||||
dataSourceStat.setConnectCount((int) dataSource.getConnectCount());
|
||||
dataSourceStat.setConnectErrorCount((int) dataSource.getConnectErrorCount());
|
||||
dataSourceStat.setCreateCount((int) dataSource.getCreateCount());
|
||||
dataSourceStat.setCreateErrorCount((int) dataSource.getCreateErrorCount());
|
||||
dataSourceStat.setDestoryCount((int) dataSource.getDestroyCount());
|
||||
dataSourceStat.setExecuteCount((int) dataSource.getExecuteCount());
|
||||
dataSourceStat.setPoolingCount(dataSource.getPoolingCount());
|
||||
dataSourceStat.setConnectionHoldHistogram(dataSource.getDataSourceStat().getConnectionHistogramValues());
|
||||
dataSourceStat.setTransactionHistogram(dataSource.getTransactionHistogramValues());
|
||||
|
||||
Collection<JdbcSqlStat> sqlStats = dataSource.getDataSourceStat().getSqlStatMap().values();
|
||||
|
||||
Map<String, SqlInfo> sqlStatInfoMap = new HashMap<String, SqlInfo>(sqlStats.size());
|
||||
for (JdbcSqlStat sqlStat : dataSource.getDataSourceStat().getSqlStatMap().values()) {
|
||||
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SqlInfo sqlStatInfo = DruidStatServiceUtils.createSqlInfo(sqlStat);
|
||||
|
||||
SqlInfo oldsqlStatInfo = sqlStatInfoMap.get(sqlStatInfo.getSql());
|
||||
if (oldsqlStatInfo != null) {
|
||||
oldsqlStatInfo.merge(sqlStatInfo);
|
||||
} else {
|
||||
sqlStatInfoMap.put(sqlStatInfo.getSql(), sqlStatInfo);
|
||||
}
|
||||
}
|
||||
|
||||
dataSourceStat.setSqlList(new ArrayList<SqlInfo>(sqlStatInfoMap.values()));
|
||||
|
||||
dataSourceStatList.add(dataSourceStat);
|
||||
}
|
||||
|
||||
store.saveDataSource(dataSourceStatList);
|
||||
}
|
||||
|
||||
private final class CollectTask implements Runnable {
|
||||
|
||||
public void run() {
|
||||
collect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.service.dto.SqlInfo;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class DruidStatServiceUtils {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidStatServiceUtils.class);
|
||||
|
||||
public static SqlInfo createSqlInfo(JdbcSqlStat sqlStat) {
|
||||
SqlInfo info = new SqlInfo();
|
||||
|
||||
String sql = sqlStat.getSql();
|
||||
try {
|
||||
sql = ParameterizedOutputVisitorUtils.parameterize(sql, sqlStat.getDbType());
|
||||
} catch (Exception e) {
|
||||
LOG.error("merge sql error", e);
|
||||
}
|
||||
|
||||
info.setSql(sql);
|
||||
info.setExecuteCount((int) sqlStat.getExecuteCount());
|
||||
info.setRunningCount((int) sqlStat.getRunningCount());
|
||||
info.setConcurrentMax((int) sqlStat.getConcurrentMax());
|
||||
info.setErrorCount((int) sqlStat.getErrorCount());
|
||||
info.setInTransactionCount((int) sqlStat.getInTransactionCount());
|
||||
info.setFetchRowCount(sqlStat.getFetchRowCount());
|
||||
info.setUpdateCount(sqlStat.getUpdateCount());
|
||||
info.setResultSetHoldTimeMilis(sqlStat.getResultSetHoldTimeMilis());
|
||||
|
||||
info.setHisogram(sqlStat.getHistogram().toArray());
|
||||
info.setExecuteAndResultHoldTimeHistogram(sqlStat.getExecuteAndResultHoldTimeHistogram().toArray());
|
||||
info.setFetchRowCountHistogram(sqlStat.getFetchRowCountHistogram().toArray());
|
||||
info.setUpdateCountHistogram(sqlStat.getUpdateCountHistogram().toArray());
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
|
||||
public interface DruidStatStore {
|
||||
|
||||
void saveDataSource(List<DataSourceInfo> dataSourceList);
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataSourceInfo {
|
||||
|
||||
private long id;
|
||||
private String url;
|
||||
private String dbType;
|
||||
|
||||
private int activeCount;
|
||||
private int activePeak;
|
||||
|
||||
private int poolingCount;
|
||||
|
||||
private int connectCount;
|
||||
private int connectErrorCount;
|
||||
|
||||
private int createCount;
|
||||
private int createErrorCount;
|
||||
|
||||
private int executeCount;
|
||||
|
||||
private int closeCount;
|
||||
private int destoryCount;
|
||||
|
||||
private long collectTimeMillis;
|
||||
|
||||
private int[] transactionHistogram;
|
||||
|
||||
private int[] connectionHoldHistogram;
|
||||
|
||||
private List<SqlInfo> sqlList;
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public List<SqlInfo> getSqlList() {
|
||||
return sqlList;
|
||||
}
|
||||
|
||||
public void setSqlList(List<SqlInfo> sqlList) {
|
||||
this.sqlList = sqlList;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int[] getTransactionHistogram() {
|
||||
return transactionHistogram;
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(long[] values) {
|
||||
this.transactionHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
transactionHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(int[] values) {
|
||||
this.transactionHistogram = values;
|
||||
}
|
||||
|
||||
public int[] getConnectionHoldHistogram() {
|
||||
return connectionHoldHistogram;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(int[] values) {
|
||||
this.connectionHoldHistogram = values;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(long[] values) {
|
||||
this.connectionHoldHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
connectionHoldHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int getActiveCount() {
|
||||
return activeCount;
|
||||
}
|
||||
|
||||
public void setActiveCount(int activeCount) {
|
||||
this.activeCount = activeCount;
|
||||
}
|
||||
|
||||
public int getActivePeak() {
|
||||
return activePeak;
|
||||
}
|
||||
|
||||
public void setActivePeak(int activePeak) {
|
||||
this.activePeak = activePeak;
|
||||
}
|
||||
|
||||
public int getPoolingCount() {
|
||||
return poolingCount;
|
||||
}
|
||||
|
||||
public void setPoolingCount(int poolingCount) {
|
||||
this.poolingCount = poolingCount;
|
||||
}
|
||||
|
||||
public int getConnectCount() {
|
||||
return connectCount;
|
||||
}
|
||||
|
||||
public void setConnectCount(int connectCount) {
|
||||
this.connectCount = connectCount;
|
||||
}
|
||||
|
||||
public int getConnectErrorCount() {
|
||||
return connectErrorCount;
|
||||
}
|
||||
|
||||
public void setConnectErrorCount(int connectErrorCount) {
|
||||
this.connectErrorCount = connectErrorCount;
|
||||
}
|
||||
|
||||
public int getCreateCount() {
|
||||
return createCount;
|
||||
}
|
||||
|
||||
public void setCreateCount(int createCount) {
|
||||
this.createCount = createCount;
|
||||
}
|
||||
|
||||
public int getCreateErrorCount() {
|
||||
return createErrorCount;
|
||||
}
|
||||
|
||||
public void setCreateErrorCount(int createErrorCount) {
|
||||
this.createErrorCount = createErrorCount;
|
||||
}
|
||||
|
||||
public int getExecuteCount() {
|
||||
return executeCount;
|
||||
}
|
||||
|
||||
public void setExecuteCount(int executeCount) {
|
||||
this.executeCount = executeCount;
|
||||
}
|
||||
|
||||
public int getCloseCount() {
|
||||
return closeCount;
|
||||
}
|
||||
|
||||
public void setCloseCount(int closeCount) {
|
||||
this.closeCount = closeCount;
|
||||
}
|
||||
|
||||
public int getDestoryCount() {
|
||||
return destoryCount;
|
||||
}
|
||||
|
||||
public void setDestoryCount(int destoryCount) {
|
||||
this.destoryCount = destoryCount;
|
||||
}
|
||||
|
||||
public long getCollectTimeMillis() {
|
||||
return collectTimeMillis;
|
||||
}
|
||||
|
||||
public void setCollectTimeMillis(long collectTimeMillis) {
|
||||
this.collectTimeMillis = collectTimeMillis;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.dto;
|
||||
|
||||
public class SqlInfo {
|
||||
|
||||
private String sql;
|
||||
private int executeCount;
|
||||
private int runningCount;
|
||||
private int concurrentMax;
|
||||
private int errorCount;
|
||||
private int inTransactionCount;
|
||||
|
||||
private long fetchRowCount;
|
||||
private long updateCount;
|
||||
|
||||
private long resultSetHoldTimeMilis;
|
||||
|
||||
private int[] histogram;
|
||||
|
||||
private int[] executeAndResultHoldTimeHistogram;
|
||||
|
||||
private int[] updateCountHistogram;
|
||||
|
||||
private int[] fetchRowCountHistogram;
|
||||
|
||||
public SqlInfo(){
|
||||
|
||||
}
|
||||
|
||||
public void setExecuteAndResultHoldTimeHistogram(long[] values) {
|
||||
executeAndResultHoldTimeHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
executeAndResultHoldTimeHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getExecuteAndResultHoldTimeHistogram() {
|
||||
return executeAndResultHoldTimeHistogram;
|
||||
}
|
||||
|
||||
public void setFetchRowCountHistogram(long[] values) {
|
||||
fetchRowCountHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
fetchRowCountHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getFetchRowCountHistogram() {
|
||||
return fetchRowCountHistogram;
|
||||
}
|
||||
|
||||
public void setUpdateCountHistogram(long[] values) {
|
||||
updateCountHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
updateCountHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getUpdateCountHistogram() {
|
||||
return updateCountHistogram;
|
||||
}
|
||||
|
||||
public void setHisogram(long[] values) {
|
||||
histogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
histogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getHistogram() {
|
||||
return histogram;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public int getExecuteCount() {
|
||||
return executeCount;
|
||||
}
|
||||
|
||||
public void setExecuteCount(int executeCount) {
|
||||
this.executeCount = executeCount;
|
||||
}
|
||||
|
||||
public int getRunningCount() {
|
||||
return runningCount;
|
||||
}
|
||||
|
||||
public void setRunningCount(int runningCount) {
|
||||
this.runningCount = runningCount;
|
||||
}
|
||||
|
||||
public int getConcurrentMax() {
|
||||
return concurrentMax;
|
||||
}
|
||||
|
||||
public void setConcurrentMax(int concurrentMax) {
|
||||
this.concurrentMax = concurrentMax;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public void setErrorCount(int errorCount) {
|
||||
this.errorCount = errorCount;
|
||||
}
|
||||
|
||||
public int getInTransactionCount() {
|
||||
return inTransactionCount;
|
||||
}
|
||||
|
||||
public void setInTransactionCount(int inTransactionCount) {
|
||||
this.inTransactionCount = inTransactionCount;
|
||||
}
|
||||
|
||||
public long getFetchRowCount() {
|
||||
return fetchRowCount;
|
||||
}
|
||||
|
||||
public void setFetchRowCount(long fetchRowCount) {
|
||||
this.fetchRowCount = fetchRowCount;
|
||||
}
|
||||
|
||||
public long getUpdateCount() {
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
public void setUpdateCount(long updateCount) {
|
||||
this.updateCount = updateCount;
|
||||
}
|
||||
|
||||
public long getResultSetHoldTimeMilis() {
|
||||
return resultSetHoldTimeMilis;
|
||||
}
|
||||
|
||||
public void setResultSetHoldTimeMilis(long resultSetHoldTimeMilis) {
|
||||
this.resultSetHoldTimeMilis = resultSetHoldTimeMilis;
|
||||
}
|
||||
|
||||
public void merge(SqlInfo o) {
|
||||
executeCount += o.executeCount;
|
||||
runningCount += o.runningCount;
|
||||
if (o.concurrentMax > concurrentMax) {
|
||||
concurrentMax = o.concurrentMax;
|
||||
}
|
||||
errorCount += o.errorCount;
|
||||
inTransactionCount += o.inTransactionCount;
|
||||
|
||||
fetchRowCount += o.fetchRowCount;
|
||||
updateCount += o.updateCount;
|
||||
|
||||
resultSetHoldTimeMilis += o.resultSetHoldTimeMilis;
|
||||
|
||||
for (int i = 0; i < histogram.length; ++i) {
|
||||
this.histogram[i] += o.histogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < executeAndResultHoldTimeHistogram.length; ++i) {
|
||||
this.executeAndResultHoldTimeHistogram[i] += o.executeAndResultHoldTimeHistogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < updateCountHistogram.length; ++i) {
|
||||
this.updateCountHistogram[i] += o.updateCountHistogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < fetchRowCountHistogram.length; ++i) {
|
||||
this.fetchRowCountHistogram[i] += o.fetchRowCountHistogram[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.impl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import com.alibaba.druid.stat.service.DruidStatStore;
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
|
||||
public class DruidStatMemoryStore implements DruidStatStore {
|
||||
|
||||
private ConcurrentHashMap<Long, DataSourceData> dataSourceDataMap = new ConcurrentHashMap<Long, DataSourceData>();
|
||||
|
||||
@Override
|
||||
public void saveDataSource(List<DataSourceInfo> dataSourceList) {
|
||||
for (DataSourceInfo dataSourceInfo : dataSourceList) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
|
||||
if (data == null) {
|
||||
dataSourceDataMap.putIfAbsent(dataSourceInfo.getId(), new DataSourceData(dataSourceInfo.getId(),
|
||||
dataSourceInfo.getUrl(),
|
||||
dataSourceInfo.getDbType()));
|
||||
data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
}
|
||||
|
||||
data.add(dataSourceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(long dataSourceId, Long startTimeMillis, Long endTimeMillis) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceId);
|
||||
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return data.scan(startTimeMillis, endTimeMillis);
|
||||
}
|
||||
|
||||
public static class DataSourceData {
|
||||
|
||||
private final Long id;
|
||||
private final String url;
|
||||
private final String dbType;
|
||||
|
||||
private ConcurrentSkipListMap<Long, DataSourceInfo> data = new ConcurrentSkipListMap<Long, DataSourceInfo>();
|
||||
|
||||
public DataSourceData(Long id, String url, String dbType){
|
||||
super();
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void add(DataSourceInfo dataSourceInfo) {
|
||||
data.put(dataSourceInfo.getCollectTimeMillis(), dataSourceInfo);
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(Long startTimeMillis, Long endTimeMillis) {
|
||||
if (data.size() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (startTimeMillis == null) {
|
||||
startTimeMillis = data.keySet().iterator().next();
|
||||
}
|
||||
|
||||
if (endTimeMillis == null) {
|
||||
endTimeMillis = data.descendingKeySet().iterator().next();
|
||||
}
|
||||
|
||||
return data.subMap(startTimeMillis, endTimeMillis).values();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user