Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef CLUSTER_MONITOR_H
12 : #define CLUSTER_MONITOR_H
13 : #include <atomic>
14 : #include <thread>
15 : #include <map>
16 : #include <deque>
17 : #include <mutex>
18 : #include "hcclCommDfx.h"
19 : #include "ring_buffer.h"
20 : #include "coll_comm.h"
21 : #include "reference_map.h"
22 : #include "log.h"
23 : #include "hccl/hccl_types.h"
24 : #include "hccl_common.h"
25 : #include "hccl_comm_socket_c_adpt.h"
26 : #include "hccl_communicator.h"
27 : #include "../../common/loggers/comm_addr_logger.h"
28 :
29 : namespace hcomm {
30 : using ClusterUIDType = struct HcclClusterMonitorUID {
31 : char id[2048] = {0}; // netInstanceId + localId 最大不超过2048字节
32 1055 : bool operator==(const HcclClusterMonitorUID& that) const { return std::string(this->id) == std::string(that.id); }
33 30 : bool operator!=(const HcclClusterMonitorUID& that) const { return std::string(this->id) != std::string(that.id); }
34 310 : bool operator<(const HcclClusterMonitorUID& that) const { return std::string(this->id) < std::string(that.id); }
35 : };
36 : } // namespace hcomm
37 :
38 : namespace std {
39 : template <>
40 : class hash<hcomm::HcclClusterMonitorUID> {
41 : public:
42 723 : size_t operator()(const hcomm::HcclClusterMonitorUID& uid) const { return hash<string>()(string(uid.id)); }
43 : };
44 : } // namespace std
45 :
46 : namespace hcomm {
47 : enum class ClusterMonitorStatus {
48 : CLUSTER_MONITOR_OK,
49 : CLUSTER_MONITOR_LOST,
50 : CLUSTER_MONITOR_NOTIFY,
51 : CLUSTER_MONITOR_CQE_ERR,
52 : CLUSTER_MONITOR_OPRETRY_NOT_SUPPORT,
53 : CLUSTER_MONITOR_STUCK,
54 : CLUSTER_MONITOR_INCONSISTENT
55 : };
56 :
57 : struct ErrorCqeInfo {
58 : u32 cqeLocalId = 0;
59 : u32 cqeRemoteLocalId = 0;
60 : uint16_t cqeStatus = 0;
61 : std::string cqeLocalEid = "";
62 : std::string cqeRemoteEid = "";
63 : std::string cqeRemoteInsId = "";
64 : std::string cqeLocalInsId = "";
65 : };
66 : const std::map<ClusterMonitorStatus, std::string> CLUSTER_MONITOR_STATUS_STR_MAP{
67 : {ClusterMonitorStatus::CLUSTER_MONITOR_OK, "OK"},
68 : {ClusterMonitorStatus::CLUSTER_MONITOR_LOST, "LOST"},
69 : {ClusterMonitorStatus::CLUSTER_MONITOR_NOTIFY, "NOTIFY"},
70 : {ClusterMonitorStatus::CLUSTER_MONITOR_CQE_ERR, "CQE ERROR"}};
71 :
72 1 : inline std::string GetClusterMonitorStatusStr(ClusterMonitorStatus status)
73 : {
74 1 : auto iter = CLUSTER_MONITOR_STATUS_STR_MAP.find(status);
75 1 : if (iter == CLUSTER_MONITOR_STATUS_STR_MAP.end()) {
76 0 : return "Unknown";
77 : } else {
78 1 : return iter->second;
79 : }
80 : }
81 :
82 : struct ClusterMonitorFrame {
83 : ClusterUIDType src{}; // 心跳建链的本端
84 : ClusterUIDType dst{}; // 心跳建链的远端
85 : ClusterUIDType crimer{}; // 异常的节点
86 : ClusterUIDType informer{}; // 把异常传输给自己的节点
87 : ClusterMonitorStatus status = ClusterMonitorStatus::CLUSTER_MONITOR_OK;
88 : HcclUs TOARelative{}; // time of arrival (Relative)
89 : HcclSystemTime TOASystem{}; // time of arrival (System)
90 : char reserved[256] = {0}; // 预留256个字段,后续扩展可存储其他信息
91 2 : ClusterMonitorFrame() {}
92 6 : ClusterMonitorFrame(
93 : ClusterUIDType& crimer, ClusterUIDType& informer, ClusterMonitorStatus status, HcclUs TOARelativeIn,
94 : HcclSystemTime TOASystemIn)
95 6 : : crimer(crimer),
96 6 : informer(informer),
97 6 : status(status),
98 6 : TOARelative(TOARelativeIn),
99 6 : TOASystem(TOASystemIn)
100 6 : {}
101 12 : ClusterMonitorFrame(
102 : ClusterUIDType& src, ClusterUIDType& dst, ClusterUIDType& crimer, ClusterUIDType& informer,
103 : ClusterMonitorStatus status)
104 12 : : src(src),
105 12 : dst(dst),
106 12 : crimer(crimer),
107 12 : informer(informer),
108 12 : status(status)
109 12 : {}
110 : };
111 :
112 : struct ClusterMonitorSocketCtx { // 原ConnInfo
113 : SocketDesc socketDesc; // 与对端连接的描述符
114 : SocketHandle socketHandler; // 引用头文件定义
115 : std::queue<ClusterMonitorFrame> sendBuffer; // 用来发送的帧队列
116 : u32 restSize = 0; // 剩余待发送的帧长度
117 : hccl::RingBuffer recvBuffer; // 用来接收的环形帧队列
118 : u32 lostNum = 0; // 丢失的心跳个数
119 : bool newConn = false; // 是否是新增的连接
120 5 : ClusterMonitorSocketCtx() {}
121 1 : ClusterMonitorSocketCtx(SocketDesc& socketDesc, bool newConn)
122 1 : : socketDesc(socketDesc),
123 1 : socketHandler(nullptr),
124 1 : newConn(newConn)
125 1 : {}
126 :
127 6 : void PrintSocketDesc(std::string tag) const
128 : {
129 6 : std::string localAddr = hcomm::logger::CommAddrLogger::ToString(socketDesc.localEndpoint.commAddr);
130 6 : std::string remoteAddr = hcomm::logger::CommAddrLogger::ToString(socketDesc.remoteEndpoint.commAddr);
131 6 : HCCL_DEBUG(
132 : "[%s] socketDesc: localEndpoint: {commAddr: %s, EndpointLocType: %d}, "
133 : "remoteEndpoint: {commAddr: %s, EndpointLocType: %d}, tag: %s, role: %d, listenPort: %u",
134 : tag.c_str(), localAddr.c_str(), socketDesc.localEndpoint.loc.locType, remoteAddr.c_str(),
135 : socketDesc.remoteEndpoint.loc.locType, socketDesc.tag, socketDesc.role, socketDesc.listenPort);
136 6 : }
137 : };
138 :
139 : struct UIDContext {
140 : ClusterUIDType uid;
141 : uint32_t netLayer{0};
142 : uint32_t rankId{0};
143 : uint32_t localId{0}; // 用来netLayer=0的时候排序使用
144 : std::string netInstId{}; // 用来netLayer>1的时候排序使用
145 : UIDContext() {}
146 21 : UIDContext(ClusterUIDType& uid, uint32_t netLayer, uint32_t rankId, uint32_t localId, std::string netInstId)
147 21 : : uid(uid),
148 21 : netLayer(netLayer),
149 21 : rankId(rankId),
150 21 : localId(localId),
151 21 : netInstId(netInstId)
152 21 : {}
153 : };
154 :
155 : struct ClusterUIDCxt {
156 : std::string netInstId;
157 : uint32_t localId;
158 : ClusterUIDCxt() {}
159 20 : ClusterUIDCxt(std::string& netInstId, uint32_t localId) : netInstId(netInstId), localId(localId) {}
160 : };
161 :
162 : class ClusterMonitor {
163 : public:
164 : HcclResult RegisterToClusterMonitor(HcclComm comm);
165 : HcclResult UnRegisterToClusterMonitor(const hccl::CollComm* collComm);
166 : ClusterUIDType FormatUID(ClusterUIDCxt cxt) const;
167 : std::string GetUID(const ClusterUIDType& uid) const;
168 : std::string FormatConnTag(HcommSocketRole role, std::pair<ClusterUIDType, ClusterUIDType> uidPair) const;
169 : HcclResult InsertClusterMonitorCtx(
170 : HcclComm comm, UIDContext remoteCtx, std::map<ClusterUIDType, ClusterMonitorSocketCtx>& needConnectRank);
171 : HcclResult GetSocketDescFromRankInfo(
172 : HcclComm comm, uint32_t remoteRank, uint32_t netLayer, const ClusterUIDType& remoteUID, SocketDesc& socketDesc);
173 : HcclResult GetSamePlaneRank(
174 : HcclComm comm, std::vector<UIDContext> singlePlaneCtx,
175 : std::map<ClusterUIDType, ClusterMonitorSocketCtx>& needConnectRank);
176 : HcclResult GetConnectRank(
177 : HcclComm comm, std::map<ClusterUIDType, ClusterMonitorSocketCtx>& needConnectRank,
178 : std::map<uint32_t, std::vector<UIDContext>> uidCtxs, std::vector<uint32_t>& netLayersVector);
179 : void CreateHBLinksAsync();
180 : void
181 : SetStatus(ClusterUIDType& crimer, ClusterUIDType& informer, ClusterMonitorStatus status, bool needBroadcast = true);
182 : void MonitorThread();
183 : HcclResult RunMonitorThread();
184 : HcclResult
185 : SendFrame(ClusterUIDType& dst, ClusterUIDType& crimer, ClusterUIDType& informer, ClusterMonitorStatus status);
186 : void DelErrorSocket();
187 : void ProcessExceptionEvent();
188 : HcclResult RecvFrame(ClusterUIDType rem);
189 : HcclResult ParseFrame(ClusterMonitorFrame& cmFrame, ClusterUIDType& src);
190 : HcclResult DeInit();
191 : void GetCqeErrInfoFromTaskException(
192 : u32 remoteLocalId, uint16_t status, std::string localEid, std::string remoteEid, std::string remoteInsId);
193 : std::vector<std::string> GetErrStatusVecFromCluserMonitor();
194 : std::vector<std::string>
195 : PrintEvents(std::map<ClusterMonitorStatus, std::queue<ClusterMonitorFrame>>& keyEvents) const;
196 : void MakeErrMsg(std::queue<ClusterMonitorFrame>& keyEvents, std::vector<std::string>& errStatusVec) const;
197 586 : ClusterMonitor() = default;
198 : ~ClusterMonitor();
199 :
200 : private:
201 : HcclResult GetRemEndpointDescs(
202 : HcclComm comm, std::map<uint32_t, std::vector<UIDContext>>& uidCtxs, std::vector<uint32_t>& netLayersVector);
203 : void GetRemEndpointDescsPerLayer(
204 : uint32_t netLayer, HcclComm comm, const Hccl::RankGraph* rankGraph, const hccl::CollComm* collComm,
205 : std::map<uint32_t, std::vector<UIDContext>>& uidCtxs, std::set<uint32_t>& rankIdsSet);
206 :
207 : HcclResult
208 : ProcessConnectRanks(const std::string& commId, std::map<ClusterUIDType, ClusterMonitorSocketCtx>& needConnectRank);
209 : void ClearClusterLinkContext(const std::string& commId, std::set<ClusterUIDType>& remInQueue);
210 : bool UnregisterCommIdFromMaps(const std::string& commId, const std::set<ClusterUIDType>& remInQueue);
211 : HcclResult CreateTransportHandle(ClusterMonitorSocketCtx& info) const;
212 : HcclResult OnConnectionEstablished(
213 : const std::string& commId, const ClusterUIDType& rem, ClusterMonitorSocketCtx& needConnectRank);
214 : HcclResult SendFrameFromBuffer(ClusterUIDType& dst, ClusterMonitorFrame& cmFrame);
215 :
216 : void CreateLinkWithRemotePonit(std::string commId, ClusterUIDType rem, ClusterMonitorSocketCtx needConnectRank);
217 :
218 : struct FrameStatus { // 专门用来给frame设置对应的状态
219 : ClusterMonitorStatus status = ClusterMonitorStatus::CLUSTER_MONITOR_OK;
220 : ClusterUIDType informer;
221 : bool needBroadcast = false;
222 7 : FrameStatus() {}
223 : };
224 :
225 : enum class MonitorLinkStatus {
226 : MONITOR_LINK_NOT_START,
227 : MONITOR_LINK_BUILDING,
228 : MONITOR_LINK_COMPLETED,
229 : };
230 :
231 : uint32_t myRankLocalId_;
232 : std::string myRankNetInstId_;
233 : ClusterUIDType myRankUID_;
234 : s32 deviceLogicId_{0};
235 :
236 : bool clusterMonitorThreadFlag_ = false;
237 : std::unique_ptr<std::thread> clusterMonitorThread_;
238 : // 防止重复初始化
239 : bool initialized_ = false;
240 : uint32_t lostThreshold_ = 0;
241 : std::atomic<bool> isDeInit_{false};
242 : std::atomic<bool> linkThreadRunning_{false};
243 :
244 : // 防止多线程同时初始化的线程锁
245 : std::mutex threadLock_;
246 : std::vector<ClusterUIDType> errorSocket_;
247 : std::queue<ClusterMonitorFrame> errStatusQueue_;
248 :
249 : // 通信域名称为key,ClusterUIDType表示1个节点,
250 : // bool表示0或1,是否连接上,用来指示是否有过这个通信域以及对应通信域里待连接的节点是否以及连接上,原groupMap_
251 : std::map<std::string, std::map<ClusterUIDType, bool>> commIdMap_;
252 :
253 : // 通信域名称为key, 一个通信域有多个待连接心跳的connInfo,存储到该结构体,待monitor线程轮询拿到, 原hbLinkConnInfo_
254 : std::map<std::string, std::queue<std::pair<ClusterUIDType, ClusterMonitorSocketCtx>>> clusterLinkContext_{};
255 : std::mutex clusertMonitorLinkMtx_; // 用来锁住clusterLinkContext_,原clusterLinkContext_
256 :
257 : // 存储UID与监控连接状态的map, NOT_START/BUILDING/COMPILETED,原rankId2LinkStatusMap_
258 : std::map<ClusterUIDType, MonitorLinkStatus> monitorLinkStatusMap_;
259 :
260 : // 存储UID与连接上下文的计数map,由于多个通信域都有可能使用同一个context去连接远端,需要计数处理,解注册时计数--,原rankId2SocketMap_
261 : hccl::ReferenceMap<ClusterUIDType, ClusterMonitorSocketCtx> uid2SocketRefMap_;
262 :
263 : // 用来做帧的统计计数,设置对应帧的状态,原rankId2StatusMap_
264 : hccl::ReferenceMap<ClusterUIDType, FrameStatus> uid2FrameStatusMap_;
265 :
266 : // uid与thread的维护关系,不同的remote起不同的异步建链线程,原linkThreadMap_
267 : std::map<ClusterUIDType, std::unique_ptr<std::thread>> linkThreadMap_{};
268 :
269 : // UnRegister 摘下、延后到 DeInit(join 之后) 再 SocketDestroy 的句柄
270 : std::vector<SocketHandle> pendingDestroySockets_;
271 :
272 : // 保存错误的节点
273 : std::queue<ClusterUIDType> errRankQueue_;
274 :
275 : ErrorCqeInfo cqeErrInfo_;
276 : };
277 : } // namespace hcomm
278 : #endif // CLUSTER_MONITOR_H
|