Search in sources :

Example 1 with AtomikosDataSourceBeanMediaDataSource

use of com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource in project LinkAgent by shulieTech.

the class DataSourceGetConnectionCutoffInterceptor method wrapAtomikosDataSourceBean.

private CutOffResult wrapAtomikosDataSourceBean(AtomikosDataSourceBean dataSourceBean) {
    DataSourceMeta<AtomikosDataSourceBean> dataSourceMeta = new DataSourceMeta<AtomikosDataSourceBean>(getUrl(dataSourceBean), getUsername(dataSourceBean), dataSourceBean);
    AtomikosDataSourceBeanWrapUtil.init(dataSourceMeta);
    Connection connection = null;
    /**
     * 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
     * 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
     * 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
     */
    if (AtomikosDataSourceBeanWrapUtil.pressureDataSources.containsKey(dataSourceMeta)) {
        AtomikosDataSourceBeanMediaDataSource mediatorDataSource = AtomikosDataSourceBeanWrapUtil.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 : AtomikosDataSourceBeanMediaDataSource(com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource) SQLException(java.sql.SQLException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Connection(java.sql.Connection) AtomikosDataSourceBean(com.atomikos.jdbc.AtomikosDataSourceBean) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)

Example 2 with AtomikosDataSourceBeanMediaDataSource

use of com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource 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)

Aggregations

AtomikosDataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource)2 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)2 AtomikosDataSourceBean (com.atomikos.jdbc.AtomikosDataSourceBean)1 AtomikosNonXADataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)1 IEvent (com.pamirs.pradar.pressurement.agent.event.IEvent)1 ClusterTestSwitchOffEvent (com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)1 ShadowDataSourceConfigModifyEvent (com.pamirs.pradar.pressurement.agent.event.impl.ShadowDataSourceConfigModifyEvent)1 EventResult (com.pamirs.pradar.pressurement.agent.listener.EventResult)1 PradarEventListener (com.pamirs.pradar.pressurement.agent.listener.PradarEventListener)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1