Search in sources :

Example 36 with ShadowDatabaseConfig

use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.

the class ProxoolDriverConnectInterceptor method getShadowDatabaseConfig.

private ShadowDatabaseConfig getShadowDatabaseConfig(String url, String username) {
    String key = DbUrlUtils.getKey(url, username);
    ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
    if (shadowDatabaseConfig == null) {
        /**
         * 解决现在影子表配置没有username的问题,再尝试使用非用户名的判断一次
         */
        key = DbUrlUtils.getKey(url, null);
    } else {
        return shadowDatabaseConfig;
    }
    shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
    if (shadowDatabaseConfig == null) {
        return null;
    }
    return shadowDatabaseConfig;
}
Also used : ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 37 with ShadowDatabaseConfig

use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.

the class DataSourceWrapUtil method selectMatchPtDataSourceConfiguration.

@SuppressWarnings("unchecked")
private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(ProxoolDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
    ProxoolDataSource dataSource = source;
    String key = DbUrlUtils.getKey(dataSource.getDriverUrl(), dataSource.getUser());
    ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
    if (shadowDatabaseConfig == null) {
        key = DbUrlUtils.getKey(dataSource.getDriverUrl(), null);
        shadowDatabaseConfig = shadowDbConfigurations.get(key);
    }
    return shadowDatabaseConfig;
}
Also used : ProxoolDataSource(org.logicalcobwebs.proxool.ProxoolDataSource) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 38 with ShadowDatabaseConfig

use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.

the class DruidDatasourceUtils method generateDatasourceFromConfiguration.

public static DruidDataSource generateDatasourceFromConfiguration(DruidDataSource sourceDatasource, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
    if (shadowDbConfigurations == null) {
        return null;
    }
    ShadowDatabaseConfig ptDataSourceConf = selectMatchPtDataSourceConfiguration(sourceDatasource, shadowDbConfigurations);
    if (ptDataSourceConf == null) {
        return null;
    }
    String url = ptDataSourceConf.getShadowUrl();
    String username = ptDataSourceConf.getShadowUsername();
    String password = ptDataSourceConf.getShadowPassword();
    if (StringUtils.isBlank(url) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
        logger.error("[druid] shadow datasource config is invalid. [url/username/password] one in them or more is blank. url:{} username:{} password:{}", url, username, password);
        return null;
    }
    String driverClassName = ptDataSourceConf.getShadowDriverClassName();
    if (StringUtils.isBlank(driverClassName)) {
        driverClassName = sourceDatasource.getDriverClassName();
    }
    DruidDataSource target = new DruidDataSource();
    target.setUrl(url);
    target.setUsername(username);
    target.setPassword(password);
    target.setDriverClassName(driverClassName);
    try {
        Integer minIdle = ptDataSourceConf.getIntProperty("minIdle");
        if (minIdle != null) {
            target.setMinIdle(minIdle);
        } else {
            target.setMinIdle(sourceDatasource.getMinIdle());
        }
        Integer maxActive = ptDataSourceConf.getIntProperty("maxActive");
        if (maxActive != null) {
            target.setMaxActive(maxActive);
        } else {
            target.setMaxActive(sourceDatasource.getMaxActive());
        }
        Long maxWait = ptDataSourceConf.getLongProperty("maxWait");
        if (maxWait != null) {
            target.setMaxWait(maxWait);
        } else {
            target.setMaxWait(sourceDatasource.getMaxWait());
        }
        Long timeBetweenEvictionRunsMillis = ptDataSourceConf.getLongProperty("timeBetweenEvictionRunsMillis");
        if (timeBetweenEvictionRunsMillis != null) {
            target.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        } else {
            target.setTimeBetweenEvictionRunsMillis(sourceDatasource.getTimeBetweenEvictionRunsMillis());
        }
        Long minEvictableIdleTimeMillis = ptDataSourceConf.getLongProperty("minEvictableIdleTimeMillis");
        if (minEvictableIdleTimeMillis != null) {
            target.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        } else {
            target.setMinEvictableIdleTimeMillis(sourceDatasource.getMinEvictableIdleTimeMillis());
        }
        String validationQuery = ptDataSourceConf.getProperty("validationQuery");
        if (StringUtils.isNotBlank(validationQuery)) {
            target.setValidationQuery(validationQuery);
        } else {
            target.setValidationQuery(sourceDatasource.getValidationQuery());
        }
        Boolean testWhileIdle = ptDataSourceConf.getBooleanProperty("testWhileIdle");
        if (testWhileIdle != null) {
            target.setTestWhileIdle(testWhileIdle);
        } else {
            target.setTestWhileIdle(sourceDatasource.isTestWhileIdle());
        }
        Integer maxPoolPrepareStatementPerConnectionSize = ptDataSourceConf.getIntProperty("maxPoolPrepareStatementPerConnectionSize");
        if (maxPoolPrepareStatementPerConnectionSize != null) {
            target.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPrepareStatementPerConnectionSize);
        } else {
            target.setMaxPoolPreparedStatementPerConnectionSize(sourceDatasource.getMaxOpenPreparedStatements());
        }
        Boolean testOnBorrow = ptDataSourceConf.getBooleanProperty("testOnBorrow");
        if (testOnBorrow != null) {
            target.setTestOnBorrow(testOnBorrow);
        } else {
            target.setTestOnBorrow(sourceDatasource.isTestOnBorrow());
        }
        Boolean testOnReturn = ptDataSourceConf.getBooleanProperty("testOnReturn");
        if (testOnReturn != null) {
            target.setTestOnReturn(testOnReturn);
        } else {
            target.setTestOnReturn(sourceDatasource.isTestOnReturn());
        }
        Integer maxPoolPreparedStatementPerConnectionSize = ptDataSourceConf.getIntProperty("maxPoolPreparedStatementPerConnectionSize");
        if (maxPoolPreparedStatementPerConnectionSize != null) {
            target.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        } else {
            target.setMaxPoolPreparedStatementPerConnectionSize(sourceDatasource.getMaxPoolPreparedStatementPerConnectionSize());
        }
        String connectionProperties = ptDataSourceConf.getProperty("connectionProperties");
        if (StringUtils.isNotBlank(connectionProperties)) {
            target.setConnectionProperties(connectionProperties);
        }
        Boolean poolPreparedStatements = ptDataSourceConf.getBooleanProperty("poolPreparedStatements");
        if (poolPreparedStatements != null) {
            target.setPoolPreparedStatements(Boolean.valueOf(poolPreparedStatements));
        } else {
            target.setPoolPreparedStatements(sourceDatasource.isPoolPreparedStatements());
        }
        Integer timeBetweenLogStatsMillis = ptDataSourceConf.getIntProperty("timeBetweenLogStatsMillis");
        if (timeBetweenLogStatsMillis != null) {
            target.setTimeBetweenLogStatsMillis(timeBetweenLogStatsMillis);
        } else {
            target.setTimeBetweenLogStatsMillis(sourceDatasource.getTimeBetweenLogStatsMillis());
        }
        Boolean useUnfairLock = ptDataSourceConf.getBooleanProperty("useUnfairLock");
        if (useUnfairLock != null) {
            target.setUseUnfairLock(useUnfairLock);
        } else {
            target.setUseUnfairLock(sourceDatasource.isUseUnfairLock());
        }
        Boolean useOracleImplicitCache = ptDataSourceConf.getBooleanProperty("useOracleImplicitCache");
        if (useOracleImplicitCache != null) {
            target.setUseOracleImplicitCache(useOracleImplicitCache);
        } else {
            target.setUseOracleImplicitCache(sourceDatasource.isUseOracleImplicitCache());
        }
        Integer transactionQueryTimeout = ptDataSourceConf.getIntProperty("transactionQueryTimeout");
        if (transactionQueryTimeout != null) {
            target.setTransactionQueryTimeout(transactionQueryTimeout);
        } else {
            target.setTransactionQueryTimeout(sourceDatasource.getTransactionQueryTimeout());
        }
        Boolean dupCloseLogEnable = ptDataSourceConf.getBooleanProperty("dupCloseLogEnable");
        if (dupCloseLogEnable != null) {
            target.setDupCloseLogEnable(dupCloseLogEnable);
        } else {
            target.setDupCloseLogEnable(sourceDatasource.isDupCloseLogEnable());
        }
        Boolean breakAfterAcquireFailure = ptDataSourceConf.getBooleanProperty("breakAfterAcquireFailure");
        if (breakAfterAcquireFailure != null) {
            target.setBreakAfterAcquireFailure(Boolean.valueOf(breakAfterAcquireFailure));
        } else {
            target.setBreakAfterAcquireFailure(sourceDatasource.isBreakAfterAcquireFailure());
        }
        Integer connectionErrorRetryAttempts = ptDataSourceConf.getIntProperty("connectionErrorRetryAttempts");
        if (connectionErrorRetryAttempts != null) {
            target.setConnectionErrorRetryAttempts(Integer.valueOf(connectionErrorRetryAttempts));
        } else {
            target.setConnectionErrorRetryAttempts(sourceDatasource.getConnectionErrorRetryAttempts());
        }
        Boolean sharePreparedStatements = ptDataSourceConf.getBooleanProperty("sharePreparedStatements");
        if (sharePreparedStatements != null) {
            target.setSharePreparedStatements(sharePreparedStatements);
        } else {
            target.setSharePreparedStatements(sourceDatasource.isSharePreparedStatements());
        }
        String validConnectionCheckerClass = ptDataSourceConf.getProperty("validConnectionCheckerClass");
        if (StringUtils.isNotBlank(validConnectionCheckerClass)) {
            target.setValidConnectionCheckerClassName(validConnectionCheckerClass);
        } else if (StringUtils.isNotBlank(sourceDatasource.getValidConnectionCheckerClassName())) {
            target.setValidConnectionCheckerClassName(sourceDatasource.getValidConnectionCheckerClassName());
        }
        String connectProperties = ptDataSourceConf.getProperty("connectProperties");
        if (StringUtils.isNotBlank(connectProperties)) {
            String[] connectPropertyArr = StringUtils.split(connectProperties, ';');
            Properties properties = new Properties();
            for (String str : connectPropertyArr) {
                if (StringUtils.isBlank(str)) {
                    continue;
                }
                String[] arr = StringUtils.split(str, '=');
                if (arr.length != 2) {
                    continue;
                }
                if (StringUtils.isBlank(arr[0]) || StringUtils.isBlank(arr[1])) {
                    continue;
                }
                properties.setProperty(StringUtils.trim(arr[0]), StringUtils.trim(arr[1]));
            }
            if (!properties.isEmpty()) {
                target.setConnectProperties(properties);
            }
        } else {
            target.setConnectProperties(sourceDatasource.getConnectProperties());
        }
        String connectionInitSqls = ptDataSourceConf.getProperty("connectionInitSqls");
        if (StringUtils.isNotBlank(connectionInitSqls)) {
            String[] initSqls = StringUtils.split(connectionInitSqls, ';');
            List<String> list = new ArrayList<String>();
            for (String str : initSqls) {
                if (StringUtils.isNotBlank(str)) {
                    list.add(StringUtils.trim(str));
                }
            }
            if (!list.isEmpty()) {
                target.setConnectionInitSqls(list);
            }
        } else {
            target.setConnectionInitSqls(sourceDatasource.getConnectionInitSqls());
        }
        Long timeBetweenConnectErrorMillis = ptDataSourceConf.getLongProperty("timeBetweenConnectErrorMillis");
        if (timeBetweenConnectErrorMillis != null) {
            target.setTimeBetweenConnectErrorMillis(Long.valueOf(timeBetweenConnectErrorMillis));
        } else {
            target.setTimeBetweenConnectErrorMillis(sourceDatasource.getTimeBetweenConnectErrorMillis());
        }
        Integer maxOpenPreparedStatements = ptDataSourceConf.getIntProperty("maxOpenPreparedStatements");
        if (maxOpenPreparedStatements != null) {
            target.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
        } else {
            target.setMaxOpenPreparedStatements(sourceDatasource.getMaxOpenPreparedStatements());
        }
        Boolean logAbandoned = ptDataSourceConf.getBooleanProperty("logAbandoned");
        if (logAbandoned != null) {
            target.setLogAbandoned(logAbandoned);
        } else {
            target.setLogAbandoned(sourceDatasource.isLogAbandoned());
        }
        Integer removeAbandonedTimeout = ptDataSourceConf.getIntProperty("removeAbandonedTimeout");
        if (removeAbandonedTimeout != null) {
            target.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        } else {
            target.setRemoveAbandonedTimeout(sourceDatasource.getRemoveAbandonedTimeout());
        }
        Long removeAbandonedTimeoutMillis = ptDataSourceConf.getLongProperty("removeAbandonedTimeoutMillis");
        if (removeAbandonedTimeoutMillis != null) {
            target.setRemoveAbandonedTimeoutMillis(removeAbandonedTimeoutMillis);
        } else {
            target.setRemoveAbandonedTimeoutMillis(sourceDatasource.getRemoveAbandonedTimeoutMillis());
        }
        Boolean removeAbandoned = ptDataSourceConf.getBooleanProperty("removeAbandoned");
        if (removeAbandoned != null) {
            target.setRemoveAbandoned(removeAbandoned);
        } else {
            target.setRemoveAbandoned(target.isRemoveAbandoned());
        }
        Integer maxWaitThreadCount = ptDataSourceConf.getIntProperty("maxWaitThreadCount");
        if (maxWaitThreadCount != null) {
            target.setMaxWaitThreadCount(maxWaitThreadCount);
        } else {
            target.setMaxWaitThreadCount(sourceDatasource.getMaxWaitThreadCount());
        }
        Integer validationQueryTimeout = ptDataSourceConf.getIntProperty("validationQueryTimeout");
        if (validationQueryTimeout != null) {
            target.setValidationQueryTimeout(validationQueryTimeout);
        } else {
            target.setValidationQueryTimeout(sourceDatasource.getValidationQueryTimeout());
        }
        Boolean accessToUnderlyingConnectionAllowed = ptDataSourceConf.getBooleanProperty("accessToUnderlyingConnectionAllowed");
        if (accessToUnderlyingConnectionAllowed != null) {
            target.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed);
        } else {
            target.setAccessToUnderlyingConnectionAllowed(sourceDatasource.isAccessToUnderlyingConnectionAllowed());
        }
        Boolean defaultAutoCommit = ptDataSourceConf.getBooleanProperty("defaultAutoCommit");
        if (defaultAutoCommit != null) {
            target.setDefaultAutoCommit(defaultAutoCommit);
        } else {
            target.setDefaultAutoCommit(sourceDatasource.isDefaultAutoCommit());
        }
        Boolean defaultReadOnly = ptDataSourceConf.getBooleanProperty("defaultReadOnly");
        if (defaultReadOnly != null) {
            target.setDefaultReadOnly(defaultReadOnly);
        } else {
            target.setDefaultReadOnly(sourceDatasource.getDefaultReadOnly());
        }
        Integer defaultTransactionIsolation = ptDataSourceConf.getIntProperty("defaultTransactionIsolation");
        if (defaultTransactionIsolation != null) {
            target.setDefaultTransactionIsolation(Integer.valueOf(defaultTransactionIsolation));
        } else {
            target.setDefaultTransactionIsolation(sourceDatasource.getDefaultTransactionIsolation());
        }
        String defaultCatalog = ptDataSourceConf.getProperty("defaultCatalog");
        if (StringUtils.isNotBlank(defaultCatalog)) {
            target.setDefaultCatalog(defaultCatalog);
        } else {
            target.setDefaultCatalog(sourceDatasource.getDefaultCatalog());
        }
        Integer queryTimeout = ptDataSourceConf.getIntProperty("queryTimeout");
        if (queryTimeout != null) {
            target.setQueryTimeout(queryTimeout);
        } else {
            target.setQueryTimeout(sourceDatasource.getQueryTimeout());
        }
        Long maxWaitMillis = ptDataSourceConf.getLongProperty("maxWaitMillis");
        if (maxWaitMillis != null) {
            target.setMaxWait(maxWaitMillis);
        } else {
            target.setMaxWait(sourceDatasource.getMaxWait());
        }
        Integer initialSize = ptDataSourceConf.getIntProperty("initialSize");
        if (initialSize != null) {
            target.setInitialSize(Integer.valueOf(initialSize));
        } else {
            target.setInitialSize(sourceDatasource.getInitialSize());
        }
        Integer loginTimeout = ptDataSourceConf.getIntProperty("loginTimeout");
        if (loginTimeout != null) {
            target.setLoginTimeout(Integer.valueOf(loginTimeout));
        } else {
            target.setLoginTimeout(sourceDatasource.getLoginTimeout());
        }
        target.setBreakAfterAcquireFailure(true);
        target.setProxyFilters(sourceDatasource.getProxyFilters());
    } catch (Throwable e) {
        logger.warn("init druid datasource failed.", e);
    }
    return target;
}
Also used : ArrayList(java.util.ArrayList) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 39 with ShadowDatabaseConfig

use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.

the class DruidDatasourceUtils method selectMatchPtDataSourceConfiguration.

@SuppressWarnings("unchecked")
private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(DruidDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
    DruidDataSource dataSource = source;
    String key = DbUrlUtils.getKey(dataSource.getUrl(), dataSource.getUsername());
    ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
    if (shadowDatabaseConfig == null) {
        key = DbUrlUtils.getKey(dataSource.getUrl(), null);
        shadowDatabaseConfig = shadowDbConfigurations.get(key);
    }
    return shadowDatabaseConfig;
}
Also used : ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Aggregations

ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)39 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)8 ServerAddress (com.mongodb.ServerAddress)6 Map (java.util.Map)4 MongoNamespace (com.mongodb.MongoNamespace)3 ClusterSettings (com.mongodb.connection.ClusterSettings)3 ErrorReporter (com.pamirs.pradar.pressurement.agent.shared.service.ErrorReporter)3 Properties (java.util.Properties)3 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 AtomikosNonXADataSourceBean (com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 MongoClient (com.mongodb.MongoClient)2 Cluster (com.mongodb.internal.connection.Cluster)2 ShadowDataSourceConfigModifyEvent (com.pamirs.pradar.pressurement.agent.event.impl.ShadowDataSourceConfigModifyEvent)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 SQLException (java.sql.SQLException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 DataSource (org.apache.tomcat.jdbc.pool.DataSource)2