Skip to content

Commit

Permalink
Use new module
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinKirs committed Dec 11, 2024
1 parent d224314 commit 005b74a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.doris.job.common.JobStatus;
import org.apache.doris.job.common.JobType;
import org.apache.doris.job.common.TaskType;
import org.apache.doris.job.disruptor.TaskDisruptor;
import org.apache.doris.job.disruptor.TimerJobEvent;
import org.apache.doris.job.task.AbstractTask;

Expand All @@ -40,9 +39,9 @@
@Log4j2
public class DispatchTaskHandler<T extends AbstractJob> implements WorkHandler<TimerJobEvent<T>> {

private final Map<JobType, TaskDisruptor<T>> disruptorMap;
private final Map<JobType, TaskProcessor> disruptorMap;

public DispatchTaskHandler(Map<JobType, TaskDisruptor<T>> disruptorMap) {
public DispatchTaskHandler(Map<JobType, TaskProcessor> disruptorMap) {
this.disruptorMap = disruptorMap;
}

Expand All @@ -66,7 +65,7 @@ public void onEvent(TimerJobEvent<T> event) {
}
JobType jobType = event.getJob().getJobType();
for (AbstractTask task : tasks) {
if (!disruptorMap.get(jobType).publishEvent(task, event.getJob().getJobConfig())) {
if (!disruptorMap.get(jobType).addTask(task)) {
task.cancel();
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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.executor;

import org.apache.doris.job.task.AbstractTask;

import lombok.extern.log4j.Log4j2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Log4j2
public class TaskProcessor {
private ExecutorService executor;

public TaskProcessor(int numberOfThreads, int queueSize, ThreadFactory threadFactory) {
this.executor = new ThreadPoolExecutor(
numberOfThreads,
numberOfThreads,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(queueSize),
threadFactory,
new ThreadPoolExecutor.AbortPolicy()
);
}

public boolean addTask(AbstractTask task) {
try {
executor.execute(() -> runTask(task)); // 直接提交任务
log.info("Add task to executor, task id: {}", task.getTaskId());
return true;
} catch (RejectedExecutionException e) {
log.warn("Failed to add task to executor, task id: {}", task.getTaskId(), e);
return false;
}
}

public void shutdown() {
log.info("Shutting down executor service...");
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
log.info("Executor service shut down successfully.");
}

private void runTask(AbstractTask task) {
try {
if (task == null) {
log.warn("Task is null, ignore. Maybe it has been canceled.");
return;
}
if (task.isCancelled()) {
log.info("Task is canceled, ignore. Task id: {}", task.getTaskId());
return;
}
log.info("Start to execute task, task id: {}", task.getTaskId());
task.runTask();
} catch (Exception e) {
log.warn("Execute task error, task id: {}", task.getTaskId(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import org.apache.doris.job.base.AbstractJob;
import org.apache.doris.job.base.JobExecutionConfiguration;
import org.apache.doris.job.common.JobType;
import org.apache.doris.job.disruptor.ExecuteTaskEvent;
import org.apache.doris.job.disruptor.TaskDisruptor;
import org.apache.doris.job.disruptor.TimerJobEvent;
import org.apache.doris.job.executor.DefaultTaskExecutorHandler;
import org.apache.doris.job.executor.DispatchTaskHandler;
import org.apache.doris.job.extensions.insert.InsertTask;
import org.apache.doris.job.extensions.mtmv.MTMVTask;
import org.apache.doris.job.executor.TaskProcessor;
import org.apache.doris.job.task.AbstractTask;

import com.lmax.disruptor.EventFactory;
Expand All @@ -44,7 +41,7 @@

public class TaskDisruptorGroupManager<T extends AbstractTask> {

private final Map<JobType, TaskDisruptor<T>> disruptorMap = new EnumMap<>(JobType.class);
private final Map<JobType, TaskProcessor> disruptorMap = new EnumMap<>(JobType.class);

@Getter
private TaskDisruptor<TimerJobEvent<AbstractJob>> dispatchDisruptor;
Expand Down Expand Up @@ -92,44 +89,27 @@ private void registerDispatchDisruptor() {
}

private void registerInsertDisruptor() {
EventFactory<ExecuteTaskEvent<InsertTask>> insertEventFactory = ExecuteTaskEvent.factory();
ThreadFactory insertTaskThreadFactory = new CustomThreadFactory("insert-task-execute");
WorkHandler[] insertTaskExecutorHandlers = new WorkHandler[DISPATCH_INSERT_THREAD_NUM];
for (int i = 0; i < DISPATCH_INSERT_THREAD_NUM; i++) {
insertTaskExecutorHandlers[i] = new DefaultTaskExecutorHandler<InsertTask>();
}
EventTranslatorVararg<ExecuteTaskEvent<InsertTask>> eventTranslator =
(event, sequence, args) -> {
event.setTask((InsertTask) args[0]);
event.setJobConfig((JobExecutionConfiguration) args[1]);
};
TaskDisruptor insertDisruptor = new TaskDisruptor<>(insertEventFactory, DISPATCH_INSERT_TASK_QUEUE_SIZE,
insertTaskThreadFactory, new LiteTimeoutBlockingWaitStrategy(10, TimeUnit.MILLISECONDS),
insertTaskExecutorHandlers, eventTranslator);
disruptorMap.put(JobType.INSERT, insertDisruptor);


TaskProcessor insertTaskProcessor = new TaskProcessor(DISPATCH_INSERT_THREAD_NUM,
DISPATCH_INSERT_TASK_QUEUE_SIZE, insertTaskThreadFactory);
disruptorMap.put(JobType.INSERT, insertTaskProcessor);
}

private void registerMTMVDisruptor() {
EventFactory<ExecuteTaskEvent<MTMVTask>> mtmvEventFactory = ExecuteTaskEvent.factory();

ThreadFactory mtmvTaskThreadFactory = new CustomThreadFactory("mtmv-task-execute");
WorkHandler[] insertTaskExecutorHandlers = new WorkHandler[DISPATCH_MTMV_THREAD_NUM];
for (int i = 0; i < DISPATCH_MTMV_THREAD_NUM; i++) {
insertTaskExecutorHandlers[i] = new DefaultTaskExecutorHandler<MTMVTask>();
}
EventTranslatorVararg<ExecuteTaskEvent<MTMVTask>> eventTranslator =
(event, sequence, args) -> {
event.setTask((MTMVTask) args[0]);
event.setJobConfig((JobExecutionConfiguration) args[1]);
};
TaskDisruptor mtmvDisruptor = new TaskDisruptor<>(mtmvEventFactory, DISPATCH_MTMV_TASK_QUEUE_SIZE,
mtmvTaskThreadFactory, new LiteTimeoutBlockingWaitStrategy(10, TimeUnit.MILLISECONDS),
insertTaskExecutorHandlers, eventTranslator);
disruptorMap.put(JobType.MV, mtmvDisruptor);
TaskProcessor mtmvTaskProcessor = new TaskProcessor(DISPATCH_MTMV_THREAD_NUM,
DISPATCH_MTMV_TASK_QUEUE_SIZE, mtmvTaskThreadFactory);
disruptorMap.put(JobType.MV, mtmvTaskProcessor);
}

public boolean dispatchInstantTask(AbstractTask task, JobType jobType,
JobExecutionConfiguration jobExecutionConfiguration) {
return disruptorMap.get(jobType).publishEvent(task, jobExecutionConfiguration);


return disruptorMap.get(jobType).addTask(task);
}


Expand Down

0 comments on commit 005b74a

Please sign in to comment.