[ Status ] 完成Agent Metric部分
This commit is contained in:
@@ -26,6 +26,4 @@ public enum AgentHealthyStatusEnum {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
228
server/src/main/java/io/wdd/rpc/status/CommonAndStatusCache.java
Normal file
228
server/src/main/java/io/wdd/rpc/status/CommonAndStatusCache.java
Normal file
@@ -0,0 +1,228 @@
|
||||
package io.wdd.rpc.status;
|
||||
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import io.wdd.server.beans.vo.ServerInfoVO;
|
||||
import io.wdd.server.coreService.CoreServerService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* Server启动或者运行的时候,需要初 缓存一系列的信息
|
||||
* <p>
|
||||
* 所有Agent的TopicName ALL_AGENT_TOPIC_NAME_SET
|
||||
* <p>
|
||||
* 2023年7月10日 此部分应该初始化全部为 False状态
|
||||
* Agent状态信息的两个Map STATUS_AGENT_LIST_MAP ALL_AGENT_STATUS_MAP
|
||||
* <p>
|
||||
* 2023年7月10日 -- 此部分作为Redis中存储的 二级缓存部分 应该严格遵循
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class CommonAndStatusCache {
|
||||
|
||||
/**
|
||||
* 存储所有的AgentTopicName的缓存
|
||||
*/
|
||||
public static final Set<String> ALL_AGENT_TOPIC_NAME_SET = new HashSet<>();
|
||||
|
||||
/**
|
||||
* 存储所有的AgentTopicName的缓存
|
||||
*/
|
||||
public static final List<String> ALL_AGENT_TOPIC_NAME_LIST = new ArrayList<>();
|
||||
/**
|
||||
* 存储 状态对应Agent列表的Map
|
||||
* Agent的状态描述为 AgentHealthyStatusEnum
|
||||
* HEALTHY -> ["agentTopicName-1", "agentTopicName-2"]
|
||||
* FAILED -> ["agentTopicName-1", "agentTopicName-2"]
|
||||
*/
|
||||
public static final Map<String, List<String>> STATUS_AGENT_LIST_MAP = new HashMap<>();
|
||||
/**
|
||||
* 存储所有Agent状态的Map
|
||||
* <p>
|
||||
* 内容为 agentTopicName- True代表健康 False代表不健康
|
||||
*/
|
||||
public static final Map<String, Boolean> ALL_AGENT_STATUS_MAP = new HashMap<>();
|
||||
/**
|
||||
* 保存所有健康运行的Agent Topic Name
|
||||
*/
|
||||
public static final List<String> ALL_HEALTHY_AGENT_TOPIC_NAME_LIST = new ArrayList<>();
|
||||
/**
|
||||
* 记录状态信息缓存的更新时间
|
||||
*/
|
||||
public static final String STATUS_UPDATE_TIME_KEY = "UPDATE_TIME";
|
||||
/**
|
||||
* 记录状态信息缓存的初始化时间
|
||||
*/
|
||||
public static final String STATUS_INIT_TIME_KEY = "INIT_TIME";
|
||||
/**
|
||||
* AgentTopicName 在Redis中緩存的Key
|
||||
*/
|
||||
private static final String ALL_AGENT_TOPIC_NAME_REDIS_KEY = "ALL_AGENT_TOPIC_NAME";
|
||||
|
||||
@Resource
|
||||
CoreServerService coreServerService;
|
||||
@Resource
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void InitToGenerateAllStatusCache() {
|
||||
|
||||
//所有Agent的TopicName ALL_AGENT_TOPIC_NAME_SET
|
||||
updateAllAgentTopicNameCache();
|
||||
|
||||
// Agent状态信息的两个Map
|
||||
// 初始化 默认创建全部失败的Map
|
||||
Map<String, Boolean> initAgentFalseStatusMap = ALL_AGENT_TOPIC_NAME_LIST
|
||||
.stream()
|
||||
.collect(Collectors.toMap(
|
||||
topicName -> topicName,
|
||||
topicName -> Boolean.FALSE
|
||||
));
|
||||
|
||||
updateAgentStatusCache(initAgentFalseStatusMap);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 从数据库中获取所有注册过的Agent名称
|
||||
* <p>
|
||||
* 2023年7月10日 写入Redis中保存一份
|
||||
*/
|
||||
public void updateAllAgentTopicNameCache() {
|
||||
|
||||
//查询DB
|
||||
List<ServerInfoVO> allAgentInfo = coreServerService.serverGetAll();
|
||||
|
||||
if (CollectionUtils.isEmpty(allAgentInfo)) {
|
||||
log.warn("[Serer Boot Up] Octopus Serer First Boot Up ! No Agent Registered Ever!");
|
||||
return;
|
||||
}
|
||||
|
||||
ALL_AGENT_TOPIC_NAME_LIST.clear();
|
||||
ALL_AGENT_TOPIC_NAME_SET.clear();
|
||||
|
||||
List<String> collect = allAgentInfo
|
||||
.stream()
|
||||
.map(ServerInfoVO::getTopicName)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
ALL_AGENT_TOPIC_NAME_LIST.addAll(collect);
|
||||
ALL_AGENT_TOPIC_NAME_SET.addAll(collect);
|
||||
|
||||
// 2023年7月10日 同步缓存至Redis中
|
||||
redisTemplate
|
||||
.opsForSet()
|
||||
.add(
|
||||
ALL_AGENT_TOPIC_NAME_REDIS_KEY,
|
||||
ALL_AGENT_TOPIC_NAME_SET
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据传入的状态Map更新二级缓存的两个状态Map和健康主机的列表
|
||||
* ALL_AGENT_STATUS_MAP
|
||||
* STATUS_AGENT_LIST_MAP
|
||||
* <p>
|
||||
* ALL_HEALTHY_AGENT_TOPIC_NAME_LIST
|
||||
*/
|
||||
public void updateAgentStatusCache(Map<String, Boolean> agentAliveStatusMap) {
|
||||
|
||||
// 检查,排除没有节点的情况
|
||||
if (CollectionUtils.isEmpty(ALL_AGENT_TOPIC_NAME_LIST)) {
|
||||
log.warn("[Serer Boot Up] Octopus Serer First Boot Up ! No Agent Registered Ever!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2023年6月15日 更新状态缓存
|
||||
ALL_AGENT_STATUS_MAP.clear();
|
||||
ALL_AGENT_STATUS_MAP.putAll(agentAliveStatusMap);
|
||||
|
||||
// 2023-01-16
|
||||
// 更新 状态-Agent容器 内容为
|
||||
// HEALTHY -> ["agentTopicName-1", "agentTopicName-2"]
|
||||
// FAILED -> ["agentTopicName-1", "agentTopicName-2"]
|
||||
Map<String, List<String>> statusAgentListMap = agentAliveStatusMap
|
||||
.entrySet()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.groupingBy(
|
||||
Map.Entry::getValue
|
||||
)
|
||||
)
|
||||
.entrySet()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
entry -> entry.getKey() ? "HEALTHY" : "FAILED",
|
||||
entry -> entry
|
||||
.getValue()
|
||||
.stream()
|
||||
.map(
|
||||
Map.Entry::getKey
|
||||
)
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
);
|
||||
|
||||
// 2023-2-3 bug fix
|
||||
STATUS_AGENT_LIST_MAP.clear();
|
||||
STATUS_AGENT_LIST_MAP.putAll(statusAgentListMap);
|
||||
|
||||
// 2023年2月21日,更新时间
|
||||
String timeString = TimeUtils.currentFormatTimeString();
|
||||
STATUS_AGENT_LIST_MAP.put(
|
||||
STATUS_UPDATE_TIME_KEY,
|
||||
Collections.singletonList(timeString)
|
||||
);
|
||||
|
||||
log.debug("Agent存活状态 状态-Agent名称-Map 已经更新了");
|
||||
|
||||
// 缓存相应的存活Agent
|
||||
List<String> allHealthyAgentTopicNames = agentAliveStatusMap
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(
|
||||
entry -> entry
|
||||
.getKey()
|
||||
.equals(Boolean.TRUE)
|
||||
)
|
||||
.map(
|
||||
Map.Entry::getKey
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.clear();
|
||||
ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.addAll(allHealthyAgentTopicNames);
|
||||
|
||||
|
||||
// help gc
|
||||
agentAliveStatusMap = null;
|
||||
statusAgentListMap = null;
|
||||
allHealthyAgentTopicNames = null;
|
||||
}
|
||||
|
||||
private String uniformHealthyStatus(String agentStatus) {
|
||||
switch (agentStatus) {
|
||||
case "0":
|
||||
return AgentHealthyStatusEnum.FAILED.getStatus();
|
||||
case "1":
|
||||
return AgentHealthyStatusEnum.HEALTHY.getStatus();
|
||||
default:
|
||||
return AgentHealthyStatusEnum.UNKNOWN.getStatus();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -34,9 +34,6 @@ public class OctopusStatusMessage {
|
||||
*/
|
||||
String statusType;
|
||||
|
||||
int metricRepeatCount;
|
||||
|
||||
int metricRepeatPinch;
|
||||
|
||||
public static OctopusMessage ConstructAgentStatusMessage(String statusType, String agentTopicName, LocalDateTime currentTime) {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
public enum AppStatusEnum {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import io.wdd.common.utils.FormatUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import io.wdd.common.utils.FormatUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wdd.rpc.status;
|
||||
package io.wdd.rpc.status.deprecate;
|
||||
|
||||
|
||||
import io.wdd.common.utils.FormatUtils;
|
||||
@@ -1,16 +0,0 @@
|
||||
package io.wdd.rpc.status.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface AsyncStatusService {
|
||||
|
||||
/**
|
||||
* 应该是同步收集 agentTopicNameList 的节点的存活状态,并返回所有的状态存活结果
|
||||
*
|
||||
* @param agentTopicNameList
|
||||
* @param aliveStatusWaitMaxTime
|
||||
* @return
|
||||
*/
|
||||
Map<String, Boolean> AsyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime);
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package io.wdd.rpc.status.service;
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import io.wdd.rpc.message.OctopusMessage;
|
||||
import io.wdd.rpc.message.OctopusMessageType;
|
||||
import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService;
|
||||
import io.wdd.rpc.message.handler.async.OctopusMessageAsyncReplayContend;
|
||||
import io.wdd.rpc.message.sender.OMessageToAgentSender;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.wdd.rpc.init.AgentStatusCacheService.ALL_AGENT_TOPIC_NAME_LIST;
|
||||
import static io.wdd.rpc.status.OctopusStatusMessage.ConstructAgentStatusMessage;
|
||||
import static io.wdd.rpc.status.OctopusStatusMessage.HEALTHY_STATUS_MESSAGE_TYPE;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AsyncStatusServiceImpl implements AsyncStatusService {
|
||||
|
||||
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.STATUS;
|
||||
|
||||
@Resource
|
||||
OMessageToAgentSender oMessageToAgentSender;
|
||||
|
||||
@Resource
|
||||
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService;
|
||||
|
||||
@Override
|
||||
public Map<String, Boolean> AsyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime) {
|
||||
|
||||
// 构造最后的结果Map
|
||||
Map<String, Boolean> agentAliveStatusMap = agentTopicNameList
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
agentTopicName -> agentTopicName,
|
||||
agentTopicName -> Boolean.FALSE
|
||||
));
|
||||
|
||||
LocalDateTime currentTime = TimeUtils.currentFormatTime();
|
||||
// 构造OctopusMessage - StatusMessage结构体, 下发所有的消息
|
||||
buildAndSendAgentAliveOctopusMessage(currentTime);
|
||||
|
||||
// 异步收集消息
|
||||
OctopusMessageAsyncReplayContend statusAsyncReplayContend = OctopusMessageAsyncReplayContend.build(
|
||||
agentTopicNameList.size(),
|
||||
CurrentAppOctopusMessageType,
|
||||
currentTime
|
||||
);
|
||||
asyncWaitOctopusMessageResultService.waitFor(statusAsyncReplayContend);
|
||||
|
||||
// 解析结果
|
||||
CountDownLatch countDownLatch = statusAsyncReplayContend.getCountDownLatch();
|
||||
|
||||
// 等待状态返回的结果
|
||||
boolean agentAliveStatusCollectResult = false;
|
||||
try {
|
||||
agentAliveStatusCollectResult = countDownLatch.await(
|
||||
aliveStatusWaitMaxTime,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (!agentAliveStatusCollectResult) {
|
||||
log.debug("Agent存活状态检查,没有检查到全部的Agent!");
|
||||
}
|
||||
|
||||
// 移除等待队列
|
||||
asyncWaitOctopusMessageResultService.stopWaiting(statusAsyncReplayContend);
|
||||
|
||||
// 处理结果
|
||||
statusAsyncReplayContend
|
||||
.getReplayOMList()
|
||||
.stream()
|
||||
.forEach(
|
||||
statusOMessage -> {
|
||||
if (statusOMessage.getResult() != null) {
|
||||
agentAliveStatusMap.put(
|
||||
statusOMessage.getUuid(),
|
||||
Boolean.TRUE
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 返回Agent的存活状态内容
|
||||
return agentAliveStatusMap;
|
||||
}
|
||||
|
||||
private void buildAndSendAgentAliveOctopusMessage(LocalDateTime currentTime) {
|
||||
|
||||
List<OctopusMessage> octopusStatusMessageList = ALL_AGENT_TOPIC_NAME_LIST
|
||||
.stream()
|
||||
.map(
|
||||
agentTopicName -> ConstructAgentStatusMessage(
|
||||
HEALTHY_STATUS_MESSAGE_TYPE,
|
||||
agentTopicName,
|
||||
currentTime
|
||||
)
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 发送信息
|
||||
oMessageToAgentSender.send(octopusStatusMessageList);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.wdd.rpc.status.service;
|
||||
|
||||
import io.wdd.rpc.status.beans.AgentStatus;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface SyncStatusService {
|
||||
|
||||
/**
|
||||
* 同步收集 agentTopicNameList 的节点的存活状态,并返回所有的状态存活结果
|
||||
*
|
||||
* @param agentTopicNameList
|
||||
* @param aliveStatusWaitMaxTime
|
||||
* @return
|
||||
*/
|
||||
Map<String, Boolean> SyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime);
|
||||
|
||||
/**
|
||||
* 同步收集 节点的运行状态
|
||||
*
|
||||
* @param agentTopicNameList
|
||||
* @param collectMetricWaitMaxTime
|
||||
* @return
|
||||
*/
|
||||
Map<String, AgentStatus> SyncCollectAgentMetricStatus(List<String> agentTopicNameList, int collectMetricWaitMaxTime);
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package io.wdd.rpc.status.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import io.wdd.rpc.message.OctopusMessage;
|
||||
import io.wdd.rpc.message.OctopusMessageType;
|
||||
import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService;
|
||||
import io.wdd.rpc.message.handler.async.OctopusMessageSynScReplayContend;
|
||||
import io.wdd.rpc.message.sender.OMessageToAgentSender;
|
||||
import io.wdd.rpc.status.beans.AgentStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.wdd.common.utils.OctopusObjectMapperConfig.OctopusObjectMapper;
|
||||
import static io.wdd.rpc.status.OctopusStatusMessage.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SyncStatusServiceImpl implements SyncStatusService {
|
||||
|
||||
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.STATUS;
|
||||
|
||||
@Resource
|
||||
OMessageToAgentSender oMessageToAgentSender;
|
||||
|
||||
@Resource
|
||||
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService;
|
||||
|
||||
@Override
|
||||
public Map<String, Boolean> SyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime) {
|
||||
|
||||
// 构造最后的结果Map
|
||||
Map<String, Boolean> agentAliveStatusMap = agentTopicNameList
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
agentTopicName -> agentTopicName,
|
||||
agentTopicName -> Boolean.FALSE
|
||||
));
|
||||
|
||||
// 当前的时间
|
||||
LocalDateTime currentTime = TimeUtils.currentFormatTime();
|
||||
|
||||
// 构造OctopusMessage - StatusMessage结构体, 下发所有的消息
|
||||
buildAndSendAgentStatusOctopusMessage(
|
||||
agentTopicNameList,
|
||||
HEALTHY_STATUS_MESSAGE_TYPE,
|
||||
currentTime
|
||||
);
|
||||
|
||||
// 同步收集消息
|
||||
OctopusMessageSynScReplayContend statusSyncReplayContend = OctopusMessageSynScReplayContend.build(
|
||||
agentTopicNameList.size(),
|
||||
CurrentAppOctopusMessageType,
|
||||
currentTime
|
||||
);
|
||||
asyncWaitOctopusMessageResultService.waitFor(statusSyncReplayContend);
|
||||
|
||||
// 解析结果
|
||||
CountDownLatch countDownLatch = statusSyncReplayContend.getCountDownLatch();
|
||||
|
||||
// 等待状态返回的结果
|
||||
boolean agentAliveStatusCollectResult = false;
|
||||
try {
|
||||
agentAliveStatusCollectResult = countDownLatch.await(
|
||||
aliveStatusWaitMaxTime,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (!agentAliveStatusCollectResult) {
|
||||
log.debug("Agent存活状态检查,没有检查到全部的Agent!");
|
||||
}
|
||||
|
||||
// 移除等待队列
|
||||
asyncWaitOctopusMessageResultService.stopWaiting(statusSyncReplayContend);
|
||||
|
||||
// 处理结果
|
||||
statusSyncReplayContend
|
||||
.getReplayOMList()
|
||||
.stream()
|
||||
.forEach(
|
||||
statusOMessage -> {
|
||||
if (statusOMessage.getResult() != null) {
|
||||
agentAliveStatusMap.put(
|
||||
statusOMessage.getUuid(),
|
||||
Boolean.TRUE
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 返回Agent的存活状态内容
|
||||
return agentAliveStatusMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, AgentStatus> SyncCollectAgentMetricStatus(List<String> agentTopicNameList, int collectMetricWaitMaxTime) {
|
||||
|
||||
// 状态的结果Map
|
||||
HashMap<String, AgentStatus> metricMap = new HashMap<>();
|
||||
|
||||
// 当前的时间
|
||||
LocalDateTime currentTime = TimeUtils.currentFormatTime();
|
||||
|
||||
// 构造所有的Metric的OM并且下发
|
||||
buildAndSendAgentStatusOctopusMessage(
|
||||
agentTopicNameList,
|
||||
METRIC_STATUS_MESSAGE_TYPE,
|
||||
currentTime
|
||||
);
|
||||
|
||||
// 同步等待结果, 并且解析结果
|
||||
OctopusMessageSynScReplayContend metricSyncReplayContend = OctopusMessageSynScReplayContend.build(
|
||||
agentTopicNameList.size(),
|
||||
CurrentAppOctopusMessageType,
|
||||
currentTime
|
||||
);
|
||||
asyncWaitOctopusMessageResultService.waitFor(metricSyncReplayContend);
|
||||
|
||||
// 解析结果
|
||||
CountDownLatch countDownLatch = metricSyncReplayContend.getCountDownLatch();
|
||||
|
||||
// 等待状态返回的结果
|
||||
boolean agentAliveStatusCollectResult = false;
|
||||
try {
|
||||
agentAliveStatusCollectResult = countDownLatch.await(
|
||||
collectMetricWaitMaxTime,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("[Agent Metric] - 收集Agent的运行状态失败!");
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (!agentAliveStatusCollectResult) {
|
||||
log.debug("Agent存活状态检查,没有检查到全部的Agent!");
|
||||
}
|
||||
|
||||
// 移除等待队列
|
||||
asyncWaitOctopusMessageResultService.stopWaiting(metricSyncReplayContend);
|
||||
|
||||
// 处理结果
|
||||
metricSyncReplayContend
|
||||
.getReplayOMList()
|
||||
.stream()
|
||||
.forEach(
|
||||
statusOMessage -> {
|
||||
if (statusOMessage.getResult() != null) {
|
||||
|
||||
// 解析Result对象为 AgentStatus
|
||||
try {
|
||||
|
||||
AgentStatus agentStatus = OctopusObjectMapper.readValue(
|
||||
(String) statusOMessage.getResult(),
|
||||
AgentStatus.class
|
||||
);
|
||||
|
||||
// 保存结果
|
||||
metricMap.put(
|
||||
statusOMessage.getUuid(),
|
||||
agentStatus
|
||||
);
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("[Agent Metric] - 解析AgentStatus失败!");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return metricMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 2023年7月10日 通用的底层构造方法 Status类型的Octopus Message
|
||||
*
|
||||
* @param agentTopicNameList
|
||||
* @param statusType
|
||||
* @param currentTime
|
||||
*/
|
||||
private void buildAndSendAgentStatusOctopusMessage(List<String> agentTopicNameList, String statusType, LocalDateTime currentTime) {
|
||||
|
||||
List<OctopusMessage> octopusStatusMessageList = agentTopicNameList
|
||||
.stream()
|
||||
.map(
|
||||
agentTopicName -> ConstructAgentStatusMessage(
|
||||
statusType,
|
||||
agentTopicName,
|
||||
currentTime
|
||||
)
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 发送信息
|
||||
oMessageToAgentSender.send(octopusStatusMessageList);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user