From d01f3bfcbf63cb67d2f96b8e486249627264177a Mon Sep 17 00:00:00 2001 From: xiaohucoding <2307520758@qq.com> Date: Thu, 22 May 2025 15:57:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/publishController.java | 84 +++++++++++++++---- .../pojo/MachineInfo.java | 39 +++++++++ .../pojo/ServicePublishVO.java | 1 + 3 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/MachineInfo.java diff --git a/src/main/java/com/bipt/intelligentapplicationorchestrationservice/controller/publishController.java b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/controller/publishController.java index 8cd5599..ea333da 100644 --- a/src/main/java/com/bipt/intelligentapplicationorchestrationservice/controller/publishController.java +++ b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/controller/publishController.java @@ -9,11 +9,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; import java.util.List; @Tag(name ="服务发布相关接口") @@ -34,29 +32,81 @@ public class publishController { @PostMapping @Operation(summary ="新增发布请求") @Transactional - public OptResult save(@RequestBody ServicePublishDTO servicePublishDTO){ + public OptResult> save(@RequestBody ServicePublishDTO servicePublishDTO) { log.info("模型发布请求:{}", servicePublishDTO); publishService.save(servicePublishDTO); Long modelId = servicePublishDTO.getModelId(); - String key="Model_" + modelId; + String key = "Model_" + modelId; //查询redis是否存在GPU相关资源数据 - ServicePublishVO servicePublishVO = (ServicePublishVO) redisTemplate.opsForValue().get(key); + List list; + list = (List) redisTemplate.opsForValue().get(key); //如果存在,直接返回,无须查询数据库 - if (servicePublishVO != null){ - return OptResult.success(servicePublishVO); + if (list != null) { + return OptResult.success(list); + }else { + list = new ArrayList<>(); + } + String modelConfig = publishService.getByModelId(modelId); + if (modelConfig == null) { + log.error("模型配置为空,modelId={}", modelId); + } + String[] keyValuePairs = modelConfig.split("\\|"); + String GPUMemorySize = null; + String GPUModel = null; + for (String pair : keyValuePairs) { + pair = pair.trim(); + if (pair.startsWith("GPU")) { + GPUModel = pair.split(";", 2)[1]; + } else if (pair.startsWith("Memory:")) { + GPUMemorySize = pair.split(":", 2)[1]; + } + } + ServicePublishVO servicePublishVO = new ServicePublishVO(); + servicePublishVO.setIp(servicePublishDTO.getIp()); + servicePublishVO.setModelId(servicePublishDTO.getModelId()); + servicePublishVO.setGPUMemorySize(GPUMemorySize); + servicePublishVO.setGPUModel(GPUModel); + //todo 调用模型部署,传递信息 + + servicePublishVO.setApiUrl(servicePublishDTO.getApiUrl()); + list.add(servicePublishVO); + redisTemplate.opsForValue().set(key,list); + //一个ip上有多个机器 + // 假设从 Redis 获取的列表元素是 MachineInfo 类型 + String ip = servicePublishVO.getIp(); + String key1 = ip; + List machineList = (List) redisTemplate.opsForValue().get(key1); + + + // 模型所需的 GPU 资源 + String requiredGPUModel = servicePublishVO.getGPUModel(); + Integer requiredGPUMemory = Integer.valueOf(servicePublishVO.getGPUMemorySize()); + + if (machineList != null) { + for (MachineInfo machine : machineList) { + // 获取机器的 GPU 资源 + String machineGPUModel = machine.getGPUModel(); + Integer machineGPUMemory = machine.getGPUMemorySize(); + + // 判断机器是否满足模型需求 + if (requiredGPUModel.equals(machineGPUModel) && + machineGPUMemory >= requiredGPUMemory) { + return OptResult.success(list); + } + } + String key3 = "wait_queue"; + redisTemplate.opsForValue().set(key3,list); + //todo资源释放时候优先分配等待队列中任务 + } - String modelConfig = publishService.getByModelId(modelId); - //todo 序列化结果提出来 - - ServicePublish servicePublish = new ServicePublish(); - String ip = servicePublishDTO.getIp(); - servicePublish.setIp(ip); - - return OptResult.success(); + return OptResult.success(list); } + + + } diff --git a/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/MachineInfo.java b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/MachineInfo.java new file mode 100644 index 0000000..ccd4920 --- /dev/null +++ b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/MachineInfo.java @@ -0,0 +1,39 @@ +package com.bipt.intelligentapplicationorchestrationservice.pojo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.Date; +import java.util.Map; + +/** + * 机器信息实体类 + * @author hky + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class MachineInfo implements Serializable { + private static final long serialVersionUID = 1L; + + private String id; // 机器唯一标识 + private String ip; // IP 地址 + private String hostname; // 主机名 + private Integer cpuCoreNum; // CPU 核心数 + private Long memoryTotal; // 总内存 (MB) + private Long memoryUsed; // 已用内存 (MB) + private String GPUModel; // GPU 型号 + private Integer GPUMemorySize; // GPU 显存大小 (GB) + private Integer GPUCount; // GPU 数量 + private Integer status; // 机器状态 (0: 离线, 1: 在线, 2: 繁忙) + private String os; // 操作系统 + private String region; // 所属区域 + private String zone; // 可用区 + private Date createTime; // 创建时间 + private Date updateTime; // 更新时间 + private Map labels; // 自定义标签 +} diff --git a/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/ServicePublishVO.java b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/ServicePublishVO.java index d49a340..0500cf5 100644 --- a/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/ServicePublishVO.java +++ b/src/main/java/com/bipt/intelligentapplicationorchestrationservice/pojo/ServicePublishVO.java @@ -20,6 +20,7 @@ public class ServicePublishVO implements Serializable { private String GPUModel; private String ip; private String GPUMemorySize; + private String apiUrl; }