martin 3 years ago
parent
commit
dd1a8e4b6d

+ 33 - 0
api-common/src/main/java/api/common/util/FileUtil.java

@@ -354,6 +354,39 @@ public class FileUtil {
         return fileList;
     }
 
+
+    /**
+     * 扫描压缩包中是否存在指定文件
+     * @param inputStream
+     * @param fileType
+     * @return
+     * @throws IOException
+     */
+    public static boolean scan(InputStream inputStream, String fileType, String targetFileName) throws IOException {
+        if (".tar".equals(fileType)) {
+            TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
+            TarArchiveEntry entry;
+            // 将 tar 文件解压到 extractPath 目录下
+            while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
+                if (entry.getName().contains(targetFileName)){
+                    return true;
+                }
+            }
+        }
+        if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)|| ".gz".equals(fileType)) {
+            TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
+            TarArchiveEntry entry;
+            // 将 tar 文件解压到 extractPath 目录下
+            while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
+                if (entry.getName().contains(targetFileName)){
+                    return true;
+                }
+            }
+        }
+        inputStream.close();
+        return false;
+    }
+
     // -------------------------------- F --------------------------------
     // -------------------------------- G --------------------------------
 

+ 19 - 11
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java

@@ -58,6 +58,8 @@ public class ManualProjectConsumer {
     @Autowired
     UserMapper userMapper;
     @Autowired
+    ClusterMapper clusterMapper;
+    @Autowired
     ManualProjectService manualProjectService;
     //    @Autowired
 //    ApiClient apiClient;
@@ -103,19 +105,25 @@ public class ManualProjectConsumer {
         String useType = userPO.getUseType();
         if (DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode) || DictConstants.ROLE_CODE_ADMIN.equals(roleCode)) {  //3-1 管理员账户和管理员子账户直接执行
             parseManualProject(projectRecord);
-        } else if (DictConstants.ROLE_CODE_UESR.equals(roleCode)) {
-//            if (DictConstants.USE_TYPE_EXCLUSIVE.equals(useType)) {   //3-2 普通独占账户,根据自己的独占节点排队
-//                // 获取独占数量
-//                // 获取该用户正在运行的项目数量
-//                // 获取
-//            } else {
-//
-//            }
-            parseManualProject(projectRecord);
+        } else if (DictConstants.ROLE_CODE_UESR.equals(roleCode)) { //3-2 普通账户,不管是独占还是共享,都在自己的集群里排队,根据自己的独占节点排队
+            // 获取拥有的节点数量,即仿真软件证书数量
+            ClusterPO clusterPO = clusterMapper.selectByUserId(userId);
+            String clusterId = clusterPO.getId();
+            int simulationLicenseNumber = clusterPO.getNumSimulationLicense();
+            // 获取该用户正在运行的项目数量
+            Set<String> runningProjectSet = redisTemplate.keys(manualProjectTopic + ":" + userId + ":monopoly:running" + "*");
+            int runningProjectNumber = CollectionUtil.isEmpty(runningProjectSet) ? 0 : runningProjectSet.size();
+            if (runningProjectNumber < simulationLicenseNumber) {
+                redisTemplate.opsForValue().set(manualProjectTopic + ":cluster:" + clusterId + ":running" + projectId, projectJson);
+                parseManualProject(projectRecord);
+            } else {
+                redisTemplate.opsForValue().set(manualProjectTopic + ":cluster:" + clusterId + ":waiting" + projectId, projectJson);
+            }
+
         } else if (DictConstants.ROLE_CODE_SUBUESR.equals(roleCode)) {
-            if (DictConstants.USE_TYPE_EXCLUSIVE.equals(useType)) {   //3-2 普通独占子账户,根据自己的独占节点排队
+            if (DictConstants.USE_TYPE_EXCLUSIVE.equals(useType)) {   //3-2 普通子账户,根据自己的独占节点排队
 
-            } else {
+            } else {    // 共享子账户需要查询父账户的集群 id
 
             }
             parseManualProject(projectRecord);

+ 24 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/mapper/ClusterMapper.java

@@ -0,0 +1,24 @@
+package com.css.simulation.resource.scheduler.mapper;
+
+
+import com.css.simulation.resource.scheduler.pojo.po.ClusterPO;
+import org.apache.ibatis.annotations.*;
+import org.apache.ibatis.type.JdbcType;
+
+@Mapper
+public interface ClusterMapper {
+
+
+    @Results(id = "user", value = {
+            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.VARCHAR),
+            @Result(column = "num_simulation_license", property = "numSimulationLicense", jdbcType = JdbcType.INTEGER),
+            @Result(column = "num_dynamic_license", property = "numDynamicLicense", jdbcType = JdbcType.INTEGER)
+    })
+    @Select("select id, user_id, num_simulation_license, num_dynamic_license\n" +
+            "from system_cluster\n" +
+            "where is_deleted = '0'\n" +
+            "  and user_id = #{userId}")
+    ClusterPO selectByUserId(@Param("userId")String userId);
+
+}

+ 15 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/pojo/po/ClusterPO.java

@@ -0,0 +1,15 @@
+package com.css.simulation.resource.scheduler.pojo.po;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class ClusterPO {
+    private String id; // id
+    private String userId; // 用户id
+    private Integer numSimulationLicense; // 仿真软件证书数量
+    private Integer numDynamicLicense; // 动力学软件证书数量
+}

+ 4 - 17
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/AlgorithmService.java

@@ -11,7 +11,6 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.io.InputStream;
-import java.util.Set;
 
 @Service
 public class AlgorithmService {
@@ -26,7 +25,7 @@ public class AlgorithmService {
     String linuxTempPath;
 
     @SneakyThrows
-    public ResponseBodyVO<String> check(String minioPath) {
+    public ResponseBodyVO<String> check(String minioPath) { 
 
         String[] split = minioPath.split("\\.");
         int length = split.length;
@@ -34,24 +33,12 @@ public class AlgorithmService {
 
         //1 根据 minio 路径获取输入流
         InputStream inputStream = MinioUtil.downloadToStream(minioClient, bucketName, minioPath);
-        //2 将输入流解压到临时目录
-        String algorithmTarLinuxTempPath = linuxTempPath + minioPath;
-        Set<String> pathList = FileUtil.decompress(inputStream, algorithmTarLinuxTempPath, "." + s);
-        //3 获取文件列表中是否有 docker-entrypoint.sh
-        boolean result = false;
-        for (String path : pathList) {
-            if (path.contains(".dockerenv")) {
-                result = true;
-                break;
-            }
-        }
-
-        FileUtil.rm(algorithmTarLinuxTempPath);
-        if (result) {
+//        FileUtil.writeInputStreamToLocalFile();
+        //2 判断压缩包中是否有 docker-entrypoint.sh
+        if (FileUtil.scan(inputStream, "." + s, ".dockerenv")){
             return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "算法可用!", null);
         } else {
             return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "算法不可用!", null);
         }
-
     }
 }