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


##########
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作为分隔符



##########
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:
   ```suggestion
        return fmt.Sprintf("pixiu-llm-endpoint-%x", sum[:8])
   ```
   前缀加一些语义信息是不是更好



-- 
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