improved DruidDataSource executeStatistic.

This commit is contained in:
wenshao
2018-05-03 14:04:23 +08:00
parent c8d8409d30
commit e46178831a
12 changed files with 352 additions and 151 deletions
@@ -68,10 +68,8 @@ import com.alibaba.druid.util.*;
* @author ljw [ljw2083@alibaba-inc.com]
*/
public abstract class DruidAbstractDataSource extends WrapperAdapter implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable {
private final static Log LOG = LogFactory.getLog(DruidAbstractDataSource.class);
private static final long serialVersionUID = 1L;
private final static Log LOG = LogFactory.getLog(DruidAbstractDataSource.class);
public final static int DEFAULT_INITIAL_SIZE = 0;
public final static int DEFAULT_MAX_ACTIVE_SIZE = 8;
@@ -86,12 +84,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
public static final long DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS = 500;
public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3;
/**
* The default value for {@link #getMinEvictableIdleTimeMillis}.
*
* @see #getMinEvictableIdleTimeMillis
* @see #setMinEvictableIdleTimeMillis
*/
public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 30L;
public static final long DEFAULT_MAX_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 60L * 7;
public static final long DEFAULT_PHY_TIMEOUT_MILLIS = -1;
@@ -131,8 +123,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile boolean inited = false;
protected PrintWriter logWriter = new PrintWriter(
System.out);
protected PrintWriter logWriter = new PrintWriter(System.out);
protected List<Filter> filters = new CopyOnWriteArrayList<Filter>();
private boolean clearFiltersEnable = true;
@@ -143,27 +134,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile int queryTimeout;
protected volatile int transactionQueryTimeout;
protected AtomicLong createErrorCount = new AtomicLong();
protected long createTimespan;
protected volatile int maxWaitThreadCount = -1;
protected volatile boolean accessToUnderlyingConnectionAllowed = true;
protected volatile long timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
protected volatile int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
protected volatile long minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
protected volatile long maxEvictableIdleTimeMillis = DEFAULT_MAX_EVICTABLE_IDLE_TIME_MILLIS;
protected volatile long phyTimeoutMillis = DEFAULT_PHY_TIMEOUT_MILLIS;
protected volatile boolean removeAbandoned;
protected volatile long removeAbandonedTimeoutMillis = 300 * 1000;
protected volatile boolean logAbandoned;
protected volatile int maxOpenPreparedStatements = -1;
@@ -176,32 +159,41 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile ValidConnectionChecker validConnectionChecker = null;
protected final AtomicLong errorCount = new AtomicLong();
protected final AtomicLong dupCloseCount = new AtomicLong();
protected final Map<DruidPooledConnection, Object> activeConnections = new IdentityHashMap<DruidPooledConnection, Object>();
protected final static Object PRESENT = new Object();
protected long id;
protected final Date createdTime = new Date();
protected Date initedTime;
protected int connectionErrorRetryAttempts = 1;
protected boolean breakAfterAcquireFailure = false;
protected long transactionThresholdMillis = 0L;
protected final AtomicLong commitCount = new AtomicLong();
protected final AtomicLong startTransactionCount = new AtomicLong();
protected final AtomicLong rollbackCount = new AtomicLong();
protected final AtomicLong cachedPreparedStatementHitCount = new AtomicLong();
protected final AtomicLong preparedStatementCount = new AtomicLong();
protected final AtomicLong closedPreparedStatementCount = new AtomicLong();
protected final AtomicLong cachedPreparedStatementCount = new AtomicLong();
protected final AtomicLong cachedPreparedStatementDeleteCount = new AtomicLong();
protected final AtomicLong cachedPreparedStatementMissCount = new AtomicLong();
protected final Date createdTime = new Date();
protected Date initedTime;
protected volatile long errorCount = 0L;
protected volatile long dupCloseCount = 0L;
protected volatile long startTransactionCount = 0L;
protected volatile long commitCount = 0L;
protected volatile long rollbackCount = 0L;
protected volatile long cachedPreparedStatementHitCount = 0L;
protected volatile long preparedStatementCount = 0L;
protected volatile long closedPreparedStatementCount = 0L;
protected volatile long cachedPreparedStatementCount = 0L;
protected volatile long cachedPreparedStatementDeleteCount = 0L;
protected volatile long cachedPreparedStatementMissCount = 0L;
final static AtomicLongFieldUpdater<DruidAbstractDataSource> errorCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "errorCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> dupCloseCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "dupCloseCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> startTransactionCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "startTransactionCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> commitCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "commitCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> rollbackCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "rollbackCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> cachedPreparedStatementHitCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "cachedPreparedStatementHitCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> preparedStatementCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "preparedStatementCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> closedPreparedStatementCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "closedPreparedStatementCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> cachedPreparedStatementCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "cachedPreparedStatementCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> cachedPreparedStatementDeleteCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "cachedPreparedStatementDeleteCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> cachedPreparedStatementMissCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "cachedPreparedStatementMissCount");
protected final Histogram transactionHistogram = new Histogram(1,
10,
@@ -214,7 +206,15 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
private ObjectName objectName;
protected final AtomicLong executeCount = new AtomicLong();
protected volatile long executeCount = 0L;
protected volatile long executeQueryCount = 0L;
protected volatile long executeUpdateCount = 0L;
protected volatile long executeBatchCount = 0L;
final static AtomicLongFieldUpdater<DruidAbstractDataSource> executeQueryCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "executeQueryCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> executeUpdateCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "executeUpdateCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> executeBatchCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "executeBatchCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> executeCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "executeCount");
protected volatile Throwable createError;
protected volatile Throwable lastError;
@@ -233,13 +233,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected ReentrantLock activeConnectionLock = new ReentrantLock();
protected volatile int createErrorCount = 0;
protected volatile int creatingCount = 0;
protected AtomicLong createCount = new AtomicLong();
protected AtomicLong destroyCount = new AtomicLong();
protected volatile int directCreateCount = 0;
protected volatile long createCount = 0L;
protected volatile long destroyCount = 0L;
final static AtomicIntegerFieldUpdater<DruidAbstractDataSource> createErrorCountUpdater = AtomicIntegerFieldUpdater.newUpdater(DruidAbstractDataSource.class, "createErrorCount");
final static AtomicIntegerFieldUpdater<DruidAbstractDataSource> creatingCountUpdater = AtomicIntegerFieldUpdater.newUpdater(DruidAbstractDataSource.class, "creatingCount");
final static AtomicIntegerFieldUpdater<DruidAbstractDataSource> directCreateCountUpdater = AtomicIntegerFieldUpdater.newUpdater(DruidAbstractDataSource.class, "directCreateCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> createCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "createCount");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> destroyCountUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "destroyCount");
private Boolean useUnfairLock = null;
private boolean useLocalSessionState = true;
protected long timeBetweenLogStatsMillis;
@@ -260,9 +266,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile String lastFatalErrorSql = null;
protected volatile Throwable lastFatalError = null;
protected static AtomicIntegerFieldUpdater<DruidAbstractDataSource> directCreateCountUpdater = AtomicIntegerFieldUpdater.newUpdater(DruidAbstractDataSource.class, "directCreateCount");
protected static AtomicIntegerFieldUpdater<DruidAbstractDataSource> createCountUpdater = AtomicIntegerFieldUpdater.newUpdater(DruidAbstractDataSource.class, "creatingCount");
public DruidAbstractDataSource(boolean lockFair){
lock = new ReentrantLock(lockFair);
@@ -404,11 +407,46 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public long getExecuteCount() {
return executeCount.get();
return executeCount + executeQueryCount + executeUpdateCount + executeBatchCount;
}
public long getExecuteUpdateCount() {
return executeUpdateCount;
}
public long getExecuteQueryCount() {
return executeQueryCount;
}
public long getExecuteBatchCount() {
return executeBatchCount;
}
public long getAndResetExecuteCount() {
return executeCountUpdater.getAndSet(this, 0)
+ executeQueryCountUpdater.getAndSet(this, 0)
+ executeUpdateCountUpdater.getAndSet(this, 0)
+ executeBatchCountUpdater.getAndSet(this, 0);
}
public long getExecuteCount2() {
return executeCount;
}
public void incrementExecuteCount() {
this.executeCount.incrementAndGet();
this.executeCountUpdater.incrementAndGet(this);
}
public void incrementExecuteUpdateCount() {
this.executeUpdateCount++;
}
public void incrementExecuteQueryCount() {
this.executeQueryCount++;
}
public void incrementExecuteBatchCount() {
this.executeBatchCount++;
}
public boolean isDupCloseLogEnable() {
@@ -432,59 +470,59 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public void incrementCachedPreparedStatementCount() {
cachedPreparedStatementCount.incrementAndGet();
cachedPreparedStatementCountUpdater.incrementAndGet(this);
}
public void decrementCachedPreparedStatementCount() {
cachedPreparedStatementCount.decrementAndGet();
cachedPreparedStatementCountUpdater.decrementAndGet(this);
}
public void incrementCachedPreparedStatementDeleteCount() {
cachedPreparedStatementDeleteCount.incrementAndGet();
cachedPreparedStatementDeleteCountUpdater.incrementAndGet(this);
}
public void incrementCachedPreparedStatementMissCount() {
cachedPreparedStatementMissCount.incrementAndGet();
cachedPreparedStatementMissCountUpdater.incrementAndGet(this);
}
public long getCachedPreparedStatementMissCount() {
return cachedPreparedStatementMissCount.get();
return cachedPreparedStatementMissCount;
}
public long getCachedPreparedStatementAccessCount() {
return cachedPreparedStatementMissCount.get() + cachedPreparedStatementHitCount.get();
return cachedPreparedStatementMissCount + cachedPreparedStatementHitCount;
}
public long getCachedPreparedStatementDeleteCount() {
return cachedPreparedStatementDeleteCount.get();
return cachedPreparedStatementDeleteCount;
}
public long getCachedPreparedStatementCount() {
return cachedPreparedStatementCount.get();
return cachedPreparedStatementCount;
}
public void incrementClosedPreparedStatementCount() {
closedPreparedStatementCount.incrementAndGet();
closedPreparedStatementCountUpdater.incrementAndGet(this);
}
public long getClosedPreparedStatementCount() {
return closedPreparedStatementCount.get();
return closedPreparedStatementCount;
}
public void incrementPreparedStatementCount() {
preparedStatementCount.incrementAndGet();
preparedStatementCountUpdater.incrementAndGet(this);
}
public long getPreparedStatementCount() {
return preparedStatementCount.get();
return preparedStatementCount;
}
public void incrementCachedPreparedStatementHitCount() {
cachedPreparedStatementHitCount.incrementAndGet();
cachedPreparedStatementHitCountUpdater.incrementAndGet(this);
}
public long getCachedPreparedStatementHitCount() {
return cachedPreparedStatementHitCount.get();
return cachedPreparedStatementHitCount;
}
public long getTransactionThresholdMillis() {
@@ -506,27 +544,27 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public long getCommitCount() {
return commitCount.get();
return commitCount;
}
public void incrementCommitCount() {
commitCount.incrementAndGet();
commitCountUpdater.incrementAndGet(this);
}
public long getRollbackCount() {
return rollbackCount.get();
return rollbackCount;
}
public void incrementRollbackCount() {
rollbackCount.incrementAndGet();
rollbackCountUpdater.incrementAndGet(this);
}
public long getStartTransactionCount() {
return startTransactionCount.get();
return startTransactionCount;
}
public void incrementStartTransactionCount() {
startTransactionCount.incrementAndGet();
startTransactionCountUpdater.incrementAndGet(this);
}
public boolean isBreakAfterAcquireFailure() {
@@ -546,7 +584,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public long getDupCloseCount() {
return dupCloseCount.get();
return dupCloseCount;
}
public int getMaxPoolPreparedStatementPerConnectionSize() {
@@ -572,7 +610,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public void incrementDupCloseCount() {
dupCloseCount.incrementAndGet();
dupCloseCountUpdater.incrementAndGet(this);
}
public ValidConnectionChecker getValidConnectionChecker() {
@@ -1023,7 +1061,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public long getCreateErrorCount() {
return createErrorCount.get();
return createErrorCount;
}
public int getMaxActive() {
@@ -1463,31 +1501,37 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
this.clearFiltersEnable = clearFiltersEnable;
}
protected final AtomicLong connectionIdSeed = new AtomicLong(10000);
protected final AtomicLong statementIdSeed = new AtomicLong(20000);
protected final AtomicLong resultSetIdSeed = new AtomicLong(50000);
protected final AtomicLong transactionIdSeed = new AtomicLong(60000);
protected final AtomicLong metaDataIdSeed = new AtomicLong(80000);
protected volatile long connectionIdSeed = 10000L;
protected volatile long statementIdSeed = 20000L;
protected volatile long resultSetIdSeed = 50000L;
protected volatile long transactionIdSeed = 60000L;
protected volatile long metaDataIdSeed = 80000L;
final static AtomicLongFieldUpdater<DruidAbstractDataSource> connectionIdSeedUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "connectionIdSeed");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> statementIdSeedUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "statementIdSeed");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> resultSetIdSeedUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "resultSetIdSeed");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> transactionIdSeedUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "transactionIdSeed");
final static AtomicLongFieldUpdater<DruidAbstractDataSource> metaDataIdSeedUpdater = AtomicLongFieldUpdater.newUpdater(DruidAbstractDataSource.class, "metaDataIdSeed");
public long createConnectionId() {
return connectionIdSeed.incrementAndGet();
return connectionIdSeedUpdater.incrementAndGet(this);
}
public long createStatementId() {
return statementIdSeed.getAndIncrement();
return statementIdSeedUpdater.getAndIncrement(this);
}
public long createMetaDataId() {
return metaDataIdSeed.getAndIncrement();
return metaDataIdSeedUpdater.getAndIncrement(this);
}
public long createResultSetId() {
return resultSetIdSeed.getAndIncrement();
return resultSetIdSeedUpdater.getAndIncrement(this);
}
@Override
public long createTransactionId() {
return transactionIdSeed.getAndIncrement();
return transactionIdSeedUpdater.getAndIncrement(this);
}
void initStatement(DruidPooledConnection conn, Statement stmt) throws SQLException {
@@ -1516,7 +1560,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
conn = new FilterChainImpl(this).connection_connect(info);
}
createCount.incrementAndGet();
createCountUpdater.incrementAndGet(this);
return conn;
}
@@ -1574,7 +1618,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
? new HashMap<String, Object>()
: null;
createCountUpdater.incrementAndGet(this);
creatingCountUpdater.incrementAndGet(this);
try {
conn = createPhysicalConnection(url, physicalConnectProperties);
connectedNanos = System.nanoTime();
@@ -1599,14 +1643,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
JdbcUtils.close(conn);
throw ex;
} catch (Error ex) {
createErrorCount.incrementAndGet();
createErrorCountUpdater.incrementAndGet(this);
setCreateError(ex);
JdbcUtils.close(conn);
throw ex;
} finally {
long nano = System.nanoTime() - connectStartNanos;
createTimespan += nano;
createCountUpdater.decrementAndGet(this);
creatingCountUpdater.decrementAndGet(this);
}
return new PhysicalConnectionInfo(conn, connectStartNanos, connectedNanos, initedNanos, validatedNanos, variables, globalVariables);
@@ -1625,7 +1669,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return;
}
createErrorCount.incrementAndGet();
createErrorCountUpdater.incrementAndGet(this);
long now = System.currentTimeMillis();
lock.lock();
try {
@@ -1840,7 +1884,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
if (stmtHolder == null) {
return;
}
closedPreparedStatementCount.incrementAndGet();
closedPreparedStatementCountUpdater.incrementAndGet(this);
decrementCachedPreparedStatementCount();
incrementCachedPreparedStatementDeleteCount();