[ server ] [ execution ]- optimize some controller - 1

This commit is contained in:
zeaslity
2023-01-16 17:11:43 +08:00
parent 9332ca5533
commit 41396e024c
13 changed files with 523 additions and 200 deletions

View File

@@ -1,23 +1,18 @@
package io.wdd.rpc.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.beans.status.AgentHealthyStatusEnum;
import io.wdd.common.beans.status.OctopusStatusMessage;
import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.scheduler.service.BuildStatusScheduleTask;
import io.wdd.rpc.scheduler.service.OctopusQuartzService;
import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.coreService.CoreServerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Trigger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -26,6 +21,9 @@ import static io.wdd.common.beans.status.OctopusStatusMessage.HEALTHY_STATUS_MES
import static io.wdd.rpc.status.AgentRuntimeMetricStatus.ALL_HEALTHY_AGENT_TOPIC_NAMES;
/**
* 更新频率被类 BuildStatusScheduleTask.class控制
*
*
* 获取所有注册的Agent
* <p>
* 发送状态检查信息, agent需要update相应的HashMap的值
@@ -41,10 +39,25 @@ import static io.wdd.rpc.status.AgentRuntimeMetricStatus.ALL_HEALTHY_AGENT_TOPIC
public class MonitorAllAgentStatus {
private static final int MAX_WAIT_AGENT_REPORT_STATUS_TIME = 5;
/**
* 存储 状态对应Agent列表的Map
* Agent的状态描述为 AgentHealthyStatusEnum
* HEALTHY -> ["agentTopicName-1" "agentTopicName-2"]
* FAILED -> ["agentTopicName-1" "agentTopicName-2"]
*/
public static final Map<String, List<String>> HEALTHY_STATUS_AGENT_LIST_MAP = new HashMap<>();
private HashMap<String, String> AGENT_HEALTHY_INIT_MAP;
private List<String> ALL_AGENT_TOPICNAME_LIST;
/**
* 存储所有Agent状态的Map
*
* 内容为 agentTopicName-健康状态
*/
public static final Map<String, String> ALL_AGENT_HEALTHY_STATUS_MAP = new HashMap<>();
/**
* 存储所有的AgentTopicName的缓存
*/
public static final Set<String> ALL_AGENT_TOPIC_NAME_LIST = new HashSet<>();
@Resource
RedisTemplate redisTemplate;
@Resource
@@ -53,6 +66,7 @@ public class MonitorAllAgentStatus {
CoreServerService coreServerService;
@Resource
BuildStatusScheduleTask buildStatusScheduleTask;
private HashMap<String, String> AGENT_HEALTHY_INIT_MAP;
public void go() {
@@ -61,8 +75,14 @@ public class MonitorAllAgentStatus {
// 1. 获取所有注册的Agent
// todo need to cache this
List<ServerInfoVO> allAgentInfo = coreServerService.serverGetAll();
Assert.notEmpty(allAgentInfo,"not agent registered ! skip the agent healthy status check !");
ALL_AGENT_TOPICNAME_LIST = allAgentInfo.stream().map(ServerInfoVO::getTopicName).collect(Collectors.toList());
Assert.notEmpty(allAgentInfo,
"not agent registered ! skip the agent healthy status check !");
Set<String> collect = allAgentInfo.stream()
.map(ServerInfoVO::getTopicName)
.collect(Collectors.toSet());
ALL_AGENT_TOPIC_NAME_LIST.clear();
ALL_AGENT_TOPIC_NAME_LIST.addAll(collect);
// 1.1 检查 Agent状态保存数据结构是否正常
checkOrCreateRedisHealthyKey();
@@ -90,56 +110,102 @@ public class MonitorAllAgentStatus {
// build the redis all agent healthy map struct
HashMap<String, String> initMap = new HashMap<>(32);
ALL_AGENT_TOPICNAME_LIST.stream().forEach(
agentTopicName -> {
initMap.put(agentTopicName, "0");
}
);
initMap.put("updateTime", TimeUtils.currentTimeString());
ALL_AGENT_TOPICNAME_LIST.stream()
.forEach(
agentTopicName -> {
initMap.put(agentTopicName,
"0");
}
);
initMap.put("updateTime",
TimeUtils.currentTimeString());
// cache this map struct
AGENT_HEALTHY_INIT_MAP = initMap;
redisTemplate.opsForHash().putAll(ALL_AGENT_STATUS_REDIS_KEY, initMap);
redisTemplate.opsForHash()
.putAll(ALL_AGENT_STATUS_REDIS_KEY,
initMap);
}
}
private void buildAndSendAgentHealthMessage() {
List<OctopusStatusMessage> collect = ALL_AGENT_TOPICNAME_LIST.stream().map(
agentTopicName -> OctopusStatusMessage.builder()
.agentTopicName(agentTopicName)
.type(HEALTHY_STATUS_MESSAGE_TYPE)
.build()
).collect(Collectors.toList());
List<OctopusStatusMessage> collect = ALL_AGENT_TOPICNAME_LIST
.stream()
.map(
agentTopicName -> OctopusStatusMessage
.builder()
.agentTopicName(agentTopicName)
.type(HEALTHY_STATUS_MESSAGE_TYPE)
.build()
)
.collect(Collectors.toList());
collectAgentStatus.statusMessageToAgent(collect);
}
private void updateAllAgentHealthyStatus() {
List statusList = redisTemplate.opsForHash().multiGet(
ALL_AGENT_STATUS_REDIS_KEY,
ALL_AGENT_TOPICNAME_LIST);
List statusList = redisTemplate
.opsForHash()
.multiGet(
ALL_AGENT_STATUS_REDIS_KEY,
ALL_AGENT_TOPICNAME_LIST);
// current log to console is ok
HashMap<String, String> tmp = new HashMap<>(32);
// agent-topic-name : STATUS(healthy, failed, unknown)
HashMap<String, String> agentStatusMap = new HashMap<>(32);
for (int i = 0; i < ALL_AGENT_TOPICNAME_LIST.size(); i++) {
tmp.put(
agentStatusMap.put(
ALL_AGENT_TOPICNAME_LIST.get(i),
uniformHealthyStatus(String.valueOf(statusList.get(i)))
);
}
String currentTimeString = TimeUtils.currentTimeString();
log.info("[ AGENT HEALTHY CHECK ] time is {} , result are => {}", currentTimeString, tmp);
log.info("[ AGENT HEALTHY CHECK ] time is {} , result are => {}",
currentTimeString,
agentStatusMap);
// 2023-01-16
ALL_AGENT_HEALTHY_STATUS_MAP.clear();
ALL_AGENT_HEALTHY_STATUS_MAP.putAll(agentStatusMap);
// 2023-01-16
Map<String, List<String>> statusAgentListMap = agentStatusMap
.entrySet()
.stream()
.collect(
Collectors.groupingBy(
Map.Entry::getValue
)
)
.entrySet()
.stream()
.collect(
Collectors.toMap(
entry -> entry.getKey(),
entry -> entry
.getValue()
.stream()
.map(
Map.Entry::getKey
)
.collect(Collectors.toList())
)
);
HEALTHY_STATUS_AGENT_LIST_MAP.putAll(statusAgentListMap);
log.debug("Agent存活状态 状态-Agent名称-Map 已经更新了");
// help gc
tmp = null;
agentStatusMap = null;
// Trigger调用Agent Metric 任务
ArrayList<String> allHealthyAgentTopicNames = new ArrayList<>(32);
for (int i = 0; i < statusList.size(); i++) {
if (statusList.get(i).equals("1")) {
if (statusList.get(i)
.equals("1")) {
allHealthyAgentTopicNames.add(ALL_AGENT_TOPICNAME_LIST.get(i));
}
}
@@ -148,19 +214,22 @@ public class MonitorAllAgentStatus {
buildStatusScheduleTask.buildAgentMetricScheduleTask();
// update time
AGENT_HEALTHY_INIT_MAP.put("updateTime", currentTimeString);
AGENT_HEALTHY_INIT_MAP.put("updateTime",
currentTimeString);
// init the healthy map
redisTemplate.opsForHash().putAll(ALL_AGENT_STATUS_REDIS_KEY, AGENT_HEALTHY_INIT_MAP);
redisTemplate.opsForHash()
.putAll(ALL_AGENT_STATUS_REDIS_KEY,
AGENT_HEALTHY_INIT_MAP);
}
private String uniformHealthyStatus(String agentStatus) {
switch (agentStatus) {
case "0":
return "FAILED";
return AgentHealthyStatusEnum.FAILED.getStatus();
case "1":
return "HEALTHY";
return AgentHealthyStatusEnum.HEALTHY.getStatus();
default:
return "UNKNOWN";
return AgentHealthyStatusEnum.UNKNOWN.getStatus();
}
}