Browse Source

缩小 podName 长度

martin 2 years ago
parent
commit
0afdee64bd

+ 20 - 0
api-common/src/main/java/api/common/util/StringUtil.java

@@ -34,6 +34,26 @@ public class StringUtil {
         return UUID.randomUUID().toString().replace("-", "");
         return UUID.randomUUID().toString().replace("-", "");
     }
     }
 
 
+
+
+
+    public static String getRandomEightBitUUID() {
+        String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
+                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
+                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
+                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
+                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
+                "W", "X", "Y", "Z" };
+        StringBuilder stringBuilder = new StringBuilder();
+        String uuid = UUID.randomUUID().toString().replace("-", "");
+        for (int i = 0; i < 8; i++) {
+            String str = uuid.substring(i * 4, i * 4 + 4);
+            int x = Integer.parseInt(str, 16);
+            stringBuilder.append(chars[x % 0x3E]);
+        }
+        return stringBuilder.toString();
+    }
+
     public static String getRandomCode() {
     public static String getRandomCode() {
         return TimeUtil.getNow() + "";
         return TimeUtil.getNow() + "";
     }
     }

+ 1 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/manager/TaskManager.java

@@ -162,7 +162,7 @@ public class TaskManager {
         KubernetesUtil.deletePod(apiClient, kubernetesNamespace, lastPodName);
         KubernetesUtil.deletePod(apiClient, kubernetesNamespace, lastPodName);
         stringRedisTemplate.delete(projectUtil.getNodeNameOfPod(lastPodName));
         stringRedisTemplate.delete(projectUtil.getNodeNameOfPod(lastPodName));
         String lastPodString = FileUtil.read(podYamlDirectory + lastPodName + ".yaml");
         String lastPodString = FileUtil.read(podYamlDirectory + lastPodName + ".yaml");
-        String nextPodName = "project-" + projectId + "-" + StringUtil.getRandomUUID();
+        String nextPodName = projectUtil.getRandomPodName(projectId);
         String nextPodString = lastPodString.replace(lastPodName, nextPodName); // pod 名称包括 projectId 和 随机字符串
         String nextPodString = lastPodString.replace(lastPodName, nextPodName); // pod 名称包括 projectId 和 随机字符串
         String nextPodFileName = nextPodName + ".yaml";     // 实际执行 pod 的文件名称
         String nextPodFileName = nextPodName + ".yaml";     // 实际执行 pod 的文件名称
         log.info("TaskManager--createNextPod 创建项目 " + projectId + " 的下一个 pod。");
         log.info("TaskManager--createNextPod 创建项目 " + projectId + " 的下一个 pod。");

+ 7 - 14
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/ProjectService.java

@@ -389,20 +389,13 @@ public class ProjectService {
         FileUtil.writeStringToLocalFile(podTemplateStringOfProject, podYamlDirectory + podTemplateFileNameOfProject);
         FileUtil.writeStringToLocalFile(podTemplateStringOfProject, podYamlDirectory + podTemplateFileNameOfProject);
 
 
         nodeMap.forEach((nodeName, parallelism) -> {
         nodeMap.forEach((nodeName, parallelism) -> {
-            String podName = "project-" + projectId + "-" + StringUtil.getRandomUUID();
-            // -------------------------------- Comment --------------------------------
-            String tempReplace4 = podTemplateStringOfProject.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
-            String tempPodString = tempReplace4.replace("node-name", nodeName);     // 指定 pod 运行节点
-            log.info("ProjectService--createPod 在节点 " + nodeName + " 开始执行 pod:" + tempPodString);
-            projectUtil.createPod(nodeName, podName, tempPodString);
-//            V1Pod v1Pod;
-//            try {
-//                FileUtil.writeStringToLocalFile(tempPodString, podYamlDirectory + tempPodFileNameOfProject);
-//                v1Pod = (V1Pod) Yaml.load(tempPodString);
-//            } catch (IOException e) {
-//                throw new RuntimeException("ProjectService--createPod 项目 " + projectId + " 创建 pod 失败:", e);
-//            }
-//            KubernetesUtil.createPod(apiClient, kubernetesNamespace, v1Pod);
+            for (int i = 0; i < parallelism; i++) {
+                String podName = projectUtil.getRandomPodName(projectId);   // 生成 podName
+                String tempReplace4 = podTemplateStringOfProject.replace("pod-name", podName); // pod 名称包括 projectId 和 随机字符串
+                String tempPodString = tempReplace4.replace("node-name", nodeName);     // 指定 pod 运行节点
+                log.info("ProjectService--createPod 在节点 " + nodeName + " 开始执行 pod:" + tempPodString);
+                projectUtil.createPod(nodeName, podName, tempPodString);
+            }
         });
         });
     }
     }
 
 

+ 2 - 2
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/ProjectUtil.java

@@ -59,8 +59,8 @@ public class ProjectUtil {
     StringRedisTemplate stringRedisTemplate;
     StringRedisTemplate stringRedisTemplate;
 
 
 
 
-    public String getParallelismOfNode(String nodeName) {
-        return stringRedisTemplate.opsForValue().get("node:" + nodeName + ":parallelism");
+    public String getRandomPodName(String projectId) {
+        return "project-" + projectId + "-" + StringUtil.getRandomEightBitUUID();
     }
     }
 
 
     public void deleteNodeNameOfPod(String podName) {
     public void deleteNodeNameOfPod(String podName) {