|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.server.master.runner; |
| 19 | + |
| 20 | +import org.apache.dolphinscheduler.common.thread.BaseDaemonThread; |
| 21 | +import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
| 22 | +import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; |
| 23 | +import org.apache.dolphinscheduler.server.master.engine.task.client.ITaskExecutorClient; |
| 24 | +import org.apache.dolphinscheduler.server.master.engine.task.runnable.ITaskExecutionRunnable; |
| 25 | +import org.apache.dolphinscheduler.server.master.runner.queue.PriorityAndDelayBasedTaskEntry; |
| 26 | +import org.apache.dolphinscheduler.server.master.runner.queue.PriorityDelayQueue; |
| 27 | + |
| 28 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 29 | + |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | + |
| 32 | +/** |
| 33 | + * WorkerGroupTaskDispatcher is responsible for dispatching tasks from the task queue. |
| 34 | + * The main responsibilities include: |
| 35 | + * 1. Continuously fetching tasks from the {@link PriorityDelayQueue} for dispatch. |
| 36 | + * 2. Re-queuing tasks that fail to dispatch according to retry logic. |
| 37 | + * 3. Ensuring thread safety and correct state transitions during task processing. |
| 38 | + */ |
| 39 | +@Slf4j |
| 40 | +public class WorkerGroupTaskDispatcher extends BaseDaemonThread { |
| 41 | + |
| 42 | + private final ITaskExecutorClient taskExecutorClient; |
| 43 | + |
| 44 | + // TODO The current queue is flawed. When a high-priority task fails, |
| 45 | + // it will be delayed and will not return to the first or second position. |
| 46 | + // Tasks with the same priority will preempt its position. |
| 47 | + // If it needs to be placed at the front of the queue, the queue needs to be re-implemented. |
| 48 | + private final PriorityDelayQueue<PriorityAndDelayBasedTaskEntry<ITaskExecutionRunnable>> workerGroupQueue; |
| 49 | + |
| 50 | + private final AtomicBoolean runningFlag = new AtomicBoolean(false); |
| 51 | + |
| 52 | + public WorkerGroupTaskDispatcher(String workerGroupName, ITaskExecutorClient taskExecutorClient) { |
| 53 | + super("WorkerGroupTaskDispatcher-" + workerGroupName); |
| 54 | + this.taskExecutorClient = taskExecutorClient; |
| 55 | + this.workerGroupQueue = new PriorityDelayQueue<>(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Adds a task to the worker group queue. |
| 60 | + * This method wraps the given task execution object into a priority and delay-based task entry and adds it to the worker group queue. |
| 61 | + * The task is only added if the current dispatcher status is either STARTED or INIT. If the dispatcher is in any other state, |
| 62 | + * the task addition will fail, and a warning message will be logged. |
| 63 | + * |
| 64 | + * @param taskExecutionRunnable The task execution object to add to the queue, which implements the {@link ITaskExecutionRunnable} interface. |
| 65 | + * @param delayTimeMills The delay time in milliseconds before the task should be executed. |
| 66 | + */ |
| 67 | + public void addTaskToWorkerGroupQueue(ITaskExecutionRunnable taskExecutionRunnable, |
| 68 | + long delayTimeMills) { |
| 69 | + workerGroupQueue.add(new PriorityAndDelayBasedTaskEntry<>(delayTimeMills, taskExecutionRunnable)); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public synchronized void start() { |
| 74 | + if (runningFlag.compareAndSet(false, true)) { |
| 75 | + log.info("The {} starting...", this.getName()); |
| 76 | + super.start(); |
| 77 | + log.info("The {} started", this.getName()); |
| 78 | + } else { |
| 79 | + log.error("The {} status is {}, will not start again", this.getName(), runningFlag.get()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public synchronized void close() { |
| 84 | + log.info("The {} closed called but not implemented", this.getName()); |
| 85 | + // todo WorkerGroupTaskDispatcher thread needs to be shut down after the WorkerGroup is deleted. |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void run() { |
| 90 | + while (runningFlag.get()) { |
| 91 | + dispatch(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void dispatch() { |
| 96 | + PriorityAndDelayBasedTaskEntry<ITaskExecutionRunnable> taskEntry = workerGroupQueue.take(); |
| 97 | + ITaskExecutionRunnable taskExecutionRunnable = taskEntry.getData(); |
| 98 | + final TaskInstance taskInstance = taskExecutionRunnable.getTaskInstance(); |
| 99 | + try { |
| 100 | + final TaskExecutionStatus taskStatus = taskInstance.getState(); |
| 101 | + if (taskStatus != TaskExecutionStatus.SUBMITTED_SUCCESS |
| 102 | + && taskStatus != TaskExecutionStatus.DELAY_EXECUTION) { |
| 103 | + log.warn("The TaskInstance {} state is : {}, will not dispatch", taskInstance.getName(), taskStatus); |
| 104 | + return; |
| 105 | + } |
| 106 | + taskExecutorClient.dispatch(taskExecutionRunnable); |
| 107 | + } catch (Exception e) { |
| 108 | + // If dispatch failed, will put the task back to the queue |
| 109 | + // The task will be dispatched after waiting time. |
| 110 | + // the waiting time will increase multiple of times, but will not exceed 60 seconds |
| 111 | + long waitingTimeMills = Math.min( |
| 112 | + taskExecutionRunnable.getTaskExecutionContext().increaseDispatchFailTimes() * 1_000L, 60_000L); |
| 113 | + workerGroupQueue.add(new PriorityAndDelayBasedTaskEntry<>(waitingTimeMills, taskExecutionRunnable)); |
| 114 | + log.error("Dispatch Task: {} failed will retry after: {}/ms", taskInstance.getName(), waitingTimeMills, e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * ony use unit test |
| 120 | + * @return size |
| 121 | + */ |
| 122 | + protected int queueSize() { |
| 123 | + return this.workerGroupQueue.size(); |
| 124 | + } |
| 125 | +} |
0 commit comments