|
@@ -3,7 +3,6 @@ package com.css.simulation.resource.scheduler.consumer;
|
|
|
|
|
|
import api.common.pojo.constants.DictConstants;
|
|
|
import api.common.pojo.dto.ProjectMessageDTO;
|
|
|
-import api.common.util.CollectionUtil;
|
|
|
import api.common.util.JsonUtil;
|
|
|
import api.common.util.StringUtil;
|
|
|
import com.css.simulation.resource.scheduler.mapper.*;
|
|
@@ -22,10 +21,7 @@ import org.springframework.kafka.annotation.KafkaListener;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
@Slf4j
|
|
@@ -66,8 +62,6 @@ public class ProjectConsumer {
|
|
|
ProjectUtil projectUtil;
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 任务运行前首先判断用户是否拥有可分配资源
|
|
|
*
|
|
@@ -149,19 +143,14 @@ public class ProjectConsumer {
|
|
|
|
|
|
String projectId = projectMessageDTO.getProjectId();
|
|
|
int parallelism = projectMessageDTO.getParallelism(); // 期望并行度
|
|
|
- int parallelismSum; //实际可用并行度
|
|
|
- //1 获取所有节点的剩余可用并行度
|
|
|
- Map<String, Integer> nodeMap = projectUtil.getNodeMapToUse(parallelism);
|
|
|
- if (CollectionUtil.isEmpty(nodeMap)) {
|
|
|
- parallelismSum = 0;
|
|
|
- } else {
|
|
|
- parallelismSum = nodeMap.keySet().stream().mapToInt(nodeMap::get).sum();
|
|
|
- }
|
|
|
+ //1 获取集群剩余可用并行度
|
|
|
+ int restParallelism = projectUtil.getRestParallelism();
|
|
|
//2 判断剩余可用并行度是否大于项目并行度,否则加入扩充队列
|
|
|
- if (parallelismSum > 0L) {
|
|
|
- log.info("ProjectConsumer--run 集群 " + clusterId + " 将项目 " + projectId + "在节点 " + nodeMap + " 上执行!");
|
|
|
- projectMessageDTO.setCurrentParallelism(parallelismSum); // 设置实际的并行度
|
|
|
- parseProject(nodeMap, projectMessageDTO, projectWaitingKey, projectRunningKey);
|
|
|
+ if (restParallelism > 0L) {
|
|
|
+ log.info("ProjectConsumer--run 集群 " + clusterId + " 执行项目 " + projectId);
|
|
|
+ // 设置实际的并行度
|
|
|
+ projectMessageDTO.setCurrentParallelism(Math.min(restParallelism, parallelism)); // 设置实际的并行度
|
|
|
+ parseProject(projectMessageDTO, projectWaitingKey, projectRunningKey);
|
|
|
} else {
|
|
|
log.info("ProjectConsumer--cacheManualProject 服务器资源不够,项目 " + projectId + " 暂时加入等待队列。");
|
|
|
wait(projectWaitingKey, projectMessageDTO);
|
|
@@ -179,21 +168,21 @@ public class ProjectConsumer {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * @param nodeMap 节点列表以及剩余可用并行度
|
|
|
* @param projectMessageDTO 初始接收到的项目启动信息
|
|
|
* @param projectWaitingKey projectWaitingKey
|
|
|
* @param projectRunningKey projectRunningKey
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public void parseProject(Map<String, Integer> nodeMap, ProjectMessageDTO projectMessageDTO, String projectWaitingKey, String projectRunningKey) {
|
|
|
+ public void parseProject(ProjectMessageDTO projectMessageDTO, String projectWaitingKey, String projectRunningKey) {
|
|
|
String projectId = projectMessageDTO.getProjectId(); // 项目 id
|
|
|
String projectType = projectMessageDTO.getType(); // 项目类型
|
|
|
+ int currentParallelism = projectMessageDTO.getCurrentParallelism(); // 当前并行度
|
|
|
String packageId = projectMessageDTO.getScenePackageId(); // 场景测试包 id
|
|
|
long videoTime = projectMessageDTO.getMaxSimulationTime(); // 结果视频的时长
|
|
|
String vehicleConfigId = projectMessageDTO.getVehicleConfigId();// 模型配置 id
|
|
|
String algorithmId = projectMessageDTO.getAlgorithmId(); // 算法 id
|
|
|
// -------------------------------- 0 准备 --------------------------------
|
|
|
- projectService.prepare(nodeMap, projectMessageDTO, projectWaitingKey, projectRunningKey);
|
|
|
+ projectService.prepare(projectMessageDTO, projectWaitingKey, projectRunningKey);
|
|
|
String userId = null;
|
|
|
if (DictConstants.PROJECT_TYPE_MANUAL.equals(projectType)) {
|
|
|
userId = manualProjectMapper.selectCreateUserById(projectId);
|
|
@@ -206,8 +195,25 @@ public class ProjectConsumer {
|
|
|
int taskTotal = scenePOList.size();
|
|
|
projectMessageDTO.setTaskTotal(taskTotal);
|
|
|
projectMessageDTO.setTaskCompleted(0);
|
|
|
- // 设置任务数量之后将项目运行信息放入 redis
|
|
|
+ // 设置任务数量之后,获取运行节点,并将项目运行信息放入 redis
|
|
|
+ Map<String, Integer> nodeMap;
|
|
|
+ if (currentParallelism < taskTotal) {
|
|
|
+ nodeMap = projectUtil.getNodeMapToUse(currentParallelism);
|
|
|
+ } else {
|
|
|
+ nodeMap = projectUtil.getNodeMapToUse(taskTotal);
|
|
|
+ }
|
|
|
+ // 将指定 node 的并行度减少
|
|
|
+ nodeMap.keySet().forEach(nodeName -> {
|
|
|
+ int parallelismToUse = nodeMap.get(nodeName);
|
|
|
+ String restParallelismKey = "node:" + nodeName + ":parallelism";
|
|
|
+ int restParallelism = Integer.parseInt(Objects.requireNonNull(stringRedisTemplate.opsForValue().get(restParallelismKey)));// 剩余可用并行度
|
|
|
+ stringRedisTemplate.opsForValue().set(restParallelismKey, (restParallelism - parallelismToUse) + "");
|
|
|
+ });
|
|
|
+ // 重新设置实际使用的并行度并保存到 redis
|
|
|
+ projectMessageDTO.setCurrentParallelism(nodeMap.values().stream().mapToInt(parallelism -> parallelism).sum());
|
|
|
+ log.info("ProjectConsume--parseProject 项目 " + projectId + " 运行在:" + nodeMap);
|
|
|
stringRedisTemplate.opsForValue().set(projectRunningKey, JsonUtil.beanToJson(projectMessageDTO));
|
|
|
+
|
|
|
Set<ScenePO> scenePOSet = new HashSet<>(scenePOList); // 如果不去重的话会出现多个场景重复关联多个指标
|
|
|
// -------------------------------- 2 查询模型 --------------------------------
|
|
|
//2-1 根据车辆配置id vehicleConfigId, 获取 模型信息和传感器信息
|