XXL源码解析

源码分析

目标:找出执行的故障转移及其他策略如何实现的(居然属于路由策略,去查看路由策略相关的源码

由官方文档[执行器 RESTful API](https://www.xuxueli.com/xxl-job/#6.2 执行器 RESTful API)引发的源码阅读

现在进度:找到了任务运行的源码,主要是密集调度时的阻塞处理策略分配方式、执行器ID及handler的优先级,具体执行方法handler发生变更时的变化,内部的任务存储容器,任务超时时间的设定、运行模式的匹配对运行产生的影响

任务超时时间的设定与生效主要体现在内部使用线程的运行代表某个任务的执行,在执行时如果有超时时间的设定采用FutureTask去运行具体逻辑,根据返回值查看是否运行超时,再执行超时出错逻辑

看到了阻塞策略的执行源码

任务阻塞策略

在执行过程中出现了策略匹配

阻塞处理策略:调度过于密集执行器来不及处理时的处理策略,策略包括:单机串行(默认)、丢弃后续调度、覆盖之前调度;

自定义Executor

触发任务传入参数

API服务位置:com.xxl.job.core.biz.ExecutorBiz
API服务请求参考代码:com.xxl.job.executorbiz.ExecutorBizTest

触发任务

说明:触发任务执行
------
地址格式:{执行器内嵌服务跟地址}/run
Header:
    XXL-JOB-ACCESS-TOKEN : {请求令牌}
请求数据格式如下,放置在 RequestBody 中,JSON格式:
    {
        "jobId":1,                                  // 任务ID
        "executorHandler":"demoJobHandler",         // 任务标识
        "executorParams":"demoJobHandler",          // 任务参数
        "executorBlockStrategy":"COVER_EARLY",      // 任务阻塞策略,可选值参考 com.xxl.job.core.enums.ExecutorBlockStrategyEnum
        "executorTimeout":0,                        // 任务超时时间,单位秒,大于零时生效
        "logId":1,                                  // 本次调度日志ID
        "logDateTime":1586629003729,                // 本次调度日志时间
        "glueType":"BEAN",                          // 任务模式,可选值参考 com.xxl.job.core.glue.GlueTypeEnum
        "glueSource":"xxx",                         // GLUE脚本代码
        "glueUpdatetime":1586629003727,             // GLUE脚本更新时间,用于判定脚本是否变更以及是否需要刷新
        "broadcastIndex":0,                         // 分片参数:当前分片
        "broadcastTotal":0                          // 分片参数:总分片
    }
    响应数据格式:
    {
      "code": 200,      // 200 表示正常、其他失败
      "msg": null       // 错误提示消息
    }

java.util.concurrent.LinkedBlockingQueue

线程安全的链式阻塞队列,阻塞队列,队列容量不足自动阻塞,队列容量为 0 自动阻塞。

Executor中使用ConcurrentHashMap<Integer,JobThread> 一个线程安全的hashmap容器来存储jobThread(任务执行线程

jobThreadRepository

private static ConcurrentMap<Integer, JobThread> jobThreadRepository = new ConcurrentHashMap<Integer, JobThread>();

id:thread的KV键值对,通过传递过来的ID获取要执行的线程Thread

使用阻塞队列保存参数

线程停止

   /**
    * kill job thread
    *
    * @param stopReason
    */
public void toStop(String stopReason) {
   /**
    * Thread.interrupt只支持终止线程的阻塞状态(wait、join、sleep),
    * 在阻塞出抛出InterruptedException异常,但是并不会终止运行的线程本身;
    * 所以需要注意,此处彻底销毁本线程,需要通过共享变量方式;
    * 所以加上volatile让多线程可见么
    */
   this.toStop = true;
   this.stopReason = stopReason;
}

toStop加上volatile修饰保证线程可见,线程停止后通知其他线程,可能避免影响任务与子任务之间的调用关系

Futuretask

运行超时,get就出错

可以看出,RunnableFuture继承了Runnable接口和Future接口,而FutureTask实现了RunnableFuture接口。所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值。

异步 可运行 带返回值

任务执行就调用IJobHandler的具体实现类去执行,这边就可以定义我们自己的任务执行逻辑

MethodJobHandler

反射,method调用,传参

主要的run方法解析

运行主要步骤:

  1. 根据传递过来的参数去任务容器中查看是否存在运行过的处理方法
  2. 根据不同的Glue(运行模式)执行不同逻辑,在BEAN的运行模式下去根据XxlJob注册的value获取处理方法
  3. 两次获取的处理方法对比,如果不同说明在页面上重新配置了处理方法并使用新的处理方法,传参时体现在executorHandler优于jobId,前者更为准确
  4. 调度过于密集时根据配置选择不同的处理策略
  5. 运行任务,并更新容器
  6. 记录结果并返回
    @Override
    public ReturnT<String> run(TriggerParam triggerParam) {
        // load old:jobHandler + jobThread
        // 从executor中获取具体要执行的线程
        // TriggerParam封装触发请求参数,这个参数由控制中心点击触发任务时传递过来
        // 根据jobid判断要执行的定时任务
        // 在这里获取的jobThread是旧的是根据界面ID来的,ID是同一个但具体handler可能发生变动
        JobThread jobThread = XxlJobExecutor.loadJobThread(triggerParam.getJobId());
        // 拿到要执行的handler否则置空
        IJobHandler jobHandler = jobThread!=null?jobThread.getHandler():null;
        String removeOldReason = null;

        // valid:jobHandler + jobThread
        // 检测Glue类型是否在限定之内
        GlueTypeEnum glueTypeEnum = GlueTypeEnum.match(triggerParam.getGlueType());
        // 根据不同任务运行模式GLUE分逻辑执行
        if (GlueTypeEnum.BEAN == glueTypeEnum) {

            // new jobhandler
            // 根据传递过来的具体任务名寻找具体处理逻辑
            // loadJobHandler方法进行了重载
            // 根据id就是跟着界面传过来的id这个id标识界面上的某个执行任务
            // 根据String的方式就是根据配置的具体任务标识,String类型的标识,与@XxlJob(value="")中的value相对应
            IJobHandler newJobHandler = XxlJobExecutor.loadJobHandler(triggerParam.getExecutorHandler());

            // 两次获取的jobHandler不一致,说明之前有注册过但是更改了配置(比如执行器/任务的具体执行方法的变更
            // valid old jobThread
            if (jobThread!=null && jobHandler != newJobHandler) {
                // change handler, need kill old thread
                // 变更执行方法停止旧任务
                removeOldReason = "change jobhandler or glue type, and terminate the old job thread.";

                // 一旦具体handler变动就清空之前的任务
                jobThread = null;
                jobHandler = null;
            }

            // valid handler
            // 旧的handler变为新的(变更过的)
            if (jobHandler == null) {
                jobHandler = newJobHandler;
                if (jobHandler == null) {
                    return new ReturnT<String>(ReturnT.FAIL_CODE, "job handler [" + triggerParam.getExecutorHandler() + "] not found.");
                }
            }

        } else if (GlueTypeEnum.GLUE_GROOVY == glueTypeEnum) {

            // valid old jobThread
            if (jobThread != null &&
                    !(jobThread.getHandler() instanceof GlueJobHandler
                        && ((GlueJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
                // change handler or gluesource updated, need kill old thread
                removeOldReason = "change job source or glue type, and terminate the old job thread.";

                jobThread = null;
                jobHandler = null;
            }

            // valid handler
            if (jobHandler == null) {
                try {
                    IJobHandler originJobHandler = GlueFactory.getInstance().loadNewInstance(triggerParam.getGlueSource());
                    jobHandler = new GlueJobHandler(originJobHandler, triggerParam.getGlueUpdatetime());
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    return new ReturnT<String>(ReturnT.FAIL_CODE, e.getMessage());
                }
            }
        } else if (glueTypeEnum!=null && glueTypeEnum.isScript()) {

            // valid old jobThread
            if (jobThread != null &&
                    !(jobThread.getHandler() instanceof ScriptJobHandler
                            && ((ScriptJobHandler) jobThread.getHandler()).getGlueUpdatetime()==triggerParam.getGlueUpdatetime() )) {
                // change script or gluesource updated, need kill old thread
                removeOldReason = "change job source or glue type, and terminate the old job thread.";

                jobThread = null;
                jobHandler = null;
            }

            // valid handler
            if (jobHandler == null) {
                jobHandler = new ScriptJobHandler(triggerParam.getJobId(), triggerParam.getGlueUpdatetime(), triggerParam.getGlueSource(), GlueTypeEnum.match(triggerParam.getGlueType()));
            }
        } else {
            return new ReturnT<String>(ReturnT.FAIL_CODE, "glueType[" + triggerParam.getGlueType() + "] is not valid.");
        }

        // executor block strategy
        // 策略执行块,通过不同的运行模式找到固定handler后去执行
        // 配置调度过于密集执行器来不及处理时的处理策略
        if (jobThread != null) {
            // 匹配执行策略
            // 可在这里进行策略扩展改动源码实现自己的阻塞处理策略
            // 策略匹配
            // 任务阻塞策略
            ExecutorBlockStrategyEnum blockStrategy = ExecutorBlockStrategyEnum.match(triggerParam.getExecutorBlockStrategy(), null);
            // 丢弃后续调度
            if (ExecutorBlockStrategyEnum.DISCARD_LATER == blockStrategy) {
                // discard when running
                if (jobThread.isRunningOrHasQueue()) {
                    return new ReturnT<String>(ReturnT.FAIL_CODE, "block strategy effect:"+ExecutorBlockStrategyEnum.DISCARD_LATER.getTitle());
                }
                // 覆盖之前调度
            } else if (ExecutorBlockStrategyEnum.COVER_EARLY == blockStrategy) {
                // kill running jobThread
                if (jobThread.isRunningOrHasQueue()) {
                    removeOldReason = "block strategy effect:" + ExecutorBlockStrategyEnum.COVER_EARLY.getTitle();

                    jobThread = null;
                }
            } else {
                // just queue trigger
            }
        }

        // replace thread (new or exists invalid)
        if (jobThread == null) {
            // 执行新的任务,如果任务有更新就中断旧任务
            jobThread = XxlJobExecutor.registJobThread(triggerParam.getJobId(), jobHandler, removeOldReason);
        }

        // push data to queue
        // 将运行结果放入容器
        ReturnT<String> pushResult = jobThread.pushTriggerQueue(triggerParam);
        return pushResult;
    }

主要涉及的JobThread及XxlJobExecutor方法:

XxlJobExecutor

    /**
     *
     * @param jobId 调度中心传递过来的要执行任务的唯一标识ID
     * @return
     */
    public static JobThread loadJobThread(int jobId){
        // 自定义的JobThread
        JobThread jobThread = jobThreadRepository.get(jobId);
        return jobThread;
    }
    public static JobThread registJobThread(int jobId, IJobHandler handler, String removeOldReason){
        JobThread newJobThread = new JobThread(jobId, handler);
        newJobThread.start();
        logger.info(">>>>>>>>>>> xxl-job regist JobThread success, jobId:{}, handler:{}", new Object[]{jobId, handler});

        // 运行新任务,更新任务容器jobThreadRepository,更新后居然会返回旧的value
        // 对旧任务进行处理和中断处理,代表任务更新
        JobThread oldJobThread = jobThreadRepository.put(jobId, newJobThread);	// putIfAbsent | oh my god, map's put method return the old value!!!
        if (oldJobThread != null) {
            oldJobThread.toStop(removeOldReason);
            oldJobThread.interrupt();
        }

        return newJobThread;
    }
    

JobThread

    /**
     * new trigger to queue
     *
     * @param triggerParam
     * @return
     */
	public ReturnT<String> pushTriggerQueue(TriggerParam triggerParam) {
		// avoid repeat
		if (triggerLogIdSet.contains(triggerParam.getLogId())) {
			logger.info(">>>>>>>>>>> repeate trigger job, logId:{}", triggerParam.getLogId());
			return new ReturnT<String>(ReturnT.FAIL_CODE, "repeate trigger job, logId:" + triggerParam.getLogId());
		}

		triggerLogIdSet.add(triggerParam.getLogId());
		// 在LinkedBlockQueue中使用add添加元素在队列满会抛出IllegalStateException
		// 建议在双端队列添加元素时采用offer,队列满也不会抛出异常,会被阻塞
		triggerQueue.add(triggerParam);
        return ReturnT.SUCCESS;
	}
    @Override
	public void run() {

    	// init
		// 处理器初始化
    	try {
			handler.init();
		} catch (Throwable e) {
    		logger.error(e.getMessage(), e);
		}

		// execute
		while(!toStop){
			// 置为false只有查到运行参数才开启
			running = false;
			// 尝试运行次数,计数自增,非线程安全
			idleTimes++;

			// 清空参数
            TriggerParam triggerParam = null;
            ReturnT<String> executeResult = null;
            try {
            	// 要检查停止信号,我们需要循环,所以我们不能使用queue.take()代替poll(timeout)
				// take不仅查看还会去除首部元素
				// to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
				triggerParam = triggerQueue.poll(3L, TimeUnit.SECONDS);
				if (triggerParam!=null) {
					// 标识当前任务(线程)已经开启
					running = true;
					// 重置尝试运行次数
					idleTimes = 0;
					// 参数调用过了,移除当前参数
					triggerLogIdSet.remove(triggerParam.getLogId());

					// 记录日志
					// log filename, like "logPath/yyyy-MM-dd/9999.log"
					String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTime()), triggerParam.getLogId());
					XxlJobContext.setXxlJobContext(new XxlJobContext(
							triggerParam.getLogId(),
							logFileName,
							triggerParam.getBroadcastIndex(),
							triggerParam.getBroadcastTotal()));

					// execute
					XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Param:" + triggerParam.getExecutorParams());

					// 如果运行定义了超时参数
					// 使用futureTask执行,方便执行并传回执行结果
					if (triggerParam.getExecutorTimeout() > 0) {
						// limit timeout
						Thread futureThread = null;
						try {
							final TriggerParam triggerParamTmp = triggerParam;
							FutureTask<ReturnT<String>> futureTask = new FutureTask<ReturnT<String>>(new Callable<ReturnT<String>>() {
								@Override
								public ReturnT<String> call() throws Exception {
									return handler.execute(triggerParamTmp.getExecutorParams());
								}
							});
							futureThread = new Thread(futureTask);
							futureThread.start();

							executeResult = futureTask.get(triggerParam.getExecutorTimeout(), TimeUnit.SECONDS);
						} catch (TimeoutException e) {

							XxlJobLogger.log("<br>----------- xxl-job job execute timeout");
							XxlJobLogger.log(e);

							executeResult = new ReturnT<String>(IJobHandler.FAIL_TIMEOUT.getCode(), "job execute timeout ");
						} finally {
							futureThread.interrupt();
						}
					} else {
						// just execute
						executeResult = handler.execute(triggerParam.getExecutorParams());
					}

					if (executeResult == null) {
						executeResult = IJobHandler.FAIL;
					} else {
						executeResult.setMsg(
								(executeResult!=null&&executeResult.getMsg()!=null&&executeResult.getMsg().length()>50000)
										?executeResult.getMsg().substring(0, 50000).concat("...")
										:executeResult.getMsg());
						executeResult.setContent(null);	// limit obj size
					}
					XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);

				} else {
					// 重试超过三十次移除任务
					if (idleTimes > 30) {
						if(triggerQueue.size() == 0) {	// avoid concurrent trigger causes jobId-lost
							XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
						}
					}
				}
			} catch (Throwable e) {
				if (toStop) {
					XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
				}

				StringWriter stringWriter = new StringWriter();
				e.printStackTrace(new PrintWriter(stringWriter));
				String errorMsg = stringWriter.toString();
				executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg);

				XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
			} finally {
                if(triggerParam != null) {
                    // callback handler info
                    if (!toStop) {
                        // commonm
                        TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogDateTime(), executeResult));
                    } else {
                        // is killed
                        ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [job running, killed]");
                        TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogDateTime(), stopResult));
                    }
                }
            }
        }

		// callback trigger request in queue
		while(triggerQueue !=null && triggerQueue.size()>0){
			TriggerParam triggerParam = triggerQueue.poll();
			if (triggerParam!=null) {
				// is killed
				ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [job not executed, in the job queue, killed.]");
				TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogDateTime(), stopResult));
			}
		}

		// destroy
		try {
			handler.destroy();
		} catch (Throwable e) {
			logger.error(e.getMessage(), e);
		}

		logger.info(">>>>>>>>>>> xxl-job JobThread stoped, hashCode:{}", Thread.currentThread());
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值