mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge branch 'master' of https://github.com/AlibabaTech/druid
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid;
|
||||
|
||||
public interface Constants {
|
||||
|
||||
public final static String DRUID_STAT_SQL_MAX_SIZE = "druid.stat.sql.MaxSize";
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
@@ -29,13 +30,19 @@ import javax.management.openmbean.TabularData;
|
||||
import javax.management.openmbean.TabularDataSupport;
|
||||
import javax.management.openmbean.TabularType;
|
||||
|
||||
import com.alibaba.druid.Constants;
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.filter.stat.StatFilter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.Histogram;
|
||||
import com.alibaba.druid.util.LRUCache;
|
||||
|
||||
public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(JdbcDataSourceStat.class);
|
||||
|
||||
private final String name;
|
||||
private final String url;
|
||||
private String dbType;
|
||||
@@ -47,10 +54,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
private int maxSize = 1000 * 1;
|
||||
|
||||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
private final LinkedHashMap<String, JdbcSqlStat> sqlStatMap = new LinkedHashMap<String, JdbcSqlStat>(
|
||||
maxSize,
|
||||
0.75f,
|
||||
false);
|
||||
private final LinkedHashMap<String, JdbcSqlStat> sqlStatMap;
|
||||
|
||||
private final Histogram connectionHoldHistogram = new Histogram(new long[] { //
|
||||
//
|
||||
@@ -59,18 +63,38 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
//
|
||||
});
|
||||
|
||||
// private final ConcurrentMap<String, JdbcSqlStat> sqlStatMap = new ConcurrentHashMap<String, JdbcSqlStat>();
|
||||
|
||||
private final ConcurrentMap<Long, JdbcConnectionStat.Entry> connections = new ConcurrentHashMap<Long, JdbcConnectionStat.Entry>();
|
||||
|
||||
public JdbcDataSourceStat(String name, String url){
|
||||
this(name, url, null);
|
||||
}
|
||||
|
||||
|
||||
public JdbcDataSourceStat(String name, String url, String dbType){
|
||||
this(name, url, dbType, null);
|
||||
}
|
||||
|
||||
public JdbcDataSourceStat(String name, String url, String dbType, Properties connectProperties){
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.dbType = dbType;
|
||||
|
||||
if (connectProperties != null) {
|
||||
Object arg = connectProperties.get(Constants.DRUID_STAT_SQL_MAX_SIZE);
|
||||
|
||||
if (arg == null) {
|
||||
arg = System.getProperty(Constants.DRUID_STAT_SQL_MAX_SIZE);
|
||||
}
|
||||
|
||||
if (arg != null) {
|
||||
try {
|
||||
maxSize = Integer.parseInt(arg.toString());
|
||||
} catch (NumberFormatException ex) {
|
||||
LOG.error("maxSize parse error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlStatMap = new LRUCache<String, JdbcSqlStat>(maxSize, 16, 0.75f, false);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
@@ -98,7 +122,6 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
for (JdbcConnectionStat.Entry connectionStat : connections.values()) {
|
||||
connectionStat.reset();
|
||||
}
|
||||
// connections.clear();
|
||||
}
|
||||
|
||||
public Histogram getConnectionHoldHistogram() {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final int maxSize;
|
||||
|
||||
public LRUCache(int maxSize, int initialCapacity, float loadFactor, boolean accessOrder){
|
||||
super(initialCapacity, loadFactor, accessOrder);
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
|
||||
return this.size() > this.maxSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.Constants;
|
||||
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
|
||||
public class JdbcDataSourceStatTest extends TestCase {
|
||||
|
||||
public void test_max() throws Exception {
|
||||
JdbcDataSourceStat stat = new JdbcDataSourceStat("", "");
|
||||
|
||||
for (int i = 0; i < 1000 * 10; ++i) {
|
||||
stat.createSqlStat("select " + i);
|
||||
}
|
||||
|
||||
Assert.assertEquals(1000, stat.getSqlStatMap().size());
|
||||
}
|
||||
|
||||
public void test_max_10() throws Exception {
|
||||
Properties connectProperties = new Properties();
|
||||
connectProperties.put(Constants.DRUID_STAT_SQL_MAX_SIZE, 10);
|
||||
JdbcDataSourceStat stat = new JdbcDataSourceStat("", "", "mysql", connectProperties);
|
||||
|
||||
for (int i = 0; i < 1000 * 1; ++i) {
|
||||
stat.createSqlStat("select " + i);
|
||||
}
|
||||
|
||||
Assert.assertEquals(10, stat.getSqlStatMap().size());
|
||||
}
|
||||
|
||||
public void test_max_10_str() throws Exception {
|
||||
Properties connectProperties = new Properties();
|
||||
connectProperties.put(Constants.DRUID_STAT_SQL_MAX_SIZE, "10");
|
||||
JdbcDataSourceStat stat = new JdbcDataSourceStat("", "", "mysql", connectProperties);
|
||||
|
||||
for (int i = 0; i < 1000 * 1; ++i) {
|
||||
stat.createSqlStat("select " + i);
|
||||
}
|
||||
|
||||
Assert.assertEquals(10, stat.getSqlStatMap().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user