improve sql parser & improve GetConnecitonTimeout error message

This commit is contained in:
wenshao
2013-08-07 19:25:55 +08:00
parent 29e84f2be2
commit 65a963f88f
10 changed files with 268 additions and 64 deletions
@@ -77,6 +77,7 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.stat.JdbcDataSourceStat;
import com.alibaba.druid.stat.JdbcSqlStat;
import com.alibaba.druid.stat.JdbcSqlStatValue;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.IOUtils;
@@ -144,6 +145,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private boolean mbeanRegistered = false;
public static ThreadLocal<Long> waitNanosLocal = new ThreadLocal<Long>();
public DruidDataSource(){
this(false);
}
@@ -1012,12 +1015,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
holder = takeLast();
}
activeCount++;
if (activeCount > activePeak) {
activePeak = activeCount;
activePeakTime = System.currentTimeMillis();
if (holder != null) {
activeCount++;
if (activeCount > activePeak) {
activePeak = activeCount;
activePeakTime = System.currentTimeMillis();
}
}
} catch (InterruptedException e) {
connectErrorCount.incrementAndGet();
throw new SQLException(e.getMessage(), e);
@@ -1028,6 +1032,38 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.unlock();
}
if (holder == null) {
long waitNanos = waitNanosLocal.get();
StringBuilder buf = new StringBuilder();
buf.append("wait millis ")//
.append(waitNanos / (1000 * 1000))//
.append(", active " + activeCount)//
;
List<JdbcSqlStatValue> sqlList = this.getDataSourceStat().getRuningSqlList();
for (int i = 0; i < sqlList.size(); ++i) {
if (i != 0) {
buf.append('\n');
} else {
buf.append(", ");
}
JdbcSqlStatValue sql = sqlList.get(i);
buf.append("runningCount ");
buf.append(sql.getRunningCount());
buf.append(" : ");
buf.append(sql.getSql());
}
String errorMessage = buf.toString();
if (this.createError == null) {
throw new GetConnectionTimeoutException(errorMessage, createError);
} else {
throw new GetConnectionTimeoutException(errorMessage);
}
}
holder.incrementUseCount();
DruidPooledConnection poolalbeConnection = new DruidPooledConnection(holder);
@@ -1313,19 +1349,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
DruidConnectionHolder pollLast(long nanos) throws InterruptedException, SQLException {
long estimate = nanos;
for (int i = 0;; ++i) {
for (;;) {
if (poolingCount == 0) {
empty.signal(); // send signal to CreateThread create connection
if (estimate <= 0) {
String errorMessage = "loopWaitCount " + i + ", wait millis " + (nanos - estimate) / (1000 * 1000)
+ ", active " + activeCount;
if (this.createError == null) {
throw new GetConnectionTimeoutException(errorMessage, createError);
} else {
throw new GetConnectionTimeoutException(errorMessage);
}
waitNanosLocal.set(nanos - estimate);
return null;
}
notEmptyWaitThreadCount++;
@@ -1358,13 +1388,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
continue;
}
String errorMessage = "loopWaitCount " + i + ", wait millis " + (nanos - estimate) / (1000 * 1000)
+ ", active " + activeCount;
if (createError != null) {
throw new GetConnectionTimeoutException(errorMessage, createError);
} else {
throw new GetConnectionTimeoutException(errorMessage);
}
waitNanosLocal.set(nanos - estimate);
return null;
}
}
@@ -97,9 +97,9 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
public static void setGlobal(JdbcDataSourceStat value) {
global = value;
}
public void configFromProperties(Properties properties) {
}
public boolean isResetStatEnable() {
@@ -207,7 +207,7 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
if (!isResetStatEnable()) {
return;
}
blobOpenCount.set(0);
clobOpenCount.set(0);
@@ -375,6 +375,33 @@ public class JdbcDataSourceStat implements JdbcDataSourceStatMBean {
return values;
}
public List<JdbcSqlStatValue> getRuningSqlList() {
List<JdbcSqlStat> stats = new ArrayList<JdbcSqlStat>(sqlStatMap.size());
lock.readLock().lock();
try {
Iterator<Map.Entry<String, JdbcSqlStat>> iter = sqlStatMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, JdbcSqlStat> entry = iter.next();
JdbcSqlStat stat = entry.getValue();
if (stat.getRunningCount() >= 0) {
stats.add(entry.getValue());
}
}
} finally {
lock.readLock().unlock();
}
List<JdbcSqlStatValue> values = new ArrayList<JdbcSqlStatValue>(stats.size());
for (int i = 0; i < stats.size(); ++i) {
JdbcSqlStat stat = stats.get(i);
JdbcSqlStatValue value = stat.getValue(false);
if (value.getRunningCount() > 0) {
values.add(value);
}
}
return values;
}
public JdbcSqlStat getSqlStat(String sql) {
lock.readLock().lock();
try {
@@ -239,6 +239,49 @@ public abstract class WallProvider {
public WallSqlStat addWhiteSql(String sql, Map<String, WallSqlTableStat> tableStats,
Map<String, WallSqlFunctionStat> functionStats, boolean syntaxError) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
WallSqlStat stat = new WallSqlStat(tableStats, functionStats, syntaxError);
stat.incrementAndGetExecuteCount();
return stat;
}
if (mergedSql != sql) {
WallSqlStat mergedStat;
lock.readLock().lock();
try {
if (whiteMergedList == null) {
whiteMergedList = new LRUCache<String, WallSqlStat>(whiteSqlMaxSize);
}
mergedStat = whiteMergedList.get(mergedSql);
} finally {
lock.readLock().unlock();
}
if (mergedStat == null) {
WallSqlStat newStat = new WallSqlStat(tableStats, functionStats, syntaxError);
newStat.setSample(sql);
lock.writeLock().lock();
try {
mergedStat = whiteMergedList.get(mergedSql);
if (mergedStat == null) {
whiteMergedList.put(mergedSql, newStat);
mergedStat = newStat;
}
} finally {
lock.writeLock().unlock();
}
}
mergedStat.incrementAndGetExecuteCount();
return mergedStat;
}
lock.writeLock().lock();
try {
if (whiteList == null) {
@@ -251,12 +294,6 @@ public abstract class WallProvider {
WallSqlStat wallStat = whiteList.get(sql);
if (wallStat == null) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
mergedSql = sql;
}
wallStat = whiteMergedList.get(mergedSql);
if (wallStat == null) {
wallStat = new WallSqlStat(tableStats, functionStats, syntaxError);
@@ -277,6 +314,14 @@ public abstract class WallProvider {
public WallSqlStat addBlackSql(String sql, Map<String, WallSqlTableStat> tableStats,
Map<String, WallSqlFunctionStat> functionStats, List<Violation> violations,
boolean syntaxError) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
// skip
mergedSql = sql;
}
lock.writeLock().lock();
try {
if (blackList == null) {
@@ -289,12 +334,6 @@ public abstract class WallProvider {
WallSqlStat wallStat = blackList.get(sql);
if (wallStat == null) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
mergedSql = sql;
}
wallStat = blackMergedList.get(mergedSql);
if (wallStat == null) {
wallStat = new WallSqlStat(tableStats, functionStats, violations, syntaxError);
@@ -401,16 +440,37 @@ public abstract class WallProvider {
}
public WallSqlStat getWhiteSql(String sql) {
WallSqlStat stat = null;
lock.readLock().lock();
try {
if (whiteList == null) {
return null;
if (whiteList != null) {
stat = whiteList.get(sql);
}
return whiteList.get(sql);
} finally {
lock.readLock().unlock();
}
if (stat == null) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
// skip
return null;
}
lock.readLock().lock();
try {
if (whiteMergedList == null) {
return null;
}
stat = whiteMergedList.get(mergedSql);
} finally {
lock.readLock().unlock();
}
}
return stat;
}
public WallSqlStat getBlackSql(String sql) {
@@ -832,13 +892,13 @@ public abstract class WallProvider {
WallTableStat tableStat = entry.getValue();
WallTableStatValue tableStatValue = tableStat.getStatValue(reset);
if (tableStatValue.getTotalExecuteCount() == 0) {
continue;
}
tableStatValue.setName(tableName);
statValue.getTables().add(tableStatValue);
}
@@ -847,7 +907,7 @@ public abstract class WallProvider {
WallFunctionStat functionStat = entry.getValue();
WallFunctionStatValue functionStatValue = functionStat.getStatValue(reset);
if (functionStatValue.getInvokeCount() == 0) {
continue;
}
@@ -864,11 +924,11 @@ public abstract class WallProvider {
String sql = entry.getKey();
WallSqlStat sqlStat = entry.getValue();
WallSqlStatValue sqlStatValue = sqlStat.getStatValue(reset);
if (sqlStatValue.getExecuteCount() == 0) {
continue;
}
sqlStatValue.setSql(sql);
statValue.getWhiteList().add(sqlStatValue);
}
@@ -879,11 +939,11 @@ public abstract class WallProvider {
String sql = entry.getKey();
WallSqlStat sqlStat = entry.getValue();
WallSqlStatValue sqlStatValue = sqlStat.getStatValue(reset);
if (sqlStatValue.getExecuteCount() == 0) {
continue;
}
sqlStatValue.setSql(sql);
statValue.getBlackList().add(sqlStatValue);
}