CalvinKirs commented on code in PR #26845:
URL: https://github.com/apache/doris/pull/26845#discussion_r1392145017


##########
fe/fe-core/src/main/java/org/apache/doris/job/scheduler/JobScheduler.java:
##########
@@ -0,0 +1,173 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.job.scheduler;
+
+import org.apache.doris.common.CustomThreadFactory;
+import org.apache.doris.common.util.TimeUtils;
+import org.apache.doris.job.base.AbstractJob;
+import org.apache.doris.job.base.JobExecuteType;
+import org.apache.doris.job.common.JobStatus;
+import org.apache.doris.job.common.TaskType;
+import org.apache.doris.job.disruptor.TaskDisruptor;
+import org.apache.doris.job.executor.TimerJobSchedulerTask;
+import org.apache.doris.job.manager.TaskDisruptorGroupManager;
+import org.apache.doris.job.task.AbstractTask;
+
+import io.netty.util.HashedWheelTimer;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+public class JobScheduler<T extends AbstractJob<?>> implements Closeable {
+
+    /**
+     * scheduler tasks, it's used to scheduler job
+     */
+    private HashedWheelTimer timerTaskScheduler;
+
+    private TaskDisruptor timerJobDisruptor;
+
+    private TaskDisruptorGroupManager taskDisruptorGroupManager;
+
+    private long latestBatchSchedulerTimerTaskTimeMs = 0L;
+
+    private static final long BATCH_SCHEDULER_INTERVAL_SECONDS = 60;
+
+    private static final int HASHED_WHEEL_TIMER_TICKS_PER_WHEEL = 660;
+
+    private final Map<Long, T> jobMap;
+
+    public JobScheduler(Map<Long, T> jobMap) {
+        this.jobMap = jobMap;
+    }
+
+    /**
+     * batch scheduler interval ms time
+     */
+    private static final long BATCH_SCHEDULER_INTERVAL_MILLI_SECONDS = 
BATCH_SCHEDULER_INTERVAL_SECONDS * 1000L;
+
+    public void start() {
+        timerTaskScheduler = new HashedWheelTimer(new 
CustomThreadFactory("timer-task-scheduler"), 1,
+                TimeUnit.SECONDS, HASHED_WHEEL_TIMER_TICKS_PER_WHEEL);
+        timerTaskScheduler.start();
+        taskDisruptorGroupManager = new TaskDisruptorGroupManager();
+        taskDisruptorGroupManager.init();
+        this.timerJobDisruptor = 
taskDisruptorGroupManager.getDispatchDisruptor();
+        latestBatchSchedulerTimerTaskTimeMs = System.currentTimeMillis();
+        batchSchedulerTimerJob();
+        cycleSystemSchedulerTasks();
+    }
+
+    /**
+     * We will cycle system scheduler tasks every 10 minutes.
+     * Jobs will be re-registered after the task is completed
+     */
+    private void cycleSystemSchedulerTasks() {
+        log.info("re-register system scheduler timer tasks" + 
TimeUtils.longToTimeString(System.currentTimeMillis()));
+        timerTaskScheduler.newTimeout(timeout -> {
+            batchSchedulerTimerJob();
+            cycleSystemSchedulerTasks();
+        }, BATCH_SCHEDULER_INTERVAL_SECONDS, TimeUnit.SECONDS);
+
+    }
+
+    private void batchSchedulerTimerJob() {
+        executeTimerJobIdsWithinLastTenMinutesWindow();
+    }
+
+    public void scheduleOneJob(T job) {
+        if (!job.getJobStatus().equals(JobStatus.RUNNING)) {
+            return;
+        }
+        if (!job.getJobConfig().checkIsTimerJob()) {
+            //manual job will not scheduler
+            if 
(JobExecuteType.MANUAL.equals(job.getJobConfig().getExecuteType())) {
+                return;
+            }
+            //todo skip streaming job,improve in the future
+            if 
(JobExecuteType.INSTANT.equals(job.getJobConfig().getExecuteType()) && 
job.isReadyForScheduling()) {
+                schedulerImmediateJob(job);
+            }
+        }
+        //if it's timer job and trigger last window already start, we will 
scheduler it immediately
+        cycleTimerJobScheduler(job);
+    }
+
+    @Override
+    public void close() throws IOException {
+
+    }
+
+
+    private void cycleTimerJobScheduler(T job) {
+        List<Long> delaySeconds = 
job.getJobConfig().getTriggerDelayTimes(System.currentTimeMillis(),
+                System.currentTimeMillis(), 
latestBatchSchedulerTimerTaskTimeMs);
+        if (CollectionUtils.isNotEmpty(delaySeconds)) {
+            delaySeconds.forEach(delaySecond -> {
+                TimerJobSchedulerTask<T> timerJobSchedulerTask = new 
TimerJobSchedulerTask<>(timerJobDisruptor, job);
+                timerTaskScheduler.newTimeout(timerJobSchedulerTask, 
delaySecond, TimeUnit.SECONDS);
+            });
+        }
+    }
+
+
+    private void schedulerImmediateJob(T job) {
+        List<? extends AbstractTask> tasks = job.createTasks(TaskType.MANUAL);
+        if (CollectionUtils.isEmpty(tasks)) {
+            return;

Review Comment:
   oops...fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to