Search in sources :

Example 1 with JdbcSqlStatValue

use of com.alibaba.druid.stat.JdbcSqlStatValue in project druid by alibaba.

the class DruidDataSource method getConnectionInternal.

private DruidPooledConnection getConnectionInternal(long maxWait) throws SQLException {
    if (closed) {
        connectErrorCount.incrementAndGet();
        throw new DataSourceClosedException("dataSource already closed at " + new Date(closeTimeMillis));
    }
    if (!enable) {
        connectErrorCount.incrementAndGet();
        throw new DataSourceDisableException();
    }
    final long nanos = TimeUnit.MILLISECONDS.toNanos(maxWait);
    final int maxWaitThreadCount = getMaxWaitThreadCount();
    DruidConnectionHolder holder;
    try {
        lock.lockInterruptibly();
    } catch (InterruptedException e) {
        connectErrorCount.incrementAndGet();
        throw new SQLException("interrupt", e);
    }
    try {
        if (maxWaitThreadCount > 0) {
            if (notEmptyWaitThreadCount >= maxWaitThreadCount) {
                connectErrorCount.incrementAndGet();
                throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count " + lock.getQueueLength());
            }
        }
        connectCount++;
        if (maxWait > 0) {
            holder = pollLast(nanos);
        } else {
            holder = takeLast();
        }
        if (holder != null) {
            activeCount++;
            if (activeCount > activePeak) {
                activePeak = activeCount;
                activePeakTime = System.currentTimeMillis();
            }
        }
    } catch (InterruptedException e) {
        connectErrorCount.incrementAndGet();
        throw new SQLException(e.getMessage(), e);
    } catch (SQLException e) {
        connectErrorCount.incrementAndGet();
        throw e;
    } finally {
        lock.unlock();
    }
    if (holder == null) {
        long waitNanos = waitNanosLocal.get();
        StringBuilder buf = new StringBuilder();
        //
        buf.append("wait millis ").append(//
        waitNanos / (1000 * 1000)).append(//
        ", active " + activeCount).append(//
        ", maxActive " + maxActive);
        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("runningSqlCount ");
            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);
    return poolalbeConnection;
}
Also used : JdbcSqlStatValue(com.alibaba.druid.stat.JdbcSqlStatValue) SQLException(java.sql.SQLException) Date(java.util.Date)

Example 2 with JdbcSqlStatValue

use of com.alibaba.druid.stat.JdbcSqlStatValue in project druid by alibaba.

the class DruidDataSourceStatLoggerImpl method log.

@Override
public void log(DruidDataSourceStatValue statValue) {
    if (!isLogEnable()) {
        return;
    }
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    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.getWaitThreadCount() > 0) {
        map.put("waitThreadCount", statValue.getWaitThreadCount());
    }
    if (statValue.getNotEmptyWaitCount() > 0) {
        map.put("notEmptyWaitCount", statValue.getNotEmptyWaitCount());
    }
    if (statValue.getNotEmptyWaitMillis() > 0) {
        map.put("notEmptyWaitMillis", statValue.getNotEmptyWaitMillis());
    }
    if (statValue.getLogicConnectErrorCount() > 0) {
        map.put("logicConnectErrorCount", statValue.getLogicConnectErrorCount());
    }
    if (statValue.getPhysicalConnectCount() > 0) {
        map.put("physicalConnectCount", statValue.getPhysicalConnectCount());
    }
    if (statValue.getPhysicalCloseCount() > 0) {
        map.put("physicalCloseCount", statValue.getPhysicalCloseCount());
    }
    if (statValue.getPhysicalConnectErrorCount() > 0) {
        map.put("physicalConnectErrorCount", statValue.getPhysicalConnectErrorCount());
    }
    if (statValue.getExecuteCount() > 0) {
        map.put("executeCount", statValue.getExecuteCount());
    }
    if (statValue.getErrorCount() > 0) {
        map.put("errorCount", statValue.getErrorCount());
    }
    if (statValue.getCommitCount() > 0) {
        map.put("commitCount", statValue.getCommitCount());
    }
    if (statValue.getRollbackCount() > 0) {
        map.put("rollbackCount", statValue.getRollbackCount());
    }
    if (statValue.getPstmtCacheHitCount() > 0) {
        map.put("pstmtCacheHitCount", statValue.getPstmtCacheHitCount());
    }
    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.getConnectCount() > 0) {
        map.put("connectionHoldTimeHistogram", rtrim(statValue.getConnectionHoldTimeHistogram()));
    }
    if (statValue.getClobOpenCount() > 0) {
        map.put("clobOpenCount", statValue.getClobOpenCount());
    }
    if (statValue.getBlobOpenCount() > 0) {
        map.put("blobOpenCount", statValue.getBlobOpenCount());
    }
    if (statValue.getSqlSkipCount() > 0) {
        map.put("sqlSkipCount", statValue.getSqlSkipCount());
    }
    ArrayList<Map<String, Object>> sqlList = new ArrayList<Map<String, Object>>();
    if (statValue.sqlList.size() > 0) {
        for (JdbcSqlStatValue sqlStat : statValue.getSqlList()) {
            Map<String, Object> sqlStatMap = new LinkedHashMap<String, Object>();
            sqlStatMap.put("sql", sqlStat.getSql());
            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);
        }
        map.put("sqlList", sqlList);
    }
    if (statValue.getKeepAliveCheckCount() > 0) {
        map.put("keepAliveCheckCount", statValue.getKeepAliveCheckCount());
    }
    String text = JSONUtils.toJSONString(map);
    log(text);
}
Also used : JdbcSqlStatValue(com.alibaba.druid.stat.JdbcSqlStatValue) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with JdbcSqlStatValue

use of com.alibaba.druid.stat.JdbcSqlStatValue in project druid by alibaba.

the class MonitorDaoJdbcImplTest method testBuildSql.

public void testBuildSql() throws Exception {
    MonitorDaoJdbcImpl dao = new MonitorDaoJdbcImpl();
    dao.setDataSource(dataSource);
    //        System.out.println(buildCreateSql(dao, new BeanInfo(WallProviderStatValue.class)));
    //        System.out.println(buildCreateSql(dao, new BeanInfo(WallSqlStatValue.class)));
    //        System.out.println(buildCreateSql(dao, new BeanInfo(WallTableStatValue.class)));
    //        System.out.println(buildCreateSql(dao, new BeanInfo(WallFunctionStatValue.class)));
    //         dao.createTables("mysql");
    MonitorClient client = new MonitorClient();
    client.setDao(dao);
    client.checkInst();
    client.collectSql();
    {
        List<JdbcSqlStatValue> sqlList = client.loadSqlList(Collections.<String, Object>emptyMap());
        for (JdbcSqlStatValue sqlStatValue : sqlList) {
            System.out.println(sqlStatValue.getData());
        }
    // Assert.assertEquals(11, sqlList.size());
    }
    client.collectSql();
    {
        List<JdbcSqlStatValue> sqlList = client.loadSqlList(Collections.<String, Object>emptyMap());
        for (JdbcSqlStatValue sqlStatValue : sqlList) {
            System.out.println(sqlStatValue.getData());
        }
    // Assert.assertEquals(14, sqlList.size());
    }
}
Also used : JdbcSqlStatValue(com.alibaba.druid.stat.JdbcSqlStatValue) MonitorClient(com.alibaba.druid.support.monitor.MonitorClient) MonitorDaoJdbcImpl(com.alibaba.druid.support.monitor.dao.MonitorDaoJdbcImpl) List(java.util.List)

Example 4 with JdbcSqlStatValue

use of com.alibaba.druid.stat.JdbcSqlStatValue in project druid by alibaba.

the class MonitorDaoJdbcImpl method saveSql.

@Override
public void saveSql(MonitorContext ctx, List<DruidDataSourceStatValue> dataSourceList) {
    save(dataSourceStatBeanInfo, ctx, dataSourceList);
    for (DruidDataSourceStatValue dataSourceStatValue : dataSourceList) {
        List<JdbcSqlStatValue> sqlList = dataSourceStatValue.getSqlList();
        save(sqlStatBeanInfo, ctx, sqlList);
    }
}
Also used : JdbcSqlStatValue(com.alibaba.druid.stat.JdbcSqlStatValue) DruidDataSourceStatValue(com.alibaba.druid.pool.DruidDataSourceStatValue)

Aggregations

JdbcSqlStatValue (com.alibaba.druid.stat.JdbcSqlStatValue)4 DruidDataSourceStatValue (com.alibaba.druid.pool.DruidDataSourceStatValue)1 MonitorClient (com.alibaba.druid.support.monitor.MonitorClient)1 MonitorDaoJdbcImpl (com.alibaba.druid.support.monitor.dao.MonitorDaoJdbcImpl)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1