Search in sources :

Example 1 with AtomikosNonXADataSourceBean

use of com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean in project LinkAgent by shulieTech.

the class AtomikosNonXADataSourceBeanWrapUtil method generate.

public static AtomikosNonXADataSourceBean generate(AtomikosNonXADataSourceBean sourceDatasource) {
    Map<String, ShadowDatabaseConfig> conf = GlobalConfig.getInstance().getShadowDatasourceConfigs();
    if (conf == null) {
        return null;
    }
    ShadowDatabaseConfig ptDataSourceConf = selectMatchPtDataSourceConfiguration(sourceDatasource, conf);
    if (ptDataSourceConf == null) {
        return null;
    }
    String url = ptDataSourceConf.getShadowUrl();
    String username = ptDataSourceConf.getShadowUsername();
    String password = ptDataSourceConf.getShadowPassword();
    String driverClassName = ptDataSourceConf.getShadowDriverClassName();
    if (StringUtils.isBlank(driverClassName)) {
        driverClassName = sourceDatasource.getDriverClassName();
    }
    if (StringUtils.isBlank(url) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
        logger.error("ATOMIKOS: url/username/password can't not be null,{} {} {}", url, username, password);
        return null;
    }
    AtomikosNonXADataSourceBean target = new AtomikosNonXADataSourceBean();
    target.setUrl(url);
    target.setUser(username);
    target.setPassword(password);
    target.setDriverClassName(driverClassName);
    Integer minPoolSize = ptDataSourceConf.getIntProperty("minPoolSize");
    if (minPoolSize != null) {
        target.setMinPoolSize(minPoolSize);
    } else {
        target.setMinPoolSize(sourceDatasource.getMinPoolSize());
    }
    Integer maxPoolSize = ptDataSourceConf.getIntProperty("maxPoolSize");
    if (maxPoolSize != null) {
        target.setMaxPoolSize(maxPoolSize);
    } else {
        target.setMaxPoolSize(sourceDatasource.getMaxPoolSize());
    }
    Integer poolSize = ptDataSourceConf.getIntProperty("poolSize");
    if (poolSize != null) {
        target.setPoolSize(poolSize);
    }
    Integer borrowConnectionTimeout = ptDataSourceConf.getIntProperty("borrowConnectionTimeout");
    if (borrowConnectionTimeout != null) {
        target.setBorrowConnectionTimeout(borrowConnectionTimeout);
    } else {
        target.setBorrowConnectionTimeout(sourceDatasource.getBorrowConnectionTimeout());
    }
    Integer reapTimeout = ptDataSourceConf.getIntProperty("reapTimeout");
    if (reapTimeout != null) {
        target.setReapTimeout(reapTimeout);
    } else {
        target.setReapTimeout(sourceDatasource.getReapTimeout());
    }
    Integer maintenanceInterval = ptDataSourceConf.getIntProperty("maintenanceInterval");
    if (maintenanceInterval != null) {
        target.setMaintenanceInterval(Integer.valueOf(maintenanceInterval));
    } else {
        target.setMaintenanceInterval(sourceDatasource.getMaintenanceInterval());
    }
    Integer maxIdleTime = ptDataSourceConf.getIntProperty("maxIdleTime");
    if (maxIdleTime != null) {
        target.setMaxIdleTime(Integer.valueOf(maxIdleTime));
    } else {
        target.setMaxIdleTime(sourceDatasource.getMaxIdleTime());
    }
    Integer maxLifetime = ptDataSourceConf.getIntProperty("maxLifetime");
    if (maxLifetime != null) {
        target.setMaxLifetime(Integer.valueOf(maxLifetime));
    } else {
        target.setMaxLifetime(sourceDatasource.getMaxLifetime());
    }
    String testQuery = ptDataSourceConf.getProperty("testQuery");
    if (StringUtils.isNotBlank(testQuery)) {
        target.setTestQuery(testQuery);
    }
    Boolean concurrentConnectionValidation = ptDataSourceConf.getBooleanProperty("concurrentConnectionValidation");
    if (concurrentConnectionValidation != null) {
        target.setConcurrentConnectionValidation(Boolean.valueOf(concurrentConnectionValidation));
    }
    String resourceName = ptDataSourceConf.getProperty("resourceName");
    if (StringUtils.isNotBlank(resourceName)) {
        target.setUniqueResourceName(resourceName);
    } else {
        target.setUniqueResourceName(Pradar.addClusterTestPrefix(sourceDatasource.getUniqueResourceName()));
    }
    Integer defaultIsolationLevel = ptDataSourceConf.getIntProperty("defaultIsolationLevel");
    if (defaultIsolationLevel != null) {
        target.setDefaultIsolationLevel(Integer.valueOf(defaultIsolationLevel));
    }
    return target;
}
Also used : AtomikosNonXADataSourceBean(com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 2 with AtomikosNonXADataSourceBean

use of com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean in project LinkAgent by shulieTech.

the class AtomikosNonXADataSourceBeanWrapUtil method init.

// static AtomicBoolean inited = new AtomicBoolean(false);
public static void init(DataSourceMeta<AtomikosNonXADataSourceBean> dataSourceMeta) {
    if (pressureDataSources.containsKey(dataSourceMeta) && pressureDataSources.get(dataSourceMeta) != null) {
        return;
    }
    AtomikosNonXADataSourceBean target = dataSourceMeta.getDataSource();
    if (isPerformanceDataSource(target)) {
        return;
    }
    if (!validate(target)) {
        // 没有配置对应的影子表或影子库
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("[atomikos] DataSourceWrapUtil:业务库配置::url: " + target.getUrl() + "; username:" + dataSourceMeta.getUsername() + "; 中间件类型:other").report();
        AtomikosNonXADataSourceBeanMediaDataSource dbMediatorDataSource = new AtomikosNonXADataSourceBeanMediaDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (isInfoEnabled) {
                logger.info("[atomikos] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUser());
            }
            old.close();
        }
        return;
    }
    if (shadowTable(target)) {
        // 影子表
        try {
            AtomikosNonXADataSourceBeanMediaDataSource dbMediatorDataSource = new AtomikosNonXADataSourceBeanMediaDataSource();
            dbMediatorDataSource.setDataSourceBusiness(target);
            DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
            if (old != null) {
                if (isInfoEnabled) {
                    logger.info("[atomikos] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUser());
                }
                old.close();
            }
        } catch (Throwable e) {
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子表设置初始化异常!").setDetail("[atomikos] DataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "|||" + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
            logger.error("[atomikos] init datasource err!", e);
        }
    } else {
        // 影子库
        try {
            AtomikosNonXADataSourceBeanMediaDataSource dataSource = new AtomikosNonXADataSourceBeanMediaDataSource();
            AtomikosNonXADataSourceBean ptDataSource = copy(target);
            dataSource.setDataSourcePerformanceTest(ptDataSource);
            dataSource.setDataSourceBusiness(target);
            DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dataSource);
            if (old != null) {
                if (isInfoEnabled) {
                    logger.info("[atomikos] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUser());
                }
                old.close();
            }
            if (isInfoEnabled) {
                logger.info("[atomikos] create shadow datasource success. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getUrl(), target.getUser(), ptDataSource.getUrl(), ptDataSource.getUser());
            }
        } catch (Throwable t) {
            logger.error("[atomikos] init datasource err!", t);
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库设置初始化异常!").setDetail("[atomikos] DataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "|||" + Throwables.getStackTraceAsString(t)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        }
    }
}
Also used : DbMediatorDataSource(com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource) AtomikosNonXADataSourceBean(com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean)

Example 3 with AtomikosNonXADataSourceBean

use of com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean in project LinkAgent by shulieTech.

the class DataSourceGetConnectionCutoffInterceptor method wrapAtomikosNonXADataSourceBean.

private CutOffResult wrapAtomikosNonXADataSourceBean(AtomikosNonXADataSourceBean dataSourceBean) {
    DataSourceMeta<AtomikosNonXADataSourceBean> dataSourceMeta = new DataSourceMeta<AtomikosNonXADataSourceBean>(dataSourceBean.getUrl(), dataSourceBean.getUser(), dataSourceBean);
    AtomikosNonXADataSourceBeanWrapUtil.init(dataSourceMeta);
    Connection connection = null;
    /**
     * 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
     * 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
     * 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
     */
    if (AtomikosNonXADataSourceBeanWrapUtil.pressureDataSources.containsKey(dataSourceMeta)) {
        AtomikosNonXADataSourceBeanMediaDataSource mediatorDataSource = AtomikosNonXADataSourceBeanWrapUtil.pressureDataSources.get(dataSourceMeta);
        if (mediatorDataSource != null) {
            try {
                connection = mediatorDataSource.getConnection();
            } catch (SQLException e) {
                throw new PressureMeasureError(e);
            }
        } else {
            if (!Pradar.isClusterTest()) {
                return CutOffResult.passed();
            }
        }
        return CutOffResult.cutoff(connection);
    } else {
        if (!Pradar.isClusterTest()) {
            return CutOffResult.passed();
        }
        return CutOffResult.cutoff(null);
    }
}
Also used : SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Connection(java.sql.Connection) AtomikosNonXADataSourceBean(com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) AtomikosNonXADataSourceBeanMediaDataSource(com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource)

Example 4 with AtomikosNonXADataSourceBean

use of com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean in project LinkAgent by shulieTech.

the class AtomikosNonXADataSourceBeanWrapUtil method selectMatchPtDataSourceConfiguration.

private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(AtomikosNonXADataSourceBean source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
    AtomikosNonXADataSourceBean dataSource = source;
    String key = DbUrlUtils.getKey(dataSource.getUrl(), dataSource.getUser());
    ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
    if (shadowDatabaseConfig == null) {
        key = DbUrlUtils.getKey(dataSource.getUrl(), null);
        shadowDatabaseConfig = shadowDbConfigurations.get(key);
    }
    return shadowDatabaseConfig;
}
Also used : AtomikosNonXADataSourceBean(com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Aggregations

AtomikosNonXADataSourceBean (com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean)4 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)2 AtomikosNonXADataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)1 DbMediatorDataSource (com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1