|
|
@ -19,12 +19,14 @@ package org.apache.dolphinscheduler.server.master.consumer; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.thread.Stopper; |
|
|
|
import org.apache.dolphinscheduler.common.thread.Stopper; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
|
|
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
|
|
|
import org.apache.dolphinscheduler.server.master.config.MasterConfig; |
|
|
|
import org.apache.dolphinscheduler.server.master.config.MasterConfig; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.ExecutorDispatcher; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.ExecutorDispatcher; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.enums.ExecutorType; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.enums.ExecutorType; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.exceptions.ExecuteException; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.exceptions.ExecuteException; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.service.exceptions.TaskPriorityQueueException; |
|
|
|
import org.apache.dolphinscheduler.service.process.ProcessService; |
|
|
|
import org.apache.dolphinscheduler.service.process.ProcessService; |
|
|
|
import org.apache.dolphinscheduler.service.queue.TaskPriority; |
|
|
|
import org.apache.dolphinscheduler.service.queue.TaskPriority; |
|
|
|
import org.apache.dolphinscheduler.service.queue.TaskPriorityQueue; |
|
|
|
import org.apache.dolphinscheduler.service.queue.TaskPriorityQueue; |
|
|
@ -33,6 +35,8 @@ import org.apache.dolphinscheduler.service.queue.entity.TaskExecutionContext; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import java.util.concurrent.CountDownLatch; |
|
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.PostConstruct; |
|
|
@ -78,30 +82,24 @@ public class TaskPriorityQueueConsumer extends Thread { |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private MasterConfig masterConfig; |
|
|
|
private MasterConfig masterConfig; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* consumer thread pool |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private ThreadPoolExecutor consumerThreadPoolExecutor; |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
public void init() { |
|
|
|
super.setName("TaskUpdateQueueConsumerThread"); |
|
|
|
this.consumerThreadPoolExecutor = (ThreadPoolExecutor) ThreadUtils.newDaemonFixedThreadExecutor("TaskUpdateQueueConsumerThread", masterConfig.getDispatchTaskNumber()); |
|
|
|
super.start(); |
|
|
|
super.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
List<TaskPriority> failedDispatchTasks = new ArrayList<>(); |
|
|
|
int fetchTaskNum = masterConfig.getDispatchTaskNumber(); |
|
|
|
while (Stopper.isRunning()) { |
|
|
|
while (Stopper.isRunning()) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
int fetchTaskNum = masterConfig.getDispatchTaskNumber(); |
|
|
|
List<TaskPriority> failedDispatchTasks = this.batchDispatch(fetchTaskNum); |
|
|
|
failedDispatchTasks.clear(); |
|
|
|
|
|
|
|
for (int i = 0; i < fetchTaskNum; i++) { |
|
|
|
|
|
|
|
TaskPriority taskPriority = taskPriorityQueue.poll(Constants.SLEEP_TIME_MILLIS, TimeUnit.MILLISECONDS); |
|
|
|
|
|
|
|
if (Objects.isNull(taskPriority)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean dispatchResult = dispatch(taskPriority); |
|
|
|
|
|
|
|
if (!dispatchResult) { |
|
|
|
|
|
|
|
failedDispatchTasks.add(taskPriority); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!failedDispatchTasks.isEmpty()) { |
|
|
|
if (!failedDispatchTasks.isEmpty()) { |
|
|
|
for (TaskPriority dispatchFailedTask : failedDispatchTasks) { |
|
|
|
for (TaskPriority dispatchFailedTask : failedDispatchTasks) { |
|
|
|
taskPriorityQueue.put(dispatchFailedTask); |
|
|
|
taskPriorityQueue.put(dispatchFailedTask); |
|
|
@ -118,13 +116,41 @@ public class TaskPriorityQueueConsumer extends Thread { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* batch dispatch with thread pool |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private List<TaskPriority> batchDispatch(int fetchTaskNum) throws TaskPriorityQueueException, InterruptedException { |
|
|
|
|
|
|
|
List<TaskPriority> failedDispatchTasks = new ArrayList<>(); |
|
|
|
|
|
|
|
CountDownLatch latch = new CountDownLatch(fetchTaskNum); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fetchTaskNum; i++) { |
|
|
|
|
|
|
|
TaskPriority taskPriority = taskPriorityQueue.poll(Constants.SLEEP_TIME_MILLIS, TimeUnit.MILLISECONDS); |
|
|
|
|
|
|
|
if (Objects.isNull(taskPriority)) { |
|
|
|
|
|
|
|
latch.countDown(); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
consumerThreadPoolExecutor.submit(() -> { |
|
|
|
|
|
|
|
boolean dispatchResult = this.dispatchTask(taskPriority); |
|
|
|
|
|
|
|
if (!dispatchResult) { |
|
|
|
|
|
|
|
failedDispatchTasks.add(taskPriority); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
latch.countDown(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
latch.await(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return failedDispatchTasks; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* dispatch task |
|
|
|
* dispatch task |
|
|
|
* |
|
|
|
* |
|
|
|
* @param taskPriority taskPriority |
|
|
|
* @param taskPriority taskPriority |
|
|
|
* @return result |
|
|
|
* @return result |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected boolean dispatch(TaskPriority taskPriority) { |
|
|
|
protected boolean dispatchTask(TaskPriority taskPriority) { |
|
|
|
boolean result = false; |
|
|
|
boolean result = false; |
|
|
|
try { |
|
|
|
try { |
|
|
|
TaskExecutionContext context = taskPriority.getTaskExecutionContext(); |
|
|
|
TaskExecutionContext context = taskPriority.getTaskExecutionContext(); |
|
|
@ -158,8 +184,6 @@ public class TaskPriorityQueueConsumer extends Thread { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* check if task need to check state, if true, refresh the checkpoint |
|
|
|
* check if task need to check state, if true, refresh the checkpoint |
|
|
|
* @param taskPriority |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private boolean isTaskNeedToCheck(TaskPriority taskPriority) { |
|
|
|
private boolean isTaskNeedToCheck(TaskPriority taskPriority) { |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|