Browse Source

Merge branch 'dev-credits' into dev

dev-credits
chenzhihang 10 months ago
parent
commit
3399d65eda
17 changed files with 146 additions and 54 deletions
  1. +1
    -0
      ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java
  2. +1
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java
  3. +10
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java
  4. +2
    -2
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java
  5. +27
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/ExperimentInstanceStatusTask.java
  6. +8
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/RayInsStatusTask.java
  7. +3
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/ResourceOccupyTask.java
  8. +5
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java
  9. +7
    -0
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java
  10. +16
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java
  11. +3
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java
  12. +1
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/RayInsServiceImpl.java
  13. +2
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/RayServiceImpl.java
  14. +37
    -20
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java
  15. +5
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java
  16. +3
    -6
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java
  17. +15
    -5
      ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml

+ 1
- 0
ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java View File

@@ -349,6 +349,7 @@ public class SysUser extends BaseEntity {
.append("dept", getDept()) .append("dept", getDept())
.append("gitLinkUsername", getGitLinkUsername()) .append("gitLinkUsername", getGitLinkUsername())
.append("gitLinkPassword", getGitLinkPassword()) .append("gitLinkPassword", getGitLinkPassword())
.append("credit", getCredit())
.toString(); .toString();
} }
} }

+ 1
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java View File

@@ -49,7 +49,7 @@ public class ComputingResource implements Serializable {
private Integer gpuNums; private Integer gpuNums;


@ApiModelProperty("积分/小时") @ApiModelProperty("积分/小时")
private Float creditPerHour;
private Double creditPerHour;


@ApiModelProperty("标签") @ApiModelProperty("标签")
private String labels; private String labels;


+ 10
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java View File

@@ -19,8 +19,14 @@ public class ResourceOccupy {
@ApiModelProperty("计算资源") @ApiModelProperty("计算资源")
private Integer computingResourceId; private Integer computingResourceId;


@ApiModelProperty("描述")
private String description;

@ApiModelProperty("积分/小时") @ApiModelProperty("积分/小时")
private Float creditPerHour;
private Double creditPerHour;

@ApiModelProperty("扣除的积分")
private Double deduceCredit;


@ApiModelProperty("上一次扣分时间") @ApiModelProperty("上一次扣分时间")
private Date deduceLastTime; private Date deduceLastTime;
@@ -36,4 +42,7 @@ public class ResourceOccupy {


@ApiModelProperty("类型id") @ApiModelProperty("类型id")
private Long taskId; private Long taskId;

@ApiModelProperty("流水线节点id")
private String nodeId;
} }

+ 2
- 2
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java View File

@@ -14,9 +14,9 @@ public interface ResourceOccupyDao {


int edit(@Param("resourceOccupy") ResourceOccupy resourceOccupy); int edit(@Param("resourceOccupy") ResourceOccupy resourceOccupy);


ResourceOccupy getResourceOccupyByTask(@Param("taskType") String taskType, @Param("taskId") Long taskId);
List<ResourceOccupy> getResourceOccupyByTask(@Param("taskType") String taskType, @Param("taskId") Long taskId, @Param("nodeId") String nodeId);


int deduceCredit(@Param("credit") Float credit, @Param("userId") Long userId);
int deduceCredit(@Param("credit") Double credit, @Param("userId") Long userId);


int updateUsed(@Param("id") Integer id, @Param("used") Integer used); int updateUsed(@Param("id") Integer id, @Param("used") Integer used);




+ 27
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/ExperimentInstanceStatusTask.java View File

@@ -1,11 +1,13 @@
package com.ruoyi.platform.scheduling; package com.ruoyi.platform.scheduling;


import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.Experiment; import com.ruoyi.platform.domain.Experiment;
import com.ruoyi.platform.domain.ExperimentIns; import com.ruoyi.platform.domain.ExperimentIns;
import com.ruoyi.platform.mapper.ExperimentDao; import com.ruoyi.platform.mapper.ExperimentDao;
import com.ruoyi.platform.mapper.ExperimentInsDao; import com.ruoyi.platform.mapper.ExperimentInsDao;
import com.ruoyi.platform.service.AimService; import com.ruoyi.platform.service.AimService;
import com.ruoyi.platform.service.ExperimentInsService; import com.ruoyi.platform.service.ExperimentInsService;
import com.ruoyi.platform.service.ResourceOccupyService;
import com.ruoyi.platform.utils.JacksonUtil; import com.ruoyi.platform.utils.JacksonUtil;
import com.ruoyi.platform.utils.JsonUtils; import com.ruoyi.platform.utils.JsonUtils;
import com.ruoyi.platform.vo.InsMetricInfoVo; import com.ruoyi.platform.vo.InsMetricInfoVo;
@@ -16,6 +18,8 @@ import org.springframework.stereotype.Component;


import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*; import java.util.*;


@Component() @Component()
@@ -28,6 +32,8 @@ public class ExperimentInstanceStatusTask {
private ExperimentInsDao experimentInsDao; private ExperimentInsDao experimentInsDao;
@Resource @Resource
private AimService aimService; private AimService aimService;
@Resource
private ResourceOccupyService resourceOccupyService;


private List<Integer> experimentIds = new ArrayList<>(); private List<Integer> experimentIds = new ArrayList<>();


@@ -43,7 +49,27 @@ public class ExperimentInstanceStatusTask {
try { try {
experimentIns = experimentInsService.queryStatusFromArgo(experimentIns); experimentIns = experimentInsService.queryStatusFromArgo(experimentIns);
} catch (Exception e) { } catch (Exception e) {
experimentIns.setStatus("Failed");
experimentIns.setStatus(Constant.Failed);
// 结束扣除积分
resourceOccupyService.endDeduce(Constant.TaskType_Workflow, Long.valueOf(experimentIns.getId()), null, null);
}

// 扣除积分
if (StringUtils.isNotEmpty(experimentIns.getNodesStatus())) {
Map<String, Object> nodesStatusMap = JsonUtils.jsonToMap(experimentIns.getNodesStatus());
for (String key : nodesStatusMap.keySet()) {
Map<String, Object> value = (Map<String, Object>) nodesStatusMap.get(key);
String startedAt = (String) value.get("startedAt");
Instant instant = Instant.parse(startedAt);
Date startTime = Date.from(instant);

String finishedAt = (String) value.get("finishedAt");
if (StringUtils.isEmpty(finishedAt)) {
resourceOccupyService.deducing(Constant.TaskType_Workflow, Long.valueOf(experimentIns.getId()), key, startTime);
} else {
resourceOccupyService.endDeduce(Constant.TaskType_Workflow, Long.valueOf(experimentIns.getId()), key, startTime);
}
}
} }
//运行成功的实验实例记录指标数值 //运行成功的实验实例记录指标数值
Map<String, Object> metricRecord = JacksonUtil.parseJSONStr2Map(experimentIns.getMetricRecord()); Map<String, Object> metricRecord = JacksonUtil.parseJSONStr2Map(experimentIns.getMetricRecord());


+ 8
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/RayInsStatusTask.java View File

@@ -42,13 +42,18 @@ public class RayInsStatusTask {
//当原本状态为null或非终止态时才调用argo接口 //当原本状态为null或非终止态时才调用argo接口
try { try {
rayIns = rayInsService.queryStatusFromArgo(rayIns); rayIns = rayInsService.queryStatusFromArgo(rayIns);

// 扣除积分
if (Constant.Running.equals(rayIns.getStatus())) { if (Constant.Running.equals(rayIns.getStatus())) {
resourceOccupyService.deducing(Constant.TaskType_Ray, rayIns.getId());
} else {
resourceOccupyService.endDeduce(Constant.TaskType_Ray, rayIns.getId());
resourceOccupyService.deducing(Constant.TaskType_Ray, rayIns.getId(), null,null);
} else if (Constant.Failed.equals(rayIns.getStatus()) || Constant.Terminated.equals(rayIns.getStatus())
|| Constant.Succeeded.equals(rayIns.getStatus())) {
resourceOccupyService.endDeduce(Constant.TaskType_Ray, rayIns.getId(), null,null);
} }
} catch (Exception e) { } catch (Exception e) {
rayIns.setStatus(Constant.Failed); rayIns.setStatus(Constant.Failed);
// 结束扣除积分
resourceOccupyService.endDeduce(Constant.TaskType_Ray, rayIns.getId(), null, null);
} }
// 线程安全的添加操作 // 线程安全的添加操作
synchronized (rayIds) { synchronized (rayIds) {


+ 3
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/scheduling/ResourceOccupyTask.java View File

@@ -25,11 +25,11 @@ public class ResourceOccupyTask {
private ServiceDao serviceDao; private ServiceDao serviceDao;


// 开发环境功能扣除积分 // 开发环境功能扣除积分
@Scheduled(cron = "0 0/10 * * * ?") // 每10分钟执行一次
@Scheduled(cron = "0 0/1 * * * ?") // 每10分钟执行一次
public void devDeduceCredit() { public void devDeduceCredit() {
List<DevEnvironment> devEnvironments = devEnvironmentDao.getRunning(); List<DevEnvironment> devEnvironments = devEnvironmentDao.getRunning();
for (DevEnvironment devEnvironment : devEnvironments) { for (DevEnvironment devEnvironment : devEnvironments) {
resourceOccupyService.deducing(Constant.TaskType_Dev, Long.valueOf(devEnvironment.getId()));
resourceOccupyService.deducing(Constant.TaskType_Dev, Long.valueOf(devEnvironment.getId()), null, null);
} }
} }


@@ -38,7 +38,7 @@ public class ResourceOccupyTask {
public void serviceDeduceCredit() { public void serviceDeduceCredit() {
List<ServiceVersion> serviceVersions = serviceDao.getRunning(); List<ServiceVersion> serviceVersions = serviceDao.getRunning();
for (ServiceVersion serviceVersion : serviceVersions) { for (ServiceVersion serviceVersion : serviceVersions) {
resourceOccupyService.deducing(Constant.TaskType_Service, serviceVersion.getId());
resourceOccupyService.deducing(Constant.TaskType_Service, serviceVersion.getId(), null, null);
} }
} }
} }

+ 5
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java View File

@@ -4,15 +4,17 @@ import com.ruoyi.platform.domain.ResourceOccupy;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;


import java.util.Date;

public interface ResourceOccupyService { public interface ResourceOccupyService {


Boolean haveResource(Integer computingResourceId) throws Exception; Boolean haveResource(Integer computingResourceId) throws Exception;


void startDeduce(Integer computingResourceId, String taskType, Long taskId);
void startDeduce(Integer computingResourceId, String taskType, Long taskId, String nodeId);


void endDeduce(String taskType, Long taskId);
void endDeduce(String taskType, Long taskId, String nodeId, Date nodeStartTime);


void deducing(String taskType, Long taskId);
void deducing(String taskType, Long taskId, String nodeId, Date nodeStartTime);


Page<ResourceOccupy> queryByPage(PageRequest pageRequest); Page<ResourceOccupy> queryByPage(PageRequest pageRequest);
} }

+ 7
- 0
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java View File

@@ -3,6 +3,7 @@ package com.ruoyi.platform.service.impl;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.DatasetTempStorage; import com.ruoyi.platform.domain.DatasetTempStorage;
import com.ruoyi.platform.domain.Experiment; import com.ruoyi.platform.domain.Experiment;
import com.ruoyi.platform.domain.ExperimentIns; import com.ruoyi.platform.domain.ExperimentIns;
@@ -12,6 +13,7 @@ import com.ruoyi.platform.mapper.ExperimentDao;
import com.ruoyi.platform.mapper.ExperimentInsDao; import com.ruoyi.platform.mapper.ExperimentInsDao;
import com.ruoyi.platform.mapper.ModelDependency1Dao; import com.ruoyi.platform.mapper.ModelDependency1Dao;
import com.ruoyi.platform.service.ExperimentInsService; import com.ruoyi.platform.service.ExperimentInsService;
import com.ruoyi.platform.service.ResourceOccupyService;
import com.ruoyi.platform.utils.*; import com.ruoyi.platform.utils.*;
import com.ruoyi.platform.vo.LogRequestVo; import com.ruoyi.platform.vo.LogRequestVo;
import com.ruoyi.platform.vo.PodLogVo; import com.ruoyi.platform.vo.PodLogVo;
@@ -66,6 +68,9 @@ public class ExperimentInsServiceImpl implements ExperimentInsService {
@Resource @Resource
private DatasetTempStorageDao datasetTempStorageDao; private DatasetTempStorageDao datasetTempStorageDao;


@Resource
private ResourceOccupyService resourceOccupyService;

private final MinioUtil minioUtil; private final MinioUtil minioUtil;


public ExperimentInsServiceImpl(MinioUtil minioUtil) { public ExperimentInsServiceImpl(MinioUtil minioUtil) {
@@ -410,6 +415,8 @@ public class ExperimentInsServiceImpl implements ExperimentInsService {
//修改实验状态 //修改实验状态
updateExperimentStatus(experimentIns.getExperimentId()); updateExperimentStatus(experimentIns.getExperimentId());


// 结束扣除积分
resourceOccupyService.endDeduce(Constant.TaskType_Workflow, Long.valueOf(experimentIns.getId()), null, null);
return true; return true;
} else { } else {
throw new Exception("终止错误"); throw new Exception("终止错误");


+ 16
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java View File

@@ -10,7 +10,6 @@ import com.ruoyi.platform.domain.*;
import com.ruoyi.platform.domain.dependencydomain.ProjectDepency; import com.ruoyi.platform.domain.dependencydomain.ProjectDepency;
import com.ruoyi.platform.domain.dependencydomain.TrainTaskDepency; import com.ruoyi.platform.domain.dependencydomain.TrainTaskDepency;
import com.ruoyi.platform.mapper.ExperimentDao; import com.ruoyi.platform.mapper.ExperimentDao;
import com.ruoyi.platform.mapper.ExperimentInsDao;
import com.ruoyi.platform.mapper.ModelDependency1Dao; import com.ruoyi.platform.mapper.ModelDependency1Dao;
import com.ruoyi.platform.service.*; import com.ruoyi.platform.service.*;
import com.ruoyi.platform.utils.HttpUtils; import com.ruoyi.platform.utils.HttpUtils;
@@ -62,6 +61,9 @@ public class ExperimentServiceImpl implements ExperimentService {
@Lazy @Lazy
private ExperimentInsService experimentInsService; private ExperimentInsService experimentInsService;


@Resource
private ResourceOccupyService resourceOccupyService;

@Resource @Resource
private ModelDependency1Dao modelDependency1Dao; private ModelDependency1Dao modelDependency1Dao;


@@ -233,6 +235,14 @@ public class ExperimentServiceImpl implements ExperimentService {
} }
Map<String, Object> converMap = JsonUtils.jsonToMap(convertRes); Map<String, Object> converMap = JsonUtils.jsonToMap(convertRes);


// 判断积分和资源是否足够
Map<String, Map<String, Object>> resourceInfo = (Map<String, Map<String, Object>>) converMap.get("resource_info");
for (Map.Entry<String, Map<String, Object>> entry : resourceInfo.entrySet()) {
Map<String, Object> node = entry.getValue();
resourceOccupyService.haveResource((Integer) node.get("computing_resource_id"));
}


// 组装运行接口json // 组装运行接口json
Map<String, Object> runReqMap = new HashMap<>(); Map<String, Object> runReqMap = new HashMap<>();
runReqMap.put("data", converMap.get("data")); runReqMap.put("data", converMap.get("data"));
@@ -300,6 +310,11 @@ public class ExperimentServiceImpl implements ExperimentService {
insertDatasetTempStorage(datasetDependendcy, trainInfo, experiment.getId(), insert.getId(), experiment.getName(), experiment.getWorkflowId()); insertDatasetTempStorage(datasetDependendcy, trainInfo, experiment.getId(), insert.getId(), experiment.getName(), experiment.getWorkflowId());
} }


// 记录开始扣积分
for (Map.Entry<String, Map<String, Object>> entry : resourceInfo.entrySet()) {
Map<String, Object> node = entry.getValue();
resourceOccupyService.startDeduce((Integer) node.get("computing_resource_id"), Constant.TaskType_Workflow, Long.valueOf(insert.getId()), entry.getKey());
}


} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);


+ 3
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java View File

@@ -133,9 +133,6 @@ public class JupyterServiceImpl implements JupyterService {
return "pod不存在!"; return "pod不存在!";
} }


// 结束扣积分
resourceOccupyService.endDeduce(Constant.TaskType_Dev, Long.valueOf(id));

// 使用 Kubernetes API 删除 Pod // 使用 Kubernetes API 删除 Pod
String deleteResult = k8sClientUtil.deletePod(podName, namespace); String deleteResult = k8sClientUtil.deletePod(podName, namespace);
// 删除service // 删除service
@@ -143,6 +140,9 @@ public class JupyterServiceImpl implements JupyterService {


devEnvironment.setStatus(Constant.Terminated); devEnvironment.setStatus(Constant.Terminated);
this.devEnvironmentService.update(devEnvironment); this.devEnvironmentService.update(devEnvironment);

// 结束扣积分
resourceOccupyService.endDeduce(Constant.TaskType_Dev, Long.valueOf(id), null, null);
return deleteResult + ",编辑器已停止"; return deleteResult + ",编辑器已停止";


} }


+ 1
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/RayInsServiceImpl.java View File

@@ -168,7 +168,7 @@ public class RayInsServiceImpl implements RayInsService {
rayInsDao.update(ins); rayInsDao.update(ins);
updateRayStatus(rayIns.getRayId()); updateRayStatus(rayIns.getRayId());
// 结束扣积分 // 结束扣积分
resourceOccupyService.endDeduce(Constant.TaskType_Ray, id);
resourceOccupyService.endDeduce(Constant.TaskType_Ray, id, null, null);
return true; return true;
} else { } else {
return false; return false;


+ 2
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/RayServiceImpl.java View File

@@ -205,7 +205,8 @@ public class RayServiceImpl implements RayService {
rayIns.setResultPath(outputPath); rayIns.setResultPath(outputPath);
rayInsDao.insert(rayIns); rayInsDao.insert(rayIns);
rayInsService.updateRayStatus(id); rayInsService.updateRayStatus(id);
resourceOccupyService.startDeduce(ray.getComputingResourceId(), Constant.TaskType_Ray, rayIns.getId());
// 记录开始扣除积分
resourceOccupyService.startDeduce(ray.getComputingResourceId(), Constant.TaskType_Ray, rayIns.getId(), null);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }


+ 37
- 20
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java View File

@@ -12,9 +12,11 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.util.Date;
import java.util.List;


@Service("resourceOccupyService") @Service("resourceOccupyService")
public class ResourceOccupyServiceImpl implements ResourceOccupyService { public class ResourceOccupyServiceImpl implements ResourceOccupyService {
@@ -48,15 +50,18 @@ public class ResourceOccupyServiceImpl implements ResourceOccupyService {
} }


@Override @Override
public void startDeduce(Integer computingResourceId, String taskType, Long taskId) {
@Transactional
public void startDeduce(Integer computingResourceId, String taskType, Long taskId, String nodeId) {
ResourceOccupy resourceOccupy = new ResourceOccupy(); ResourceOccupy resourceOccupy = new ResourceOccupy();
ComputingResource computingResource = computingResourceDao.queryById(computingResourceId); ComputingResource computingResource = computingResourceDao.queryById(computingResourceId);
resourceOccupy.setComputingResourceId(computingResourceId); resourceOccupy.setComputingResourceId(computingResourceId);
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser loginUser = SecurityUtils.getLoginUser();
resourceOccupy.setUserId(loginUser.getUserid()); resourceOccupy.setUserId(loginUser.getUserid());
resourceOccupy.setCreditPerHour(computingResource.getCreditPerHour()); resourceOccupy.setCreditPerHour(computingResource.getCreditPerHour());
resourceOccupy.setDescription(computingResource.getDescription());
resourceOccupy.setTaskType(taskType); resourceOccupy.setTaskType(taskType);
resourceOccupy.setTaskId(taskId); resourceOccupy.setTaskId(taskId);
resourceOccupy.setNodeId(nodeId);
resourceOccupyDao.save(resourceOccupy); resourceOccupyDao.save(resourceOccupy);


if (Constant.Computing_Resource_GPU.equals(computingResource.getComputingResource())) { if (Constant.Computing_Resource_GPU.equals(computingResource.getComputingResource())) {
@@ -67,31 +72,43 @@ public class ResourceOccupyServiceImpl implements ResourceOccupyService {
} }


@Override @Override
public void endDeduce(String taskType, Long taskId) {
ResourceOccupy resourceOccupy = resourceOccupyDao.getResourceOccupyByTask(taskType, taskId);
deducing(taskType, taskId);
resourceOccupy.setState(Constant.State_invalid);
resourceOccupyDao.edit(resourceOccupy);
@Transactional
public void endDeduce(String taskType, Long taskId, String nodeId, Date nodeStartTime) {
List<ResourceOccupy> resourceOccupys = resourceOccupyDao.getResourceOccupyByTask(taskType, taskId, nodeId);
for (ResourceOccupy resourceOccupy : resourceOccupys) {
deducing(taskType, taskId, nodeId, nodeStartTime);
resourceOccupy.setState(Constant.State_invalid);
resourceOccupyDao.edit(resourceOccupy);


ComputingResource computingResource = computingResourceDao.queryById(resourceOccupy.getComputingResourceId());
if (Constant.Computing_Resource_GPU.equals(computingResource.getComputingResource())) {
resourceOccupyDao.updateUnUsed(computingResource.getResourceId(), computingResource.getGpuNums());
} else {
resourceOccupyDao.updateUnUsed(computingResource.getResourceId(), computingResource.getCpuCores());
ComputingResource computingResource = computingResourceDao.queryById(resourceOccupy.getComputingResourceId());
if (Constant.Computing_Resource_GPU.equals(computingResource.getComputingResource())) {
resourceOccupyDao.updateUnUsed(computingResource.getResourceId(), computingResource.getGpuNums());
} else {
resourceOccupyDao.updateUnUsed(computingResource.getResourceId(), computingResource.getCpuCores());
}
} }
} }


@Override @Override
public void deducing(String taskType, Long taskId) {
ResourceOccupy resourceOccupy = resourceOccupyDao.getResourceOccupyByTask(taskType, taskId);
long timeDifferenceMillis = new Date().getTime() - resourceOccupy.getDeduceLastTime().getTime();
Float hours = (float) (timeDifferenceMillis / (1000 * 60 * 60));

float deduceCredit = resourceOccupy.getCreditPerHour() * hours;
resourceOccupyDao.deduceCredit(deduceCredit, resourceOccupy.getUserId());
@Transactional
public void deducing(String taskType, Long taskId, String nodeId, Date nodeStartTime) {
List<ResourceOccupy> resourceOccupys = resourceOccupyDao.getResourceOccupyByTask(taskType, taskId, nodeId);
for (ResourceOccupy resourceOccupy : resourceOccupys) {
Date now = new Date();
long timeDifferenceMillis;
if (nodeStartTime != null && resourceOccupy.getDeduceLastTime().before(nodeStartTime)) {
timeDifferenceMillis = now.getTime() - nodeStartTime.getTime();
} else {
timeDifferenceMillis = now.getTime() - resourceOccupy.getDeduceLastTime().getTime();
}
Double hours = (double) timeDifferenceMillis / (1000 * 60 * 60);
Double deduceCredit = resourceOccupy.getCreditPerHour() * hours;
resourceOccupyDao.deduceCredit(deduceCredit, resourceOccupy.getUserId());


resourceOccupy.setDeduceLastTime(new Date());
resourceOccupyDao.edit(resourceOccupy);
resourceOccupy.setDeduceCredit(resourceOccupy.getDeduceCredit() + deduceCredit);
resourceOccupy.setDeduceLastTime(now);
resourceOccupyDao.edit(resourceOccupy);
}
} }


@Override @Override


+ 5
- 3
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ServiceServiceImpl.java View File

@@ -252,19 +252,21 @@ public class ServiceServiceImpl implements ServiceService {
paramMap.put("service_type", service.getServiceType()); paramMap.put("service_type", service.getServiceType());
paramMap.put("deploy_type", serviceVersion.getDeployType()); paramMap.put("deploy_type", serviceVersion.getDeployType());


// 记录开始扣积分
// 判断是否有资源
if (resourceOccupyService.haveResource(serviceVersion.getComputingResourceId())) { if (resourceOccupyService.haveResource(serviceVersion.getComputingResourceId())) {
String req = HttpUtils.sendPost(argoUrl + modelService + "/create", JSON.toJSONString(paramMap)); String req = HttpUtils.sendPost(argoUrl + modelService + "/create", JSON.toJSONString(paramMap));
if (StringUtils.isNotEmpty(req)) { if (StringUtils.isNotEmpty(req)) {
Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req);
if ((Integer) reqMap.get("code") == 200) { if ((Integer) reqMap.get("code") == 200) {
resourceOccupyService.startDeduce(serviceVersion.getComputingResourceId(), Constant.TaskType_Service, serviceVersion.getId());
Map<String, String> data = (Map<String, String>) reqMap.get("data"); Map<String, String> data = (Map<String, String>) reqMap.get("data");
serviceVersion.setUrl(data.get("url")); serviceVersion.setUrl(data.get("url"));
serviceVersion.setDeploymentName(data.get("deployment_name")); serviceVersion.setDeploymentName(data.get("deployment_name"));
serviceVersion.setSvcName(data.get("svc_name")); serviceVersion.setSvcName(data.get("svc_name"));
serviceVersion.setRunState(Constant.Pending); serviceVersion.setRunState(Constant.Pending);
serviceDao.updateServiceVersion(serviceVersion); serviceDao.updateServiceVersion(serviceVersion);

// 记录开始扣积分
resourceOccupyService.startDeduce(serviceVersion.getComputingResourceId(), Constant.TaskType_Service, serviceVersion.getId(), null);
return "启动成功"; return "启动成功";
} else { } else {
throw new RuntimeException("启动失败"); throw new RuntimeException("启动失败");
@@ -286,7 +288,7 @@ public class ServiceServiceImpl implements ServiceService {
serviceVersion.setRunState(Constant.Stopped); serviceVersion.setRunState(Constant.Stopped);
serviceDao.updateServiceVersion(serviceVersion); serviceDao.updateServiceVersion(serviceVersion);
// 结束扣积分 // 结束扣积分
resourceOccupyService.endDeduce(Constant.TaskType_Service, id);
resourceOccupyService.endDeduce(Constant.TaskType_Service, id, null, null);
return "停止成功"; return "停止成功";
} else { } else {
throw new RuntimeException("停止失败"); throw new RuntimeException("停止失败");


+ 3
- 6
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java View File

@@ -1,6 +1,5 @@
package com.ruoyi.platform.utils; package com.ruoyi.platform.utils;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.platform.constant.Constant; import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.ComputingResource; import com.ruoyi.platform.domain.ComputingResource;
import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.domain.DevEnvironment;
@@ -11,7 +10,6 @@ import io.kubernetes.client.custom.IntOrString;
import io.kubernetes.client.custom.Quantity; import io.kubernetes.client.custom.Quantity;
import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.*; import io.kubernetes.client.openapi.models.*;
import io.kubernetes.client.util.ClientBuilder; import io.kubernetes.client.util.ClientBuilder;
@@ -19,7 +17,6 @@ import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.credentials.AccessTokenAuthentication; import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -519,7 +516,8 @@ public class K8sClientUtil {
pod = api.createNamespacedPod(namespace, pod, null, null, null); pod = api.createNamespacedPod(namespace, pod, null, null, null);
String nodeName = getNodeName(podName, namespace); String nodeName = getNodeName(podName, namespace);


resourceOccupyService.startDeduce(devEnvironment.getComputingResourceId(), Constant.TaskType_Dev, Long.valueOf(devEnvironment.getId()));
// 记录开始扣除积分
resourceOccupyService.startDeduce(devEnvironment.getComputingResourceId(), Constant.TaskType_Dev, Long.valueOf(devEnvironment.getId()), null);
} }
} catch (ApiException e) { } catch (ApiException e) {
throw new RuntimeException("创建pod异常:" + e.getResponseBody()); throw new RuntimeException("创建pod异常:" + e.getResponseBody());
@@ -699,8 +697,7 @@ public class K8sClientUtil {
ComputingResource computingResource = computingResourceDao.queryById(computingResourceId); ComputingResource computingResource = computingResourceDao.queryById(computingResourceId);


//配置pod资源 //配置pod资源
String memory = computingResource.getMemoryGb().toString();
memory = memory.substring(0, memory.length() - 1).concat("i");
String memory = computingResource.getMemoryGb().toString().concat("Gi");
HashMap<String, Quantity> limitMap = new HashMap<>(); HashMap<String, Quantity> limitMap = new HashMap<>();
if (computingResource.getGpuNums() != null && computingResource.getGpuNums() != 0) { if (computingResource.getGpuNums() != null && computingResource.getGpuNums() != 0) {
limitMap.put("nvidia.com/gpu", new Quantity(String.valueOf(computingResource.getGpuNums()))); limitMap.put("nvidia.com/gpu", new Quantity(String.valueOf(computingResource.getGpuNums())));


+ 15
- 5
ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml View File

@@ -2,8 +2,11 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.platform.mapper.ResourceOccupyDao"> <mapper namespace="com.ruoyi.platform.mapper.ResourceOccupyDao">
<insert id="save"> <insert id="save">
insert into resource_occupy (user_id, computing_resource_id, credit_per_hour)
values (#{resourceOccupy.userId}, #{resourceOccupy.computingResourceId}, #{resourceOccupy.creditPerHour})
insert into resource_occupy (user_id, computing_resource_id, credit_per_hour, description, task_type, task_id,
node_id)
values (#{resourceOccupy.userId}, #{resourceOccupy.computingResourceId}, #{resourceOccupy.creditPerHour},
#{resourceOccupy.description}, #{resourceOccupy.taskType}, #{resourceOccupy.taskId},
#{resourceOccupy.nodeId})
</insert> </insert>


<update id="edit"> <update id="edit">
@@ -15,6 +18,9 @@
<if test="resourceOccupy.deduceLastTime != null"> <if test="resourceOccupy.deduceLastTime != null">
deduce_last_time = #{resourceOccupy.deduceLastTime}, deduce_last_time = #{resourceOccupy.deduceLastTime},
</if> </if>
<if test="resourceOccupy.deduceCredit != null">
deduce_credit = #{resourceOccupy.deduceCredit},
</if>
</set> </set>
where id = #{resourceOccupy.id} where id = #{resourceOccupy.id}
</update> </update>
@@ -46,12 +52,16 @@
<select id="getResourceOccupyByTask" resultType="com.ruoyi.platform.domain.ResourceOccupy"> <select id="getResourceOccupyByTask" resultType="com.ruoyi.platform.domain.ResourceOccupy">
select * select *
from resource_occupy from resource_occupy
where task_type = #{task_type}
and task_id = #{task_id}
where task_type = #{taskType}
and task_id = #{taskId}
<if test="nodeId != null and nodeId !=''">
and node_id = #{nodeId}
</if>
and state = 1
</select> </select>


<select id="count" resultType="java.lang.Long"> <select id="count" resultType="java.lang.Long">
select count(1) resource_occupy
select count(1) from resource_occupy
</select> </select>


<select id="queryByPage" resultType="com.ruoyi.platform.domain.ResourceOccupy"> <select id="queryByPage" resultType="com.ruoyi.platform.domain.ResourceOccupy">


Loading…
Cancel
Save