mochengqian commented on code in PR #930:
URL: https://github.com/apache/dubbo-go-pixiu/pull/930#discussion_r3292506704


##########
pkg/model/cluster.go:
##########
@@ -128,3 +129,50 @@ func (c *ClusterConfig) CreateConsistentHash() {
 func (e Endpoint) GetHost() string {
        return fmt.Sprintf("%s:%d", e.Address.Address, e.Address.Port)
 }
+
+// GenerateEndpointID returns a deterministic runtime identity for endpoints
+// that do not provide an explicit ID. The hash material includes cluster name,
+// endpoint address, and (when present) LLM provider + API key, so endpoints
+// that differ only by credential do not collide and the same endpoint hashes
+// to the same ID across process restarts.
+//
+// Design notes:
+//   - The output is sha256(material) truncated to the first 8 bytes (64 bits).
+//     Birthday-collision probability becomes meaningful only around 2^32
+//     endpoints, far above any realistic per-cluster scale.
+//   - clusterName is part of the material so endpoints in different clusters
+//     never alias. Callers from the Nacos LLM path supply
+//     instance.Metadata["cluster"]; if that metadata is missing the value is
+//     the empty string, and identity is then determined by address +
+//     credential only.
+//   - The LLM API key is included on purpose so two endpoints that share an
+//     address but use different credentials never alias to the same ID. The
+//     output is one-way (sha256), so the raw key never appears in the ID.
+func GenerateEndpointID(clusterName string, endpoint *Endpoint) string {
+       sum := sha256.Sum256([]byte(endpointIDMaterial(clusterName, endpoint)))
+       return fmt.Sprintf("generated-%x", sum[:8])
+}
+
+// endpointIDMaterial builds the byte string fed into the hash inside
+// GenerateEndpointID.
+//
+// Contract: this function MUST NOT depend on endpoint.Name. Callers
+// (notably ClusterStore.assembleClusterEndpoints) rely on being able to
+// derive the ID before assigning a default Name. Adding Name into the
+// material would also break the rename-invariance guarantee asserted by
+// TestGenerateEndpointIDIgnoresEndpointName.
+func endpointIDMaterial(clusterName string, endpoint *Endpoint) string {
+       if endpoint == nil {
+               return clusterName
+       }
+       provider := ""
+       apiKey := ""
+       if endpoint.LLMMeta != nil {
+               provider = endpoint.LLMMeta.Provider
+               apiKey = endpoint.LLMMeta.APIKey
+       }
+       return clusterName + "\x00" +

Review Comment:
   已采纳这个方向:这里不再依赖裸 `\x00` 分隔,而是改成字段名 + 长度前缀的结构化 material,避免边界歧义,也不影响 hash 的稳定性。



##########
pkg/model/cluster.go:
##########
@@ -128,3 +129,50 @@ func (c *ClusterConfig) CreateConsistentHash() {
 func (e Endpoint) GetHost() string {
        return fmt.Sprintf("%s:%d", e.Address.Address, e.Address.Port)
 }
+
+// GenerateEndpointID returns a deterministic runtime identity for endpoints
+// that do not provide an explicit ID. The hash material includes cluster name,
+// endpoint address, and (when present) LLM provider + API key, so endpoints
+// that differ only by credential do not collide and the same endpoint hashes
+// to the same ID across process restarts.
+//
+// Design notes:
+//   - The output is sha256(material) truncated to the first 8 bytes (64 bits).
+//     Birthday-collision probability becomes meaningful only around 2^32
+//     endpoints, far above any realistic per-cluster scale.
+//   - clusterName is part of the material so endpoints in different clusters
+//     never alias. Callers from the Nacos LLM path supply
+//     instance.Metadata["cluster"]; if that metadata is missing the value is
+//     the empty string, and identity is then determined by address +
+//     credential only.
+//   - The LLM API key is included on purpose so two endpoints that share an
+//     address but use different credentials never alias to the same ID. The
+//     output is one-way (sha256), so the raw key never appears in the ID.
+func GenerateEndpointID(clusterName string, endpoint *Endpoint) string {
+       sum := sha256.Sum256([]byte(endpointIDMaterial(clusterName, endpoint)))
+       return fmt.Sprintf("generated-%x", sum[:8])

Review Comment:
   这个建议我收了,但前缀没有直接写成 `pixiu-llm-endpoint-`,而是改成更通用的 
`pixiu-generated-endpoint-`:这个 helper 同时服务静态 cluster 和 LLM 
registry,两条路径都能读懂,而且日志/面板里一眼就能看出是 Pixiu 自动生成的 ID。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to