|
@@ -1,10 +1,8 @@
|
|
package com.css.simulation.resource.scheduler.scheduler;
|
|
package com.css.simulation.resource.scheduler.scheduler;
|
|
|
|
|
|
import api.common.pojo.constants.DictConstants;
|
|
import api.common.pojo.constants.DictConstants;
|
|
-import api.common.util.CollectionUtil;
|
|
|
|
-import api.common.util.SshUtil;
|
|
|
|
-import api.common.util.StringUtil;
|
|
|
|
-import api.common.util.TimeUtil;
|
|
|
|
|
|
+import api.common.pojo.dto.ProjectMessageDTO;
|
|
|
|
+import api.common.util.*;
|
|
import com.css.simulation.resource.scheduler.consumer.ManualProjectConsumer;
|
|
import com.css.simulation.resource.scheduler.consumer.ManualProjectConsumer;
|
|
import com.css.simulation.resource.scheduler.mapper.ClusterMapper;
|
|
import com.css.simulation.resource.scheduler.mapper.ClusterMapper;
|
|
import com.css.simulation.resource.scheduler.mapper.ManualProjectMapper;
|
|
import com.css.simulation.resource.scheduler.mapper.ManualProjectMapper;
|
|
@@ -14,6 +12,7 @@ import com.css.simulation.resource.scheduler.pojo.po.ProjectPO;
|
|
import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
|
|
import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
|
|
import com.css.simulation.resource.scheduler.service.TaskService;
|
|
import com.css.simulation.resource.scheduler.service.TaskService;
|
|
import io.kubernetes.client.openapi.ApiClient;
|
|
import io.kubernetes.client.openapi.ApiClient;
|
|
|
|
+import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.session.ClientSession;
|
|
import org.apache.sshd.client.session.ClientSession;
|
|
@@ -62,35 +61,52 @@ public class ProjectScheduler {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 调度项目启动
|
|
* 调度项目启动
|
|
- *
|
|
|
|
- * @throws IOException 超时时间
|
|
|
|
*/
|
|
*/
|
|
@Scheduled(fixedDelay = 60 * 1000)
|
|
@Scheduled(fixedDelay = 60 * 1000)
|
|
- public void dispatchProject() throws IOException {
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public void dispatchProject() {
|
|
//1 查询等待执行的项目
|
|
//1 查询等待执行的项目
|
|
List<ProjectPO> projectList = manualProjectMapper.selectByNowRunState(DictConstants.PROJECT_WAITING);
|
|
List<ProjectPO> projectList = manualProjectMapper.selectByNowRunState(DictConstants.PROJECT_WAITING);
|
|
- //2
|
|
|
|
- if (CollectionUtil.isEmpty(projectList)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- projectList.forEach(project -> {
|
|
|
|
|
|
+ for (ProjectPO project : projectList) {
|
|
String projectId = project.getId();
|
|
String projectId = project.getId();
|
|
String userId = project.getCreateUserId();
|
|
String userId = project.getCreateUserId();
|
|
ClusterPO clusterPO = clusterMapper.selectByUserId(userId);
|
|
ClusterPO clusterPO = clusterMapper.selectByUserId(userId);
|
|
|
|
+ if (clusterPO == null) {
|
|
|
|
+ log.error("ProjectScheduler--dispatchProject 项目 " + projectId + " 的创建用户 " + userId + " 没有分配集群!");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
String clusterId = clusterPO.getId();
|
|
String clusterId = clusterPO.getId();
|
|
int simulationLicenseNumber = clusterPO.getNumSimulationLicense();
|
|
int simulationLicenseNumber = clusterPO.getNumSimulationLicense();
|
|
// 获取该用户正在运行的项目数量
|
|
// 获取该用户正在运行的项目数量
|
|
- Set<String> runningProjectSet = redisTemplate.keys(manualProjectTopic + ":cluster:" + clusterId + ":running" + "*");
|
|
|
|
- int runningProjectNumber = CollectionUtil.isEmpty(runningProjectSet) ? 0 : runningProjectSet.size();
|
|
|
|
- if (runningProjectNumber < simulationLicenseNumber) {
|
|
|
|
- String projectJson = redisTemplate.opsForValue().get(manualProjectTopic + ":cluster:" + clusterId + ":waiting" + projectId);
|
|
|
|
- assert projectJson != null;
|
|
|
|
- redisTemplate.delete(manualProjectTopic + ":cluster:" + clusterId + ":waiting" + projectId);
|
|
|
|
- redisTemplate.opsForValue().set(manualProjectTopic + ":cluster:" + clusterId + ":running" + projectId, projectJson);
|
|
|
|
- manualProjectConsumer.parseManualProject(projectJson);
|
|
|
|
|
|
+ Set<String> runningProjectSet = redisTemplate.keys(manualProjectTopic + ":cluster:" + clusterId + ":running:" + "*");
|
|
|
|
+ if (CollectionUtil.isNotEmpty(runningProjectSet)) {
|
|
|
|
+ long parallelismSum = 0;
|
|
|
|
+ for (String runningProjectKey : runningProjectSet) {
|
|
|
|
+ String runningProjectJsonTemp = redisTemplate.opsForValue().get(runningProjectKey);
|
|
|
|
+ ProjectMessageDTO runningProjectMessageTemp = JsonUtil.jsonToBean(runningProjectJsonTemp, ProjectMessageDTO.class);
|
|
|
|
+ parallelismSum += runningProjectMessageTemp.getParallelism();
|
|
|
|
+ }
|
|
|
|
+ if (parallelismSum < simulationLicenseNumber) {
|
|
|
|
+ Set<String> waitingProjectSet = redisTemplate.keys(manualProjectTopic + ":cluster:" + clusterId + ":waiting:" + "*");
|
|
|
|
+ if (CollectionUtil.isEmpty(waitingProjectSet)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ for (String waitingProjectKey : waitingProjectSet) {
|
|
|
|
+ String waitingProjectJsonTemp = redisTemplate.opsForValue().get(waitingProjectKey);
|
|
|
|
+ ProjectMessageDTO waitingProjectMessageTemp = JsonUtil.jsonToBean(waitingProjectJsonTemp, ProjectMessageDTO.class);
|
|
|
|
+ int parallelism = waitingProjectMessageTemp.getParallelism();
|
|
|
|
+ if (parallelismSum + parallelism < simulationLicenseNumber) {
|
|
|
|
+ String projectJson = redisTemplate.opsForValue().get(manualProjectTopic + ":cluster:" + clusterId + ":waiting:" + projectId);
|
|
|
|
+ redisTemplate.delete(manualProjectTopic + ":cluster:" + clusterId + ":waiting:" + projectId);
|
|
|
|
+ assert projectJson != null;
|
|
|
|
+ redisTemplate.opsForValue().set(manualProjectTopic + ":cluster:" + clusterId + ":running:" + projectId, projectJson);
|
|
|
|
+ log.info("ProjectScheduler--dispatchProject 项目 " + projectId + " 从等待队列进入执行状态!");
|
|
|
|
+ manualProjectConsumer.parseManualProject(projectJson);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- });
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|