Search in sources :

Example 1 with ClusterTestSwitchOnEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent in project LinkAgent by shulieTech.

the class ShadowJobPlugin method onActive.

@Override
public boolean onActive() throws Throwable {
    final ShadowImplListener shaDowImplListener = new ShadowImplListener();
    try {
        XxlJobAdapter xxlJobAdapter = new XxlJobAdapter();
        GlobalConfig.getInstance().addJobAdaptor(xxlJobAdapter.getJobName(), xxlJobAdapter);
    } catch (Throwable t) {
    // ignore
    }
    EventRouter.router().addListener(shaDowImplListener);
    EventRouter.router().addListener(new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (event instanceof ClusterTestSwitchOnEvent) {
            } else if (event instanceof ClusterTestSwitchOffEvent) {
                String message = shaDowImplListener.disableAll();
                if ("".equals(message)) {
                    return EventResult.success("shadow-job");
                }
                return EventResult.error("shadow-job", message);
            }
            return EventResult.IGNORE;
        }

        @Override
        public int order() {
            return 6;
        }
    });
    enhanceTemplate.enhance(this, "org.activiti.engine.impl.cmd.AcquireAsyncJobsDueCmd", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod constructor = target.getConstructor("org.activiti.engine.impl.asyncexecutor.AsyncExecutor");
            constructor.addInterceptor(Listeners.of(AcquireAsyncJobsDueCmdInterceptor.class));
        }
    });
    enhanceTemplate.enhance(this, "org.quartz.core.QuartzScheduler", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod constructor = target.getConstructors();
            constructor.addInterceptor(Listeners.of(QuartzInitAdapterInterceptor.class));
        }
    });
    // enhanceTemplate.enhance(this, "com.dangdang.ddframe.job.lite.api.JobScheduler", new EnhanceCallback() {
    // @Override
    // public void doEnhance(InstrumentClass target) {
    // InstrumentMethod constructor = target.getConstructors();
    // constructor.addInterceptor(Listeners.of(ElasticInitAdapterInterceptor.class));
    // }
    // });
    enhanceTemplate.enhance(this, "com.xxl.job.core.executor.XxlJobExecutor", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod declaredMethod = target.getDeclaredMethod("start");
            declaredMethod.addInterceptor(Listeners.of(XxlInitAdapterInterceptor.class));
        }
    });
    // enhanceTemplate.enhance(this, "org.springframework.context.support.ApplicationContextAwareProcessor", new EnhanceCallback() {
    // @Override
    // public void doEnhance(InstrumentClass target) {
    // InstrumentMethod getMethod = target.getDeclaredMethod("invokeAwareInterfaces", "java.lang.Object");
    // getMethod.addInterceptor(Listeners.of(SpringContextInterceptor.class));
    // }
    // });
    enhanceTemplate.enhance(this, "org.springframework.context.support.AbstractRefreshableApplicationContext", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getDeclaredMethod("refreshBeanFactory");
            getMethod.addInterceptor(Listeners.of(SpringContextInterceptor.class));
        }
    });
    /**
     * com.github.ltsopensource.spring.tasktracker.JobRunnerHolder
     * lts spring任务注册切点
     */
    enhanceTemplate.enhance(this, "com.github.ltsopensource.spring.tasktracker.JobRunnerHolder", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getDeclaredMethod("add", "java.lang.String", "com.github.ltsopensource.tasktracker.runner.JobRunner");
            getMethod.addInterceptor(Listeners.of(JobRunnerHolderAddInterceptor.class));
        }
    });
    /**
     * com.github.ltsopensource.spring.tasktracker.JobRunnerHolder
     * lts spring任务注册切点
     */
    enhanceTemplate.enhance(this, "com.github.ltsopensource.jobtracker.support.JobReceiver", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getConstructor("com.github.ltsopensource.jobtracker.domain.JobTrackerAppContext");
            getMethod.addInterceptor(Listeners.of(LtsJobReceiverInterceptor.class));
        }
    });
    /**
     * com.github.ltsopensource.spring.tasktracker.JobRunnerHolder
     * lts taskTracker切点
     */
    enhanceTemplate.enhance(this, "com.github.ltsopensource.tasktracker.TaskTracker", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod constructor = target.getConstructors();
            constructor.addInterceptor(Listeners.of(LtsInitAdapterInterceptor.class));
        }
    });
    // enhanceTemplate.enhance(this, "org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor", new EnhanceCallback() {
    // @Override
    // public void doEnhance(InstrumentClass target) {
    // InstrumentMethod getMethod = target.getDeclaredMethod("processScheduled",
    // "org.springframework.scheduling.annotation.Scheduled", "java.lang.reflect.Method", "java.lang.Object");
    // getMethod.addInterceptor(Listeners.of(ScheduleJobInitAdapterInterceptor.class));
    // }
    // });
    enhanceTemplate.enhance(this, "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            // springmvc 5.x版本
            InstrumentMethod getMethod1 = target.getDeclaredMethod("invokeHandlerMethod", "javax.servlet.http.HttpServletRequest", "javax.servlet.http.HttpServletResponse", "org.springframework.web.method.HandlerMethod");
            getMethod1.addInterceptor(Listeners.of(RequestMappingHandlerAdapterInterceptor.class));
            // springmvc 4.x版本
            InstrumentMethod getMethod2 = target.getDeclaredMethod("invokeHandleMethod", "javax.servlet.http.HttpServletRequest", "javax.servlet.http.HttpServletResponse", "org.springframework.web.method.HandlerMethod");
            getMethod2.addInterceptor(Listeners.of(RequestMappingHandlerAdapterInterceptor.class));
        }
    });
    // 新增对 spring bean 的调用拦截获取 BeanFactory,通过拦截器 TransactionInterceptor 获取到 BeanFactory
    enhanceTemplate.enhance(this, "org.springframework.aop.framework.ReflectiveMethodInvocation", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod method = target.getDeclaredMethod("proceed");
            method.addInterceptor(Listeners.of(ReflectiveMethodInvocationProceedInterceptor.class));
        }
    });
    enhanceTemplate.enhance(this, "com.xxl.job.core.handler.impl.MethodJobHandler", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getDeclaredMethod("execute", "java.lang.String");
            getMethod.addInterceptor(Listeners.of(MethodJobHandlerExecuteInterceptor.class));
        }
    });
    enhanceTemplate.enhance(this, "com.dangdang.ddframe.job.executor.JobExecutorFactory", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getDeclaredMethod("getJobExecutor", "com.dangdang.ddframe.job.api.ElasticJob", "com.dangdang.ddframe.job.executor.JobFacade");
            getMethod.addInterceptor(Listeners.of(JobExecutorFactoryGetJobExecutorInterceptor.class));
        }
    });
    enhanceTemplate.enhance(this, "org.springframework.scheduling.support.ScheduledMethodRunnable", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod getMethod = target.getDeclaredMethod("run");
            getMethod.addInterceptor(Listeners.of(ScheduledMethodRunnableRunInterceptor.class));
        }
    });
    // 
    // enhanceTemplate.enhance(this, "org.springframework.scheduling.quartz.QuartzJobBean", new EnhanceCallback() {
    // @Override
    // public void doEnhance(InstrumentClass target) {
    // InstrumentMethod getMethod = target.getDeclaredMethod("execute", "org.quartz.JobExecutionContext");
    // getMethod.addInterceptor(Listeners.of(QuartzJobBeanExecuteInterceptor.class));
    // }
    // });
    enhanceTemplate.enhance(this, "org.quartz.JobExecutionContext", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            InstrumentMethod constructor = target.getConstructor("org.quartz.Scheduler", "org.quartz.spi.TriggerFiredBundle", "org.quartz.Job");
            constructor.addInterceptor(Listeners.of(JobExecutionContextInterceptor.class));
        }
    });
    // org.quartz.core.JobRunShell.initialize quartz2.x
    enhanceTemplate.enhance(this, "org.quartz.core.JobRunShell", new EnhanceCallback() {

        @Override
        public void doEnhance(InstrumentClass target) {
            // 2.x
            InstrumentMethod declaredMethod_1 = target.getDeclaredMethod("initialize", "org.quartz.core.QuartzScheduler");
            if (declaredMethod_1 != null) {
                declaredMethod_1.addInterceptor(Listeners.of(JobRunShellInitializeInterceptor.class));
            }
            // 1.x
            InstrumentMethod declaredMethod_2 = target.getDeclaredMethod("initialize", "org.quartz.core.QuartzScheduler", "org.quartz.spi.TriggerFiredBundle");
            if (declaredMethod_2 != null) {
                declaredMethod_2.addInterceptor(Listeners.of(JobRunShellInitializeInterceptor_1.class));
            }
        }
    });
    try {
        Class.forName("com.dangdang.ddframe.job.spring.schedule.SpringJobScheduler");
        PradarSpringUtil.onApplicationContextLoad(new Runnable() {

            @Override
            public void run() {
                ElasticJobRegisterUtil.addShadowJob();
            }
        });
    } catch (ClassNotFoundException e) {
    // do nothing
    }
    return true;
}
Also used : IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) InstrumentClass(com.shulie.instrument.simulator.api.instrument.InstrumentClass) InstrumentMethod(com.shulie.instrument.simulator.api.instrument.InstrumentMethod) ShadowImplListener(com.pamirs.pradar.pressurement.agent.listener.impl.ShadowImplListener) EnhanceCallback(com.shulie.instrument.simulator.api.instrument.EnhanceCallback) PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) EventResult(com.pamirs.pradar.pressurement.agent.listener.EventResult) XxlJobAdapter(com.pamirs.attach.plugin.shadowjob.adapter.XxlJobAdapter) ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)

Example 2 with ClusterTestSwitchOnEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent in project LinkAgent by shulieTech.

the class ConsumerRegistry method addListener.

private static void addListener(final DefaultMQPushConsumer businessConsumer) {
    final RebalanceImpl rebalance = businessConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl();
    final PradarEventListener listener = new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (event instanceof ClusterTestSwitchOnEvent) {
                try {
                    // 取出配置创建影子消费者
                    DefaultMQPushConsumer defaultMQPushConsumer = buildMQPushConsumer(businessConsumer);
                    if (defaultMQPushConsumer != null) {
                        defaultMQPushConsumer.start();
                    }
                } catch (MQClientException e) {
                    logger.error(e.getMessage());
                    return EventResult.error("apache-rocketmq-plugin-open", "Apache-RocketMQ PT Consumer start failed: " + e.getMessage());
                }
                return EventResult.success("apache-rocketmq-plugin-open");
            } else if (event instanceof ClusterTestSwitchOffEvent || event instanceof SilenceSwitchOnEvent) {
                return shutdownShadowConsumer(businessConsumer);
            } else if (event instanceof ShadowConsumerDisableEvent) {
                String group = businessConsumer.getConsumerGroup();
                Set<String> topics = rebalance.getSubscriptionInner().keySet();
                for (String topic : topics) {
                    List<ShadowConsumerDisableInfo> disableInfos = ((ShadowConsumerDisableEvent) event).getTarget();
                    for (ShadowConsumerDisableInfo disableInfo : disableInfos) {
                        if (topic.equals(disableInfo.getTopic()) && group.equals(disableInfo.getConsumerGroup())) {
                            return shutdownShadowConsumer(businessConsumer);
                        }
                    }
                }
            }
            return EventResult.IGNORE;
        }

        @Override
        public int order() {
            return 4;
        }
    };
    PradarEventListener old = listeners.putIfAbsent(businessConsumer, listener);
    if (old == null) {
        EventRouter.router().addListener(listener);
    }
}
Also used : IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) SilenceSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.SilenceSwitchOnEvent) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ShadowConsumerDisableEvent(com.pamirs.pradar.pressurement.agent.event.impl.ShadowConsumerDisableEvent) ShadowConsumerDisableInfo(com.pamirs.pradar.pressurement.agent.listener.model.ShadowConsumerDisableInfo) PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent) MQClientException(org.apache.rocketmq.client.exception.MQClientException) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)

Example 3 with ClusterTestSwitchOnEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent in project LinkAgent by shulieTech.

the class GlobalSwitch method compareIsChangeAndSet.

@Override
public Boolean compareIsChangeAndSet(ClusterTestConfig config, Boolean newValue) {
    boolean clusterTestSwitchOn = PradarSwitcher.clusterTestSwitchOn();
    if (ObjectUtils.equals(newValue, clusterTestSwitchOn)) {
        return Boolean.FALSE;
    }
    // 变更后配置更新到内存
    if (newValue) {
        ClusterTestSwitchOnEvent event = new ClusterTestSwitchOnEvent(this);
        EventRouter.router().publish(event);
        PradarSwitcher.turnClusterTestSwitchOn();
        config.setGlobalSwitchOn(true);
    } else {
        ClusterTestSwitchOffEvent event = new ClusterTestSwitchOffEvent(this);
        EventRouter.router().publish(event);
        PradarSwitcher.turnClusterTestSwitchOff();
        config.setGlobalSwitchOn(false);
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("publish global switch config successful. config={}", newValue);
    }
    return Boolean.TRUE;
}
Also used : ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)

Example 4 with ClusterTestSwitchOnEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent in project LinkAgent by shulieTech.

the class ClusterTestConfigHttpResolver method pradarSwitchOpen.

/**
 * 压测开关开启,执行开关前,判断开关执行状态
 */
private void pradarSwitchOpen() {
    try {
        ErrorReporter.getInstance().clear();
        ClusterTestSwitchOnEvent event = new ClusterTestSwitchOnEvent(this);
        boolean isSuccess = EventRouter.router().publish(event);
        if (!isSuccess) {
            PradarSwitcher.turnClusterTestSwitchOff();
        }
    } catch (Throwable e) {
        LOGGER.warn("SIMULATOR: [FetchConfig] switch do opened has error", e);
        /**
         * 将开关重置为关闭
         */
        PradarSwitcher.turnClusterTestSwitchOff();
    } finally {
        pradarSwitchProcessing.set(false);
    }
}
Also used : ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent)

Example 5 with ClusterTestSwitchOnEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent in project LinkAgent by shulieTech.

the class ConsumerRegistry method addListener.

private static void addListener(final DefaultMQPushConsumer businessConsumer) {
    final PradarEventListener listener = new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (event instanceof ClusterTestSwitchOnEvent) {
                try {
                    // 取出配置创建影子消费者
                    DefaultMQPushConsumer defaultMQPushConsumer = buildMQPushConsumer(businessConsumer);
                    if (defaultMQPushConsumer != null) {
                        defaultMQPushConsumer.start();
                    }
                } catch (MQClientException e) {
                    logger.error(e.getMessage());
                    return EventResult.error("alibaba-rocketmq-plugin-open", "Alibaba-RocketMQ PT Consumer start failed: " + e.getMessage());
                }
                return EventResult.success("alibaba-rocketmq-plugin-open");
            } else if (event instanceof ClusterTestSwitchOffEvent || event instanceof SilenceSwitchOnEvent) {
                return shutdownShadowConsumer(businessConsumer);
            } else if (event instanceof ShadowConsumerDisableEvent) {
                String group = businessConsumer.getConsumerGroup();
                Set<String> topics = businessConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl().getSubscriptionInner().keySet();
                for (String topic : topics) {
                    List<ShadowConsumerDisableInfo> disableInfos = ((ShadowConsumerDisableEvent) event).getTarget();
                    for (ShadowConsumerDisableInfo disableInfo : disableInfos) {
                        if (topic.equals(disableInfo.getTopic()) && group.equals(disableInfo.getConsumerGroup())) {
                            return shutdownShadowConsumer(businessConsumer);
                        }
                    }
                }
            }
            return EventResult.IGNORE;
        }

        @Override
        public int order() {
            return 4;
        }
    };
    PradarEventListener old = listeners.putIfAbsent(businessConsumer, listener);
    if (old == null) {
        EventRouter.router().addListener(listener);
    }
}
Also used : PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent) SilenceSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.SilenceSwitchOnEvent) DefaultMQPushConsumer(com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) ShadowConsumerDisableEvent(com.pamirs.pradar.pressurement.agent.event.impl.ShadowConsumerDisableEvent) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent) ShadowConsumerDisableInfo(com.pamirs.pradar.pressurement.agent.listener.model.ShadowConsumerDisableInfo)

Aggregations

ClusterTestSwitchOnEvent (com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent)5 ClusterTestSwitchOffEvent (com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)4 IEvent (com.pamirs.pradar.pressurement.agent.event.IEvent)3 PradarEventListener (com.pamirs.pradar.pressurement.agent.listener.PradarEventListener)3 ShadowConsumerDisableEvent (com.pamirs.pradar.pressurement.agent.event.impl.ShadowConsumerDisableEvent)2 SilenceSwitchOnEvent (com.pamirs.pradar.pressurement.agent.event.impl.SilenceSwitchOnEvent)2 ShadowConsumerDisableInfo (com.pamirs.pradar.pressurement.agent.listener.model.ShadowConsumerDisableInfo)2 DefaultMQPushConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)1 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)1 XxlJobAdapter (com.pamirs.attach.plugin.shadowjob.adapter.XxlJobAdapter)1 EventResult (com.pamirs.pradar.pressurement.agent.listener.EventResult)1 ShadowImplListener (com.pamirs.pradar.pressurement.agent.listener.impl.ShadowImplListener)1 EnhanceCallback (com.shulie.instrument.simulator.api.instrument.EnhanceCallback)1 InstrumentClass (com.shulie.instrument.simulator.api.instrument.InstrumentClass)1 InstrumentMethod (com.shulie.instrument.simulator.api.instrument.InstrumentMethod)1 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 RebalanceImpl (org.apache.rocketmq.client.impl.consumer.RebalanceImpl)1