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 : #include "new_rank_info.h"
12 :
13 : #include <sstream>
14 : #include <vector>
15 : #include <string>
16 : #include <unordered_map>
17 : #include "json_parser.h"
18 : #include "const_val.h"
19 : #include "exception_util.h"
20 :
21 : namespace Hccl {
22 60 : void CheakDeviceIdAndDevicePort(u32 deviceId, u32& devicePort)
23 : {
24 60 : if (deviceId > MAX_VALUE_DEVICEID) {
25 1 : THROW<InvalidParamsException>(
26 3 : StringFormat("device_id [%u] is out of range [%u] to [%u]", deviceId, MIN_VALUE_U32, MAX_VALUE_DEVICEID));
27 : }
28 59 : if (devicePort > MAX_VALUE_TCPPORT || devicePort < MIN_VALUE_TCPPORT) {
29 1 : THROW<InvalidParamsException>(StringFormat(
30 : "device_port [%u] is out of range [%u] to [%u]", devicePort, MIN_VALUE_TCPPORT, MAX_VALUE_TCPPORT));
31 : }
32 58 : }
33 :
34 56 : void CheakLevelJsonsSize(u64 levelJsonsSize)
35 : {
36 56 : if (levelJsonsSize > MAX_LEVEL_lIST) {
37 1 : THROW<InvalidParamsException>(
38 2 : StringFormat("level_list size [%u], exceeds the maximum limit [%u]", levelJsonsSize, MAX_LEVEL_lIST));
39 : }
40 55 : }
41 :
42 63 : void NewRankInfo::Deserialize(const nlohmann::json& newRankInfoJson)
43 : {
44 126 : std::string msgRankid = "error occurs when parser object of propName \"rank_id\"";
45 63 : std::string msgLocalid = "error occurs when parser object of propName \"local_id\"";
46 63 : TRY_CATCH_THROW(InvalidParamsException, msgRankid, rankId = GetJsonPropertyUInt(newRankInfoJson, "rank_id"););
47 63 : TRY_CATCH_THROW(InvalidParamsException, msgLocalid, localId = GetJsonPropertyUInt(newRankInfoJson, "local_id"););
48 63 : if (localId > BACKUP_LOCAL_ID) {
49 1 : THROW<InvalidParamsException>(
50 3 : StringFormat("local_id [%u] is out of range [%u] to [%u]", localId, MIN_VALUE_U32, BACKUP_LOCAL_ID));
51 : }
52 62 : if (localId == BACKUP_LOCAL_ID) {
53 2 : std::string msgReplacedId = "error occurs when parser object of propName \"replaced_local_id\"";
54 3 : TRY_CATCH_THROW(InvalidParamsException, msgReplacedId,
55 : replacedLocalId = GetJsonPropertyUInt(newRankInfoJson, "replaced_local_id"););
56 1 : if (replacedLocalId > BACKUP_LOCAL_ID - 1) {
57 0 : THROW<InvalidParamsException>(StringFormat(
58 : "replaced_local_id [%u] is out of range [%u] to [%u]", replacedLocalId, MIN_VALUE_U32,
59 : BACKUP_LOCAL_ID - 1));
60 : }
61 2 : } else {
62 60 : replacedLocalId = localId;
63 : }
64 122 : std::string msgDeviceid = "error occurs when parser object of propName \"device_id\"";
65 122 : std::string msgdeviceport = "error occurs when parser object of propName \"device_port\"";
66 61 : std::string msghostport = "error occurs when parser object of propName \"host_port\"";
67 62 : TRY_CATCH_THROW(InvalidParamsException, msgDeviceid, deviceId = GetJsonPropertyUInt(newRankInfoJson, "device_id"););
68 60 : TRY_CATCH_THROW(InvalidParamsException, msgdeviceport,
69 : devicePort = GetJsonPropertyUInt(newRankInfoJson, "device_port", false, DEFAULT_VALUE_TCPPORT););
70 60 : TRY_CATCH_THROW(InvalidParamsException, msghostport,
71 : hostPort = GetJsonPropertyUInt(newRankInfoJson, "host_port", false, DEFAULT_VALUE_TCPPORT););
72 60 : CheakDeviceIdAndDevicePort(deviceId, devicePort);
73 58 : if (hostPort > MAX_VALUE_TCPPORT || hostPort < MIN_VALUE_TCPPORT) {
74 4 : THROW<InvalidParamsException>(StringFormat(
75 : "host_port [%u] is out of range [%u] to [%u]", hostPort, MIN_VALUE_TCPPORT, MAX_VALUE_TCPPORT));
76 : }
77 56 : nlohmann::json levelJsons;
78 56 : std::string msgLevellist = "error occurs when parser object of propName \"level_list\"";
79 56 : TRY_CATCH_THROW(InvalidParamsException, msgLevellist,
80 : GetJsonPropertyList(newRankInfoJson, "level_list", levelJsons););
81 56 : CheakLevelJsonsSize(levelJsons.size());
82 150 : for (auto& levelJson : levelJsons) {
83 95 : RankLevelInfo levelInfo;
84 95 : levelInfo.Deserialize(levelJson);
85 358 : for (auto& addrsInfo : levelInfo.rankAddrs) {
86 263 : addrsInfo.socketPort_ = devicePort;
87 : }
88 95 : rankLevelInfos.emplace_back(levelInfo);
89 95 : }
90 :
91 55 : std::vector<u32> levelSequence;
92 150 : for (auto& levelInfos : rankLevelInfos) {
93 95 : levelSequence.emplace_back(levelInfos.netLayer);
94 : }
95 :
96 95 : for (u32 i = 1; i < levelSequence.size(); i++) {
97 40 : if (levelSequence[i] <= levelSequence[i - 1]) {
98 0 : THROW<InvalidParamsException>(StringFormat(
99 : "[NewRankInfo::%s] failed with level is not increased "
100 : "in sequence. rankId[%d], localId[%d], levelSequence[%u]",
101 : __func__, rankId, localId, levelSequence.size()));
102 : }
103 : }
104 :
105 55 : if (newRankInfoJson.contains("control_plane")) {
106 0 : nlohmann::json controlJsons;
107 0 : std::string msgControlPlane = "error occurs when parser object of propName \"control_plane\"";
108 0 : controlJsons = newRankInfoJson.at("control_plane");
109 0 : controlPlane.Deserialize(controlJsons);
110 0 : }
111 91 : }
112 :
113 8 : std::string NewRankInfo::Describe() const
114 : {
115 : return StringFormat(
116 : "NewRankInfo[rankId=%d, localId=%d, replacedLocalId=%d, ranklevelInfos size=%d, device_port=%d, "
117 : "host_port=%d, tlsStatus=%d]",
118 8 : rankId, localId, replacedLocalId, rankLevelInfos.size(), devicePort, hostPort, static_cast<int>(tlsStatus));
119 : }
120 :
121 8 : NewRankInfo::NewRankInfo(BinaryStream& binStream)
122 : {
123 8 : binStream >> rankId >> localId >> replacedLocalId >> deviceId >> devicePort >> hostPort;
124 12 : HCCL_DEBUG("[NewRankInfo] localId[%d]", localId);
125 : size_t rankLevelNum;
126 8 : binStream >> rankLevelNum;
127 16 : for (u32 i = 0; i < rankLevelNum; i++) {
128 8 : RankLevelInfo levelInfo(binStream);
129 8 : rankLevelInfos.emplace_back(levelInfo);
130 8 : }
131 8 : ControlPlane controlPlanes(binStream);
132 8 : controlPlane = controlPlanes;
133 8 : binStream >> tlsStatus;
134 8 : }
135 :
136 31 : void NewRankInfo::GetBinStream(bool isContainLoaId, BinaryStream& binStream) const
137 : {
138 31 : if (rankLevelInfos.size() == 0) {
139 0 : std::string msg = StringFormat("rankLevelInfos size is zero.");
140 0 : THROW<InvalidParamsException>(msg);
141 0 : }
142 31 : if (isContainLoaId) {
143 31 : binStream << rankId << localId << replacedLocalId << deviceId << devicePort << hostPort;
144 : } else {
145 0 : binStream << rankId << INVALID_RANKID << INVALID_RANKID << deviceId << devicePort << hostPort;
146 : }
147 31 : binStream << rankLevelInfos.size();
148 75 : HCCL_INFO("[NewRankInfo] rankLevelInfos size[%u], rankId[%d]", rankLevelInfos.size(), rankId);
149 102 : for (auto& it : rankLevelInfos) {
150 71 : it.GetBinStream(binStream);
151 : }
152 31 : controlPlane.GetBinStream(binStream);
153 31 : binStream << tlsStatus;
154 31 : }
155 :
156 : } // namespace Hccl
|