Prechádzať zdrojové kódy

将项目运行节点拆分成GPU节点和CPU节点

LingxinMeng 2 rokov pred
rodič
commit
97b1961ddd

+ 19 - 0
api-common/src/main/java/api/common/util/HttpUtil.java

@@ -134,6 +134,25 @@ public class HttpUtil {
         return result;
     }
 
+    /**
+     * 默认请求头的 get 请求,参数拼接到 url 上
+     *
+     * @param url 请求路径带参数
+     * @return 请求结果
+     */
+    public static String get(String url) {
+        String result;
+        try {
+            //1 创建 GET 对象
+            HttpGet get = new HttpGet(url);
+            get.setConfig(getRequestConfig());
+            //2 执行获取请求结果
+            result = EntityUtils.toString(getCloseableHttpClient().execute(get).getEntity());
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return result;
+    }
 
     /**
      * 默认请求头的 get 请求

+ 1 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/constant/ConstantConfiguration.java

@@ -10,4 +10,5 @@ import org.springframework.context.annotation.Configuration;
 public class ConstantConfiguration {
     private String temporaryDirectory;
     private String uploadOsgbUrl;
+    private String generateVideoUrl;
 }

+ 8 - 6
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/TaskUtil.java

@@ -2,7 +2,7 @@ package com.css.simulation.resource.scheduler.util;
 
 import api.common.pojo.constants.DictConstants;
 import api.common.util.*;
-import com.css.simulation.resource.scheduler.configuration.feign.VideoFeignClient;
+import com.css.simulation.resource.scheduler.configuration.constant.ConstantConfiguration;
 import com.css.simulation.resource.scheduler.configuration.kubernetes.KubernetesConfiguration;
 import com.css.simulation.resource.scheduler.configuration.redis.CustomRedisClient;
 import com.css.simulation.resource.scheduler.data.entity.*;
@@ -75,8 +75,6 @@ public class TaskUtil {
     @Resource
     private ProjectUtil projectUtil;
     @Resource
-    private VideoFeignClient videoFeignClient;
-    @Resource
     private SqlSessionFactory sqlSessionFactory;
     @Resource
     private KubernetesConfiguration kubernetesConfiguration;
@@ -88,6 +86,8 @@ public class TaskUtil {
     private TaskUtil taskUtil;
     @Resource
     private CustomRedisClient customRedisClient;
+    @Resource
+    private ConstantConfiguration constantConfiguration;
 
     public void batchInsertTask(List<TaskEntity> taskEntityList) {
         try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
@@ -148,8 +148,10 @@ public class TaskUtil {
                     log.info("项目 {} 使用 CPU 生成视频。", projectId);
                     String generateVideoKey = "task:" + taskId + ":generateVideo";
                     customRedisClient.set(generateVideoKey, "0");
-                    videoFeignClient.generateVideo(generateVideoKey, nodeName, projectId, projectType, maxSimulationTime, taskId);
-                    log.info("任务 {} 的视频生成开始。", taskId);
+                    HttpUtil.get(constantConfiguration.getGenerateVideoUrl().replace("simulation-resource-video", nodeName) + "?generateVideoKey=" + generateVideoKey + "&nodeName=" + nodeName + "&projectId" + projectId + "&projectType" + projectType + "&maxSimulationTime" + maxSimulationTime + "&taskId" + taskId);
+//                    HttpUtil.get("http://" + nodeName + ":8007//simulation/resource/video/generate" + "?generateVideoKey=" + generateVideoKey + "&nodeName=" + nodeName + "&projectId" + projectId + "&projectType" + projectType + "&maxSimulationTime" + maxSimulationTime + "&taskId" + taskId);
+//                    videoFeignClient.generateVideo(generateVideoKey, nodeName, projectId, projectType, maxSimulationTime, taskId);
+                    log.info("任务 {} 使用 CPU 生成视频中>>>>>>>", taskId);
                     while (true) {
                         TimeUnit.SECONDS.sleep(1);
                         final String generateVideoValue = customRedisClient.get(generateVideoKey);
@@ -157,7 +159,7 @@ public class TaskUtil {
                             break;
                         }
                     }
-                    log.info("等待任务 {} 的视频生成结束。", taskId);
+                    log.info("任务 {} 使用 CPU 生成视频生成结束<<<<<<<", taskId);
                 }
             }
             // -------------------------------- 判断项目是否结束 --------------------------------

+ 2 - 1
simulation-resource-scheduler/src/main/resources/bootstrap-dev.yaml

@@ -6,7 +6,8 @@ server:
 #* -------------------------------- Comment --------------------------------
 constant:
   temporary-directory: "/mnt/disk001/simulation-cloud/simulation-resource-scheduler-8004/temp/"
-  upload-osgb-url: "http://simulation-resource-video/simulation/resource/video/uploadToLocal"
+  upload-osgb-url: "http://simulation-resource-video:8007/simulation/resource/video/uploadToLocal"
+  generate-video-url: "http://simulation-resource-video:8007/simulation/resource/video/generate"
 
 scheduler:
   simulation-cloud-ip: 10.14.85.241

+ 1 - 4
simulation-resource-server/src/main/java/com/css/simulation/resource/util/ProjectUtil.java

@@ -2,8 +2,8 @@ package com.css.simulation.resource.util;
 
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.param.project.SimulationManualProjectParam;
-import api.common.pojo.po.scheduler.SchedulerProjectPO;
 import api.common.pojo.po.project.SimulationManualProjectPO;
+import api.common.pojo.po.scheduler.SchedulerProjectPO;
 import api.common.util.StringUtil;
 import com.css.simulation.resource.algorithm.mapper.AlgorithmMapper;
 import com.css.simulation.resource.model.mapper.ModelConfigMapper;
@@ -13,7 +13,6 @@ import com.css.simulation.resource.project.mapper.ManualProjectMapper;
 import com.css.simulation.resource.project.mapper.SimulationProjectMapper;
 import com.css.simulation.resource.scene.mapper.ScenePackageMapper;
 import com.css.simulation.resource.scene.mapper.ScenePackageSublistMapper;
-import com.css.simulation.resource.scene.mapper.ScoringRulesMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
@@ -34,8 +33,6 @@ public class ProjectUtil {
     @Resource
     private AlgorithmMapper algorithmMapper;
     @Resource
-    private ScoringRulesMapper scoringRulesMapper;
-    @Resource
     private ModelConfigMapper modelConfigMapper;
     @Resource
     private ModelVehicleMapper modelVehicleMapper;

+ 13 - 19
simulation-resource-video/src/main/java/com/css/simulation/resource/video/service/VideoService.java

@@ -33,7 +33,6 @@ import java.math.RoundingMode;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Iterator;
-import java.util.Set;
 
 @Service
 @Slf4j
@@ -69,18 +68,18 @@ public class VideoService {
      * 生成视频
      */
     public void generateVideo(String generateVideoKey, String nodeName, String projectId, String projectType, String maxSimulationTime, String taskId) {
-        final Set<String> cpuNodeParallelismKeys = customRedisClient.keys("cpu-node*");
+//        final Set<String> cpuNodeParallelismKeys = customRedisClient.keys("cpu-node*");
         // 获取本地 hostname
-        final String hostName = OsUtil.getHostName();
-        String cpuNodeParallelismKey = "cpu-node:" + nodeName + ":parallelism";
-        for (String tempCpuNodeParallelismKey : cpuNodeParallelismKeys) {
-            if (tempCpuNodeParallelismKey.contains(hostName)) {
-                cpuNodeParallelismKey = tempCpuNodeParallelismKey;
-            }
-        }
+//        final String hostName = OsUtil.getHostName();
+//        String cpuNodeParallelismKey = "cpu-node:" + nodeName + ":parallelism";
+//        for (String tempCpuNodeParallelismKey : cpuNodeParallelismKeys) {
+//            if (tempCpuNodeParallelismKey.contains(hostName)) {
+//                cpuNodeParallelismKey = tempCpuNodeParallelismKey;
+//            }
+//        }
         String lockName = "video-lock-" + taskId;
         customRedisClient.lock(lockName, 1000L, (Long.parseLong(maxSimulationTime) + 10) * 1000L);
-        customRedisClient.decrement(cpuNodeParallelismKey, 1);
+//        customRedisClient.decrement(cpuNodeParallelismKey, 1);
         try {
             String rootDirectoryPathOfMinio = minioConfiguration.getProjectResultDirectory() + projectId + "/" + taskId + "/";
             String xodrPathOfMinio = rootDirectoryPathOfMinio + taskId + ".xodr";
@@ -98,8 +97,7 @@ public class VideoService {
             MinioUtil.downloadToFile(minioClient, minioConfiguration.getBucketName(), csv1PathOfMinio, csv1PathOfLinux);
             MinioUtil.downloadToFile(minioClient, minioConfiguration.getBucketName(), csv2PathOfMinio, csv2PathOfLinux);
             log.info("生成 xosc 文件。");
-            String xoscPath = generateXosc(rootDirectoryPathOfLinux, xodrPathOfLinux, osgbPathOfLinux, projectId,
-                    projectType);
+            String xoscPath = generateXosc(rootDirectoryPathOfLinux, xodrPathOfLinux, osgbPathOfLinux, projectId, projectType);
             log.info("启动虚拟窗口并通过 esmini 截图。");
             String pictureDirectoryPath = rootDirectoryPathOfLinux + "picture";
             FileUtil.createDirectory(pictureDirectoryPath);
@@ -107,9 +105,7 @@ public class VideoService {
             final long newScreenNum = customRedisClient.increment("screen-number", 1);
             log.info("获取新的屏幕号:" + newScreenNum);
             final String xvfbCommand2 = constantConfiguration.getXvfbCommand().replaceAll("screen-num", String.valueOf(newScreenNum));
-            final String esminiCommand2 = constantConfiguration.getEsminiCommand().replaceAll("screen-num", String.valueOf(newScreenNum))
-                    + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double
-                    .parseDouble(maxSimulationTime), 2);
+            final String esminiCommand2 = constantConfiguration.getEsminiCommand().replaceAll("screen-num", String.valueOf(newScreenNum)) + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
             OsUtil.exec(xvfbCommand2);
             OsUtil.exec(esminiCommand2);
             log.info("删除 esmini 进程。");
@@ -125,9 +121,7 @@ public class VideoService {
             String videoTargetPathOfLinux = rootDirectoryPathOfLinux + "video/" + videoName;
             FileUtil.createParentDirectory(videoTargetPathOfLinux);
             String videoTargetPathOfMinio = rootDirectoryPathOfMinio + videoName;
-            OsUtil.exec("ffmpeg -f image2 -framerate 30 -start_number " + num + " -i "
-                    + pictureDirectoryPath + "/screenshot_%05d.tga" + " -c:v libx264 -vf format=yuv420p -crf 20 "
-                    + videoTargetPathOfLinux);
+            OsUtil.exec("ffmpeg -f image2 -framerate 30 -start_number " + num + " -i " + pictureDirectoryPath + "/screenshot_%05d.tga" + " -c:v libx264 -vf format=yuv420p -crf 20 " + videoTargetPathOfLinux);
             //删除生成的临时文件
             String removeAll = "rm -rf " + pictureDirectoryPath;
             log.info("删除全部图片==" + removeAll);
@@ -140,7 +134,7 @@ public class VideoService {
         } catch (Exception e) {
             throw new RuntimeException("视频生成失败。", e);
         } finally {
-            customRedisClient.increment(cpuNodeParallelismKey, 1);
+//            customRedisClient.increment(cpuNodeParallelismKey, 1);
             customRedisClient.unlock(lockName);
             customRedisClient.set(generateVideoKey, "1");
         }