Skip to content

Commit

Permalink
Merge pull request #373 from paderlol/add_validation_cluster
Browse files Browse the repository at this point in the history
修复有任务运行时可以删除集群信息的问题 #371
  • Loading branch information
paderlol authored Jul 31, 2024
2 parents 6645b00 + 17439be commit 81610e5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,82 +40,86 @@
*/
@Service
public class TaskAccessService implements PageQueryService<TaskDO> {

private final TaskRepository taskRepository;

public TaskAccessService(TaskRepository taskRepository) {
this.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<String> taskIds) {
List<TaskDO> tds=taskRepository.findAllByTaskIdIn(taskIds);
List<TaskDO> tds = taskRepository.findAllByTaskIdIn(taskIds);
taskRepository.deleteAllInBatch(tds);
}

public Iterable<TaskDO> 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<Predicate> predicates) {
Predicate[] p = new Predicate[predicates.size()];
return criteriaBuilder.and(predicates.toArray(p));
}

private List<Predicate> getPredicates(Root<TaskDO> root, CriteriaBuilder criteriaBuilder, QueryCondition queryCondition) {


private List<Predicate> getPredicates(Root<TaskDO> root, CriteriaBuilder criteriaBuilder,
QueryCondition queryCondition) {

List<Predicate> predicates = new ArrayList<>();
predicates.add(criteriaBuilder.like(root.get("serviceName"), "%" + queryCondition.getServiceName() + "%"));

return predicates;
}

@Override
public Page<TaskDO> findPageNoCriteria(Integer pageNum, Integer size) {

Pageable pageable = PageRequest.of(pageNum, size, Sort.Direction.DESC, "id");

return taskRepository.findAll(pageable);
}

@Override
public Page<TaskDO> findPageCriteria(Integer pageNum, Integer size, QueryCondition queryCondition) {

Pageable pageable = PageRequest.of(pageNum, size, Sort.Direction.DESC, "id");

return getTaskDOS(queryCondition, pageable);
}

private Page<TaskDO> getTaskDOS(QueryCondition queryCondition, Pageable pageable) {
return taskRepository.findAll(
(Specification<TaskDO>) (root, criteriaQuery, criteriaBuilder) -> {

List<Predicate> predicates = getPredicates(root,
criteriaBuilder, queryCondition);

return getPredicate(criteriaBuilder, predicates);

}, pageable);
return taskRepository.findAll((Specification<TaskDO>) (root, criteriaQuery, criteriaBuilder) -> {

List<Predicate> predicates = getPredicates(root, criteriaBuilder, queryCondition);

return getPredicate(criteriaBuilder, predicates);

}, pageable);
}

public List<TaskDO> findAllByServiceNameEqualAll() {
Expand All @@ -124,5 +129,5 @@ public List<TaskDO> findAllByServiceNameEqualAll() {
public List<TaskDO> findAllByServiceNameNotEqualAll() {
return taskRepository.findAllByServiceNameNotIgnoreCase(SkyWalkerConstants.NACOS_ALL_SERVICE_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ public interface TaskRepository extends CrudRepository<TaskDO, Integer>, JpaRepo
TaskDO findByTaskId(String taskId);

@Transactional
int deleteByTaskId(String taskId);
void deleteByTaskId(String taskId);

List<TaskDO> findAllByTaskIdIn(List<String> taskIds);

List<TaskDO> getAllByWorkerIp(String workerIp);

/**
* query service is all,use ns leven sync data
*/
List<TaskDO> findAllByServiceNameEqualsIgnoreCase(String serviceName);
List<TaskDO> findAllByServiceNameNotIgnoreCase(String serviceName);

int countByDestClusterIdOrSourceClusterId(String destClusterId,String sourceClusterId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -38,16 +36,17 @@
@Slf4j
@Service
public class ClusterAddProcessor implements Processor<ClusterAddRequest, ClusterAddResult> {


@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,43 @@
* 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
* @version $Id: ClusterDeleteProcessor.java, v 0.1 2018-09-30 PM2:43 NacosSync Exp $$
*/
@Service
public class ClusterDeleteProcessor implements Processor<ClusterDeleteRequest, ClusterDeleteResult> {

@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());

}
}

0 comments on commit 81610e5

Please sign in to comment.