Search in sources :

Example 1 with DataSourceMeta

use of com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta 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 DataSourceMeta

use of com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta in project LinkAgent by shulieTech.

the class DataSourceGetConnectionCutoffInterceptor method cutoff0.

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

Example 3 with DataSourceMeta

use of com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta in project LinkAgent by shulieTech.

the class Neo4jSessionConstructorInterceptor method afterLast.

@Override
public void afterLast(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    /**
     * 压测状态判断
     * 影子session回调
     */
    if (!PradarSwitcher.isClusterTestEnabled() || target instanceof Neo4JSessionExt) {
        return;
    }
    try {
        // 业务库session
        Neo4jSession sourceSession = (Neo4jSession) target;
        DriverConfiguration driverConfiguration = ((HttpDriver) args[1]).getConfiguration();
        String s = driverConfiguration.getURI();
        Credentials credentials = driverConfiguration.getCredentials();
        String username = null;
        if (credentials instanceof AuthTokenCredentials) {
            username = ((AuthTokenCredentials) credentials).credentials();
        } else if (credentials instanceof UsernamePasswordCredentials) {
            username = ((UsernamePasswordCredentials) credentials).getUsername();
        }
        DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta = new DataSourceMeta<Neo4jSession>(s, username, sourceSession);
        if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta) && DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta) != null) {
            // 该业务库数据源已经初始化过影子库
            return;
        }
        if (isPerformanceDataSource(driverConfiguration)) {
            // 业务库配置是已初始化的影子库
            return;
        }
        // 从应用的影子库配置中获取
        String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
        if (!GlobalConfig.getInstance().containsShadowDatabaseConfig(key)) {
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + s + "; 中间件类型:other").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
            return;
        }
        MetaData metaData = (MetaData) args[0];
        DataSourceWrapUtil.metaDataMap.put(sourceSession, metaData);
        DataSourceWrapUtil.wrap(neo4jSessionDataSourceMeta);
    } catch (Throwable e) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + ((HttpDriver) args[1]).getConfiguration().getURI() + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        throw new PressureMeasureError("Neo4J-002:影子库初始化失败:", e);
    }
}
Also used : Neo4JSessionExt(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials) MetaData(org.neo4j.ogm.MetaData) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) HttpDriver(org.neo4j.ogm.drivers.http.driver.HttpDriver) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) Credentials(org.neo4j.ogm.authentication.Credentials) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials)

Example 4 with DataSourceMeta

use of com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta in project LinkAgent by shulieTech.

the class Neo4jSessionOperationCutOffInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) {
    Object[] args = advice.getParameterArray();
    String methodName = advice.getBehaviorName();
    Object target = advice.getTarget();
    /**
     * 压测状态为关闭,如果当前为压测流量则直接报错
     */
    if (!PradarSwitcher.isClusterTestEnabled()) {
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(PradarSwitcher.PRADAR_SWITCHER_OFF + ":" + AppNameUtils.appName());
        }
        return CutOffResult.passed();
    }
    if (!Pradar.isClusterTest()) {
        // 非压测流量
        return CutOffResult.passed();
    }
    if (target instanceof Neo4JSessionExt) {
        // 影子配置session回调
        return CutOffResult.passed();
    }
    // 避免因配置重新生效而导致使用旧配置
    Driver driver1 = Reflect.on(target).get(Neo4JConstants.DYNAMIC_FIELD_DRIVER);
    DriverConfiguration configuration = driver1.getConfiguration();
    String uri = configuration.getURI();
    Credentials credentials = configuration.getCredentials();
    String username = null;
    if (credentials instanceof AuthTokenCredentials) {
        username = ((AuthTokenCredentials) credentials).credentials();
    } else if (credentials instanceof UsernamePasswordCredentials) {
        username = ((UsernamePasswordCredentials) credentials).getUsername();
    }
    DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta = new DataSourceMeta<Neo4jSession>(uri, username, (Neo4jSession) target);
    DataSourceWrapUtil.wrap(neo4jSessionDataSourceMeta);
    // 压测流量转发影子库
    if (!DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta) || null == DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta)) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子库!").setDetail("Neo4J").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        throw new PressureMeasureError("Neo4J-002:影子库配置不存在!");
    }
    DbMediatorDataSource<?> dbMediatorDataSource = DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta);
    Neo4JSessionExt dataSourcePerformanceTest = (Neo4JSessionExt) dbMediatorDataSource.getDataSourcePerformanceTest();
    Operation operation = Neo4JSessionOperation.of(methodName);
    PradarSwitcher.httpPassPrefix.set(dataSourcePerformanceTest.getDriver().getConfiguration().getURI());
    return CutOffResult.cutoff(operation.invoke(dataSourcePerformanceTest, args));
}
Also used : Neo4JSessionExt(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) Driver(org.neo4j.ogm.driver.Driver) Operation(com.pamirs.attach.plugin.neo4j.operation.Operation) Neo4JSessionOperation(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionOperation) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) Credentials(org.neo4j.ogm.authentication.Credentials) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials)

Example 5 with DataSourceMeta

use of com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta in project LinkAgent by shulieTech.

the class DataSourceGetConnectionArgsCutoffInterceptor method cutoff0.

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

Aggregations

DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)16 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)14 Connection (java.sql.Connection)12 SQLException (java.sql.SQLException)12 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 AtomikosDataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource)2 AtomikosNonXADataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource)2 C3p0MediaDataSource (com.pamirs.attach.plugin.c3p0.utils.C3p0MediaDataSource)2 DbcpMediaDataSource (com.pamirs.attach.plugin.dbcp2.utils.DbcpMediaDataSource)2 Neo4JSessionExt (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 AuthTokenCredentials (org.neo4j.ogm.authentication.AuthTokenCredentials)2 Credentials (org.neo4j.ogm.authentication.Credentials)2 UsernamePasswordCredentials (org.neo4j.ogm.authentication.UsernamePasswordCredentials)2 DriverConfiguration (org.neo4j.ogm.config.DriverConfiguration)2 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 AtomikosDataSourceBean (com.atomikos.jdbc.AtomikosDataSourceBean)1