vipshop / Saturn

The vip.com's distributed job scheduling platform.
Apache License 2.0
2.28k stars 701 forks source link

请问,已经运行的作业是否会重复执行? #473

Open iKenApple opened 6 years ago

iKenApple commented 6 years ago

假如作业执行需要10秒,但是作业触发间隔是5秒,那么第六秒的时候,作业会再次触发吗?

我测试下来不会触发,但是阅读源码并没有发现控制作业运行就不再触发运行的代码, SaturnWorker.java: run():

boolean goAhead;
// 触发执行只有两个条件:1.时间到了 2.点立即执行
synchronized (sigLock) {
    goAhead = !halted.get() && !paused;
    // 重置立即执行标志;
    if (triggered) {
        triggered = false;
    } else if (goAhead) { // 非立即执行。即,执行时间到了,或者没有下次执行时间
        goAhead = goAhead && !noFireTime; // 有下次执行时间,即执行时间到了,才执行作业
        if (goAhead) { // 执行时间到了,更新执行时间
             if (triggerObj != null) {
                  triggerObj.triggered(null);
             }
        } else { // 没有下次执行时间,则尝试睡一秒,防止不停的循环导致CPU使用率过高(如果cron不再改为周期性执行)
             try {
                  sigLock.wait(1000L);
              } catch (InterruptedException ignore) {
              }
          }
    }
}
if (goAhead) {
    job.execute();
}

想了解一下保证幂等性的机制,谢谢!

kfchu commented 6 years ago

saturn worker会判断作业是否正在运行(zk的“running”)节点,如果正在运行则不会调度。