[ Status ] add the async way to get agent status
This commit is contained in:
@@ -14,20 +14,18 @@ public class OctopusStatusMessage {
|
||||
// below two will be used by both server and agent
|
||||
// 存储所有Agent的实时健康状态, 1代表健康 0代表失败
|
||||
public static final String ALL_AGENT_STATUS_REDIS_KEY = "ALL_AGENT_HEALTHY_STATUS";
|
||||
public static final String HEALTHY_STATUS_MESSAGE_TYPE = "ping";
|
||||
public static final String ALL_STATUS_MESSAGE_TYPE = "all";
|
||||
public static final String METRIC_STATUS_MESSAGE_TYPE = "metric";
|
||||
public static final String APP_STATUS_MESSAGE_TYPE = "app";
|
||||
public static final String HEALTHY_STATUS_MESSAGE_TYPE = "PING";
|
||||
public static final String ALL_STATUS_MESSAGE_TYPE = "ALL";
|
||||
public static final String METRIC_STATUS_MESSAGE_TYPE = "METRIC";
|
||||
public static final String APP_STATUS_MESSAGE_TYPE = "APP";
|
||||
|
||||
/**
|
||||
* which kind of status should be return
|
||||
* which kind of status should be return
|
||||
* metric => short time message
|
||||
* all => all agent status message
|
||||
* healthy => check for healthy
|
||||
* */
|
||||
String type;
|
||||
|
||||
String agentTopicName;
|
||||
*/
|
||||
String statusType;
|
||||
|
||||
int metricRepeatCount;
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package io.wdd.rpc.status.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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 io.wdd.rpc.status.OctopusStatusMessage;
|
||||
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.HEALTHY_STATUS_MESSAGE_TYPE;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AsyncStatusServiceImpl implements AsyncStatusService {
|
||||
|
||||
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.STATUS;
|
||||
|
||||
@Resource
|
||||
OMessageToAgentSender oMessageToAgentSender;
|
||||
|
||||
@Resource
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
private OctopusMessage ConstructAgentStatusMessage(String statusType, String agentTopicName, LocalDateTime currentTime) {
|
||||
|
||||
OctopusStatusMessage statusMessage = OctopusStatusMessage
|
||||
.builder()
|
||||
.statusType(statusType)
|
||||
.build();
|
||||
|
||||
String ops;
|
||||
try {
|
||||
ops = objectMapper.writeValueAsString(statusMessage);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return OctopusMessage
|
||||
.builder()
|
||||
.type(CurrentAppOctopusMessageType)
|
||||
.uuid(agentTopicName)
|
||||
.init_time(currentTime)
|
||||
.content(ops)
|
||||
.build();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user