Search in sources :

Example 1 with TomcatJdbcMediatorDataSource

use of com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource in project LinkAgent by shulieTech.

the class DataSourceWrapUtil method doWrap.

public static void doWrap(DataSourceMeta<DataSource> dataSourceMeta) {
    DataSource target = dataSourceMeta.getDataSource();
    if (pressureDataSources.containsKey(dataSourceMeta) && pressureDataSources.get(dataSourceMeta) != null) {
        return;
    }
    if (isPerformanceDataSource(target)) {
        return;
    }
    if (!TomcatJdbcDatasourceUtils.configured(target)) {
        // 没有配置对应的影子表或影子库
        LOGGER.error("[tomcat-jdbc] 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() + "; 中间件类型:other").report();
        TomcatJdbcMediatorDataSource dbMediatorDataSource = new TomcatJdbcMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("[tomcat-jdbc] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
        return;
    }
    if (TomcatJdbcDatasourceUtils.shadowTable(target)) {
        // 影子表
        TomcatJdbcMediatorDataSource dbMediatorDataSource = new TomcatJdbcMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("[tomcat-jdbc] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
    } else {
        // 影子库
        // 初始化影子数据源配置
        TomcatJdbcMediatorDataSource dbMediatorDataSource = new TomcatJdbcMediatorDataSource();
        dbMediatorDataSource.setDataSourceBusiness(target);
        DataSource ptDataSource = TomcatJdbcDatasourceUtils.generateDatasourceFromConfiguration(target, GlobalConfig.getInstance().getShadowDatasourceConfigs());
        if (ptDataSource == null) {
            LOGGER.error("[tomcat-jdbc] Configuration error for datasource, url: {} username:{} configurations:{}", target.getUrl(), target.getUsername(), GlobalConfig.getInstance().getShadowDatasourceConfigs());
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("TomcatJdbcDataSourceWrapUtil:业务库配置:::url: " + target.getUrl() + "; username: " + target.getUsername() + "; 中间件类型:other").report();
            return;
        }
        dbMediatorDataSource.setDataSourcePerformanceTest(ptDataSource);
        DbMediatorDataSource old = pressureDataSources.put(dataSourceMeta, dbMediatorDataSource);
        if (old != null) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("[tomcat-jdbc] destroyed shadow table datasource success. url:{} ,username:{}", target.getUrl(), target.getUsername());
            }
            old.close();
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("[tomcat-jdbc] create shadow datasource success. target:{} url:{} ,username:{} shadow-url:{},shadow-username:{}", target.hashCode(), target.getUrl(), target.getUsername(), ptDataSource.getUrl(), ptDataSource.getUsername());
        }
    }
}
Also used : DbMediatorDataSource(com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource) TomcatJdbcMediatorDataSource(com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource) DbMediatorDataSource(com.pamirs.pradar.pressurement.datasource.DbMediatorDataSource) DataSource(org.apache.tomcat.jdbc.pool.DataSource) TomcatJdbcMediatorDataSource(com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource)

Example 2 with TomcatJdbcMediatorDataSource

use of com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource in project LinkAgent by shulieTech.

the class TomcatJdbcDataSourceProxyGetConnectionInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) {
    Object target = advice.getTarget();
    DataSource dataSource = (DataSource) target;
    DataSourceMeta<DataSource> dataSourceMeta = new DataSourceMeta<DataSource>(dataSource.getUrl(), dataSource.getUsername(), dataSource);
    /**
     * 压测状态为关闭,如果当前为压测流量则直接报错
     */
    ClusterTestUtils.validateClusterTest();
    DataSourceWrapUtil.doWrap(dataSourceMeta);
    Connection connection = null;
    /**
     * 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
     * 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
     * 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
     */
    if (DataSourceWrapUtil.pressureDataSources.containsKey(dataSourceMeta)) {
        TomcatJdbcMediatorDataSource mediatorDataSource = DataSourceWrapUtil.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 : TomcatJdbcMediatorDataSource(com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource) SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Connection(java.sql.Connection) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) DataSource(org.apache.tomcat.jdbc.pool.DataSource) TomcatJdbcMediatorDataSource(com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource)

Aggregations

TomcatJdbcMediatorDataSource (com.pamirs.attach.plugin.apache.tomcatjdbc.obj.TomcatJdbcMediatorDataSource)2 DataSource (org.apache.tomcat.jdbc.pool.DataSource)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