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 e587de7f49 [Fix-18733][API] Use target task relations when switching 
versions (#18734)
e587de7f49 is described below

commit e587de7f49ea5b565d3de50774dd78986d04bfc7
Author: Kerwin <[email protected]>
AuthorDate: Sat Oct 24 09:49:26 2026 +0800

    [Fix-18733][API] Use target task relations when switching versions (#18734)
---
 .../impl/WorkflowDefinitionServiceImpl.java        | 13 ++++---
 .../api/service/WorkflowDefinitionServiceTest.java | 43 +++++++++++++++++++---
 2 files changed, 45 insertions(+), 11 deletions(-)

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 73e09aea63..3527fa501a 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
@@ -1694,12 +1694,15 @@ public class WorkflowDefinitionServiceImpl extends 
BaseServiceImpl implements Wo
                     
Status.SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR,
                     workflowDefinition.getCode(), version);
         }
-        List<WorkflowTaskRelation> workflowTaskRelationList = 
workflowTaskRelationDao
-                
.queryWorkflowTaskRelationsByWorkflowDefinitionCode(workflowDefinitionLog.getCode(),
-                        workflowDefinitionLog.getVersion());
+        List<WorkflowTaskRelation> workflowTaskRelationList = 
workflowTaskRelationLogMapper
+                
.queryByWorkflowCodeAndVersion(workflowDefinitionLog.getCode(), 
workflowDefinitionLog.getVersion())
+                .stream()
+                .map(WorkflowTaskRelation::new)
+                .collect(Collectors.toList());
         List<TaskCodeVersionDto> taskDefinitionList = 
getTaskCodeVersionDtos(workflowTaskRelationList);
-        List<TaskDefinitionLog> taskDefinitionLogList =
-                
taskDefinitionLogMapper.queryByTaskDefinitions(taskDefinitionList.stream()
+        List<TaskDefinitionLog> taskDefinitionLogList = 
CollectionUtils.isEmpty(taskDefinitionList)
+                ? Collections.emptyList()
+                : 
taskDefinitionLogMapper.queryByTaskDefinitions(taskDefinitionList.stream()
                         .flatMap(taskCodeVersionDto -> {
                             TaskDefinitionLog taskDefinitionLog = new 
TaskDefinitionLog();
                             
taskDefinitionLog.setCode(taskCodeVersionDto.getCode());
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 3824116092..3eba2e410e 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
@@ -63,6 +63,7 @@ import 
org.apache.dolphinscheduler.dao.entity.UserWithWorkflowDefinitionCode;
 import org.apache.dolphinscheduler.dao.entity.WorkflowDefinition;
 import org.apache.dolphinscheduler.dao.entity.WorkflowDefinitionLog;
 import org.apache.dolphinscheduler.dao.entity.WorkflowTaskRelation;
+import org.apache.dolphinscheduler.dao.entity.WorkflowTaskRelationLog;
 import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper;
 import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionLogMapper;
 import org.apache.dolphinscheduler.dao.mapper.WorkflowTaskRelationLogMapper;
@@ -1114,8 +1115,8 @@ public class WorkflowDefinitionServiceTest extends 
BaseServiceTestTool {
         
when(workflowDefinitionDao.queryByCode(processDefinitionCode)).thenReturn(Optional.of(workflowDefinition));
         
when(workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(processDefinitionCode,
 1))
                 .thenReturn(workflowDefinitionLog);
-        
when(workflowTaskRelationDao.queryWorkflowTaskRelationsByWorkflowDefinitionCode(processDefinitionCode,
 1))
-                .thenReturn(Collections.singletonList(workflowTaskRelation));
+        
when(workflowTaskRelationLogMapper.queryByWorkflowCodeAndVersion(processDefinitionCode,
 1))
+                .thenReturn(Collections.singletonList(new 
WorkflowTaskRelationLog(workflowTaskRelation)));
         when(taskDefinitionLogMapper.queryByTaskDefinitions(anyList()))
                 .thenReturn(Collections.singletonList(new 
TaskDefinitionLog()));
         doThrow(new 
ServiceException(Status.RESOURCE_NOT_EXIST_OR_NO_PERMISSION))
@@ -1126,10 +1127,40 @@ public class WorkflowDefinitionServiceTest extends 
BaseServiceTestTool {
                         user, projectCode, processDefinitionCode, 1));
 
         
Assertions.assertEquals(Status.RESOURCE_NOT_EXIST_OR_NO_PERMISSION.getCode(), 
exception.getCode());
+        
verify(workflowTaskRelationLogMapper).queryByWorkflowCodeAndVersion(processDefinitionCode,
 1);
+        verify(workflowTaskRelationDao, Mockito.never())
+                .queryWorkflowTaskRelationsByWorkflowDefinitionCode(anyLong(), 
Mockito.anyInt());
+        verify(taskDefinitionLogMapper)
+                .queryByTaskDefinitions(Mockito.argThat(taskDefinitions -> 
taskDefinitions.size() == 1
+                        && taskDefinitions.iterator().next().getCode() == 
workflowTaskRelation.getPostTaskCode()
+                        && taskDefinitions.iterator().next().getVersion() == 
workflowTaskRelation
+                                .getPostTaskVersion()));
         Mockito.verify(processService, Mockito.never())
                 .switchVersion(any(WorkflowDefinition.class), 
any(WorkflowDefinitionLog.class));
     }
 
+    @Test
+    public void 
testSwitchWorkflowDefinitionVersionShouldNotQueryAllTasksWhenTargetVersionHasNoRelations()
 {
+        WorkflowDefinition workflowDefinition = getWorkflowDefinition();
+        WorkflowDefinitionLog workflowDefinitionLog = new 
WorkflowDefinitionLog();
+        workflowDefinitionLog.setCode(processDefinitionCode);
+        workflowDefinitionLog.setProjectCode(projectCode);
+        workflowDefinitionLog.setVersion(1);
+
+        
when(workflowDefinitionDao.queryByCode(processDefinitionCode)).thenReturn(Optional.of(workflowDefinition));
+        
when(workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(processDefinitionCode,
 1))
+                .thenReturn(workflowDefinitionLog);
+        
when(workflowTaskRelationLogMapper.queryByWorkflowCodeAndVersion(processDefinitionCode,
 1))
+                .thenReturn(Collections.emptyList());
+
+        Assertions.assertThrows(ServiceException.class,
+                () -> 
workflowDefinitionService.switchWorkflowDefinitionVersion(
+                        user, projectCode, processDefinitionCode, 1));
+
+        verify(taskDefinitionLogMapper, 
Mockito.never()).queryByTaskDefinitions(anyList());
+        verify(processService).switchVersion(workflowDefinition, 
workflowDefinitionLog);
+    }
+
     @Test
     public void 
testSwitchWorkflowDefinitionVersionShouldRejectUnavailableSubWorkflow() {
         WorkflowDefinition workflowDefinition = getWorkflowDefinition();
@@ -1143,8 +1174,8 @@ public class WorkflowDefinitionServiceTest extends 
BaseServiceTestTool {
         
when(workflowDefinitionDao.queryByCode(processDefinitionCode)).thenReturn(Optional.of(workflowDefinition));
         
when(workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(processDefinitionCode,
 1))
                 .thenReturn(workflowDefinitionLog);
-        
when(workflowTaskRelationDao.queryWorkflowTaskRelationsByWorkflowDefinitionCode(processDefinitionCode,
 1))
-                .thenReturn(Collections.singletonList(workflowTaskRelation));
+        
when(workflowTaskRelationLogMapper.queryByWorkflowCodeAndVersion(processDefinitionCode,
 1))
+                .thenReturn(Collections.singletonList(new 
WorkflowTaskRelationLog(workflowTaskRelation)));
         when(taskDefinitionLogMapper.queryByTaskDefinitions(anyList()))
                 .thenReturn(Collections.singletonList(new 
TaskDefinitionLog()));
         doThrow(new 
ServiceException(Status.RESOURCE_NOT_EXIST_OR_NO_PERMISSION))
@@ -1174,8 +1205,8 @@ public class WorkflowDefinitionServiceTest extends 
BaseServiceTestTool {
         
when(workflowDefinitionDao.queryByCode(processDefinitionCode)).thenReturn(Optional.of(workflowDefinition));
         
when(workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(processDefinitionCode,
 1))
                 .thenReturn(workflowDefinitionLog);
-        
when(workflowTaskRelationDao.queryWorkflowTaskRelationsByWorkflowDefinitionCode(processDefinitionCode,
 1))
-                .thenReturn(Collections.singletonList(workflowTaskRelation));
+        
when(workflowTaskRelationLogMapper.queryByWorkflowCodeAndVersion(processDefinitionCode,
 1))
+                .thenReturn(Collections.singletonList(new 
WorkflowTaskRelationLog(workflowTaskRelation)));
         when(taskDefinitionLogMapper.queryByTaskDefinitions(anyList()))
                 .thenReturn(Collections.singletonList(taskDefinitionLog));
         when(processService.switchVersion(workflowDefinition, 
workflowDefinitionLog)).thenReturn(1);

Reply via email to