|
@@ -11,6 +11,7 @@ import com.css.simulation.resource.scheduler.mapper.UserMapper;
|
|
|
import com.css.simulation.resource.scheduler.pojo.po.ProjectPO;
|
|
|
import com.css.simulation.resource.scheduler.pojo.po.UserPO;
|
|
|
import com.css.simulation.resource.scheduler.pojo.to.KubernetesNodeTO;
|
|
|
+import com.css.simulation.resource.scheduler.pojo.to.NodeTO;
|
|
|
import com.css.simulation.resource.scheduler.pojo.to.PrefixTO;
|
|
|
import io.kubernetes.client.openapi.ApiClient;
|
|
|
import lombok.SneakyThrows;
|
|
@@ -22,8 +23,8 @@ import org.springframework.kafka.core.KafkaTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -60,24 +61,23 @@ public class ProjectUtil {
|
|
|
@Resource
|
|
|
ApiClient apiClient;
|
|
|
|
|
|
-
|
|
|
- public synchronized String getLeastCountNodeOfNodeMapToCount(Map<String, Integer> nodeMapToCount) {
|
|
|
- AtomicReference<String> result = new AtomicReference<>();
|
|
|
- result.set("");
|
|
|
- int tempCount = 1;
|
|
|
- nodeMapToCount.forEach((nodeName, count) -> {
|
|
|
- if (count < tempCount) {
|
|
|
- result.set(nodeName);
|
|
|
+ @SneakyThrows
|
|
|
+ public void deleteYamlByProjectId(String projectId){
|
|
|
+ List<String> absolutePathList = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
|
+ for (String absolutePath : absolutePathList) {
|
|
|
+ if(absolutePath.contains(projectId)){
|
|
|
+ boolean delete = new File(absolutePath).delete();
|
|
|
}
|
|
|
- });
|
|
|
- return result.get();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public Map<String, Integer> getNodeMapToCount(Map<String, Integer> nodeMap) {
|
|
|
- Map<String, Integer> result = new HashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+ public List<NodeTO> getNodeListToCount(Map<String, Integer> nodeMap) {
|
|
|
+ List<NodeTO> result = new ArrayList<>();
|
|
|
nodeMap.forEach((nodeName, parallelism) -> {
|
|
|
for (int i = 0; i < parallelism; i++) {
|
|
|
- result.put(nodeName, 0);
|
|
|
+ result.add(new NodeTO(nodeName, 0));
|
|
|
}
|
|
|
});
|
|
|
return result;
|
|
@@ -165,19 +165,21 @@ public class ProjectUtil {
|
|
|
@SneakyThrows
|
|
|
public void createNextPod2(String projectId, String nodeName, String lastPodName) {
|
|
|
deletePod(lastPodName);
|
|
|
-
|
|
|
//1 删除上一个 pod 和 redis 键值对 和 旧的 yaml 文件
|
|
|
deletePod(lastPodName);
|
|
|
- List<String> list1 = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
|
- list1.forEach(absolutePath -> {
|
|
|
+ List<String> list = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
|
+ Iterator<String> iterator1 = list.iterator();
|
|
|
+ while (iterator1.hasNext()){
|
|
|
+ String absolutePath = iterator1.next();
|
|
|
if (absolutePath.contains(nodeName) && absolutePath.contains(lastPodName)) {
|
|
|
FileUtil.rm(absolutePath);
|
|
|
+ list.remove(absolutePath);
|
|
|
+ break;
|
|
|
}
|
|
|
- });
|
|
|
- List<String> list2 = FileUtil.listAbsolutePath(podYamlDirectory);
|
|
|
- for (String absolutePath : list2) {
|
|
|
- if (absolutePath.contains(nodeName) && absolutePath.contains(lastPodName)) {
|
|
|
- createPod2(absolutePath);
|
|
|
+ }
|
|
|
+ for (String absolutePath : list) {
|
|
|
+ if (absolutePath.contains(nodeName) && absolutePath.contains(projectId)) {
|
|
|
+ createPod2(projectId, absolutePath);
|
|
|
log.info("ProjectUtil--createNextPod 创建项目 " + projectId + " 的下一个 pod。");
|
|
|
return;
|
|
|
}
|
|
@@ -206,14 +208,19 @@ public class ProjectUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * @param nodeName 节点名称
|
|
|
* @param podYamlPath pod 文件内容
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public void createPod2(String podYamlPath) {
|
|
|
+ public void createPod2(String nodeName, String podYamlPath) {
|
|
|
+ String podName = podYamlPath.split("#")[1].split("\\.")[0];
|
|
|
+ stringRedisTemplate.opsForValue().set("pod:" + podName + ":node", nodeName); // 将 pod 运行在哪个 node 上记录到 redis
|
|
|
+ KubernetesUtil.createNs(apiClient, kubernetesConfiguration.getNamespace());
|
|
|
KubernetesUtil.applyYaml(hostname, username, password, podYamlPath);
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
public String getProjectTypeByProjectId(String projectId) {
|
|
|
String projectType = null;
|
|
|
ProjectPO manualProjectPO = manualProjectMapper.selectById(projectId);
|
|
@@ -552,6 +559,9 @@ public class ProjectUtil {
|
|
|
public void addOneParallelismToNode(String nodeName) {
|
|
|
String key = "node:" + nodeName + ":parallelism";
|
|
|
String parallelismString = stringRedisTemplate.opsForValue().get(key);
|
|
|
+ if(StringUtil.isEmpty(parallelismString)){
|
|
|
+ throw new RuntimeException("ProjectUtil--addOneParallelismToNode redisKey " + key + " 为空。");
|
|
|
+ }
|
|
|
int parallelism = Integer.parseInt(parallelismString);
|
|
|
stringRedisTemplate.opsForValue().set(key, (parallelism + 1) + "");
|
|
|
log.info("ProjectUtil--addOneParallelismToNode 归还并行度从 " + parallelism + " 加一。");
|