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