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 SHARED_JETTY_MGR_H
11 : #define SHARED_JETTY_MGR_H
12 :
13 : #include <cstdint>
14 : #include <memory>
15 : #include <mutex>
16 : #include <unordered_set>
17 : #include <unordered_map>
18 : #include "hccl/hccl_types.h"
19 : #include "hcomm_res_defs.h"
20 : #include "hcomm_channel.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 : * 底层 jetty 句柄的缓存与引用计数由 Endpoint::sharedJettyCtx_ 管理(见 endpoint.h),
30 : * 本管理器仅维护 channelHandle 集合用于销毁校验,不持有 jetty 句柄。
31 : */
32 : class SharedJettyMgr {
33 : public:
34 : struct SharedJettyContext {
35 : uint32_t channelCount{0};
36 : std::unordered_set<ChannelHandle> channelHandles;
37 : };
38 :
39 : static SharedJettyMgr& GetInstance();
40 :
41 : /**
42 : * @brief 注册 Channel 到共享 Jetty 上下文
43 : * @param[in] endpointHandle Endpoint 句柄
44 : * @param[in] channels 要注册的 Channel 句柄数组
45 : * @param[in] channelNum Channel 数量
46 : * @return HcclResult 执行结果
47 : */
48 : HcclResult RegisterChannels(EndpointHandle endpointHandle, const ChannelHandle* channels, uint32_t channelNum);
49 :
50 : /**
51 : * @brief 注销 Channel
52 : * @param[in] channels 要注销的 Channel 句柄数组
53 : * @param[in] channelNum Channel 数量
54 : * @return HcclResult 执行结果
55 : */
56 : HcclResult UnregisterChannels(const ChannelHandle* channels, uint32_t channelNum);
57 :
58 : /**
59 : * @brief 校验 Endpoint 是否可以销毁(所有共享 Jetty 的 Channel 已销毁)
60 : * @param[in] endpointHandle Endpoint 句柄
61 : * @return HcclResult HCCL_SUCCESS 表示可以销毁,HCCL_E_UNAVAIL 表示仍有 Channel 未销毁
62 : */
63 : HcclResult CheckEndpointDestroy(EndpointHandle endpointHandle);
64 :
65 : /**
66 : * @brief 判断 Endpoint 是否已存在共享 Jetty 上下文
67 : */
68 : bool HasContext(EndpointHandle endpointHandle);
69 :
70 : private:
71 4 : SharedJettyMgr() = default;
72 4 : ~SharedJettyMgr() = default;
73 : SharedJettyMgr(const SharedJettyMgr&) = delete;
74 : SharedJettyMgr& operator=(const SharedJettyMgr&) = delete;
75 :
76 : std::mutex mtx_;
77 : std::unordered_map<EndpointHandle, SharedJettyContext> contexts_;
78 : };
79 :
80 : } // namespace hcomm
81 :
82 : #endif // SHARED_JETTY_MGR_H
|