[ server] [ agent ] - 完成核心信息,关机的功能

This commit is contained in:
zeaslity
2023-02-09 10:51:01 +08:00
parent 8231dd21e4
commit 47d13ca4d7
8 changed files with 247 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package io.wdd.rpc.agent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.beans.agent.AgentOperationMessage;
import io.wdd.common.beans.agent.AgentOperationType;
@@ -25,6 +26,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_TOPIC_NAME_SET;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST;
import static io.wdd.rpc.message.handler.OctopusMessageHandler.AGENT_LATEST_VERSION;
import static io.wdd.rpc.message.handler.OctopusMessageHandler.OCTOPUS_MESSAGE_FROM_AGENT;
@@ -100,12 +102,125 @@ public class OctopusAgentServiceImpl implements OctopusAgentService {
@Override
public Map<String, ServerInfoVO> getAllAgentCoreInfo() {
return null;
HashMap<String, ServerInfoVO> result = new HashMap<>();
// 获取Agent的版本信息 -- 超时时间
// 发送OctopusMessage-Agent
// 从OctopusToServer中收集到所有Agent的版本信息
// 组装信息至集合中
LocalDateTime currentTime = TimeUtils.currentFormatTime();
buildAndSendToAllAgent(
AgentOperationType.INFO,
currentTime
);
CountDownLatch countDownLatch = new CountDownLatch(ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.size());
CompletableFuture<Void> getAllAgentCoreInfoFuture = waitCollectAllAgentCoreInfo(
result,
currentTime,
countDownLatch
);
try {
// 超时等待5秒钟, 或者所有的Agent均已经完成上报
countDownLatch.await(
5,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
log.warn("存在部分Agent没有上报 核心信息!");
} finally {
// 必须关闭释放线程
getAllAgentCoreInfoFuture.cancel(true);
}
return result;
}
private CompletableFuture<Void> waitCollectAllAgentCoreInfo(HashMap<String, ServerInfoVO> result, LocalDateTime startTime, CountDownLatch countDownLatch) {
ExecutorService pool = ServerCommonPool.pool;
// 从OCTOPUS_MESSAGE_FROM_AGENT中获取符合条件的信息
return CompletableFuture.runAsync(
() -> {
while (true) {
if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) {
// 开始收集等待 200ms
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 返回,继续死循环
continue;
}
OctopusMessage message = OCTOPUS_MESSAGE_FROM_AGENT.poll();
// 获取到OM
// 判断信息是否是需要的类型
// 根据 init_time + OctopusMessageType + AgentOperationMessage
if (!judgyIsCurrentServerReplyMessage(
message,
startTime
)) {
// 不是当前应用需要的的OM将信息放置与Cache队列的末尾
try {
OCTOPUS_MESSAGE_FROM_AGENT.put(message);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 返回,继续死循环
continue;
}
// 是当前需要的消息信息
try {
ServerInfoVO serverInfoVO = objectMapper.readValue(
(String) message.getResult(),
new TypeReference<ServerInfoVO>() {
}
);
result.put(
message.getUuid(),
serverInfoVO
);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
countDownLatch.countDown();
}
},
pool
);
}
@Override
public String shutdownAgentDanger(String agentTopicName) {
if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) {
log.error("agentTopicName Error !");
return "agentTopicName Error!";
}
LocalDateTime formatTime = TimeUtils.currentFormatTime();
buildAndSendToAllAgent(
AgentOperationType.SHUTDOWN,
formatTime
);
// 是否需要检查相应的状态
return null;
}

View File

@@ -4,6 +4,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.wdd.common.beans.response.R;
import io.wdd.rpc.agent.OctopusAgentService;
import io.wdd.server.beans.vo.ServerInfoVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -26,4 +27,12 @@ public class AgentController {
return R.ok(octopusAgentService.getAllAgentVersion());
}
@GetMapping("/coreInfo")
@ApiOperation("获取所有OctopusAgent的核心信息")
public R<Map<String, ServerInfoVO>> getAllAgentCoreInfo(){
return R.ok(octopusAgentService.getAllAgentCoreInfo());
}
}