---
title: 建链
description: "调用LinkLlmClusters接口在PD节点之间建立通信链路，链路主要用于KV Cache的传输。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/hixlug/llmcdv2_42_14.html
sourcePath: /source/zh/canncommercial/900/API/hixlug/llmcdv2_42_14.html
indexId: 58e91e48dea0780af98443c02052738df61b2a4aeae4f5d6e85e4e54a639056059
---
# 建链

#### 功能介绍

调用LinkLlmClusters接口在PD节点之间建立通信链路，链路主要用于KV Cache的传输。


#### 使用场景

建链操作是节点之间进行数据传输的前提，建链接口采用类TCP建链流程，Server侧初始化时提供侦听信息，由Client侧发起建链，Server/Client与prompt/decoder角色无关，可以根据需求自行设置。

根据业务繁忙情况，在需要调整集群PD节点配比时，通过建链扩容节点。


#### 功能示例

初始化LLM-DataDist。其中Server侧需要设置侦听的Host IP和port。

```
// Server侧
LlmDataDist llm_data_dist(PROMPT_CLUSTER_ID, LlmRole::kPrompt);
std::map<AscendString, AscendString> options;
options[OPTION_DEVICE_ID] = "0";
//替换成真实IP端口
options[OPTION_LISTEN_IP_INFO] = "ip:port";
auto ret = llm_data_dist.Initialize(options);
if (ret != LLM_SUCCESS) {
    printf("[ERROR] Initialize failed, ret = %u\n", ret);
    return -1;
}
// Client侧
LlmDataDist llm_data_dist(DECODER_CLUSTER_ID, LlmRole::kDecoder);
std::map<AscendString, AscendString> options;
options[OPTION_DEVICE_ID] = "0";
auto ret = llm_data_dist.Initialize(options);
if (ret != LLM_SUCCESS) {
    printf("[ERROR] Initialize failed, ret = %u\n", ret);
    return -1;
}
```


在Client侧调用LinkLlmClusters发起建链操作。

```
std::vector<Status> rets;
std::vector<ClusterInfo> clusters;
ClusterInfo cluster_info;
IpInfo local_ip_info{};
// 替换成本地真实IP
local_ip_info.ip = "ip";
IpInfo remote_ip_info{};
// 替换成对端真实IP
remote_ip_info.ip = "ip";
// 替换成Server侧初始化时的侦听端口
remote_ip_info.port = "port";
cluster_info.remote_cluster_id = PROMPT_CLUSTER_ID;
cluster_info.local_ip_infos.emplace_back(std::move(local_ip_info));
cluster_info.remote_ip_infos.emplace_back(std::move(remote_ip_info));
clusters.emplace_back(std::move(cluster_info));
auto ret = llm_data_dist.LinkLlmClusters(clusters, rets);
if (ret != LLM_SUCCESS) {
    printf("[ERROR] LinkLlmClusters failed, ret = %u\n", ret);
    return -1;
}
for (const auto &inner_ret : rets) {
    if (inner_ret != LLM_SUCCESS) {
        printf("[ERROR] LinkLlmClusters failed, ret = %u\n", inner_ret);
        return -1;
    }
}
```


#### 异常处理

当调用LinkLlmClusters失败时，需排查两台机器网络是否连通，Host IP是否正确，port是否被占用。
