From 17439be724d168854a4406f2ddbdb13da931b582 Mon Sep 17 00:00:00 2001 From: paderlol Date: Wed, 31 Jul 2024 15:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=89=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E5=8F=AF=E4=BB=A5=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=9B=86=E7=BE=A4=E4=BF=A1=E6=81=AF=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20#371?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacossync/dao/TaskAccessService.java | 69 ++++++++++--------- .../dao/repository/TaskRepository.java | 7 +- .../processor/ClusterAddProcessor.java | 19 +++-- .../processor/ClusterDeleteProcessor.java | 36 ++++++---- 4 files changed, 73 insertions(+), 58 deletions(-) diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/TaskAccessService.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/TaskAccessService.java index bb215a21..ad4f11c9 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/TaskAccessService.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/TaskAccessService.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacossync.dao; import com.alibaba.nacossync.constant.SkyWalkerConstants; @@ -39,7 +40,7 @@ */ @Service public class TaskAccessService implements PageQueryService { - + private final TaskRepository taskRepository; public TaskAccessService(TaskRepository taskRepository) { @@ -47,74 +48,78 @@ public TaskAccessService(TaskRepository taskRepository) { } public TaskDO findByTaskId(String taskId) { - + return taskRepository.findByTaskId(taskId); } - + public void deleteTaskById(String taskId) { taskRepository.deleteByTaskId(taskId); } /** * batch delete tasks by taskIds - * @author yongchao9 + * * @param taskIds + * @author yongchao9 */ public void deleteTaskInBatch(List taskIds) { - List tds=taskRepository.findAllByTaskIdIn(taskIds); + List tds = taskRepository.findAllByTaskIdIn(taskIds); taskRepository.deleteAllInBatch(tds); } - + public Iterable findAll() { - + return taskRepository.findAll(); } - + public void addTask(TaskDO taskDO) { - + taskRepository.save(taskDO); - + } - + + public int countByDestClusterIdOrSourceClusterId(String destClusterId, String sourceClusterId) { + return taskRepository.countByDestClusterIdOrSourceClusterId(destClusterId, sourceClusterId); + } + private Predicate getPredicate(CriteriaBuilder criteriaBuilder, List predicates) { Predicate[] p = new Predicate[predicates.size()]; return criteriaBuilder.and(predicates.toArray(p)); } - - private List getPredicates(Root root, CriteriaBuilder criteriaBuilder, QueryCondition queryCondition) { - + + private List getPredicates(Root root, CriteriaBuilder criteriaBuilder, + QueryCondition queryCondition) { + List predicates = new ArrayList<>(); predicates.add(criteriaBuilder.like(root.get("serviceName"), "%" + queryCondition.getServiceName() + "%")); - + return predicates; } - + @Override public Page findPageNoCriteria(Integer pageNum, Integer size) { - + Pageable pageable = PageRequest.of(pageNum, size, Sort.Direction.DESC, "id"); - + return taskRepository.findAll(pageable); } - + @Override public Page findPageCriteria(Integer pageNum, Integer size, QueryCondition queryCondition) { - + Pageable pageable = PageRequest.of(pageNum, size, Sort.Direction.DESC, "id"); - + return getTaskDOS(queryCondition, pageable); } - + private Page getTaskDOS(QueryCondition queryCondition, Pageable pageable) { - return taskRepository.findAll( - (Specification) (root, criteriaQuery, criteriaBuilder) -> { - - List predicates = getPredicates(root, - criteriaBuilder, queryCondition); - - return getPredicate(criteriaBuilder, predicates); - - }, pageable); + return taskRepository.findAll((Specification) (root, criteriaQuery, criteriaBuilder) -> { + + List predicates = getPredicates(root, criteriaBuilder, queryCondition); + + return getPredicate(criteriaBuilder, predicates); + + }, pageable); } public List findAllByServiceNameEqualAll() { @@ -124,5 +129,5 @@ public List findAllByServiceNameEqualAll() { public List findAllByServiceNameNotEqualAll() { return taskRepository.findAllByServiceNameNotIgnoreCase(SkyWalkerConstants.NACOS_ALL_SERVICE_NAME); } - + } diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/repository/TaskRepository.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/repository/TaskRepository.java index 90456366..bfbc7023 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/repository/TaskRepository.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/dao/repository/TaskRepository.java @@ -34,16 +34,15 @@ public interface TaskRepository extends CrudRepository, JpaRepo TaskDO findByTaskId(String taskId); @Transactional - int deleteByTaskId(String taskId); + void deleteByTaskId(String taskId); List findAllByTaskIdIn(List taskIds); - - List getAllByWorkerIp(String workerIp); - /** * query service is all,use ns leven sync data */ List findAllByServiceNameEqualsIgnoreCase(String serviceName); List findAllByServiceNameNotIgnoreCase(String serviceName); + + int countByDestClusterIdOrSourceClusterId(String destClusterId,String sourceClusterId); } diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java index 5b99725e..1087977f 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java @@ -19,7 +19,6 @@ import com.alibaba.nacossync.constant.ClusterTypeEnum; import com.alibaba.nacossync.dao.ClusterAccessService; import com.alibaba.nacossync.exception.SkyWalkerException; -import com.alibaba.nacossync.monitor.MetricsManager; import com.alibaba.nacossync.pojo.model.ClusterDO; import com.alibaba.nacossync.pojo.request.ClusterAddRequest; import com.alibaba.nacossync.pojo.result.ClusterAddResult; @@ -28,7 +27,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -38,16 +36,17 @@ @Slf4j @Service public class ClusterAddProcessor implements Processor { + - @Autowired - private MetricsManager metricsManager; - - @Autowired - private ClusterAccessService clusterAccessService; - - @Autowired - private ObjectMapper objectMapper; + private final ClusterAccessService clusterAccessService; + private final ObjectMapper objectMapper; + + public ClusterAddProcessor(ClusterAccessService clusterAccessService, ObjectMapper objectMapper) { + this.clusterAccessService = clusterAccessService; + this.objectMapper = objectMapper; + } + @Override public void process(ClusterAddRequest clusterAddRequest, ClusterAddResult clusterAddResult, Object... others) throws Exception { diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterDeleteProcessor.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterDeleteProcessor.java index 95536d4a..07d2b1e5 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterDeleteProcessor.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterDeleteProcessor.java @@ -14,15 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacossync.template.processor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; +package com.alibaba.nacossync.template.processor; import com.alibaba.nacossync.dao.ClusterAccessService; -import com.alibaba.nacossync.pojo.result.ClusterDeleteResult; +import com.alibaba.nacossync.dao.TaskAccessService; +import com.alibaba.nacossync.exception.SkyWalkerException; import com.alibaba.nacossync.pojo.request.ClusterDeleteRequest; +import com.alibaba.nacossync.pojo.result.ClusterDeleteResult; import com.alibaba.nacossync.template.Processor; +import org.springframework.stereotype.Service; /** * @author NacosSync @@ -30,15 +31,26 @@ */ @Service public class ClusterDeleteProcessor implements Processor { - - @Autowired - private ClusterAccessService clusterAccessService; - + + private final ClusterAccessService clusterAccessService; + + private final TaskAccessService taskAccessService; + + public ClusterDeleteProcessor(ClusterAccessService clusterAccessService, TaskAccessService taskAccessService) { + this.clusterAccessService = clusterAccessService; + this.taskAccessService = taskAccessService; + } + @Override - public void process(ClusterDeleteRequest clusterDeleteRequest, - ClusterDeleteResult clusterDeleteResult, Object... others) throws Exception { - + public void process(ClusterDeleteRequest clusterDeleteRequest, ClusterDeleteResult clusterDeleteResult, + Object... others) throws Exception { + int count = taskAccessService.countByDestClusterIdOrSourceClusterId(clusterDeleteRequest.getClusterId(), + clusterDeleteRequest.getClusterId()); + if (count > 0) { + throw new SkyWalkerException(String.format("集群下有%d个任务,请先删除任务", count)); + } + clusterAccessService.deleteByClusterId(clusterDeleteRequest.getClusterId()); - + } }