diff --git a/server/odc-migrate/src/main/resources/migrate/common/V_4_2_4_5__add_flow_instance_index.sql b/server/odc-migrate/src/main/resources/migrate/common/V_4_2_4_5__add_flow_instance_index.sql new file mode 100644 index 0000000000..ae4eb8996c --- /dev/null +++ b/server/odc-migrate/src/main/resources/migrate/common/V_4_2_4_5__add_flow_instance_index.sql @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 OceanBase. + * + * Licensed 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. + */ + + +alter table `flow_instance` add index `flow_instance_parent_ins_id`(`parent_instance_id`); + +alter table `flow_instance_node_task` add index `flow_instance_node_task_flow_instance_id`(`flow_instance_id`); + +alter table `flow_instance` add index `flow_instance_parent_oid_ct_id`(`organization_id`,`create_time`,`id`); + +CREATE or replace VIEW `list_flow_instance_view` AS +select + `flow_instance`.`id` AS `id`, + `flow_instance`.`create_time` AS `create_time`, + `flow_instance`.`update_time` AS `update_time`, + `flow_instance`.`name` AS `name`, + `flow_instance`.`flow_config_id` AS `flow_config_id`, + `flow_instance`.`creator_id` AS `creator_id`, + `flow_instance`.`organization_id` AS `organization_id`, + `flow_instance`.`process_definition_id` AS `process_definition_id`, + `flow_instance`.`process_instance_id` AS `process_instance_id`, + `flow_instance`.`status` AS `status`, + `flow_instance`.`flow_config_snapshot_xml` AS `flow_config_snapshot_xml`, + `flow_instance`.`description` AS `description`, + `flow_instance`.`parent_instance_id` AS `parent_instance_id`, + `flow_instance`.`project_id` AS `project_id`, + `flow_instance_node_task`.`task_type` AS `task_type` +from + ( + `flow_instance` join `flow_instance_node_task` on ( `flow_instance`.`id` = `flow_instance_node_task`.`flow_instance_id`) + ) +group by `flow_instance`.`id`,`flow_instance_node_task`.`task_type` \ No newline at end of file diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/metadb/flow/FlowInstanceRepository.java b/server/odc-service/src/main/java/com/oceanbase/odc/metadb/flow/FlowInstanceRepository.java index 5bf5f6a3df..1ae1def040 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/metadb/flow/FlowInstanceRepository.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/metadb/flow/FlowInstanceRepository.java @@ -21,12 +21,12 @@ import javax.transaction.Transactional; -import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import com.oceanbase.odc.config.jpa.OdcJpaRepository; import com.oceanbase.odc.core.shared.constant.FlowStatus; import com.oceanbase.odc.core.shared.constant.TaskType; @@ -39,7 +39,7 @@ * @see org.springframework.data.jpa.repository.JpaRepository */ public interface FlowInstanceRepository - extends JpaRepository, JpaSpecificationExecutor { + extends OdcJpaRepository, JpaSpecificationExecutor { List findByIdIn(Collection ids); @@ -82,6 +82,12 @@ int updateProcessDefinitionIdById(@Param("flowInstanceId") Long flowInstanceId, List findByParentInstanceId(Long parentInstanceId); + List findByParentInstanceIdIn(Collection parentInstanceId); + + default List partitionFindByParentInstanceIdIn(Collection parentInstanceId) { + return partitionFind(parentInstanceId, 200, this::findByParentInstanceIdIn); + } + @Query(value = "select a.parent_instance_id from flow_instance a left join flow_instance_node_task b on a.id = b.flow_instance_id" + " where a.id=:id and task_task_id is not null and b.task_type='ALTER_SCHEDULE' LIMIT 1", nativeQuery = true) diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/flow/factory/FlowResponseMapperFactory.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/flow/factory/FlowResponseMapperFactory.java index e3d52f8bce..ec04b42215 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/flow/factory/FlowResponseMapperFactory.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/flow/factory/FlowResponseMapperFactory.java @@ -282,8 +282,13 @@ private FlowInstanceMapper generateMapper(@NonNull Collection flowInstance .collect(Collectors.groupingBy(ServiceTaskInstanceEntity::getFlowInstanceId, Collectors.mapping(ServiceTaskInstanceEntity::getStrategy, Collectors.toList()))); + Map> parentInstanceIdMap = flowInstanceRepository + .partitionFindByParentInstanceIdIn( + flowInstanceIds) + .stream().collect(Collectors.groupingBy(FlowInstanceEntity::getParentInstanceId)); + Map flowInstanceId2Rollbackable = flowInstanceIds.stream().collect(Collectors - .toMap(Function.identity(), id -> flowInstanceRepository.findByParentInstanceId(id).size() == 0)); + .toMap(Function.identity(), id -> CollectionUtils.isEmpty(parentInstanceIdMap.get(id)))); /** * In order to improve the interface efficiency, it is necessary to find out the task entity