Search in sources :

Example 1 with DbDruidMediatorDataSource

use of com.pamirs.attach.plugin.alibaba.druid.obj.DbDruidMediatorDataSource in project LinkAgent by shulieTech.

the class DruidInjectGetConnectionInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) {
    DruidDataSource target1 = (DruidDataSource) advice.getTarget();
    addAttachment(advice);
    DataSourceMeta<DruidDataSource> dataSourceMeta = new DataSourceMeta<DruidDataSource>(target1.getUrl(), target1.getUsername(), target1);
    ClusterTestUtils.validateClusterTest();
    DbDruidMediatorDataSource mediatorDataSource = DataSourceWrapUtil.doWrap(dataSourceMeta);
    // 判断带有压测标示,是否初始化
    // 初始化
    Connection connection = null;
    /**
     * 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
     * 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
     * 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
     */
    if (mediatorDataSource != null) {
        try {
            connection = mediatorDataSource.getConnection();
        } catch (SQLException e) {
            throw new PressureMeasureError(e);
        }
        return CutOffResult.cutoff(connection);
    } else {
        if (!Pradar.isClusterTest()) {
            return CutOffResult.passed();
        }
        return CutOffResult.cutoff(null);
    }
}
Also used : DbDruidMediatorDataSource(com.pamirs.attach.plugin.alibaba.druid.obj.DbDruidMediatorDataSource) SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Connection(java.sql.Connection) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)

Example 2 with DbDruidMediatorDataSource

use of com.pamirs.attach.plugin.alibaba.druid.obj.DbDruidMediatorDataSource in project LinkAgent by shulieTech.

the class DataSourceWrapUtil method doWrap.

public static DbDruidMediatorDataSource doWrap(DataSourceMeta<DruidDataSource> dataSourceMeta) {
    DbDruidMediatorDataSource cacheValue = pressureDataSources.get(dataSourceMeta);
    if (cacheValue != null) {
        return cacheValue;
    }
    DruidDataSource target = dataSourceMeta.getDataSource();
    if (isPerformanceDataSource(target)) {
        LOGGER.warn("[druid] current datasource is performance datasource. ignore it. url={}, username={}", target.getUrl(), target.getUsername());
        return null;
    }
    boolean infoEnabled = LOGGER.isInfoEnabled();
    if (!DruidDatasourceUtils.configured(target)) {
        // 没有配置对应的影子表或影子库
        LOGGER.error("[druid] No configuration found for datasource, url:{} username:{}", target.getUrl(), target.getUsername());
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + target.getUrl() + " ; username: " + target.getUsername() + "; 中间件类型:druid").report();
        /**
         * 如果未配置,则返回包装的数据源,类似于影子表
         */
        DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (infoEnabled) {
                LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
        return dbMediatorDataSource;
    }
    if (DruidDatasourceUtils.shadowTable(target)) {
        // 影子表
        DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (infoEnabled) {
                LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
        return dbMediatorDataSource;
    } else {
        // 影子库
        // 初始化影子数据源配置
        DbDruidMediatorDataSource dbMediatorDataSource = new DbDruidMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        if (infoEnabled) {
            LOGGER.info("[druid] use db shadow config:{}", GlobalConfig.getInstance().getShadowDatasourceConfigs());
        }
        DruidDataSource ptDataSource = DruidDatasourceUtils.generateDatasourceFromConfiguration(target, GlobalConfig.getInstance().getShadowDatasourceConfigs());
        if (ptDataSource == null) {
            LOGGER.error("[druid] create shadow datasource error. maybe datasource config is not correct, url: {} username:{} configurations:{}", target.getUrl(), target.getUsername(), GlobalConfig.getInstance().getShadowDatasourceConfigs());
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + target.getUrl() + " username: " + target.getUsername()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
            return null;
        }
        dbMediatorDataSource.setDataSourcePerformanceTest(ptDataSource);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (infoEnabled) {
                LOGGER.info("[druid] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
        if (infoEnabled) {
            LOGGER.info("[druid] create shadow datasource success. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getUrl(), target.getUsername(), ptDataSource.getUrl(), ptDataSource.getUsername());
        }
        pressureDatasourceSet.add(ptDataSource);
        return dbMediatorDataSource;
    }
}
Also used : DbMediatorDataSource(com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource) DbDruidMediatorDataSource(com.pamirs.attach.plugin.alibaba.druid.obj.DbDruidMediatorDataSource) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Aggregations

DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 DbDruidMediatorDataSource (com.pamirs.attach.plugin.alibaba.druid.obj.DbDruidMediatorDataSource)2 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