服务发布

This commit is contained in:
2025-05-22 15:57:15 +08:00
parent 5af1b7a577
commit d01f3bfcbf
3 changed files with 107 additions and 17 deletions

View File

@ -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<ServicePublishVO> save(@RequestBody ServicePublishDTO servicePublishDTO){
public OptResult<List<ServicePublishVO>> 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<ServicePublishVO> list;
list = (List<ServicePublishVO>) 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<MachineInfo> machineList = (List<MachineInfo>) 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);
}
}