Search in sources :

Example 26 with ShadowDatabaseConfig

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

the class DatabaseUtils method isTestTable.

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

Example 27 with ShadowDatabaseConfig

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

the class DelegateOperationExecutorInterceptor method getShadowDatabaseConfig.

private ShadowDatabaseConfig getShadowDatabaseConfig(Object mongoClientDelegate) {
    ClusterSettings clusterSettings = ((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings();
    List<ServerAddress> serverAddresses = clusterSettings.getHosts();
    ShadowDatabaseConfig shadowDatabaseConfig = null;
    for (ShadowDatabaseConfig config : GlobalConfig.getInstance().getShadowDatasourceConfigs().values()) {
        for (ServerAddress serverAddress : serverAddresses) {
            if (config.getUrl().contains(serverAddress.toString())) {
                shadowDatabaseConfig = config;
                break;
            }
        }
    }
    return shadowDatabaseConfig;
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.internal.connection.Cluster) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 28 with ShadowDatabaseConfig

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

the class DelegateOperationExecutorInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) throws Throwable {
    if (!Pradar.isClusterTest()) {
        return CutOffResult.passed();
    }
    // 影子库重入
    if (manager.getDynamicField(advice.getTarget(), "isCluster") != null) {
        return CutOffResult.passed();
    }
    Object[] args = advice.getParameterArray();
    Class operationClass = args[0].getClass();
    Object operation = args[0];
    OperationAccessor operationAccessor = OperationAccessorFactory.getOperationAccessor(operationClass);
    if (operationAccessor == null) {
        LOGGER.error("not support operation class is {} ", args[0].getClass().getName());
        throw new PressureMeasureError("mongo not support pressure operation class is " + args[0].getClass().getName());
    }
    Object mongoClientDelegate = Caches.getMongoClientDelegate(advice.getTarget());
    ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(mongoClientDelegate);
    if (shadowDatabaseConfig == null) {
        if (operationAccessor.isRead()) {
            return CutOffResult.passed();
        } else {
            MongoNamespace busMongoNamespace = operationAccessor.getMongoNamespace(operation);
            ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0005").setMessage("mongodb 不存在对应影子表或影子库:" + busMongoNamespace.getFullName()).setDetail("mongodb " + StringUtils.join(((Cluster) (ReflectionUtils.getFieldValue(mongoClientDelegate, "cluster"))).getSettings().getHosts(), ",") + "不存在对应影子表或影子库:" + busMongoNamespace.getFullName());
            error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
            error.report();
            throw new PressureMeasureError("mongo 对应影子表或影子库:" + busMongoNamespace.getFullName());
        }
    }
    if (shadowDatabaseConfig.isShadowTable()) {
        doShadowTable(operation, operationAccessor, shadowDatabaseConfig);
        return CutOffResult.passed();
    } else {
        return CutOffResult.cutoff(doShadowDb(args, operationAccessor, shadowDatabaseConfig));
    }
}
Also used : ErrorReporter(com.pamirs.pradar.pressurement.agent.shared.service.ErrorReporter) OperationAccessor(com.pamirs.attach.plugin.mongodb.utils.OperationAccessor) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Cluster(com.mongodb.internal.connection.Cluster) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) MongoNamespace(com.mongodb.MongoNamespace)

Example 29 with ShadowDatabaseConfig

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

the class DataSourceWrapUtil method wrap.

public static void wrap(DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta) {
    if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta)) {
        return;
    }
    // 生成影子库配置
    // 初始化影子库配置
    DriverConfiguration sourceConfig = Components.driver().getConfiguration();
    Configuration shadowConfig = new Configuration();
    DriverConfiguration configuration = new DriverConfiguration(shadowConfig);
    configuration.setDriverClassName(sourceConfig.getDriverClassName());
    configuration.setConnectionPoolSize(sourceConfig.getConnectionPoolSize());
    configuration.setEncryptionLevel(sourceConfig.getEncryptionLevel());
    configuration.setTrustCertFile(sourceConfig.getTrustCertFile());
    configuration.setTrustStrategy(sourceConfig.getTrustStrategy());
    String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
    ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
    if (null == shadowDatabaseConfig) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + neo4jSessionDataSourceMeta.getUrl()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        return;
    }
    configuration.setURI(shadowDatabaseConfig.getShadowUrl());
    configuration.setCredentials(shadowDatabaseConfig.getShadowUsername(), shadowDatabaseConfig.getShadowPassword());
    Driver shadowDriver = DriverService.load(configuration);
    // 生成影子库session
    Neo4jSession sourceSession = neo4jSessionDataSourceMeta.getDataSource();
    Neo4JSessionExt shadowSession = new Neo4JSessionExt(metaDataMap.get(sourceSession), shadowDriver);
    // 放入本地
    DbMediatorDataSource<Neo4jSession> driverConfigurationDbMediatorDataSource = new DriverConfig();
    driverConfigurationDbMediatorDataSource.setDataSourceBusiness(sourceSession);
    driverConfigurationDbMediatorDataSource.setDataSourcePerformanceTest(shadowSession);
    DataSourceWrapUtil.pressureDataSources.put(neo4jSessionDataSourceMeta, driverConfigurationDbMediatorDataSource);
}
Also used : Neo4JSessionExt(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) Configuration(org.neo4j.ogm.config.Configuration) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Driver(org.neo4j.ogm.driver.Driver) DriverConfig(com.pamirs.attach.plugin.neo4j.config.DriverConfig) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 30 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(BasicDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
    BasicDataSource 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) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

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