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 HCCL_OPRETRY_MANAGER_H
12 : #define HCCL_OPRETRY_MANAGER_H
13 : #include <thread>
14 : #include <mutex>
15 : #include "opretry_base.h"
16 :
17 : namespace hccl {
18 : struct RetryCtrl {
19 : std::unique_ptr<std::thread> thread;
20 : std::shared_ptr<RetryContext> retryCtx = nullptr;
21 : bool startExec = false;
22 : };
23 :
24 : class OpRetryManager
25 : {
26 : public:
27 0 : OpRetryManager() = default;
28 0 : ~OpRetryManager()
29 : {
30 0 : HCCL_DEBUG("Destroy OpRetryManager");
31 0 : (void)DeInit();
32 0 : }
33 : HcclResult RegisterOpRetryMachine(OpRetryAgentParam &agentParam, u32 rankSize, bool isRoot,
34 : std::map<u32, std::shared_ptr<HcclSocket> > &serverConnections, const OpRetryServerInfo& serverInfo);
35 : HcclResult UnRegisterOpRetryManager(const std::string& group);
36 :
37 : static HcclResult AddLinkInfoByIdentifier(s32 deviceLogicID, const std::string &identifier,
38 : const std::string &newTag, std::vector<u32> &remoteRankList, bool incre = false);
39 : static HcclResult GetLinkInfoByIdentifier(s32 deviceLogicID, const std::string &identifier,
40 : const std::string &newTag, std::vector<u32> &remoteRankList, bool isGetGroupAllRemoteRank = false);
41 : static HcclResult DeleteLinkInfoByIdentifier(s32 deviceLogicID, const std::string &identifier);
42 : HcclResult SetRetryStateToWaitResume(const std::string& group, bool isRoot);
43 : HcclResult ExitWaitResumeState(const std::string& group, bool isRoot, bool haveCommEnableBackupLink, bool& isChangedLink);
44 : bool IsPaused(const std::string &group);
45 : bool IsResumed(const std::string &group);
46 : private:
47 : HcclResult Init();
48 0 : HcclResult DeInit()
49 : {
50 0 : std::unique_lock<std::mutex> lock(ProcessLock_);
51 0 : if (initialized_) {
52 0 : initialized_ = false;
53 0 : for (auto it = agentOpRetry_.begin(); it != agentOpRetry_.end(); ++it) {
54 0 : if (it->second.thread != nullptr && it->second.thread->joinable()) {
55 0 : it->second.thread->join();
56 : }
57 : }
58 0 : agentOpRetry_.clear();
59 :
60 0 : for (auto it = serverOpRetry.begin(); it != serverOpRetry.end(); ++it) {
61 0 : if (it->second.thread != nullptr && it->second.thread->joinable()) {
62 0 : it->second.thread->join();
63 : }
64 : }
65 0 : serverOpRetry.clear();
66 0 : HCCL_INFO("OpRetryManager DeInit success");
67 : }
68 0 : return HCCL_SUCCESS;
69 0 : }
70 : HcclResult RegisterAgentRetryMachine(OpRetryAgentParam &agentParam);
71 : HcclResult RegisterServerRetryMachine(const std::string& group,
72 : std::map<u32, std::shared_ptr<HcclSocket>> &serverConnections, const OpRetryAgentInfo& agentInfo);
73 : void RetryStateMonitor(const std::string &group, std::shared_ptr<RetryContext> retryCtx, const bool &startExec,
74 : HcclRtContext rtCtx_);
75 :
76 : private:
77 : std::map<std::string, RetryCtrl> serverOpRetry;
78 : std::map<std::string, RetryCtrl> agentOpRetry_;
79 : bool initialized_ = false;
80 : std::mutex ProcessLock_;
81 : };
82 : }
83 : #endif
|