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 SHARED_JETTY_MGR_H
12 : #define SHARED_JETTY_MGR_H
13 :
14 : #include <cstdint>
15 : #include <memory>
16 : #include <mutex>
17 : #include <unordered_set>
18 : #include <unordered_map>
19 : #include "hccl/hccl_types.h"
20 : #include "hcomm_res_defs.h"
21 :
22 : namespace hcomm {
23 :
24 : /**
25 : * @note 职责:管理共享 Jetty 的 channel 注册记录,用于 endpoint 销毁前校验。
26 : * 当 IS_SHARED_QUEUE=true 时,HcommChannelCreateWithConfig 通过本管理器注册 Channel,
27 : * HcommChannelDestroy 通过本管理器注销 Channel,
28 : * HcommEndpointDestroy 通过本管理器校验所有共享 Jetty 的 Channel 是否已销毁。
29 : *
30 : * endpointHandle 作为不透明 key(builtin 路径为 Endpoint*,plugin 路径为 PluginEndpointCtx*),
31 : * 本管理器不做 static_cast 解引用,plugin/builtin 路径均安全。
32 : * 底层 jetty 句柄的缓存与引用计数由 Endpoint::JettyContext 管理(见 endpoint.h),
33 : * 本管理器仅维护 channelHandle 集合用于销毁校验,不持有 jetty 句柄。
34 : *
35 : * Endpoint/plugin 析构时调 UnregisterEndpoint 摘除记录,避免 endpointHandle 复用导致误判。
36 : */
37 : class SharedJettyMgr {
38 : public:
39 : struct SharedJettyContext {
40 : uint32_t channelCount{0};
41 : std::unordered_set<ChannelHandle> channelHandles;
42 : };
43 :
44 : static SharedJettyMgr& GetInstance();
45 :
46 : /**
47 : * @brief 注册 Channel 到共享 Jetty 上下文
48 : * @param[in] endpointHandle Endpoint 句柄(不透明 key,builtin 为 Endpoint*,plugin 为 PluginEndpointCtx*)
49 : * @param[in] channels 要注册的 Channel 句柄数组
50 : * @param[in] channelNum Channel 数量
51 : * @return HcclResult 执行结果
52 : */
53 : HcclResult RegisterChannels(EndpointHandle endpointHandle, const ChannelHandle* channels, uint32_t channelNum);
54 :
55 : /**
56 : * @brief 注销 Channel
57 : * @param[in] channels 要注销的 Channel 句柄数组
58 : * @param[in] channelNum Channel 数量
59 : * @return HcclResult 执行结果
60 : */
61 : HcclResult UnregisterChannels(const ChannelHandle* channels, uint32_t channelNum);
62 :
63 : /**
64 : * @brief 校验 Endpoint 是否可以销毁(所有共享 Jetty 的 Channel 已销毁)
65 : * @param[in] endpointHandle Endpoint 句柄(不透明 key)
66 : * @return HcclResult HCCL_SUCCESS 表示可以销毁,HCCL_E_UNAVAIL 表示仍有 Channel 未销毁
67 : */
68 : HcclResult CheckEndpointDestroy(EndpointHandle endpointHandle);
69 :
70 : /**
71 : * @brief 判断 Endpoint 是否已存在共享 Jetty 上下文
72 : */
73 : bool HasContext(EndpointHandle endpointHandle);
74 :
75 : /**
76 : * @brief Endpoint/plugin 析构时摘除该 endpointHandle 的注册记录,避免句柄复用误判
77 : * @note builtin 路径由 Endpoint::~Endpoint 调用,plugin 路径由 DestroyPluginEndpoint 调用
78 : */
79 : void UnregisterEndpoint(EndpointHandle endpointHandle);
80 :
81 : private:
82 4 : SharedJettyMgr() = default;
83 4 : ~SharedJettyMgr() = default;
84 : SharedJettyMgr(const SharedJettyMgr&) = delete;
85 : SharedJettyMgr& operator=(const SharedJettyMgr&) = delete;
86 :
87 : std::mutex mtx_;
88 : std::unordered_map<EndpointHandle, SharedJettyContext> contexts_;
89 : };
90 :
91 : } // namespace hcomm
92 :
93 : #endif // SHARED_JETTY_MGR_H
|