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 : #ifndef HCCLV2_CFG_FIELD_H
11 : #define HCCLV2_CFG_FIELD_H
12 :
13 : #include <string>
14 : #include <utility>
15 : #include <vector>
16 : #include <functional>
17 : #include "env_func.h"
18 :
19 : #include "sal.h"
20 : #include "exception_util.h"
21 : #include "invalid_params_exception.h"
22 : #include "adapter_error_manager_pub.h"
23 :
24 : namespace Hccl {
25 :
26 : template <typename T>
27 : class CfgField {
28 : public:
29 1088 : CfgField(
30 : std::string name, const T& defaultValue, const std::function<T(const std::string&)> cast,
31 : const std::function<void(const T&)> validate = {}, const std::function<void(T&)> postProc = {})
32 1088 : : name(std::move(name)),
33 1088 : value(defaultValue),
34 1088 : defaultBackup(defaultValue),
35 1088 : cast(cast),
36 1088 : validate(validate),
37 1088 : postProc(postProc),
38 1088 : isParsed(false),
39 1088 : isSetByEnvironment_(false) {};
40 :
41 679 : void Parse()
42 : {
43 679 : std::string str = SalGetEnv(name.c_str());
44 679 : if (str.empty() || str == "EmptyString") {
45 560 : isParsed = true;
46 560 : value = defaultBackup;
47 560 : return;
48 : }
49 119 : isSetByEnvironment_ = true;
50 : // 类型转换
51 119 : if (cast) {
52 : try {
53 119 : value = cast(str);
54 52 : } catch (const InvalidParamsException& e) {
55 : // 有异常上报故障码EI0001
56 364 : RPT_ENV_ERR(
57 : true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
58 : std::vector<std::string>({str, name, e.what()}));
59 26 : THROW<InvalidParamsException>(
60 78 : StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
61 0 : } catch (const NotSupportException& e) { // 临时修改方案 HCCL_SOCKET_IFNAME等当前不支持配置 且需要报错
62 0 : THROW<NotSupportException>(StringFormat(
63 : "[Init][EnvVarParam]Env config \"%s\" or its value is currently unsupported.%s", name.c_str(),
64 0 : e.what()));
65 : }
66 : } else {
67 0 : THROW<InvalidParamsException>(
68 0 : StringFormat("[Init][EnvVarParam]Env config \"%s\" No cast function is assigned.", name.c_str()));
69 : }
70 : // 校验,环境变量取值范围异常时抛异获取到范围值
71 93 : if (validate) {
72 : try {
73 61 : validate(value);
74 22 : } catch (const InvalidParamsException& e) {
75 : // 有异常上报故障码EI0001
76 154 : RPT_ENV_ERR(
77 : true, "EI0001", std::vector<std::string>({"value", "env", "expect"}),
78 : std::vector<std::string>({str, name, e.what()}));
79 11 : THROW<InvalidParamsException>(
80 33 : StringFormat("[Init][EnvVarParam]Env config \"%s\" value is invalid.%s", name.c_str(), e.what()));
81 : }
82 : }
83 : // 后处理
84 82 : if (postProc) {
85 31 : postProc(value);
86 : }
87 79 : isParsed = true;
88 753 : }
89 :
90 : const std::string& GetEnvName() const { return name; }
91 :
92 1997 : const T& Get() const
93 : {
94 1997 : if (UNLIKELY(!isParsed)) {
95 0 : THROW<InvalidParamsException>(
96 0 : StringFormat("Env config %s is not parsed. Should not use it.", name.c_str()));
97 : }
98 1997 : return value;
99 : }
100 :
101 31 : bool IsSetByEnvironment() const { return isSetByEnvironment_; }
102 :
103 609 : const char* GetSource() const { return isSetByEnvironment_ ? "environment" : "default"; }
104 :
105 : private:
106 : std::string name;
107 : T value;
108 : T defaultBackup; // 将默认值备份一份,便于后续恢复
109 : std::function<T(const std::string&)> cast;
110 : std::function<void(const T&)> validate;
111 : std::function<void(T&)> postProc;
112 : bool isParsed;
113 : bool isSetByEnvironment_;
114 : };
115 :
116 : } // namespace Hccl
117 :
118 : #endif // HCCLV2_CFG_FIELD_H
|