Search in sources :

Example 1 with AtomikosNonXADataSourceBeanMediaDataSource

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

the class DataSourceGetConnectionCutoffInterceptor method wrapAtomikosNonXADataSourceBean.

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

Example 2 with AtomikosNonXADataSourceBeanMediaDataSource

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

AtomikosNonXADataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosNonXADataSourceBeanMediaDataSource)2 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)2 AtomikosNonXADataSourceBean (com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean)1 AtomikosDataSourceBeanMediaDataSource (com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource)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