From 87c5244a02f82552638abbd4504f27b43ebb535c Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 1 Aug 2013 14:18:30 +0800 Subject: [PATCH] improve wall-provider --- .../pool/DruidDataSourceStatLoggerImpl.java | 270 +++++++++--------- .../com/alibaba/druid/wall/WallProvider.java | 13 +- ...ogger.java => WallProviderStatLogger.java} | 9 +- .../wall/WallProviderStatLoggerAdapter.java | 35 +++ .../wall/WallProviderStatLoggerImpl.java | 70 +++++ .../druid/wall/WallProviderStatTimer.java | 72 +++++ .../druid/wall/WallProviderStatValue.java | 20 +- 7 files changed, 348 insertions(+), 141 deletions(-) rename src/main/java/com/alibaba/druid/wall/{spi/WallStatLogger.java => WallProviderStatLogger.java} (75%) create mode 100644 src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerAdapter.java create mode 100644 src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerImpl.java create mode 100644 src/main/java/com/alibaba/druid/wall/WallProviderStatTimer.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatLoggerImpl.java b/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatLoggerImpl.java index 2a4cd2d67d..fff2b122e8 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatLoggerImpl.java +++ b/src/main/java/com/alibaba/druid/pool/DruidDataSourceStatLoggerImpl.java @@ -51,7 +51,7 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap setLoggerName(property); } } - + public Log getLogger() { return logger; } @@ -60,183 +60,183 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap public void setLoggerName(String loggerName) { logger = LogFactory.getLog(loggerName); } - + @Override public void setLogger(Log logger) { if (logger == null) { - throw new IllegalArgumentException("logger can not be null"); + throw new IllegalArgumentException("logger can not be null"); } this.logger = logger; } - + public boolean isLogEnable() { return logger.isInfoEnabled(); } - + public void log(String value) { logger.info(value); } @Override public void log(DruidDataSourceStatValue statValue) { - if (isLogEnable()) { - Map map = new LinkedHashMap(); + if (!isLogEnable()) { + return; + } + Map map = new LinkedHashMap(); - map.put("url", statValue.url); - map.put("dbType", statValue.getDbType()); - map.put("name", statValue.getName()); - map.put("activeCount", statValue.getActiveCount()); + map.put("url", statValue.url); + map.put("dbType", statValue.getDbType()); + map.put("name", statValue.getName()); + map.put("activeCount", statValue.getActiveCount()); - if (statValue.getActivePeak() > 0) { - map.put("activePeak", statValue.getActivePeak()); - map.put("activePeakTime", statValue.getActivePeakTime()); - } - map.put("poolingCount", statValue.getPoolingCount()); - if (statValue.getPoolingPeak() > 0) { - map.put("poolingPeak", statValue.getPoolingPeak()); - map.put("poolingPeakTime", statValue.getPoolingPeakTime()); - } - map.put("connectCount", statValue.getConnectCount()); - map.put("closeCount", statValue.getCloseCount()); + if (statValue.getActivePeak() > 0) { + map.put("activePeak", statValue.getActivePeak()); + map.put("activePeakTime", statValue.getActivePeakTime()); + } + map.put("poolingCount", statValue.getPoolingCount()); + if (statValue.getPoolingPeak() > 0) { + map.put("poolingPeak", statValue.getPoolingPeak()); + map.put("poolingPeakTime", statValue.getPoolingPeakTime()); + } + map.put("connectCount", statValue.getConnectCount()); + map.put("closeCount", statValue.getCloseCount()); - if (statValue.getWaitThreadCount() > 0) { - map.put("waitThreadCount", statValue.getWaitThreadCount()); - } + if (statValue.getWaitThreadCount() > 0) { + map.put("waitThreadCount", statValue.getWaitThreadCount()); + } - if (statValue.getNotEmptyWaitCount() > 0) { - map.put("notEmptyWaitCount", statValue.getNotEmptyWaitCount()); - } + if (statValue.getNotEmptyWaitCount() > 0) { + map.put("notEmptyWaitCount", statValue.getNotEmptyWaitCount()); + } - if (statValue.getNotEmptyWaitMillis() > 0) { - map.put("notEmptyWaitMillis", statValue.getNotEmptyWaitMillis()); - } + if (statValue.getNotEmptyWaitMillis() > 0) { + map.put("notEmptyWaitMillis", statValue.getNotEmptyWaitMillis()); + } - if (statValue.getLogicConnectErrorCount() > 0) { - map.put("logicConnectErrorCount", statValue.getLogicConnectErrorCount()); - } + if (statValue.getLogicConnectErrorCount() > 0) { + map.put("logicConnectErrorCount", statValue.getLogicConnectErrorCount()); + } - if (statValue.getPhysicalConnectCount() > 0) { - map.put("physicalConnectCount", statValue.getPhysicalConnectCount()); - } + if (statValue.getPhysicalConnectCount() > 0) { + map.put("physicalConnectCount", statValue.getPhysicalConnectCount()); + } - if (statValue.getPhysicalCloseCount() > 0) { - map.put("physicalCloseCount", statValue.getPhysicalCloseCount()); - } + if (statValue.getPhysicalCloseCount() > 0) { + map.put("physicalCloseCount", statValue.getPhysicalCloseCount()); + } - if (statValue.getPhysicalConnectErrorCount() > 0) { - map.put("physicalConnectErrorCount", statValue.getPhysicalConnectErrorCount()); - } + if (statValue.getPhysicalConnectErrorCount() > 0) { + map.put("physicalConnectErrorCount", statValue.getPhysicalConnectErrorCount()); + } - if (statValue.getExecuteCount() > 0) { - map.put("executeCount", statValue.getExecuteCount()); - } + if (statValue.getExecuteCount() > 0) { + map.put("executeCount", statValue.getExecuteCount()); + } - if (statValue.getErrorCount() > 0) { - map.put("errorCount", statValue.getErrorCount()); - } + if (statValue.getErrorCount() > 0) { + map.put("errorCount", statValue.getErrorCount()); + } - if (statValue.getCommitCount() > 0) { - map.put("commitCount", statValue.getCommitCount()); - } + if (statValue.getCommitCount() > 0) { + map.put("commitCount", statValue.getCommitCount()); + } - if (statValue.getRollbackCount() > 0) { - map.put("rollbackCount", statValue.getRollbackCount()); - } + if (statValue.getRollbackCount() > 0) { + map.put("rollbackCount", statValue.getRollbackCount()); + } - if (statValue.getPstmtCacheHitCount() > 0) { - map.put("pstmtCacheHitCount", statValue.getPstmtCacheHitCount()); - } + if (statValue.getPstmtCacheHitCount() > 0) { + map.put("pstmtCacheHitCount", statValue.getPstmtCacheHitCount()); + } - if (statValue.getPstmtCacheMissCount() > 0) { - map.put("pstmtCacheMissCount", statValue.getPstmtCacheMissCount()); - } + if (statValue.getPstmtCacheMissCount() > 0) { + map.put("pstmtCacheMissCount", statValue.getPstmtCacheMissCount()); + } - if (statValue.getStartTransactionCount() > 0) { - map.put("startTransactionCount", statValue.getStartTransactionCount()); - map.put("transactionHistogram", rtrim(statValue.getTransactionHistogram())); - } + if (statValue.getStartTransactionCount() > 0) { + map.put("startTransactionCount", statValue.getStartTransactionCount()); + map.put("transactionHistogram", rtrim(statValue.getTransactionHistogram())); + } - if (statValue.getConnectCount() > 0) { - map.put("connectionHoldTimeHistogram", rtrim(statValue.getConnectionHoldTimeHistogram())); - } + if (statValue.getConnectCount() > 0) { + map.put("connectionHoldTimeHistogram", rtrim(statValue.getConnectionHoldTimeHistogram())); + } - if (statValue.getClobOpenCount() > 0) { - map.put("clobOpenCount", statValue.getClobOpenCount()); - } + if (statValue.getClobOpenCount() > 0) { + map.put("clobOpenCount", statValue.getClobOpenCount()); + } - if (statValue.getBlobOpenCount() > 0) { - map.put("blobOpenCount", statValue.getBlobOpenCount()); - } + if (statValue.getBlobOpenCount() > 0) { + map.put("blobOpenCount", statValue.getBlobOpenCount()); + } - if (statValue.getSqlSkipCount() > 0) { - map.put("sqlSkipCount", statValue.getSqlSkipCount()); - } + if (statValue.getSqlSkipCount() > 0) { + map.put("sqlSkipCount", statValue.getSqlSkipCount()); + } - ArrayList> sqlList = new ArrayList>(); - if (statValue.sqlList.size() > 0) { - for (JdbcSqlStatValue sqlStat : statValue.getSqlList()) { - Map sqlStatMap = new LinkedHashMap(); - sqlStatMap.put("sql", sqlStat.getSql()); + ArrayList> sqlList = new ArrayList>(); + if (statValue.sqlList.size() > 0) { + for (JdbcSqlStatValue sqlStat : statValue.getSqlList()) { + Map sqlStatMap = new LinkedHashMap(); + sqlStatMap.put("sql", sqlStat.getSql()); - if (sqlStat.getExecuteCount() > 0) { - sqlStatMap.put("executeCount", sqlStat.getExecuteCount()); - sqlStatMap.put("executeMillisMax", sqlStat.getExecuteMillisMax()); - sqlStatMap.put("executeMillisTotal", sqlStat.getExecuteMillisTotal()); + if (sqlStat.getExecuteCount() > 0) { + sqlStatMap.put("executeCount", sqlStat.getExecuteCount()); + sqlStatMap.put("executeMillisMax", sqlStat.getExecuteMillisMax()); + sqlStatMap.put("executeMillisTotal", sqlStat.getExecuteMillisTotal()); - sqlStatMap.put("executeHistogram", rtrim(sqlStat.getExecuteHistogram())); - sqlStatMap.put("executeAndResultHoldHistogram", - rtrim(sqlStat.getExecuteAndResultHoldHistogram())); - } - - long executeErrorCount = sqlStat.getExecuteErrorCount(); - if (executeErrorCount > 0) { - sqlStatMap.put("executeErrorCount", executeErrorCount); - } - - int runningCount = sqlStat.getRunningCount(); - if (runningCount > 0) { - sqlStatMap.put("runningCount", runningCount); - } - - int concurrentMax = sqlStat.getConcurrentMax(); - if (concurrentMax > 0) { - sqlStatMap.put("concurrentMax", concurrentMax); - } - - if (sqlStat.getFetchRowCount() > 0) { - sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCount()); - sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCountMax()); - sqlStatMap.put("fetchRowHistogram", rtrim(sqlStat.getFetchRowHistogram())); - } - - if (sqlStat.getUpdateCount() > 0) { - sqlStatMap.put("updateCount", sqlStat.getUpdateCount()); - sqlStatMap.put("updateCountMax", sqlStat.getUpdateCountMax()); - sqlStatMap.put("updateHistogram", rtrim(sqlStat.getUpdateHistogram())); - } - - if (sqlStat.getInTransactionCount() > 0) { - sqlStatMap.put("inTransactionCount", sqlStat.getInTransactionCount()); - } - - if (sqlStat.getClobOpenCount() > 0) { - sqlStatMap.put("clobOpenCount", sqlStat.getClobOpenCount()); - } - - if (sqlStat.getBlobOpenCount() > 0) { - sqlStatMap.put("blobOpenCount", sqlStat.getBlobOpenCount()); - } - - sqlList.add(sqlStatMap); + sqlStatMap.put("executeHistogram", rtrim(sqlStat.getExecuteHistogram())); + sqlStatMap.put("executeAndResultHoldHistogram", rtrim(sqlStat.getExecuteAndResultHoldHistogram())); } - map.put("sqlList", sqlList); + long executeErrorCount = sqlStat.getExecuteErrorCount(); + if (executeErrorCount > 0) { + sqlStatMap.put("executeErrorCount", executeErrorCount); + } + + int runningCount = sqlStat.getRunningCount(); + if (runningCount > 0) { + sqlStatMap.put("runningCount", runningCount); + } + + int concurrentMax = sqlStat.getConcurrentMax(); + if (concurrentMax > 0) { + sqlStatMap.put("concurrentMax", concurrentMax); + } + + if (sqlStat.getFetchRowCount() > 0) { + sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCount()); + sqlStatMap.put("fetchRowCount", sqlStat.getFetchRowCountMax()); + sqlStatMap.put("fetchRowHistogram", rtrim(sqlStat.getFetchRowHistogram())); + } + + if (sqlStat.getUpdateCount() > 0) { + sqlStatMap.put("updateCount", sqlStat.getUpdateCount()); + sqlStatMap.put("updateCountMax", sqlStat.getUpdateCountMax()); + sqlStatMap.put("updateHistogram", rtrim(sqlStat.getUpdateHistogram())); + } + + if (sqlStat.getInTransactionCount() > 0) { + sqlStatMap.put("inTransactionCount", sqlStat.getInTransactionCount()); + } + + if (sqlStat.getClobOpenCount() > 0) { + sqlStatMap.put("clobOpenCount", sqlStat.getClobOpenCount()); + } + + if (sqlStat.getBlobOpenCount() > 0) { + sqlStatMap.put("blobOpenCount", sqlStat.getBlobOpenCount()); + } + + sqlList.add(sqlStatMap); } - String text = JSONUtils.toJSONString(map); - - log(text); + map.put("sqlList", sqlList); } + + String text = JSONUtils.toJSONString(map); + + log(text); } } diff --git a/src/main/java/com/alibaba/druid/wall/WallProvider.java b/src/main/java/com/alibaba/druid/wall/WallProvider.java index 20a8cabe77..d130c839fd 100644 --- a/src/main/java/com/alibaba/druid/wall/WallProvider.java +++ b/src/main/java/com/alibaba/druid/wall/WallProvider.java @@ -47,6 +47,8 @@ import com.alibaba.druid.wall.violation.SyntaxErrorViolation; public abstract class WallProvider { + private String name; + private final Map attributes = new ConcurrentHashMap( 1, 0.75f, @@ -99,6 +101,14 @@ public abstract class WallProvider { this.dbType = dbType; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public Map getAttributes() { return attributes; } @@ -144,7 +154,7 @@ public abstract class WallProvider { public void addUpdateCount(WallSqlStat sqlStat, long updateCount) { sqlStat.addUpdateCount(updateCount); - + Map sqlTableStats = sqlStat.getTableStats(); if (sqlTableStats == null) { return; @@ -808,6 +818,7 @@ public abstract class WallProvider { public WallProviderStatValue getStatValue(boolean reset) { WallProviderStatValue statValue = new WallProviderStatValue(); + statValue.setName(name); statValue.setCheckCount(get(checkCount, reset)); statValue.setHardCheckCount(get(hardCheckCount, reset)); statValue.setViolationCount(get(violationCount, reset)); diff --git a/src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatLogger.java similarity index 75% rename from src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java rename to src/main/java/com/alibaba/druid/wall/WallProviderStatLogger.java index baefce5e36..040e81c0b5 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/WallStatLogger.java +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatLogger.java @@ -13,8 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.druid.wall.spi; +package com.alibaba.druid.wall; -public interface WallStatLogger { +import java.util.Properties; +public interface WallProviderStatLogger { + + void log(WallProviderStatValue statValue); + + void configFromProperties(Properties properties); } diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerAdapter.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerAdapter.java new file mode 100644 index 0000000000..250eac1ca9 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerAdapter.java @@ -0,0 +1,35 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.wall; + +import java.util.Properties; + +public abstract class WallProviderStatLoggerAdapter implements WallProviderStatLogger { + + public WallProviderStatLoggerAdapter(){ + + } + + @Override + public void log(WallProviderStatValue statValue) { + + } + + @Override + public void configFromProperties(Properties properties) { + + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerImpl.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerImpl.java new file mode 100644 index 0000000000..e9ba4dc3e1 --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatLoggerImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.wall; + +import java.util.Map; +import java.util.Properties; + +import com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl; +import com.alibaba.druid.support.json.JSONUtils; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +public class WallProviderStatLoggerImpl extends WallProviderStatLoggerAdapter implements WallProviderStatLogger { + + private static Log LOG = LogFactory.getLog(DruidDataSourceStatLoggerImpl.class); + + private Log logger = LOG; + + @Override + public void configFromProperties(Properties properties) { + String property = properties.getProperty("druid.stat.loggerName"); + if (property != null && property.length() > 0) { + setLoggerName(property); + } + } + + public void setLoggerName(String loggerName) { + logger = LogFactory.getLog(loggerName); + } + + public void setLogger(Log logger) { + if (logger == null) { + throw new IllegalArgumentException("logger can not be null"); + } + this.logger = logger; + } + + public boolean isLogEnable() { + return logger.isInfoEnabled(); + } + + public void log(String value) { + logger.info(value); + } + + @Override + public void log(WallProviderStatValue statValue) { + if (!isLogEnable()) { + return; + } + + Map map = statValue.toMap(); + String text = JSONUtils.toJSONString(map); + + log(text); + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatTimer.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatTimer.java new file mode 100644 index 0000000000..b256f7fd6b --- /dev/null +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatTimer.java @@ -0,0 +1,72 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.wall; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class WallProviderStatTimer implements Runnable { + + private ScheduledExecutorService scheduler; + private int threadCount = 1; + private long period = 60 * 5; + private TimeUnit unit = TimeUnit.SECONDS; + + private final List providers = new CopyOnWriteArrayList(); + + private WallProviderStatLogger statLogger; + + public WallProviderStatTimer(){ + + } + + public List getProviders() { + return providers; + } + + public void setProviders(List providers) { + this.providers.clear(); + this.providers.addAll(providers); + } + + public void start() { + if (statLogger == null) { + throw new IllegalStateException("statLogger is null"); + } + + scheduler = Executors.newScheduledThreadPool(threadCount); + scheduler.scheduleAtFixedRate(this, period, period, unit); + } + + @Override + public void run() { + for (WallProvider provider : this.providers) { + WallProviderStatValue statValue = getStatValue(provider); + log(statValue); + } + } + + protected WallProviderStatValue getStatValue(WallProvider provider) { + return provider.getStatValue(true); + } + + protected void log(WallProviderStatValue statValue) { + statLogger.log(statValue); + } +} diff --git a/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java index 0d92f7e5d2..5e35c7ee27 100644 --- a/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java +++ b/src/main/java/com/alibaba/druid/wall/WallProviderStatValue.java @@ -22,12 +22,14 @@ import java.util.Map; public class WallProviderStatValue { + private String name; + private long checkCount; private long hardCheckCount; private long violationCount; private long whiteListHitCount; private long blackListHitCount; - private long syntaxErrroCount; + private long syntaxErrorCount; private long violationEffectRowCount; private final List tables = new ArrayList(); @@ -36,6 +38,18 @@ public class WallProviderStatValue { private final List whiteList = new ArrayList(); private final List blackList = new ArrayList(); + public WallProviderStatValue(){ + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public long getCheckCount() { return checkCount; } @@ -77,11 +91,11 @@ public class WallProviderStatValue { } public long getSyntaxErrorCount() { - return syntaxErrroCount; + return syntaxErrorCount; } public void setSyntaxErrorCount(long syntaxErrroCount) { - this.syntaxErrroCount = syntaxErrroCount; + this.syntaxErrorCount = syntaxErrroCount; } public long getViolationEffectRowCount() {