[ Service ] [ Executor ] 重构Executor部分

This commit is contained in:
zeaslity
2023-08-11 17:06:53 +08:00
parent 0a78f9a02b
commit a2b6b01fd3
11 changed files with 812 additions and 1061 deletions

View File

@@ -1,6 +1,7 @@
package io.wdd.common.config; package io.wdd.common.config;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
@@ -32,6 +33,39 @@ public class OctopusObjectMapperConfig {
public static ObjectMapper OctopusObjectMapper = null; public static ObjectMapper OctopusObjectMapper = null;
public static String WriteToString(Object data) {
try {
return OctopusObjectMapper.writeValueAsString(data);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static String WriteToStringPretty(Object data) {
try {
return OctopusObjectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(data);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static byte[] WriteToBytes(Object data) {
try {
return OctopusObjectMapper.writeValueAsBytes(data);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@PostConstruct @PostConstruct
public void setOctopusObjectMapper() { public void setOctopusObjectMapper() {

View File

@@ -1,420 +1,419 @@
package io.wdd.rpc.agent; //package io.wdd.rpc.agent;
//
import com.fasterxml.jackson.core.JsonProcessingException; //import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; //import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; //import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.common.utils.TimeUtils; //import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.message.OctopusMessage; //import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType; //import io.wdd.rpc.message.OctopusMessageType;
import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService; //import io.wdd.rpc.message.handler.OMessageReplayContent;
import io.wdd.rpc.message.handler.async.OctopusMessageSyncReplayContend; //import io.wdd.rpc.message.sender.OMessageToAgentSender;
import io.wdd.rpc.message.sender.OMessageToAgentSender; //import io.wdd.server.beans.vo.ServerInfoVO;
import io.wdd.server.beans.vo.ServerInfoVO; //import io.wdd.server.config.ServerCommonPool;
import io.wdd.server.config.ServerCommonPool; //import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.Slf4j; //import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.RedisTemplate; //
//import javax.annotation.Resource;
import javax.annotation.Resource; //import java.time.LocalDateTime;
import java.time.LocalDateTime; //import java.util.HashMap;
import java.util.HashMap; //import java.util.List;
import java.util.List; //import java.util.Map;
import java.util.Map; //import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletableFuture; //import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CountDownLatch; //import java.util.concurrent.ExecutorService;
import java.util.concurrent.ExecutorService; //import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit; //import java.util.stream.Collectors;
import java.util.stream.Collectors; //
//import static io.wdd.rpc.message.handler.OMessageToServerListener.LATEST_VERSION;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.LATEST_VERSION; //import static io.wdd.rpc.message.handler.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT; //import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET;
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET; //import static io.wdd.rpc.status.CommonAndStatusCache.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST;
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_HEALTHY_AGENT_TOPIC_NAME_LIST; //
////@Service
//@Service //@Slf4j
@Slf4j //public class OctopusAgentServiceImpl implements OctopusAgentService {
public class OctopusAgentServiceImpl implements OctopusAgentService { //
// private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.AGENT;
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.AGENT; // @Resource
@Resource // OMessageToAgentSender oMessageToAgentSender;
OMessageToAgentSender oMessageToAgentSender; // @Resource
@Resource // ObjectMapper objectMapper;
ObjectMapper objectMapper; //
// @Resource
@Resource // RedisTemplate redisTemplate;
RedisTemplate redisTemplate; //
// @Resource
@Resource // AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService;
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService; //
// @Override
@Override // public Map<String, String> getAllAgentVersion() {
public Map<String, String> getAllAgentVersion() { // HashMap<String, String> result = new HashMap<>();
HashMap<String, String> result = new HashMap<>(); //
// // 查询获取到最新的Agent版本信息
// 查询获取到最新的Agent版本信息 // result.put(
result.put( // "LATEST_VERSION",
"LATEST_VERSION", // getRealAgentLatestVersion()
getRealAgentLatestVersion() // );
); //
// // 获取Agent的版本信息 -- 超时时间
// 获取Agent的版本信息 -- 超时时间 //
// // 从OctopusToServer中收集到所有Agent的版本信息
// 从OctopusToServer中收集到所有Agent的版本信息 // // 组装信息至集合中
// 组装信息至集合中 // LocalDateTime currentTime = TimeUtils.currentFormatTime();
LocalDateTime currentTime = TimeUtils.currentFormatTime(); //
// // 发送OctopusMessage-Agent
// 发送OctopusMessage-Agent // buildOMessageAndSendToAllHealthyAgent(
buildOMessageAndSendToAllHealthyAgent( // AgentOperationType.VERSION,
AgentOperationType.VERSION, // currentTime
currentTime // );
); //
// // 构造 异步结果监听内容
// 构造 异步结果监听内容 // OMessageReplayContent agentReplayContend = OMessageReplayContent.build(
OctopusMessageSyncReplayContend agentReplayContend = OctopusMessageSyncReplayContend.build( // ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.size(),
ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.size(), // CurrentAppOctopusMessageType,
CurrentAppOctopusMessageType, // currentTime
currentTime // );
); //
// CountDownLatch countDownLatch = agentReplayContend.getCountDownLatch();
CountDownLatch countDownLatch = agentReplayContend.getCountDownLatch(); //
// // 调用后台接收处理所有的Replay信息
// 调用后台接收处理所有的Replay信息 // asyncWaitOctopusMessageResultService.waitFor(agentReplayContend);
asyncWaitOctopusMessageResultService.waitFor(agentReplayContend); //
// boolean isAllHealthyAgentReport = false;
boolean isAllHealthyAgentReport = false; // try {
try { // // 超时等待5秒钟, 或者所有的Agent均已经完成上报
// 超时等待5秒钟, 或者所有的Agent均已经完成上报 // isAllHealthyAgentReport = countDownLatch.await(
isAllHealthyAgentReport = countDownLatch.await( // 5,
5, // TimeUnit.SECONDS
TimeUnit.SECONDS // );
); // } catch (InterruptedException e) {
} catch (InterruptedException e) { //
// } finally {
} finally { // // 超时,或者 全部信息已经收集
// 超时,或者 全部信息已经收集 // if (!isAllHealthyAgentReport) {
if (!isAllHealthyAgentReport) { // log.warn("存在部分Agent没有上报 版本信息!");
log.warn("存在部分Agent没有上报 版本信息!"); // }
} //
// // 此处调用,即可中断 异步任务的收集工作
// 此处调用,即可中断 异步任务的收集工作 // asyncWaitOctopusMessageResultService.stopWaiting(agentReplayContend);
asyncWaitOctopusMessageResultService.stopWaiting(agentReplayContend); //
// // 处理结果
// 处理结果 // agentReplayContend
agentReplayContend // .getReplayOMList()
.getReplayOMList() // .stream()
.stream() // .forEach(
.forEach( // mMessage -> {
mMessage -> { // result.put(
result.put( // mMessage.getUuid(),
mMessage.getUuid(), // (String) mMessage.getResult()
(String) mMessage.getResult() // );
); // }
} // );
); //
// // help gc
// help gc // agentReplayContend = null;
agentReplayContend = null; // }
} //
// return result;
return result; // }
} //
// private String getRealAgentLatestVersion() {
private String getRealAgentLatestVersion() { //
// String latestVersion = (String) redisTemplate
String latestVersion = (String) redisTemplate // .opsForValue()
.opsForValue() // .get(
.get( // LATEST_VERSION
LATEST_VERSION // );
); //
// return latestVersion;
return latestVersion; // }
} //
// @Override
@Override // public Map<String, ServerInfoVO> getAllAgentCoreInfo() {
public Map<String, ServerInfoVO> getAllAgentCoreInfo() { //
// HashMap<String, ServerInfoVO> result = new HashMap<>();
HashMap<String, ServerInfoVO> result = new HashMap<>(); //
// // 获取Agent的版本信息 -- 超时时间
// 获取Agent的版本信息 -- 超时时间 // // 从OctopusToServer中收集到所有Agent的版本信息
// 从OctopusToServer中收集到所有Agent的版本信息 // // 组装信息至集合中
// 组装信息至集合中 // LocalDateTime currentTime = TimeUtils.currentFormatTime();
LocalDateTime currentTime = TimeUtils.currentFormatTime(); //
// // 发送OctopusMessage-Agent
// 发送OctopusMessage-Agent // buildOMessageAndSendToAllHealthyAgent(
buildOMessageAndSendToAllHealthyAgent( // AgentOperationType.INFO,
AgentOperationType.INFO, // currentTime
currentTime // );
); //
// // 构造结果
// 构造结果 // OMessageReplayContent oMessageReplayContent = OMessageReplayContent.build(
OctopusMessageSyncReplayContend octopusMessageSyncReplayContend = OctopusMessageSyncReplayContend.build( // ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.size(),
ALL_HEALTHY_AGENT_TOPIC_NAME_LIST.size(), // CurrentAppOctopusMessageType,
CurrentAppOctopusMessageType, // currentTime
currentTime // );
); //
// CountDownLatch countDownLatch = oMessageReplayContent.getCountDownLatch();
CountDownLatch countDownLatch = octopusMessageSyncReplayContend.getCountDownLatch(); //
// // 调用后台接收处理所有的Replay信息
// 调用后台接收处理所有的Replay信息 // asyncWaitOctopusMessageResultService.waitFor(oMessageReplayContent);
asyncWaitOctopusMessageResultService.waitFor(octopusMessageSyncReplayContend); //
// /* CompletableFuture<Void> getAllAgentCoreInfoFuture = waitCollectAllAgentCoreInfo(
/* CompletableFuture<Void> getAllAgentCoreInfoFuture = waitCollectAllAgentCoreInfo( // result,
result, // currentTime,
currentTime, // countDownLatch
countDownLatch // );
); //*/
*/ // try {
try { // // 超时等待5秒钟, 或者所有的Agent均已经完成上报ddo
// 超时等待5秒钟, 或者所有的Agent均已经完成上报ddo // countDownLatch.await(
countDownLatch.await( // 5,
5, // TimeUnit.SECONDS
TimeUnit.SECONDS // );
); // } catch (InterruptedException e) {
} catch (InterruptedException e) { // log.warn("存在部分Agent没有上报 核心信息!");
log.warn("存在部分Agent没有上报 核心信息!"); // } finally {
} finally { // // 超时,或者 全部信息已经收集
// 超时,或者 全部信息已经收集 //
// // 此处调用,即可中断 异步任务的收集工作
// 此处调用,即可中断 异步任务的收集工作 // asyncWaitOctopusMessageResultService.stopWaiting(oMessageReplayContent);
asyncWaitOctopusMessageResultService.stopWaiting(octopusMessageSyncReplayContend); //
// // 处理结果
// 处理结果 // oMessageReplayContent
octopusMessageSyncReplayContend // .getReplayOMList()
.getReplayOMList() // .stream()
.stream() // .forEach(
.forEach( // mMessage -> {
mMessage -> { //
// try {
try { //
// // 解析当前信息
// 解析当前信息 // ServerInfoVO serverInfoVO = objectMapper.readValue(
ServerInfoVO serverInfoVO = objectMapper.readValue( // (String) mMessage.getResult(),
(String) mMessage.getResult(), // new TypeReference<ServerInfoVO>() {
new TypeReference<ServerInfoVO>() { // }
} // );
); //
// result.put(
result.put( // mMessage.getUuid(),
mMessage.getUuid(), // serverInfoVO
serverInfoVO // );
); //
// } catch (JsonProcessingException e) {
} catch (JsonProcessingException e) { // throw new RuntimeException(e);
throw new RuntimeException(e); // }
} //
// }
} // );
); //
// // help gc
// help gc // oMessageReplayContent = null;
octopusMessageSyncReplayContend = null; // }
} //
// return result;
return result; // }
} //
//
// @Override
@Override // public String shutdownAgentDanger(String agentTopicName) {
public String shutdownAgentDanger(String agentTopicName) { //
// if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) {
if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) { // log.error("agentTopicName Error !");
log.error("agentTopicName Error !"); // return "agentTopicName Error!";
return "agentTopicName Error!"; // }
} //
// LocalDateTime formatTime = TimeUtils.currentFormatTime();
LocalDateTime formatTime = TimeUtils.currentFormatTime(); // buildOMessageAndSendToAllHealthyAgent(
buildOMessageAndSendToAllHealthyAgent( // AgentOperationType.SHUTDOWN,
AgentOperationType.SHUTDOWN, // formatTime
formatTime // );
); //
// // 是否需要检查相应的状态
// 是否需要检查相应的状态 //
//
// return null;
return null; // }
} //
// private CompletableFuture<Void> waitCollectAllAgentVersionInfo(HashMap<String, String> result, LocalDateTime startTime, CountDownLatch countDownLatch) {
private CompletableFuture<Void> waitCollectAllAgentVersionInfo(HashMap<String, String> result, LocalDateTime startTime, CountDownLatch countDownLatch) { //
// ExecutorService pool = ServerCommonPool.pool;
ExecutorService pool = ServerCommonPool.pool; //
// // 从OCTOPUS_MESSAGE_FROM_AGENT中获取符合条件的信息
// 从OCTOPUS_MESSAGE_FROM_AGENT中获取符合条件的信息 // return CompletableFuture.runAsync(
return CompletableFuture.runAsync( // () -> {
() -> { // while (true) {
while (true) { // if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) {
if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) { // // 开始收集等待 200ms
// 开始收集等待 200ms // try {
try { // TimeUnit.MILLISECONDS.sleep(50);
TimeUnit.MILLISECONDS.sleep(50); // } catch (InterruptedException e) {
} catch (InterruptedException e) { // throw new RuntimeException(e);
throw new RuntimeException(e); // }
} // // 返回,继续死循环
// 返回,继续死循环 // continue;
continue; // }
} //
// OctopusMessage message = OCTOPUS_MESSAGE_FROM_AGENT.poll();
OctopusMessage message = OCTOPUS_MESSAGE_FROM_AGENT.poll(); //
// // 获取到OM
// 获取到OM // // 判断信息是否是需要的类型
// 判断信息是否是需要的类型 // // 根据 init_time + OctopusMessageType + AgentOperationMessage
// 根据 init_time + OctopusMessageType + AgentOperationMessage // if (!judgyIsCurrentServerReplyMessage(
if (!judgyIsCurrentServerReplyMessage( // message,
message, // startTime
startTime // )) {
)) { //
// // 不是当前应用需要的的OM将信息放置与Cache队列的末尾
// 不是当前应用需要的的OM将信息放置与Cache队列的末尾 //
// OCTOPUS_MESSAGE_FROM_AGENT.add(message);
OCTOPUS_MESSAGE_FROM_AGENT.add(message); //
// // 返回,继续死循环
// 返回,继续死循环 // continue;
continue; // }
} //
// // 是当前需要的消息信息
// 是当前需要的消息信息 // result.put(
result.put( // message.getUuid(),
message.getUuid(), // (String) message.getResult()
(String) message.getResult() // );
); // countDownLatch.countDown();
countDownLatch.countDown(); // }
} // },
}, // pool
pool // );
); //
//
// }
} //
// private CompletableFuture<Void> waitCollectAllAgentCoreInfo(HashMap<String, ServerInfoVO> result, LocalDateTime startTime, CountDownLatch countDownLatch) {
private CompletableFuture<Void> waitCollectAllAgentCoreInfo(HashMap<String, ServerInfoVO> result, LocalDateTime startTime, CountDownLatch countDownLatch) { //
// ExecutorService pool = ServerCommonPool.pool;
ExecutorService pool = ServerCommonPool.pool; //
// // 从OCTOPUS_MESSAGE_FROM_AGENT中获取符合条件的信息
// 从OCTOPUS_MESSAGE_FROM_AGENT中获取符合条件的信息 // return CompletableFuture.runAsync(
return CompletableFuture.runAsync( // () -> {
() -> { // while (true) {
while (true) { // if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) {
if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) { // // 开始收集等待 200ms
// 开始收集等待 200ms // try {
try { // TimeUnit.MILLISECONDS.sleep(50);
TimeUnit.MILLISECONDS.sleep(50); // } catch (InterruptedException e) {
} catch (InterruptedException e) { // throw new RuntimeException(e);
throw new RuntimeException(e); // }
} // // 返回,继续死循环
// 返回,继续死循环 // continue;
continue; // }
} //
// OctopusMessage message = OCTOPUS_MESSAGE_FROM_AGENT.poll();
OctopusMessage message = OCTOPUS_MESSAGE_FROM_AGENT.poll(); //
// // 获取到OM
// 获取到OM // // 判断信息是否是需要的类型
// 判断信息是否是需要的类型 // // 根据 init_time + OctopusMessageType + AgentOperationMessage
// 根据 init_time + OctopusMessageType + AgentOperationMessage // if (!judgyIsCurrentServerReplyMessage(
if (!judgyIsCurrentServerReplyMessage( // message,
message, // startTime
startTime // )) {
)) { //
// // 不是当前应用需要的的OM将信息放置与Cache队列的末尾
// 不是当前应用需要的的OM将信息放置与Cache队列的末尾 //
// OCTOPUS_MESSAGE_FROM_AGENT.offer(message);
OCTOPUS_MESSAGE_FROM_AGENT.offer(message); //
// // 返回,继续死循环
// 返回,继续死循环 // continue;
continue; // }
} //
// // 是当前需要的消息信息
// 是当前需要的消息信息 // try {
try { //
// // 解析当前信息
// 解析当前信息 // ServerInfoVO serverInfoVO = objectMapper.readValue(
ServerInfoVO serverInfoVO = objectMapper.readValue( // (String) message.getResult(),
(String) message.getResult(), // new TypeReference<ServerInfoVO>() {
new TypeReference<ServerInfoVO>() { // }
} // );
); //
// result.put(
result.put( // message.getUuid(),
message.getUuid(), // serverInfoVO
serverInfoVO // );
); //
// } catch (JsonProcessingException e) {
} catch (JsonProcessingException e) { // throw new RuntimeException(e);
throw new RuntimeException(e); // }
} //
// countDownLatch.countDown();
countDownLatch.countDown(); // }
} // },
}, // pool
pool // );
); //
// }
} //
// /**
/** // * 判断信息是否是需要的类型
* 判断信息是否是需要的类型 // * 根据 init_time + OctopusMessageType + AgentOperationMessage
* 根据 init_time + OctopusMessageType + AgentOperationMessage // *
* // * @return
* @return // */
*/ // private boolean judgyIsCurrentServerReplyMessage(OctopusMessage message, LocalDateTime startTime) {
private boolean judgyIsCurrentServerReplyMessage(OctopusMessage message, LocalDateTime startTime) { //
// // init_time 时间判断
// init_time 时间判断 // boolean startTimeEqual = startTime.isEqual(message.getInit_time());
boolean startTimeEqual = startTime.isEqual(message.getInit_time()); //
// // OctopusMessageType判断
// OctopusMessageType判断 // boolean OMTypeEqual = message
boolean OMTypeEqual = message // .getOctopusMessageType()
.getOctopusMessageType() // .equals(CurrentAppOctopusMessageType);
.equals(CurrentAppOctopusMessageType); //
// return startTimeEqual && OMTypeEqual;
return startTimeEqual && OMTypeEqual; // }
} //
// private void buildOMessageAndSendToAllHealthyAgent(AgentOperationType operationType, LocalDateTime currentTime) {
private void buildOMessageAndSendToAllHealthyAgent(AgentOperationType operationType, LocalDateTime currentTime) { //
// List<OctopusMessage> octopusMessageList = ALL_HEALTHY_AGENT_TOPIC_NAME_LIST
List<OctopusMessage> octopusMessageList = ALL_HEALTHY_AGENT_TOPIC_NAME_LIST // .stream()
.stream() // .map(
.map( // agentTopicName ->
agentTopicName -> // ConstructAgentOperationMessage(
ConstructAgentOperationMessage( // agentTopicName,
agentTopicName, // operationType,
operationType, // currentTime
currentTime // )
) // )
) // .collect(Collectors.toList());
.collect(Collectors.toList()); //
// // 发送相应的消息
// 发送相应的消息 // oMessageToAgentSender.send(octopusMessageList);
oMessageToAgentSender.send(octopusMessageList); // }
} //
// /**
/** // * 专门构造 Agent 类型的 OctopusMessage
* 专门构造 Agent 类型的 OctopusMessage // * 通常只能在此类中使用
* 通常只能在此类中使用 // *
* // * @param agentTopicName
* @param agentTopicName // * @param operationType
* @param operationType // * @param currentTime
* @param currentTime // * @return
* @return // */
*/ // private OctopusMessage ConstructAgentOperationMessage(String agentTopicName, AgentOperationType operationType, LocalDateTime currentTime) {
private OctopusMessage ConstructAgentOperationMessage(String agentTopicName, AgentOperationType operationType, LocalDateTime currentTime) { //
// AgentOperationMessage operationMessage = AgentOperationMessage
AgentOperationMessage operationMessage = AgentOperationMessage // .builder()
.builder() // .opType(operationType)
.opType(operationType) // .build();
.build(); //
// String ops;
String ops; // try {
try { // ops = objectMapper.writeValueAsString(operationMessage);
ops = objectMapper.writeValueAsString(operationMessage); // } catch (JsonProcessingException e) {
} catch (JsonProcessingException e) { // throw new RuntimeException(e);
throw new RuntimeException(e); // }
} //
// return OctopusMessage
return OctopusMessage // .builder()
.builder() // .octopusMessageType(CurrentAppOctopusMessageType)
.octopusMessageType(CurrentAppOctopusMessageType) // .uuid(agentTopicName)
.uuid(agentTopicName) // .init_time(currentTime)
.init_time(currentTime) // .content(ops)
.content(ops) // .build();
.build(); //
// }
} //}
}

View File

@@ -3,6 +3,7 @@ package io.wdd.rpc.execute;
import io.wdd.common.utils.TimeUtils; import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.message.OctopusMessage; import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType; import io.wdd.rpc.message.OctopusMessageType;
import io.wdd.rpc.message.handler.OMessageReplayContent;
import io.wdd.rpc.message.sender.OMessageToAgentSender; import io.wdd.rpc.message.sender.OMessageToAgentSender;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -11,9 +12,10 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.StopWaitingResult; import static io.wdd.rpc.message.handler.OMessageHandler.*;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.WaitFromAgent;
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET; import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET;
@Service @Service
@@ -53,41 +55,53 @@ public class ExecutionServiceImpl implements ExecutionService {
// send the message // send the message
oMessageToAgentSender.send(octopusMessage); oMessageToAgentSender.send(octopusMessage);
System.out.println("originOctopusMessage = " + octopusMessage.hashCode()); log.debug(
"发送的 matchKey为 {}, 内容为 {}",
GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(),
octopusMessage.getInit_time()
),
octopusMessage
);
// 需要返回结果 // 需要返回结果
if (!durationTask) { if (!durationTask) {
// 等待结果 // 等待结果
WaitFromAgent(octopusMessage); OMessageReplayContent replayContent = WaitFromAgent(
octopusMessage,
1
);
synchronized (octopusMessage) { CountDownLatch replayLatch = replayContent.getCountDownLatch();
boolean waitOK = false;
try { try {
octopusMessage.wait(10000); waitOK = replayLatch.await(
10,
TimeUnit.SECONDS
log.debug("等待结束!"); );
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} } finally {
// 释放等待队列
StopWaitingResult(octopusMessage);
}
// 转换结果 // 转换结果
commandResultLog = (ArrayList<String>) octopusMessage.getResult(); commandResultLog = (ArrayList<String>) octopusMessage.getResult();
// debug // debug
log.debug( log.debug(
"执行命令 {} 结果为 {} 内容为 {}", "执行命令 {} {} 在规定时间内结束, 结果为 {} 返回内容为 {}",
executionMessage.getSingleLineCommand() == null ? executionMessage.getMultiLineCommand() : executionMessage.getSingleLineCommand(), executionMessage.getSingleLineCommand() == null ? executionMessage.getMultiLineCommand() : executionMessage.getSingleLineCommand(),
waitOK ? "已经" : "",
octopusMessage.getResultCode(), octopusMessage.getResultCode(),
octopusMessage.getResult() octopusMessage.getResult()
); );
} }
// 释放等待队列 }
StopWaitingResult(octopusMessage);
return commandResultLog; return commandResultLog;
} }
@@ -98,7 +112,7 @@ public class ExecutionServiceImpl implements ExecutionService {
return OctopusMessage return OctopusMessage
.builder() .builder()
.octopusMessageType(OctopusMessageType.EXECUTOR) .octopusMessageType(OctopusMessageType.EXECUTOR)
.init_time(TimeUtils.currentTime()) .init_time(TimeUtils.currentFormatTime())
.uuid(agentTopicName) .uuid(agentTopicName)
.content( .content(
executionMessage executionMessage

View File

@@ -1,47 +0,0 @@
package io.wdd.rpc.execute.service;
import java.util.ArrayList;
import java.util.List;
/**
* 同步命令执行的核心类
* 需要等待命令执行完毕,完后返回相应的结果
*/
public interface SyncExecutionService {
List<ArrayList<String>> SyncSendCommandToAgentComplete(
List<String> agentTopicNameList,
String type,
List<String> funcContent,
List<String> commandList,
List<List<String>> commandListComplete,
boolean needResultReplay,
String futureKey,
boolean durationTask
);
/**
* 调用 完整脚本的 最底层函数
*
* @param agentTopicName
* @param type
* @param funcContent
* @param commandList
* @param commandListComplete
* @param futureKey
* @param durationTask
* @return resultKey 本次操作在Redis中记录的结果Key
*/
ArrayList<String> SyncSendCommandToAgentComplete(
String agentTopicName,
String type,
List<String> funcContent,
List<String> commandList,
List<List<String>> commandListComplete,
boolean needResultReplay,
String futureKey,
boolean durationTask
);
}

View File

@@ -1,135 +0,0 @@
package io.wdd.rpc.execute.service;
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.OctopusMessageSyncReplayContend;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
//@Service
@Slf4j
public class SyncExecutionServiceImpl implements SyncExecutionService {
private static final boolean COMMAND_EXEC_NEED_REPLAY = true;
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.EXECUTOR;
@Resource
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService;
@Resource
AsyncExecutionService asyncExecutionService;
/**
* 一个命令执行的最长等待时间
*/
int processMaxWaitSeconds = 10;
@Override
public List<ArrayList<String>> SyncSendCommandToAgentComplete(List<String> agentTopicNameList, String type, List<String> funcContent, List<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, String futureKey, boolean durationTask) {
return agentTopicNameList
.stream()
.map(
agentTopicName -> {
return this.SyncSendCommandToAgentComplete(
agentTopicName,
type,
null,
commandList,
commandListComplete,
needResultReplay,
futureKey,
durationTask
);
}
)
.collect(Collectors.toList());
}
@Override
public ArrayList<String> SyncSendCommandToAgentComplete(String agentTopicName, String type, List<String> funcContent, List<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, String futureKey, boolean durationTask) {
// 异步访问
OctopusMessage octopusMessage = asyncExecutionService.AsyncCallSendCommandToAgent(
agentTopicName,
type,
null,
commandList,
commandListComplete,
needResultReplay,
futureKey,
durationTask
);
LocalDateTime initTime = octopusMessage.getInit_time();
// OM 中的result保存
ArrayList<String> result = new ArrayList<>();
// 构造消息等待对象
int commandCount = 1;
if (null != commandListComplete) {
commandCount = Math.max(
commandListComplete.size(),
1
);
}
// 构造回复信息的内容
OctopusMessageSyncReplayContend executionReplayContent = OctopusMessageSyncReplayContend.build(
commandCount,
CurrentAppOctopusMessageType,
initTime
);
CountDownLatch countDownLatch = executionReplayContent.getCountDownLatch();
// 开始等待结果
asyncWaitOctopusMessageResultService.waitFor(executionReplayContent);
// 监听结果
try {
boolean await = countDownLatch.await(
processMaxWaitSeconds,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
// 等待所有的结果返回
// 停止等待结果
asyncWaitOctopusMessageResultService.stopWaiting(executionReplayContent);
// 解析结果
executionReplayContent
.getReplayOMList()
.stream()
.map(
om -> {
log.debug(
"replay message is => {}",
om
);
return (ArrayList<String>) om.getResult();
}
)
.forEachOrdered(
singleResult -> result.addAll(singleResult)
);
}
// 返回 执行的结果
return result;
}
}

View File

@@ -1,4 +1,4 @@
package io.wdd.rpc.message.handler.sync; package io.wdd.rpc.message.handler;
import io.wdd.rpc.message.OctopusMessage; import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType; import io.wdd.rpc.message.OctopusMessageType;
@@ -11,8 +11,8 @@ import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.FROM_AGENT_MATCH_TO_AGENT_MAP; import static io.wdd.rpc.message.handler.OMessageToServerListener.FROM_AGENT_MATCH_TO_AGENT_MAP;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT; import static io.wdd.rpc.message.handler.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT;
@Service @Service
@Slf4j(topic = "Octopus Message Handler") @Slf4j(topic = "Octopus Message Handler")
@@ -44,18 +44,24 @@ public class OMessageHandler {
} }
public static void WaitFromAgent(OctopusMessage octopusMessage) { public static OMessageReplayContent WaitFromAgent(OctopusMessage originOMessage, int waitFroReplayNum) {
// 构建 MatchKey // 构建 MatchKey
String matchKey = GenerateOMessageMatchKey( String matchKey = GenerateOMessageMatchKey(
octopusMessage.getOctopusMessageType(), originOMessage.getOctopusMessageType(),
octopusMessage.getInit_time() originOMessage.getInit_time()
);
// 构造等待对象
OMessageReplayContent replayContent = OMessageReplayContent.build(
originOMessage,
waitFroReplayNum
); );
// 开始等待 // 开始等待
FROM_AGENT_MATCH_TO_AGENT_MAP.put( FROM_AGENT_MATCH_TO_AGENT_MAP.put(
matchKey, matchKey,
octopusMessage replayContent
); );
//debug //debug
@@ -63,6 +69,9 @@ public class OMessageHandler {
"wait from agent map is => {}", "wait from agent map is => {}",
FROM_AGENT_MATCH_TO_AGENT_MAP FROM_AGENT_MATCH_TO_AGENT_MAP
); );
return replayContent;
} }
public static void StopWaitingResult(OctopusMessage octopusMessage) { public static void StopWaitingResult(OctopusMessage octopusMessage) {
@@ -108,10 +117,16 @@ public class OMessageHandler {
replayOMessage.getInit_time() replayOMessage.getInit_time()
); );
log.info(
"接收到的 matchKey为 {}, 内容为 {}",
matchKey,
replayOMessage
);
if (!FROM_AGENT_MATCH_TO_AGENT_MAP.containsKey(matchKey)) { if (!FROM_AGENT_MATCH_TO_AGENT_MAP.containsKey(matchKey)) {
// 没有这个Key,说明等待结果已经超时了,直接丢弃,然后继续循环 // 没有这个Key,说明等待结果已经超时了,直接丢弃,然后继续循环
// todo 错误的数据需要放置于某处 // todo 错误的数据需要放置于某处
log.debug( log.warn(
"等待队列里面没有该回复的结果key =>", "等待队列里面没有该回复的结果key =>",
matchKey matchKey
); );
@@ -121,13 +136,16 @@ public class OMessageHandler {
// 归还信息 // 归还信息
// 拿到原始信息 // 拿到原始信息
OctopusMessage originOMessage = FROM_AGENT_MATCH_TO_AGENT_MAP.get(matchKey); OMessageReplayContent oMessageReplayContent = FROM_AGENT_MATCH_TO_AGENT_MAP.get(matchKey);
OctopusMessage originOMessage = oMessageReplayContent.getOriginOMessage();
originOMessage.setResultCode(replayOMessage.getResultCode()); originOMessage.setResultCode(replayOMessage.getResultCode());
originOMessage.setResult(replayOMessage.getResult()); originOMessage.setResult(replayOMessage.getResult());
// 通知等待线程 // 通知等待线程
originOMessage.notify(); oMessageReplayContent
.getCountDownLatch()
.countDown();
} }
} }

View File

@@ -1,4 +1,4 @@
package io.wdd.rpc.message.handler.async; package io.wdd.rpc.message.handler;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
@@ -11,20 +11,19 @@ import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.GenerateOMessageMatchKey; import static io.wdd.rpc.message.handler.OMessageHandler.GenerateOMessageMatchKey;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@SuperBuilder(toBuilder = true) @SuperBuilder(toBuilder = true)
@ApiModel("众多业务调用RPC,异步等待需要确定返回消息是谁的") @ApiModel("众多业务调用RPC,异步等待需要确定返回消息是谁的")
public class OctopusMessageSyncReplayContend { public class OMessageReplayContent {
@ApiModelProperty("rpc消息的类型") @ApiModelProperty("rpc消息的类型")
OctopusMessageType type; OctopusMessageType octopusMessageType;
@ApiModelProperty("rpc消息发送的时间, 精确匹配,去掉毫秒") @ApiModelProperty("rpc消息发送的时间, 精确匹配,去掉毫秒")
@JsonFormat(pattern = "yyyy-MM-dd hh:MM:ss") @JsonFormat(pattern = "yyyy-MM-dd hh:MM:ss")
@@ -36,31 +35,26 @@ public class OctopusMessageSyncReplayContend {
@ApiModelProperty("需要等待的消息个数") @ApiModelProperty("需要等待的消息个数")
CountDownLatch countDownLatch; CountDownLatch countDownLatch;
@ApiModelProperty("回复的结果列表, 临时保存") @ApiModelProperty("发送的OctopusMessage")
ArrayList<OctopusMessage> replayOMList; OctopusMessage originOMessage;
/** public static OMessageReplayContent build(OctopusMessage originOMessage, int waitForReplayNum) {
* Execution模块使用的模板
*
* @return
*/
public static OctopusMessageSyncReplayContend build(int waitForReplayNum, OctopusMessageType currentOMType, LocalDateTime currentTime) {
CountDownLatch latch = null; CountDownLatch latch = null;
if (waitForReplayNum != 0) { if (waitForReplayNum != 0) {
latch = new CountDownLatch(waitForReplayNum); latch = new CountDownLatch(waitForReplayNum);
} }
return new OctopusMessageSyncReplayContend( return new OMessageReplayContent(
currentOMType, originOMessage.getOctopusMessageType(),
currentTime, originOMessage.getInit_time(),
GenerateOMessageMatchKey( GenerateOMessageMatchKey(
currentOMType, originOMessage.getOctopusMessageType(),
currentTime originOMessage.getInit_time()
), ),
latch, latch,
new ArrayList<OctopusMessage>(16) originOMessage
); );
} }

View File

@@ -1,4 +1,4 @@
package io.wdd.rpc.message.handler.sync; package io.wdd.rpc.message.handler;
import io.wdd.common.handler.MyRuntimeException; import io.wdd.common.handler.MyRuntimeException;
@@ -16,6 +16,7 @@ import java.util.ArrayDeque;
import java.util.HashMap; import java.util.HashMap;
import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper; import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
import static io.wdd.common.config.OctopusObjectMapperConfig.WriteToString;
@Configuration @Configuration
@Slf4j(topic = "Octopus Message Listener") @Slf4j(topic = "Octopus Message Listener")
@@ -39,10 +40,11 @@ public class OMessageToServerListener {
/** /**
* 发送出去的OctopusMessage需要和返回回来的内容对比 * 发送出去的OctopusMessage需要和返回回来的内容对比
* 返回来的OM反序列化之后就不是原对象需要进行 通过MatchKey比较 * 返回来的OM反序列化之后就不是原对象需要进行 通过MatchKey比较
* 2023年8月11日 修改为OMessageReplayContent
* <p> * <p>
* omMatchKey -- OctopusMessage * omMatchKey -- OMessageReplayContent
*/ */
public static HashMap<String, OctopusMessage> FROM_AGENT_MATCH_TO_AGENT_MAP = new HashMap<>(); public static HashMap<String, OMessageReplayContent> FROM_AGENT_MATCH_TO_AGENT_MAP = new HashMap<>();
@Resource @Resource
RedisTemplate redisTemplate; RedisTemplate redisTemplate;
@@ -67,12 +69,11 @@ public class OMessageToServerListener {
} }
// Octopus Message Handler // Octopus Message Handler
log.debug( log.info(
"received from agent : {} ", "received from agent : {} ",
octopusMessage WriteToString(octopusMessage)
); );
System.out.println("receivedOctopusMessage = " + octopusMessage.hashCode());
// 获取Agent的版本信息 // 获取Agent的版本信息
if (octopusMessage if (octopusMessage

View File

@@ -1,126 +0,0 @@
package io.wdd.rpc.message.handler.async;
import io.wdd.rpc.message.OctopusMessage;
import io.wdd.server.config.ServerCommonPool;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static io.wdd.rpc.message.handler.sync.OMessageHandler.GenerateOMessageMatchKey;
import static io.wdd.rpc.message.handler.sync.OMessageToServerListener.OCTOPUS_MESSAGE_FROM_AGENT;
/**
* 从Agent收集返回信息的统一处理地点
* 使用方法: 业务类构造 OMReplayContend对象调用AsyncWaitOMResult.waitFor()方法
* <p>
* 调用结束之后,需要从 REPLAY_WAITING_TARGET 中移除此部分内容
*/
//@Service
@Slf4j
public class AsyncWaitOctopusMessageResultService {
@PostConstruct
public void daemonHandleReplayOMFromAgent() {
// 异步任务启动
CompletableFuture.runAsync(
() -> doHandleReplayOMFromAgent(),
ServerCommonPool.pool
);
}
/**
* 为了避免线程不安全的问题,增加一层缓存,仅仅由当前类操作此部分
* KEY -> replayMatchKey
* VALUE -> OctopusMessageSyncReplayContend - 包含countDownLatch 和 result
*/
private static final HashMap<String, OctopusMessageSyncReplayContend> OM_REPLAY_WAITING_TARGET_MAP = new HashMap<>();
/**
* 操作 OCTOPUS_MESSAGE_FROM_AGENT 获取相应的Message放入内容中
*/
private void doHandleReplayOMFromAgent() {
// 死循环,不断的轮询 OCTOPUS_MESSAGE_FROM_AGENT
while (true) {
if (OCTOPUS_MESSAGE_FROM_AGENT.isEmpty()) {
// 开始收集等待 50 ms
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 返回,继续死循环
continue;
}
// 拿到消息
OctopusMessage replayOMessage = OCTOPUS_MESSAGE_FROM_AGENT.poll();
// 构造 replayMatchKey
String matchKey = GenerateOMessageMatchKey(
replayOMessage.getOctopusMessageType(),
replayOMessage.getInit_time()
);
if (!OM_REPLAY_WAITING_TARGET_MAP.containsKey(matchKey)) {
// 没有这个Key,说明等待结果已经超时了,直接丢弃,然后继续循环
// todo 错误的数据需要放置于某处
log.debug(
"等待队列里面没有该回复的结果key =>",
matchKey
);
continue;
}
// Map中包含有Key,那么放置进去
OctopusMessageSyncReplayContend replayContend = OM_REPLAY_WAITING_TARGET_MAP.get(matchKey);
replayContend
.getReplayOMList()
.add(replayOMessage);
// 需要操作countDown
replayContend
.getCountDownLatch()
.countDown();
// 结束操作,继续循环
}
}
public void waitFor(OctopusMessageSyncReplayContend OctopusMessageSyncReplayContend) {
// 向 REPLAY_CACHE_MAP中写入 Key
OM_REPLAY_WAITING_TARGET_MAP.put(
OctopusMessageSyncReplayContend.getReplayMatchKey(),
OctopusMessageSyncReplayContend
);
// 在调用线程的countDownLunch结束之后,关闭
// 清除 REPLAY_CACHE_MAP 中的队列
}
public void stopWaiting(OctopusMessageSyncReplayContend OctopusMessageSyncReplayContend) {
// 在调用线程的countDownLunch结束之后,关闭 清除 REPLAY_CACHE_MAP 中的队列
OctopusMessageSyncReplayContend contend = OM_REPLAY_WAITING_TARGET_MAP.get(OctopusMessageSyncReplayContend.getReplayMatchKey());
// 移除该内容
OM_REPLAY_WAITING_TARGET_MAP.remove(OctopusMessageSyncReplayContend.getReplayMatchKey());
// help gc
contend = null;
}
}

View File

@@ -1,265 +1,265 @@
package io.wdd.rpc.status.service; //package io.wdd.rpc.status.service;
//
import com.fasterxml.jackson.core.JsonProcessingException; //import com.fasterxml.jackson.core.JsonProcessingException;
import io.wdd.common.utils.TimeUtils; //import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.beans.request.MetricQueryEntity; //import io.wdd.rpc.beans.request.MetricQueryEntity;
import io.wdd.rpc.message.OctopusMessage; //import io.wdd.rpc.message.OctopusMessage;
import io.wdd.rpc.message.OctopusMessageType; //import io.wdd.rpc.message.OctopusMessageType;
import io.wdd.rpc.message.handler.async.AsyncWaitOctopusMessageResultService; //import io.wdd.rpc.message.handler.OMessageReplayContent;
import io.wdd.rpc.message.handler.async.OctopusMessageSyncReplayContend; //import io.wdd.rpc.message.sender.OMessageToAgentSender;
import io.wdd.rpc.message.sender.OMessageToAgentSender; //import io.wdd.rpc.status.beans.AgentStatus;
import io.wdd.rpc.status.beans.AgentStatus; //import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.Slf4j; //import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.RedisTemplate; //
//import javax.annotation.Resource;
import javax.annotation.Resource; //import java.time.LocalDateTime;
import java.time.LocalDateTime; //import java.util.ArrayList;
import java.util.ArrayList; //import java.util.HashMap;
import java.util.HashMap; //import java.util.List;
import java.util.List; //import java.util.Map;
import java.util.Map; //import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CountDownLatch; //import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit; //import java.util.stream.Collectors;
import java.util.stream.Collectors; //
//import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper;
import static io.wdd.common.config.OctopusObjectMapperConfig.OctopusObjectMapper; //import static io.wdd.rpc.message.handler.OMessageHandler.WaitFromAgent;
import static io.wdd.rpc.status.OctopusStatusMessage.*; //import static io.wdd.rpc.status.OctopusStatusMessage.*;
//
@Slf4j //@Slf4j
//@Service ////@Service
public class SyncStatusServiceImpl implements SyncStatusService { //public class SyncStatusServiceImpl implements SyncStatusService {
//
private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.STATUS; // private static final OctopusMessageType CurrentAppOctopusMessageType = OctopusMessageType.STATUS;
//
@Resource // @Resource
OMessageToAgentSender oMessageToAgentSender; // OMessageToAgentSender oMessageToAgentSender;
//
@Resource // @Resource
AsyncWaitOctopusMessageResultService asyncWaitOctopusMessageResultService; // RedisTemplate redisTemplate;
//
@Resource // @Override
RedisTemplate redisTemplate; // public Map<String, Boolean> SyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime) {
//
@Override // // 构造最后的结果Map
public Map<String, Boolean> SyncCollectAgentAliveStatus(List<String> agentTopicNameList, int aliveStatusWaitMaxTime) { // Map<String, Boolean> agentAliveStatusMap = agentTopicNameList
// .stream()
// 构造最后的结果Map // .collect(
Map<String, Boolean> agentAliveStatusMap = agentTopicNameList // Collectors.toMap(
.stream() // agentTopicName -> agentTopicName,
.collect( // agentTopicName -> Boolean.FALSE
Collectors.toMap( // ));
agentTopicName -> agentTopicName, //
agentTopicName -> Boolean.FALSE // // 当前的时间
)); // LocalDateTime currentTime = TimeUtils.currentFormatTime();
//
// 当前的时间 // // 构造OctopusMessage - StatusMessage结构体, 下发所有的消息
LocalDateTime currentTime = TimeUtils.currentFormatTime(); // buildAndSendAgentStatusOctopusMessage(
// agentTopicNameList,
// 构造OctopusMessage - StatusMessage结构体, 下发所有的消息 // HEALTHY_STATUS_MESSAGE_TYPE,
buildAndSendAgentStatusOctopusMessage( // currentTime
agentTopicNameList, // );
HEALTHY_STATUS_MESSAGE_TYPE, //
currentTime // // 同步收集消息
); // OMessageReplayContent statusSyncReplayContend = OMessageReplayContent.build(
// agentTopicNameList.size(),
// 同步收集消息 // CurrentAppOctopusMessageType,
OctopusMessageSyncReplayContend statusSyncReplayContend = OctopusMessageSyncReplayContend.build( // currentTime
agentTopicNameList.size(), // );
CurrentAppOctopusMessageType, // WaitFromAgent();
currentTime //
); // // 解析结果
asyncWaitOctopusMessageResultService.waitFor(statusSyncReplayContend); // CountDownLatch countDownLatch = statusSyncReplayContend.getCountDownLatch();
//
// 解析结果 // // 等待状态返回的结果
CountDownLatch countDownLatch = statusSyncReplayContend.getCountDownLatch(); // boolean agentAliveStatusCollectResult = false;
// try {
// 等待状态返回的结果 // agentAliveStatusCollectResult = countDownLatch.await(
boolean agentAliveStatusCollectResult = false; // aliveStatusWaitMaxTime,
try { // TimeUnit.SECONDS
agentAliveStatusCollectResult = countDownLatch.await( // );
aliveStatusWaitMaxTime, // } catch (InterruptedException e) {
TimeUnit.SECONDS // throw new RuntimeException(e);
); // } finally {
} catch (InterruptedException e) { // if (!agentAliveStatusCollectResult) {
throw new RuntimeException(e); // log.debug("Agent存活状态检查没有检查到全部的Agent");
} finally { // }
if (!agentAliveStatusCollectResult) { //
log.debug("Agent存活状态检查没有检查到全部的Agent"); // // 移除等待队列
} // asyncWaitOctopusMessageResultService.stopWaiting(statusSyncReplayContend);
//
// 移除等待队列 // // 处理结果
asyncWaitOctopusMessageResultService.stopWaiting(statusSyncReplayContend); // statusSyncReplayContend
// .getReplayOMList()
// 处理结果 // .stream()
statusSyncReplayContend // .forEach(
.getReplayOMList() // statusOMessage -> {
.stream() // if (statusOMessage.getResult() != null) {
.forEach( // agentAliveStatusMap.put(
statusOMessage -> { // statusOMessage.getUuid(),
if (statusOMessage.getResult() != null) { // Boolean.TRUE
agentAliveStatusMap.put( // );
statusOMessage.getUuid(), // }
Boolean.TRUE // }
); // );
} // }
} //
); // // 返回Agent的存活状态内容
} // return agentAliveStatusMap;
// }
// 返回Agent的存活状态内容 //
return agentAliveStatusMap; // @Override
} // public Map<String, AgentStatus> SyncCollectAgentMetricStatus(List<String> agentTopicNameList, int collectMetricWaitMaxTime) {
//
@Override // // 状态的结果Map
public Map<String, AgentStatus> SyncCollectAgentMetricStatus(List<String> agentTopicNameList, int collectMetricWaitMaxTime) { // HashMap<String, AgentStatus> metricMap = new HashMap<>();
//
// 状态的结果Map // // 当前的时间
HashMap<String, AgentStatus> metricMap = new HashMap<>(); // LocalDateTime currentTime = TimeUtils.currentFormatTime();
//
// 当前的时间 // // 构造所有的Metric的OM并且下发
LocalDateTime currentTime = TimeUtils.currentFormatTime(); // buildAndSendAgentStatusOctopusMessage(
// agentTopicNameList,
// 构造所有的Metric的OM并且下发 // METRIC_STATUS_MESSAGE_TYPE,
buildAndSendAgentStatusOctopusMessage( // currentTime
agentTopicNameList, // );
METRIC_STATUS_MESSAGE_TYPE, //
currentTime // // 同步等待结果, 并且解析结果
); // OMessageReplayContent metricSyncReplayContend = OMessageReplayContent.build(
// agentTopicNameList.size(),
// 同步等待结果, 并且解析结果 // CurrentAppOctopusMessageType,
OctopusMessageSyncReplayContend metricSyncReplayContend = OctopusMessageSyncReplayContend.build( // currentTime
agentTopicNameList.size(), // );
CurrentAppOctopusMessageType, // asyncWaitOctopusMessageResultService.waitFor(metricSyncReplayContend);
currentTime //
); // // 解析结果
asyncWaitOctopusMessageResultService.waitFor(metricSyncReplayContend); // CountDownLatch countDownLatch = metricSyncReplayContend.getCountDownLatch();
//
// 解析结果 // // 等待状态返回的结果
CountDownLatch countDownLatch = metricSyncReplayContend.getCountDownLatch(); // boolean agentAliveStatusCollectResult = false;
// try {
// 等待状态返回的结果 // agentAliveStatusCollectResult = countDownLatch.await(
boolean agentAliveStatusCollectResult = false; // collectMetricWaitMaxTime,
try { // TimeUnit.SECONDS
agentAliveStatusCollectResult = countDownLatch.await( // );
collectMetricWaitMaxTime, // } catch (InterruptedException e) {
TimeUnit.SECONDS // log.error("[Agent Metric] - 收集Agent的运行状态失败!");
); // throw new RuntimeException(e);
} catch (InterruptedException e) { // } finally {
log.error("[Agent Metric] - 收集Agent的运行状态失败!"); // if (!agentAliveStatusCollectResult) {
throw new RuntimeException(e); // log.debug("Agent存活状态检查没有检查到全部的Agent");
} finally { // }
if (!agentAliveStatusCollectResult) { //
log.debug("Agent存活状态检查没有检查到全部的Agent"); // // 移除等待队列
} // asyncWaitOctopusMessageResultService.stopWaiting(metricSyncReplayContend);
//
// 移除等待队列 // // 处理结果
asyncWaitOctopusMessageResultService.stopWaiting(metricSyncReplayContend); // metricSyncReplayContend
// .getReplayOMList()
// 处理结果 // .stream()
metricSyncReplayContend // .forEach(
.getReplayOMList() // statusOMessage -> {
.stream() // if (statusOMessage.getResult() != null) {
.forEach( //
statusOMessage -> { // // 解析Result对象为 AgentStatus
if (statusOMessage.getResult() != null) { // try {
//
// 解析Result对象为 AgentStatus // AgentStatus agentStatus = OctopusObjectMapper.readValue(
try { // (String) statusOMessage.getResult(),
// AgentStatus.class
AgentStatus agentStatus = OctopusObjectMapper.readValue( // );
(String) statusOMessage.getResult(), //
AgentStatus.class // // 保存结果
); // metricMap.put(
// statusOMessage.getUuid(),
// 保存结果 // agentStatus
metricMap.put( // );
statusOMessage.getUuid(), //
agentStatus // } catch (JsonProcessingException e) {
); // log.error("[Agent Metric] - 解析AgentStatus失败!");
// throw new RuntimeException(e);
} catch (JsonProcessingException e) { // }
log.error("[Agent Metric] - 解析AgentStatus失败!"); //
throw new RuntimeException(e); // }
} // }
// );
} // }
} //
); // return metricMap;
} // }
//
return metricMap; // @Override
} // public ArrayList<AgentStatus> QueryMetricStatus(MetricQueryEntity metricQueryEntity) {
//
@Override // String queryRedisKey = metricQueryEntity.getAgentTopicName() + "-Metric";
public ArrayList<AgentStatus> QueryMetricStatus(MetricQueryEntity metricQueryEntity) { //
// if (!redisTemplate.hasKey(queryRedisKey)) {
String queryRedisKey = metricQueryEntity.getAgentTopicName() + "-Metric"; // log.error(
// "[Metric] - 查询到没有此Agent {} 的Metric信息直接返回",
if (!redisTemplate.hasKey(queryRedisKey)) { // metricQueryEntity.getAgentTopicName()
log.error( // );
"[Metric] - 查询到没有此Agent {} 的Metric信息直接返回", // }
metricQueryEntity.getAgentTopicName() //
); // double start = metricQueryEntity.getMetricStartTimeStamp();
} // double end = metricQueryEntity.getMetricEndTimeStamp();
//
double start = metricQueryEntity.getMetricStartTimeStamp(); // ArrayList<AgentStatus> statusArrayList = new ArrayList<>();
double end = metricQueryEntity.getMetricEndTimeStamp(); //
// redisTemplate
ArrayList<AgentStatus> statusArrayList = new ArrayList<>(); // .opsForZSet()
// .rangeByScore(
redisTemplate // queryRedisKey,
.opsForZSet() // start,
.rangeByScore( // end
queryRedisKey, // )
start, // .stream()
end // .forEachOrdered(
) // metricString -> {
.stream() // try {
.forEachOrdered( //
metricString -> { // AgentStatus agentStatus = OctopusObjectMapper.readValue(
try { // (String) metricString,
// AgentStatus.class
AgentStatus agentStatus = OctopusObjectMapper.readValue( // );
(String) metricString, //
AgentStatus.class // statusArrayList.add(agentStatus);
); //
// } catch (JsonProcessingException e) {
statusArrayList.add(agentStatus); // throw new RuntimeException(e);
// }
} catch (JsonProcessingException e) { // }
throw new RuntimeException(e); // );
} //
} //
); // return statusArrayList;
// }
//
return statusArrayList; //
} // /**
// * 2023年7月10日 通用的底层构造方法 Status类型的Octopus Message
// *
/** // * @param agentTopicNameList
* 2023年7月10日 通用的底层构造方法 Status类型的Octopus Message // * @param statusType
* // * @param currentTime
* @param agentTopicNameList // * @return
* @param statusType // */
* @param currentTime // private List<OctopusMessage> buildAndSendAgentStatusOctopusMessage(List<String> agentTopicNameList, String statusType, LocalDateTime currentTime) {
*/ //
private void buildAndSendAgentStatusOctopusMessage(List<String> agentTopicNameList, String statusType, LocalDateTime currentTime) { // List<OctopusMessage> octopusStatusMessageList = agentTopicNameList
// .stream()
List<OctopusMessage> octopusStatusMessageList = agentTopicNameList // .map(
.stream() // agentTopicName -> ConstructAgentStatusMessage(
.map( // statusType,
agentTopicName -> ConstructAgentStatusMessage( // agentTopicName,
statusType, // currentTime
agentTopicName, // )
currentTime // )
) // .collect(Collectors.toList());
) //
.collect(Collectors.toList()); // // 发送信息
// oMessageToAgentSender.send(octopusStatusMessageList);
// 发送信息 //
oMessageToAgentSender.send(octopusStatusMessageList); // return octopusStatusMessageList;
} // }
//
//
} //}

View File

@@ -164,6 +164,5 @@ oss:
# 开启debug模式 # 开启debug模式
logging: logging:
level: level:
io.wdd.rpc.execute: debug io.wdd.rpc: debug
debug: true