Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix-16934][api] When creating workflows containing switch nodes in different orders, the copied workflows may lose associations. #16939

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2118,37 +2118,38 @@ protected void doBatchOperateWorkflowDefinition(User loginUser,
List<TaskDefinitionLog> taskDefinitionLogs =
taskDefinitionLogDao.queryTaskDefineLogList(workflowTaskRelations);
Map<Long, Long> taskCodeMap = new HashMap<>();
for (TaskDefinitionLog taskDefinitionLog : taskDefinitionLogs) {
taskDefinitionLogs.forEach(taskDefinitionLog -> {
try {
long taskCode = CodeGenerateUtils.genCode();
taskCodeMap.put(taskDefinitionLog.getCode(), taskCode);
taskDefinitionLog.setCode(taskCode);
if (TaskTypeUtils.isSwitchTask(taskDefinitionLog.getTaskType())) {
final String taskParams = taskDefinitionLog.getTaskParams();
final SwitchParameters switchParameters =
JSONUtils.parseObject(taskParams, SwitchParameters.class);
if (switchParameters == null) {
throw new IllegalArgumentException(
"Switch task params: " + taskParams + " is invalid.");
}
SwitchParameters.SwitchResult switchResult = switchParameters.getSwitchResult();
switchResult.getDependTaskList().forEach(switchResultVo -> {
switchResultVo.setNextNode(taskCodeMap.get(switchResultVo.getNextNode()));
});
if (switchResult.getNextNode() != null) {
switchResult.setNextNode(
taskCodeMap.get(switchResult.getNextNode()));
}
taskDefinitionLog.setTaskParams(JSONUtils.toJsonString(switchParameters));
}
taskCodeMap.put(taskDefinitionLog.getCode(), CodeGenerateUtils.genCode());
} catch (CodeGenerateException e) {
log.error("Generate task definition code error, projectCode:{}.", targetProjectCode, e);
putMsg(result, Status.INTERNAL_SERVER_ERROR_ARGS);
throw new ServiceException(Status.INTERNAL_SERVER_ERROR_ARGS);
}
});
for (TaskDefinitionLog taskDefinitionLog : taskDefinitionLogs) {
taskDefinitionLog.setCode(taskCodeMap.get(taskDefinitionLog.getCode()));
taskDefinitionLog.setProjectCode(targetProjectCode);
taskDefinitionLog.setVersion(0);
taskDefinitionLog.setName(taskDefinitionLog.getName());
if (TaskTypeUtils.isSwitchTask(taskDefinitionLog.getTaskType())) {
final String taskParams = taskDefinitionLog.getTaskParams();
final SwitchParameters switchParameters =
JSONUtils.parseObject(taskParams, SwitchParameters.class);
if (switchParameters == null) {
throw new IllegalArgumentException(
"Switch task params: " + taskParams + " is invalid.");
}
SwitchParameters.SwitchResult switchResult = switchParameters.getSwitchResult();
switchResult.getDependTaskList().forEach(switchResultVo -> {
switchResultVo.setNextNode(taskCodeMap.get(switchResultVo.getNextNode()));
});
if (switchResult.getNextNode() != null) {
switchResult.setNextNode(
taskCodeMap.get(switchResult.getNextNode()));
}
taskDefinitionLog.setTaskParams(JSONUtils.toJsonString(switchParameters));
}
Comment on lines +2135 to +2152
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this code to here?

Copy link
Contributor Author

@llllkid llllkid Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this code to here?

I think this is git's display problem,
I don't move this code to here,
I added taskDefinitionLogs.forEach before for (TaskDefinitionLog taskDefinitionLog : taskDefinitionLogs), because we need put all taskDefinitionLog's new code into taskCodeMap before taskDefinitionLog.setTaskParams.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@llllkid llllkid Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit shows it's not a display issue. https://github.com/apache/dolphinscheduler/blob/48304f2a6c20de7471739b8889c2d02c41eef17d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java

I think I misunderstood your question.
Do you want to ask me why I move this code after taskDefinitionLog.setName?
Because for better readability:

 taskDefinitionLog.setCode();
 taskDefinitionLog.setProjectCode();
 taskDefinitionLog.setVersion();
 taskDefinitionLog.setName();
 if (TaskTypeUtils.isSwitchTask(taskDefinitionLog.getTaskType())) {
 ...
      taskDefinitionLog.setTaskParams();
 ...
 }

Is better than

 taskDefinitionLog.setCode();
 if (TaskTypeUtils.isSwitchTask(taskDefinitionLog.getTaskType())) {
 ...
      taskDefinitionLog.setTaskParams();
 ...
 }
 taskDefinitionLog.setProjectCode();
 taskDefinitionLog.setVersion();
 taskDefinitionLog.setName();

}
for (WorkflowTaskRelationLog workflowTaskRelationLog : taskRelationList) {
if (workflowTaskRelationLog.getPreTaskCode() > 0) {
Expand Down
Loading