Skip to content

Commit a911cf2

Browse files
committed
[DSIP-73] Add dolphinscheduler-task-executor module to unify the task execution logic
1 parent 5e2abd7 commit a911cf2

File tree

255 files changed

+5515
-6781
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+5515
-6781
lines changed

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskInstanceServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
import org.apache.dolphinscheduler.dao.utils.TaskCacheUtils;
4848
import org.apache.dolphinscheduler.extract.base.client.Clients;
4949
import org.apache.dolphinscheduler.extract.common.ILogService;
50+
import org.apache.dolphinscheduler.extract.worker.IPhysicalTaskExecutorOperator;
5051
import org.apache.dolphinscheduler.extract.worker.IStreamingTaskInstanceOperator;
51-
import org.apache.dolphinscheduler.extract.worker.ITaskInstanceOperator;
52-
import org.apache.dolphinscheduler.extract.worker.transportor.TaskInstanceKillRequest;
53-
import org.apache.dolphinscheduler.extract.worker.transportor.TaskInstanceKillResponse;
5452
import org.apache.dolphinscheduler.extract.worker.transportor.TaskInstanceTriggerSavepointRequest;
5553
import org.apache.dolphinscheduler.extract.worker.transportor.TaskInstanceTriggerSavepointResponse;
5654
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus;
5755
import org.apache.dolphinscheduler.service.process.ProcessService;
56+
import org.apache.dolphinscheduler.task.executor.operations.TaskExecutorKillRequest;
57+
import org.apache.dolphinscheduler.task.executor.operations.TaskExecutorKillResponse;
5858

5959
import org.apache.commons.lang3.StringUtils;
6060
import org.apache.commons.lang3.tuple.Pair;
@@ -306,11 +306,11 @@ public Result stopTask(User loginUser, long projectCode, Integer taskInstanceId)
306306
}
307307

308308
// todo: we only support streaming task for now
309-
final TaskInstanceKillResponse taskInstanceKillResponse = Clients
310-
.withService(ITaskInstanceOperator.class)
309+
final TaskExecutorKillResponse taskExecutorKillResponse = Clients
310+
.withService(IPhysicalTaskExecutorOperator.class)
311311
.withHost(taskInstance.getHost())
312-
.killTask(new TaskInstanceKillRequest(taskInstanceId));
313-
log.info("TaskInstance kill response: {}", taskInstanceKillResponse);
312+
.killTask(TaskExecutorKillRequest.of(taskInstanceId));
313+
log.info("TaskInstance kill response: {}", taskExecutorKillResponse);
314314

315315
putMsg(result, Status.SUCCESS);
316316
return result;

dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/thread/ThreadUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
@Slf4j
3232
public class ThreadUtils {
3333

34+
/**
35+
* Create a daemon fixed thread pool, the thread name will be formatted with the given name.
36+
*
37+
* @param threadNameFormat the thread name format, e.g. "DemonThread-%d"
38+
* @param threadsNum the number of threads in the pool
39+
*/
3440
public static ThreadPoolExecutor newDaemonFixedThreadExecutor(String threadNameFormat, int threadsNum) {
3541
return (ThreadPoolExecutor) Executors.newFixedThreadPool(threadsNum, newDaemonThreadFactory(threadNameFormat));
3642
}
@@ -43,9 +49,10 @@ public static ScheduledExecutorService newSingleDaemonScheduledExecutorService(S
4349
* Create a daemon scheduler thread pool, the thread name will be formatted with the given name.
4450
*
4551
* @param threadNameFormat the thread name format, e.g. "DemonThread-%d"
46-
* @param threadsNum the number of threads in the pool
52+
* @param threadsNum the number of threads in the pool
4753
*/
48-
public static ScheduledExecutorService newDaemonScheduledExecutorService(String threadNameFormat, int threadsNum) {
54+
public static ScheduledExecutorService newDaemonScheduledExecutorService(final String threadNameFormat,
55+
final int threadsNum) {
4956
return Executors.newScheduledThreadPool(threadsNum, newDaemonThreadFactory(threadNameFormat));
5057
}
5158

dolphinscheduler-eventbus/src/main/java/org/apache/dolphinscheduler/eventbus/AbstractDelayEvent.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,47 @@
2020
import java.util.concurrent.Delayed;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import lombok.experimental.SuperBuilder;
24+
2325
/**
2426
* The abstract class of delay event, the event will be triggered after the delay time.
2527
* <p> You can extend this class to implement your own delay event.
2628
*/
29+
@SuperBuilder
2730
public abstract class AbstractDelayEvent implements IEvent, Delayed {
2831

2932
protected long delayTime;
3033

3134
protected long triggerTimeInMillis;
3235

36+
@Deprecated
37+
protected RetryPolicy retryPolicy;
38+
3339
public AbstractDelayEvent() {
3440
this(0);
3541
}
3642

3743
public AbstractDelayEvent(long delayTime) {
44+
this(delayTime, null);
45+
}
46+
47+
public AbstractDelayEvent(final long delayTime, final RetryPolicy retryPolicy) {
3848
if (delayTime == 0) {
3949
this.triggerTimeInMillis = System.currentTimeMillis();
4050
} else {
4151
this.triggerTimeInMillis = System.currentTimeMillis() + delayTime;
4252
}
53+
this.retryPolicy = retryPolicy;
54+
}
55+
56+
@Override
57+
public boolean isRetryable() {
58+
return retryPolicy != null;
59+
}
60+
61+
@Override
62+
public RetryPolicy getRetryPolicy() {
63+
return retryPolicy;
4364
}
4465

4566
@Override

dolphinscheduler-eventbus/src/main/java/org/apache/dolphinscheduler/eventbus/AbstractDelayEventBus.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public Optional<T> poll() {
3737
return Optional.ofNullable(delayEventQueue.poll());
3838
}
3939

40+
@Override
41+
public Optional<T> peek() {
42+
return Optional.ofNullable(delayEventQueue.peek());
43+
}
44+
45+
@Override
46+
public Optional<T> remove() {
47+
return Optional.ofNullable(delayEventQueue.remove());
48+
}
49+
4050
@Override
4151
public boolean isEmpty() {
4252
return delayEventQueue.isEmpty();

dolphinscheduler-eventbus/src/main/java/org/apache/dolphinscheduler/eventbus/IEvent.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,37 @@
2222
*/
2323
public interface IEvent {
2424

25+
/**
26+
* Whether the event can retry, if true the event will be stored in the event bus until the retry policy break.
27+
*/
28+
boolean isRetryable();
29+
30+
/**
31+
* Get the retry policy.
32+
* <p> Use the control retry times and interval.
33+
*/
34+
RetryPolicy getRetryPolicy();
35+
36+
interface RetryPolicy {
37+
38+
/**
39+
* Whether the retry policy has been broken.
40+
*/
41+
boolean isRetryPolicyBroken();
42+
43+
/**
44+
* Increase the retry times.
45+
*/
46+
void increaseRetryTimes();
47+
48+
void setNextRetryTimeStampWithFixedStep();
49+
50+
void setNextRetryTimeStampWithExponentialStep();
51+
52+
/**
53+
* Get the next time which the event can retry.
54+
*/
55+
long getNextRetryTimeStamp();
56+
57+
}
2558
}

dolphinscheduler-eventbus/src/main/java/org/apache/dolphinscheduler/eventbus/IEventBus.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public interface IEventBus<T extends IEvent> {
5454
*/
5555
Optional<T> poll() throws InterruptedException;
5656

57+
/**
58+
* peek the head event from the bus. This method will not block if the event bus is empty will return empty optional.
59+
*/
60+
Optional<T> peek();
61+
62+
/**
63+
* Remove the head event from the bus. This method will not block if the event bus is empty will return empty optional.
64+
*/
65+
Optional<T> remove();
66+
5767
/**
5868
* Whether the bus is empty.
5969
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.eventbus;
19+
20+
import java.time.Duration;
21+
22+
public class LinearTimeRetryPolicy implements IEvent.RetryPolicy {
23+
24+
private int currentRetryTimes = 0;
25+
26+
private final long firstRetryTimeStamp = System.currentTimeMillis();
27+
28+
private long nextRetryTimeStamp = firstRetryTimeStamp;
29+
30+
private static final int maxRetryTimes = 180;
31+
32+
private static final long maxRetryDuration = Duration.ofDays(1).toMillis();
33+
34+
private static final long baseRetryInterval = Duration.ofSeconds(10).toMillis();
35+
36+
@Override
37+
public boolean isRetryPolicyBroken() {
38+
if (currentRetryTimes > maxRetryTimes) {
39+
return true;
40+
}
41+
if (nextRetryTimeStamp > firstRetryTimeStamp + maxRetryDuration) {
42+
return true;
43+
}
44+
return false;
45+
}
46+
47+
@Override
48+
public void increaseRetryTimes() {
49+
currentRetryTimes++;
50+
setNextRetryTimeStampWithExponentialStep();
51+
}
52+
53+
@Override
54+
public void setNextRetryTimeStampWithFixedStep() {
55+
nextRetryTimeStamp += baseRetryInterval;
56+
}
57+
58+
@Override
59+
public void setNextRetryTimeStampWithExponentialStep() {
60+
nextRetryTimeStamp += baseRetryInterval * currentRetryTimes;
61+
}
62+
63+
@Override
64+
public long getNextRetryTimeStamp() {
65+
return nextRetryTimeStamp;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "LinearTimeRetryPolicy{" +
71+
"firstRetryTimeStamp=" + firstRetryTimeStamp +
72+
", currentRetryTimes=" + currentRetryTimes +
73+
'}';
74+
}
75+
}

dolphinscheduler-extract/dolphinscheduler-extract-base/src/main/java/org/apache/dolphinscheduler/extract/base/serialize/JsonSerializer.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@
2525

2626
import org.apache.dolphinscheduler.common.utils.JSONUtils;
2727

28-
import java.io.IOException;
2928
import java.nio.charset.StandardCharsets;
3029
import java.text.SimpleDateFormat;
3130
import java.time.LocalDateTime;
3231
import java.util.TimeZone;
3332

33+
import lombok.SneakyThrows;
3434
import lombok.extern.slf4j.Slf4j;
3535

3636
import com.fasterxml.jackson.core.JsonProcessingException;
3737
import com.fasterxml.jackson.databind.ObjectMapper;
3838
import com.fasterxml.jackson.databind.json.JsonMapper;
3939
import com.fasterxml.jackson.databind.module.SimpleModule;
4040

41-
/**
42-
* json serialize or deserialize
43-
*/
4441
@Slf4j
4542
public class JsonSerializer {
4643

@@ -60,13 +57,6 @@ private JsonSerializer() {
6057

6158
}
6259

63-
/**
64-
* serialize to byte
65-
*
66-
* @param obj object
67-
* @param <T> object type
68-
* @return byte array
69-
*/
7060
public static <T> byte[] serialize(T obj) {
7161
if (obj == null) {
7262
return null;
@@ -79,44 +69,14 @@ public static <T> byte[] serialize(T obj) {
7969
}
8070
}
8171

82-
/**
83-
* serialize to string
84-
*
85-
* @param obj object
86-
* @param <T> object type
87-
* @return string
88-
*/
89-
public static <T> String serializeToString(T obj) {
90-
String json = "";
91-
try {
92-
json = objectMapper.writeValueAsString(obj);
93-
} catch (JsonProcessingException e) {
94-
log.error("serializeToString exception!", e);
95-
}
96-
97-
return json;
98-
}
99-
100-
/**
101-
* deserialize
102-
*
103-
* @param src byte array
104-
* @param clazz class
105-
* @param <T> deserialize type
106-
* @return deserialize type
107-
*/
72+
@SneakyThrows
10873
public static <T> T deserialize(byte[] src, Class<T> clazz) {
10974
if (src == null) {
11075
return null;
11176
}
11277

11378
String json = new String(src, StandardCharsets.UTF_8);
114-
try {
115-
return objectMapper.readValue(json, clazz);
116-
} catch (IOException e) {
117-
log.error("deserialize exception!", e);
118-
return null;
119-
}
79+
return objectMapper.readValue(json, clazz);
12080
}
12181

12282
}

dolphinscheduler-extract/dolphinscheduler-extract-base/src/main/java/org/apache/dolphinscheduler/extract/base/server/JdkDynamicServerHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ private void processReceived(final Channel channel, final Transporter transporte
102102
} else {
103103
args = new Object[standardRpcRequest.getArgs().length];
104104
for (int i = 0; i < standardRpcRequest.getArgs().length; i++) {
105-
args[i] = JsonSerializer.deserialize(standardRpcRequest.getArgs()[i],
105+
args[i] = JsonSerializer.deserialize(
106+
standardRpcRequest.getArgs()[i],
106107
standardRpcRequest.getArgsTypes()[i]);
107108
}
108109
}

dolphinscheduler-extract/dolphinscheduler-extract-common/src/main/java/org/apache/dolphinscheduler/extract/common/ILogService.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import org.apache.dolphinscheduler.extract.base.RpcMethod;
2121
import org.apache.dolphinscheduler.extract.base.RpcService;
22-
import org.apache.dolphinscheduler.extract.common.transportor.GetAppIdRequest;
23-
import org.apache.dolphinscheduler.extract.common.transportor.GetAppIdResponse;
2422
import org.apache.dolphinscheduler.extract.common.transportor.TaskInstanceLogFileDownloadRequest;
2523
import org.apache.dolphinscheduler.extract.common.transportor.TaskInstanceLogFileDownloadResponse;
2624
import org.apache.dolphinscheduler.extract.common.transportor.TaskInstanceLogPageQueryRequest;
@@ -35,9 +33,6 @@ public interface ILogService {
3533
@RpcMethod
3634
TaskInstanceLogPageQueryResponse pageQueryTaskInstanceLog(TaskInstanceLogPageQueryRequest taskInstanceLogPageQueryRequest);
3735

38-
@RpcMethod
39-
GetAppIdResponse getAppId(GetAppIdRequest getAppIdRequest);
40-
4136
@RpcMethod
4237
void removeTaskInstanceLog(String taskInstanceLogAbsolutePath);
4338

0 commit comments

Comments
 (0)