Search in sources :

Example 31 with ShadowDatabaseConfig

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

the class DataSourceGetConnectionCutoffInterceptor method addListener.

private void addListener() {
    EventRouter.router().addListener(new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (!(event instanceof ClusterTestSwitchOffEvent)) {
                return EventResult.IGNORE;
            }
            // 关闭压测数据源
            AtomikosDataSourceBeanWrapUtil.destroy();
            AtomikosNonXADataSourceBeanWrapUtil.destroy();
            return EventResult.success("tomcat-jdbc-plugin");
        }

        @Override
        public int order() {
            return 8;
        }
    }).addListener(new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (!(event instanceof ShadowDataSourceConfigModifyEvent)) {
                return EventResult.IGNORE;
            }
            ShadowDataSourceConfigModifyEvent shadowDataSourceConfigModifyEvent = (ShadowDataSourceConfigModifyEvent) event;
            Set<ShadowDatabaseConfig> target = shadowDataSourceConfigModifyEvent.getTarget();
            if (null == target || target.size() == 0) {
                return EventResult.IGNORE;
            }
            for (ShadowDatabaseConfig config : target) {
                Iterator<Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource>> it = AtomikosDataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource> entry = it.next();
                    if (StringUtils.equalsIgnoreCase(DbUrlUtils.getKey(config.getUrl(), config.getUsername()), DbUrlUtils.getKey(entry.getKey().getUrl(), entry.getKey().getUsername()))) {
                        AtomikosDataSourceBeanMediaDataSource value = entry.getValue();
                        it.remove();
                        try {
                            value.close();
                            if (logger.isInfoEnabled()) {
                                logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
                            }
                        } catch (Throwable e) {
                            logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
                        }
                        break;
                    }
                }
                Iterator<Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource>> nonIt = AtomikosNonXADataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
                while (nonIt.hasNext()) {
                    Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource> entry = nonIt.next();
                    if (StringUtils.equalsIgnoreCase(config.getUrl(), entry.getKey().getUrl()) && StringUtils.equalsIgnoreCase(config.getUsername(), entry.getKey().getUsername())) {
                        AtomikosNonXADataSourceBeanMediaDataSource value = entry.getValue();
                        nonIt.remove();
                        try {
                            value.close();
                            if (logger.isInfoEnabled()) {
                                logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
                            }
                        } catch (Throwable e) {
                            logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
                        }
                        break;
                    }
                }
            }
            return EventResult.success("module-atomikos-datasource: destroyed shadow table datasource success.");
        }

        @Override
        public int order() {
            return 2;
        }
    });
}
Also used : AtomikosDataSourceBeanMediaDataSource(com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource) Set(java.util.Set) IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) ShadowDataSourceConfigModifyEvent(com.pamirs.pradar.pressurement.agent.event.impl.ShadowDataSourceConfigModifyEvent) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) AtomikosNonXADataSourceBeanMediaDataSource(com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource) PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) EventResult(com.pamirs.pradar.pressurement.agent.listener.EventResult) Iterator(java.util.Iterator) Map(java.util.Map) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)

Example 32 with ShadowDatabaseConfig

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

the class DelegateOperationExecutorInterceptor method getShadowDatabaseConfig.

private ShadowDatabaseConfig getShadowDatabaseConfig(MongoClientDelegate mongoClientDelegate) {
    ClusterSettings clusterSettings = mongoClientDelegate.getCluster().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) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 33 with ShadowDatabaseConfig

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

the class MongoDBMongoClientDelegateConstructorInterceptor method check.

/**
 * 判断是否影子库
 */
private void check(Object baseCluster, Mongo businessMongo) {
    List<ServerAddress> addressList;
    if (baseCluster instanceof SingleServerCluster) {
        addressList = ((SingleServerCluster) baseCluster).getSettings().getHosts();
    } else {
        addressList = ((MultiServerCluster) baseCluster).getSettings().getHosts();
    }
    for (Map.Entry<String, ShadowDatabaseConfig> entry : GlobalConfig.getInstance().getShadowDatasourceConfigs().entrySet()) {
        String businessUrl = entry.getValue().getUrl();
        for (ServerAddress address : addressList) {
            if (businessUrl != null && businessUrl.contains(address.toString())) {
                if (!MongoClientHolder.mongoClientMap.containsKey(address.toString())) {
                    MongoClientHolder.mongoIntegerMap.put(businessMongo, 2);
                    break;
                } else {
                    throw new PressureMeasureError("配置了相同地址的的影子库!");
                }
            }
        }
    }
    for (ServerAddress address : addressList) {
        ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(address.toString());
        if (shadowDatabaseConfig == null) {
            continue;
        }
        if (!shadowDatabaseConfig.isShadowTable()) {
            continue;
        }
        Map<String, String> map = shadowDatabaseConfig.getBusinessShadowTables();
        if (null != map && map.size() > 0) {
            MongoClientHolder.mongoIntegerMap.put(businessMongo, 1);
            MongoClientHolder.mongoTableMappingMap.put(businessMongo, map);
            break;
        }
    }
}
Also used : SingleServerCluster(com.mongodb.internal.connection.SingleServerCluster) MultiServerCluster(com.mongodb.internal.connection.MultiServerCluster) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) ServerAddress(com.mongodb.ServerAddress) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig) Map(java.util.Map)

Example 34 with ShadowDatabaseConfig

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

the class MongoExecuteCutoffInterceptor method cutoff0.

@Override
public CutOffResult cutoff0(Advice advice) throws Throwable {
    if (!Pradar.isClusterTest()) {
        return CutOffResult.passed();
    }
    if (ClientManagerUtils.getBusClient2ptClientMapping().containsValue(advice.getTarget())) {
        return CutOffResult.passed();
    }
    List<ServerAddress> serverAddresses = ((MongoClient) advice.getTarget()).getAllAddress();
    ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(serverAddresses);
    if (shadowDatabaseConfig == null) {
        ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0005").setMessage("mongo 未配置对应影子表").setDetail("mongo 未配置对应影子表");
        error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
        error.report();
        throw new PressureMeasureError("mongo 未配置对应影子表");
    }
    if (!shadowDatabaseConfig.isShadowDatabase()) {
        return CutOffResult.passed();
    }
    Mongo ptMongo = ClientManagerUtils.getBusClient2ptClientMapping().get(advice.getTarget());
    if (ptMongo == null) {
        return CutOffResult.passed();
    }
    if (advice.getParameterArray().length == 1) {
        return CutOffResult.cutoff(writeMethod.invoke(ptMongo, advice.getParameterArray()));
    } else {
        return CutOffResult.cutoff(readMethod.invoke(ptMongo, advice.getParameterArray()));
    }
}
Also used : MongoClient(com.mongodb.MongoClient) ErrorReporter(com.pamirs.pradar.pressurement.agent.shared.service.ErrorReporter) Mongo(com.mongodb.Mongo) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) ServerAddress(com.mongodb.ServerAddress) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

Example 35 with ShadowDatabaseConfig

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

the class ProxoolDriverConnectInterceptor method getParameter0.

@Override
public Object[] getParameter0(Advice advice) {
    if (!Pradar.isClusterTest()) {
        return advice.getParameterArray();
    }
    // proxool.ds
    String connectUrl = (String) advice.getParameterArray()[0];
    if (!connectUrl.startsWith("proxool")) {
        return advice.getParameterArray();
    }
    String alias = getAlias(connectUrl);
    // 说明url有问题
    if (alias == null || alias.length() == 0) {
        return advice.getParameterArray();
    }
    Object[] args = advice.getParameterArray();
    if (ConnectionPoolUtils.getConnectionPoolMap().containsKey(Pradar.CLUSTER_TEST_PREFIX + alias)) {
        args[0] = "proxool." + Pradar.CLUSTER_TEST_PREFIX + alias;
        return args;
    }
    String username = null;
    String url = null;
    Properties definitionProperties = null;
    try {
        if (getDefinitionMethod == null) {
            getDefinitionMethod = ConnectionPoolUtils.getConnectionPoolMap().get(alias).getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_DEFINITION_METHOD);
            getDefinitionMethod.setAccessible(true);
        }
        Object definition = getDefinitionMethod.invoke(ConnectionPoolUtils.getConnectionPoolMap().get(alias));
        getUrlMethod = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_URL_METHOD);
        getUserMethod = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_USER_METHOD);
        Method getCompleteInfo = definition.getClass().getDeclaredMethod(ProxoolConst.REFLECT_GET_COMPLETE_INFO_METHOD);
        getUrlMethod.setAccessible(true);
        getUserMethod.setAccessible(true);
        getCompleteInfo.setAccessible(true);
        url = (String) getUrlMethod.invoke(definition);
        username = (String) getUserMethod.invoke(definition);
        definitionProperties = (Properties) getCompleteInfo.invoke(definition);
    } catch (Exception e) {
        logger.error("proxool init getDefinitionMethod and definition error.", e);
    }
    ShadowDatabaseConfig shadowDatabaseConfig = getShadowDatabaseConfig(url, username);
    if (shadowDatabaseConfig == null) {
        // 未配置影子库表
        throw new PressureMeasureError("未配置影子库表: " + url + " " + username);
    }
    // 影子表 直接返回
    if (shadowDatabaseConfig.isShadowTable()) {
        return args;
    }
    StringBuffer ptUrl = new StringBuffer();
    ptUrl.append("proxool");
    ptUrl.append(ProxoolConstants.ALIAS_DELIMITER);
    ptUrl.append(Pradar.CLUSTER_TEST_PREFIX + alias);
    ptUrl.append(ProxoolConstants.URL_DELIMITER);
    ptUrl.append(shadowDatabaseConfig.getShadowDriverClassName());
    ptUrl.append(ProxoolConstants.URL_DELIMITER);
    ptUrl.append(shadowDatabaseConfig.getShadowUrl());
    try {
        Class connectionPoolDefinitionClass = Thread.currentThread().getContextClassLoader().loadClass("org.logicalcobwebs.proxool.ConnectionPoolDefinition");
        Constructor connectionPoolDefinitionOfConstructor = connectionPoolDefinitionClass.getDeclaredConstructor(String.class, Properties.class, boolean.class);
        connectionPoolDefinitionOfConstructor.setAccessible(true);
        Object ptConnectionPoolDefinitionOfInstance = connectionPoolDefinitionOfConstructor.newInstance(ptUrl.toString(), buildPtProperties(definitionProperties, shadowDatabaseConfig), true);
        Class connectionPoolClass = Thread.currentThread().getContextClassLoader().loadClass("org.logicalcobwebs.proxool.ConnectionPool");
        Constructor connectionPoolOfConstructor = connectionPoolClass.getDeclaredConstructors()[0];
        connectionPoolOfConstructor.setAccessible(true);
        Object connectionPoolOfInstance = connectionPoolOfConstructor.newInstance(ptConnectionPoolDefinitionOfInstance);
        Method startMethod = connectionPoolClass.getDeclaredMethod("start");
        startMethod.setAccessible(true);
        startMethod.invoke(connectionPoolOfInstance);
        ConnectionPoolUtils.getConnectionPoolMap().put(Pradar.CLUSTER_TEST_PREFIX + alias, connectionPoolOfInstance);
        ConnectionPoolUtils.getConnectionPools().add(connectionPoolOfInstance);
    } catch (Exception e) {
        logger.error("proxool init ConnectionPool error.", e);
    }
    args[0] = "proxool." + Pradar.CLUSTER_TEST_PREFIX + alias;
    return args;
}
Also used : PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Properties(java.util.Properties) ShadowDatabaseConfig(com.pamirs.pradar.internal.config.ShadowDatabaseConfig)

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