This is an automated email from the ASF dual-hosted git repository.
SbloodyS pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new 89817f16f6 [Improvement-18675][API] Return TASK_DEFINE_NOT_EXIST
instead of NPE when taskRelation references missing task definitions (#18676)
89817f16f6 is described below
commit 89817f16f69b30ae3a12631f4f96653e1d3c96ce
Author: wank125 <[email protected]>
AuthorDate: Wed Oct 21 17:07:00 2026 +0800
[Improvement-18675][API] Return TASK_DEFINE_NOT_EXIST instead of NPE when
taskRelation references missing task definitions (#18676)
---
.../apache/dolphinscheduler/api/enums/Status.java | 1 +
.../impl/WorkflowDefinitionServiceImpl.java | 33 +++++++++-
.../api/service/WorkflowDefinitionServiceTest.java | 71 ++++++++++++++++++++++
.../api/service/WorkflowInstanceServiceTest.java | 40 ++++++++++++
.../service/process/ProcessServiceImpl.java | 22 ++++++-
5 files changed, 163 insertions(+), 4 deletions(-)
diff --git
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java
index 9c954e1757..790f30aa8b 100644
---
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java
+++
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java
@@ -413,6 +413,7 @@ public enum Status {
TASK_PARALLELISM_PARAMS_ERROR(50080, "task parallelism parameter is not
valid", "任务并行度参数无效"),
TASK_COMPLEMENT_DATA_DATE_ERROR(50081, "The range of date for
complementing date is not valid", "补数选择的日期范围无效"),
+ WORKFLOW_DAG_IS_INVALID(50082, "workflow dag is invalid, {0}",
"工作流dag无效,{0}"),
HDFS_NOT_STARTUP(60001, "hdfs not startup", "hdfs未启用"),
STORAGE_NOT_STARTUP(60002, "storage not startup", "存储未启用"),
diff --git
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java
index 771e47ccdb..5711f60db3 100644
---
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java
+++
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java
@@ -410,6 +410,26 @@ public class WorkflowDefinitionServiceImpl extends
BaseServiceImpl implements Wo
JSONUtils.toJsonString(workflowTaskRelationLog),
WorkflowTaskRelation.class))
.collect(Collectors.toList());
+ // Check that both the pre and post taskCodes in taskRelation
exist in taskDefinition,
+ // otherwise transformTask will be broken by missing task
definitions
+ Set<Long> taskDefinitionCodes =
+
taskDefinitionLogs.stream().map(TaskDefinitionLog::getCode).collect(Collectors.toSet());
+ Set<Long> missedTaskCodes = new HashSet<>();
+ for (WorkflowTaskRelationLog workflowTaskRelationLog :
taskRelationList) {
+ if
(!taskDefinitionCodes.contains(workflowTaskRelationLog.getPostTaskCode())) {
+
missedTaskCodes.add(workflowTaskRelationLog.getPostTaskCode());
+ }
+ if (workflowTaskRelationLog.getPreTaskCode() != 0L
+ &&
!taskDefinitionCodes.contains(workflowTaskRelationLog.getPreTaskCode())) {
+
missedTaskCodes.add(workflowTaskRelationLog.getPreTaskCode());
+ }
+ }
+ if (CollectionUtils.isNotEmpty(missedTaskCodes)) {
+ String taskCodes = StringUtils.join(missedTaskCodes,
Constants.COMMA);
+ log.error("Task definitions do not exist, taskCodes:{}.",
taskCodes);
+ throw new ServiceException(Status.WORKFLOW_DAG_IS_INVALID,
+ "the task definitions of taskCodes: [" + taskCodes +
"] do not exist");
+ }
List<TaskNode> taskNodeList =
processService.transformTask(workflowTaskRelations, taskDefinitionLogs);
if (taskNodeList.size() != taskRelationList.size()) {
Set<Long> postTaskCodes =
taskRelationList.stream().map(WorkflowTaskRelationLog::getPostTaskCode)
@@ -419,7 +439,8 @@ public class WorkflowDefinitionServiceImpl extends
BaseServiceImpl implements Wo
if (CollectionUtils.isNotEmpty(codes)) {
String taskCodes = StringUtils.join(codes,
Constants.COMMA);
log.error("Task definitions do not exist, taskCodes:{}.",
taskCodes);
- throw new ServiceException(Status.TASK_DEFINE_NOT_EXIST,
taskCodes);
+ throw new ServiceException(Status.WORKFLOW_DAG_IS_INVALID,
+ "the task definitions of taskCodes: [" + taskCodes
+ "] do not exist");
}
}
if (graphHasCycle(taskNodeList)) {
@@ -979,7 +1000,15 @@ public class WorkflowDefinitionServiceImpl extends
BaseServiceImpl implements Wo
List<WorkflowTaskRelation> taskRelationList =
JSONUtils.toList(workflowTaskRelationJson,
WorkflowTaskRelation.class);
// Check whether the task node is normal
- List<TaskNode> taskNodes =
processService.transformTask(taskRelationList, taskDefinitionLogsList);
+ List<TaskNode> taskNodes;
+ try {
+ taskNodes = processService.transformTask(taskRelationList,
taskDefinitionLogsList);
+ } catch
(org.apache.dolphinscheduler.service.exceptions.ServiceException e) {
+ // transformTask reports exactly what makes the dag
inconsistent, e.g.
+ // task codes referenced by relations but missing in task
definitions
+ log.error("Failed to build the workflow dag from the given
workflowTaskRelationJson", e);
+ throw new ServiceException(Status.WORKFLOW_DAG_IS_INVALID,
e.getMessage());
+ }
if (CollectionUtils.isEmpty(taskNodes)) {
log.error("Task node data is empty.");
diff --git
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowDefinitionServiceTest.java
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowDefinitionServiceTest.java
index 0dcc07f315..102c91cc71 100644
---
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowDefinitionServiceTest.java
+++
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowDefinitionServiceTest.java
@@ -29,6 +29,7 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -83,6 +84,7 @@ import
org.apache.dolphinscheduler.plugin.task.api.parameters.ConditionsParamete
import org.apache.dolphinscheduler.plugin.task.api.parameters.SwitchParameters;
import org.apache.dolphinscheduler.service.model.TaskNode;
import org.apache.dolphinscheduler.service.process.ProcessService;
+import org.apache.dolphinscheduler.service.process.ProcessServiceImpl;
import org.apache.commons.lang3.StringUtils;
@@ -803,6 +805,43 @@ public class WorkflowDefinitionServiceTest extends
BaseServiceTestTool {
Assertions.assertEquals(Status.WORKFLOW_DAG_IS_EMPTY.getCode(),
emptyDagEx.getCode());
}
+ @Test
+ public void testTransformTaskShouldRejectMissingPreTaskDefinition() {
+ // relation references preTaskCode 123451235 which has no task
definition; the shared
+ // transformation must fail explicitly instead of silently dropping
the dependency
+ String taskRelationJsonWithUnknownPreTaskCode =
+
"[{\"name\":\"\",\"preTaskCode\":123451235,\"preTaskVersion\":1,\"postTaskCode\":123456789,"
+ +
"\"postTaskVersion\":1,\"conditionType\":\"NONE\",\"conditionParams\":\"{}\"}]";
+ List<WorkflowTaskRelation> taskRelations =
+ JSONUtils.toList(taskRelationJsonWithUnknownPreTaskCode,
WorkflowTaskRelation.class);
+ List<TaskDefinitionLog> taskDefinitionLogs =
+ JSONUtils.toList(taskDefinitionJson, TaskDefinitionLog.class);
+
+ org.apache.dolphinscheduler.service.exceptions.ServiceException
exception =
+
Assertions.assertThrows(org.apache.dolphinscheduler.service.exceptions.ServiceException.class,
+ () -> new
ProcessServiceImpl().transformTask(taskRelations, taskDefinitionLogs));
+ Assertions.assertTrue(exception.getMessage().contains("123451235"));
+ }
+
+ @Test
+ public void testCheckWorkflowNodeListShouldReportMissingTaskCode() {
+ // the workflow-instance update path goes through
checkWorkflowNodeList -> transformTask,
+ // the missing task code must be reported as an invalid dag naming the
missing definitions
+ when(processService.transformTask(anyList(), anyList())).thenThrow(
+ new
org.apache.dolphinscheduler.service.exceptions.ServiceException(
+ "The task definitions of pre taskCodes: [123451235] do
not exist"));
+ List<TaskDefinitionLog> taskDefinitionLogs =
JSONUtils.toList(taskDefinitionJson, TaskDefinitionLog.class);
+
+ String taskRelationJsonWithUnknownPreTaskCode =
+
"[{\"name\":\"\",\"preTaskCode\":123451235,\"preTaskVersion\":1,\"postTaskCode\":123456789,"
+ +
"\"postTaskVersion\":1,\"conditionType\":\"NONE\",\"conditionParams\":\"{}\"}]";
+ ServiceException exception =
Assertions.assertThrows(ServiceException.class,
+ () ->
workflowDefinitionService.checkWorkflowNodeList(taskRelationJsonWithUnknownPreTaskCode,
+ taskDefinitionLogs));
+ Assertions.assertEquals(Status.WORKFLOW_DAG_IS_INVALID.getCode(),
exception.getCode());
+ Assertions.assertTrue(exception.getMessage().contains("123451235"));
+ }
+
@Test
public void testGetTaskNodeListByDefinitionCode() {
Project project = getProject(projectCode);
@@ -1004,6 +1043,38 @@ public class WorkflowDefinitionServiceTest extends
BaseServiceTestTool {
.saveTaskDefine(eq(user), eq(projectCode), anyList(),
eq(Boolean.TRUE));
}
+ @Test
+ public void testCreateWorkflowDefinitionShouldRejectUnknownPreTaskCode() {
+ Project project = getProject(projectCode);
+ when(projectDao.queryByCode(projectCode)).thenReturn(project);
+
Mockito.doNothing().when(projectService).checkHasProjectWritePermissionThrowException(eq(user),
eq(project));
+ when(workflowDefinitionDao.verifyByDefineName(projectCode,
name)).thenReturn(null);
+ // mimic the real transformTask: one node for the (existing) post task
of the single relation;
+ // lenient since with the fix the validation fails fast before
transformTask is even called
+ TaskNode taskNode = new TaskNode();
+ taskNode.setCode(123456789L);
+ taskNode.setPreTasks(JSONUtils.toJsonString(Collections.emptyList()));
+ lenient().when(processService.transformTask(anyList(), anyList()))
+ .thenReturn(Collections.singletonList(taskNode));
+
+ // preTaskCode 123451235 is referenced by the relation but missing in
taskDefinitionJson,
+ // this used to end up as a NullPointerException in transformTask and
was returned to the client
+ // as a generic REQUEST_PARAMS_NOT_VALID_ERROR
+ String taskRelationJsonWithUnknownPreTaskCode =
+
"[{\"name\":\"\",\"preTaskCode\":123451235,\"preTaskVersion\":1,\"postTaskCode\":123456789,"
+ +
"\"postTaskVersion\":1,\"conditionType\":0,\"conditionParams\":\"{}\"}]";
+
+ ServiceException exception =
Assertions.assertThrows(ServiceException.class,
+ () -> workflowDefinitionService.createWorkflowDefinition(
+ user, projectCode, name, description, "[]", "[]",
timeout,
+ taskRelationJsonWithUnknownPreTaskCode,
taskDefinitionJson, null,
+ WorkflowExecutionTypeEnum.PARALLEL));
+
+ Assertions.assertEquals(Status.WORKFLOW_DAG_IS_INVALID.getCode(),
exception.getCode());
+ Mockito.verify(processService, Mockito.never())
+ .saveTaskDefine(eq(user), eq(projectCode), anyList(),
eq(Boolean.TRUE));
+ }
+
@Test
public void
testCreateWorkflowDefinitionShouldRejectUnavailableSubWorkflow() {
Project project = getProject(projectCode);
diff --git
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
index 97bf50e731..b8b2ac9814 100644
---
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
+++
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkflowInstanceServiceTest.java
@@ -673,6 +673,46 @@ public class WorkflowInstanceServiceTest {
.saveTaskDefine(eq(loginUser), eq(projectCode),
Mockito.anyList(), eq(Boolean.TRUE));
}
+ @Test
+ public void testUpdateWorkflowInstanceShouldRejectUnknownPreTaskCode() {
+ long projectCode = 1L;
+ User loginUser = getAdminUser();
+ WorkflowInstance workflowInstance = getProcessInstance();
+ workflowInstance.setProjectCode(projectCode);
+ workflowInstance.setState(WorkflowExecutionStatus.SUCCESS);
+ WorkflowDefinition workflowDefinition = new WorkflowDefinition();
+ workflowDefinition.setProjectCode(projectCode);
+ workflowDefinition.setCode(46L);
+
+
doNothing().when(projectService).checkHasProjectWritePermissionThrowException(loginUser,
projectCode);
+
when(processService.findWorkflowInstanceDetailById(1)).thenReturn(Optional.of(workflowInstance));
+ when(processService.saveTaskDefine(eq(loginUser), eq(projectCode),
Mockito.anyList(), eq(Boolean.TRUE)))
+ .thenReturn(1);
+
when(workflowDefinitionDao.queryByCode(46L)).thenReturn(Optional.of(workflowDefinition));
+ // transformTask rejects the relation referencing a preTaskCode
without task definition
+ doThrow(new ServiceException(Status.WORKFLOW_DAG_IS_INVALID,
"123451235"))
+
.when(workflowDefinitionService).checkWorkflowNodeList(Mockito.anyString(),
Mockito.anyList());
+
+ try (
+ MockedStatic<TaskPluginManager> taskPluginManagerMockedStatic =
+ Mockito.mockStatic(TaskPluginManager.class)) {
+ taskPluginManagerMockedStatic
+ .when(() ->
TaskPluginManager.checkTaskParameters(Mockito.any(), Mockito.any()))
+ .thenReturn(true);
+
+ assertThrowsServiceException(Status.WORKFLOW_DAG_IS_INVALID,
+ () ->
workflowInstanceService.updateWorkflowInstance(loginUser, projectCode, 1,
+ taskRelationJson, taskDefinitionJson, "2020-02-21
00:00:00", true, "", "", 0));
+ }
+
+ // no invalid relation and no new definition version may be persisted
when the check fails
+ Mockito.verify(processService, Mockito.never())
+ .saveTaskRelation(Mockito.any(), Mockito.anyLong(),
Mockito.anyLong(), Mockito.anyInt(),
+ Mockito.anyList(), Mockito.anyList(),
Mockito.anyBoolean());
+ Mockito.verify(processService, Mockito.never())
+ .saveWorkflowDefine(Mockito.any(), Mockito.any(),
Mockito.anyBoolean(), Mockito.anyBoolean());
+ }
+
@Test
public void testUpdateWorkflowInstanceWithMismatchedProjectCode() {
long projectCode = 1L;
diff --git
a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
index 1469a909b2..fc6116309d 100644
---
a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
+++
b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
@@ -64,6 +64,7 @@ import
org.apache.dolphinscheduler.dao.repository.TaskInstanceDao;
import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao;
import
org.apache.dolphinscheduler.plugin.task.api.parameters.SubWorkflowParameters;
import
org.apache.dolphinscheduler.plugin.task.api.parameters.TaskTimeoutParameter;
+import org.apache.dolphinscheduler.service.exceptions.ServiceException;
import org.apache.dolphinscheduler.service.model.TaskNode;
import org.apache.dolphinscheduler.service.utils.ClusterConfUtils;
import org.apache.dolphinscheduler.service.utils.DagHelper;
@@ -700,6 +701,10 @@ public class ProcessServiceImpl implements ProcessService {
taskNode.setVersion(taskDefinitionLog.getVersion());
taskNode.setName(taskDefinitionLog.getName());
taskNode.setDesc(taskDefinitionLog.getDescription());
+ if (taskDefinitionLog.getTaskType() == null) {
+ throw new ServiceException("The taskType of taskDefinition
is null, taskCode: "
+ + taskDefinitionLog.getCode() + ", taskName: " +
taskDefinitionLog.getName());
+ }
taskNode.setType(taskDefinitionLog.getTaskType().toUpperCase());
taskNode.setRunFlag(taskDefinitionLog.getFlag() == Flag.YES ?
Constants.FLOWNODE_RUN_FLAG_NORMAL
: Constants.FLOWNODE_RUN_FLAG_FORBIDDEN);
@@ -714,8 +719,21 @@ public class ProcessServiceImpl implements ProcessService {
taskDefinitionLog.getTimeoutNotifyStrategy(),
taskDefinitionLog.getTimeout())));
taskNode.setDelayTime(taskDefinitionLog.getDelayTime());
-
taskNode.setPreTasks(JSONUtils.toJsonString(code.getValue().stream().map(taskDefinitionLogMap::get)
-
.map(TaskDefinition::getCode).collect(Collectors.toList())));
+ List<Long> missedPreTaskCodes = new ArrayList<>();
+ List<Long> preTaskCodes = new ArrayList<>();
+ for (Long preTaskCode : code.getValue()) {
+ if (taskDefinitionLogMap.containsKey(preTaskCode)) {
+ preTaskCodes.add(preTaskCode);
+ } else {
+ missedPreTaskCodes.add(preTaskCode);
+ }
+ }
+ if (!missedPreTaskCodes.isEmpty()) {
+ throw new ServiceException("The task definitions of pre
taskCodes: " + missedPreTaskCodes
+ + " do not exist, postTaskCode: " + code.getKey()
+ + ", please check the task taskRelation");
+ }
+ taskNode.setPreTasks(JSONUtils.toJsonString(preTaskCodes));
taskNode.setTaskGroupId(taskDefinitionLog.getTaskGroupId());
taskNode.setTaskGroupPriority(taskDefinitionLog.getTaskGroupPriority());
taskNode.setCpuQuota(taskDefinitionLog.getCpuQuota());