mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve dynamic config
This commit is contained in:
@@ -39,7 +39,6 @@ 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 {
|
||||
|
||||
@@ -53,11 +52,13 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
private final JdbcResultSetStat resultSetStat = new JdbcResultSetStat();
|
||||
private final JdbcStatementStat statementStat = new JdbcStatementStat();
|
||||
|
||||
private int maxSize = 1000 * 1;
|
||||
private int maxSqlSize = 1000 * 1;
|
||||
|
||||
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
private final LinkedHashMap<String, JdbcSqlStat> sqlStatMap;
|
||||
|
||||
private final AtomicLong skipSqlCount = new AtomicLong();
|
||||
|
||||
private final Histogram connectionHoldHistogram = new Histogram(new long[] { //
|
||||
//
|
||||
1, 10, 100, 1000, 10 * 1000, //
|
||||
@@ -100,6 +101,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
this(name, url, dbType, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public JdbcDataSourceStat(String name, String url, String dbType, Properties connectProperties){
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
@@ -114,14 +116,36 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
|
||||
if (arg != null) {
|
||||
try {
|
||||
maxSize = Integer.parseInt(arg.toString());
|
||||
maxSqlSize = Integer.parseInt(arg.toString());
|
||||
} catch (NumberFormatException ex) {
|
||||
LOG.error("maxSize parse error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlStatMap = new LRUCache<String, JdbcSqlStat>(maxSize, 16, 0.75f, false);
|
||||
sqlStatMap = new LinkedHashMap<String, JdbcSqlStat>(16, 0.75f, false) {
|
||||
|
||||
protected boolean removeEldestEntry(Map.Entry<String, JdbcSqlStat> eldest) {
|
||||
boolean remove = (size() > maxSqlSize);
|
||||
|
||||
if (remove) {
|
||||
JdbcSqlStat sqlStat = eldest.getValue();
|
||||
if (sqlStat.getRunningCount() > 0 || sqlStat.getExecuteCount() > 0) {
|
||||
skipSqlCount.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
return remove;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public int getMaxSqlSize() {
|
||||
return this.maxSqlSize;
|
||||
}
|
||||
|
||||
public void setMaxSqlSize(int value) {
|
||||
this.maxSqlSize = value;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
@@ -131,6 +155,14 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public long getSkipSqlCount() {
|
||||
return skipSqlCount.get();
|
||||
}
|
||||
|
||||
public long getSkipSqlCountAndReset() {
|
||||
return skipSqlCount.getAndSet(0);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
blobOpenCount.set(0);
|
||||
@@ -140,6 +172,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
statementStat.reset();
|
||||
resultSetStat.reset();
|
||||
connectionHoldHistogram.reset();
|
||||
skipSqlCount.set(0);
|
||||
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
@@ -286,7 +319,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
|
||||
|
||||
List<JdbcSqlStatValue> values = new ArrayList<JdbcSqlStatValue>(stats.size());
|
||||
for (int i = 0; i < stats.size(); ++i) {
|
||||
JdbcSqlStat stat = stats.get(i);
|
||||
@@ -375,7 +408,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
public long getClobOpenCount() {
|
||||
return clobOpenCount.get();
|
||||
}
|
||||
|
||||
|
||||
public long getClobOpenCountAndReset() {
|
||||
return clobOpenCount.getAndSet(0);
|
||||
}
|
||||
@@ -387,7 +420,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
|
||||
public long getBlobOpenCount() {
|
||||
return blobOpenCount.get();
|
||||
}
|
||||
|
||||
|
||||
public long getBlobOpenCountAndReset() {
|
||||
return blobOpenCount.getAndSet(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user