diff --git a/weed/command/volume.go b/weed/command/volume.go index 98bd52b98..514553172 100644 --- a/weed/command/volume.go +++ b/weed/command/volume.go @@ -256,10 +256,7 @@ func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, v } // Determine volume server ID: if not specified, use ip:port - volumeServerId := strings.TrimSpace(*v.id) - if volumeServerId == "" { - volumeServerId = util.JoinHostPort(*v.ip, *v.port) - } + volumeServerId := util.GetVolumeServerId(*v.id, *v.ip, *v.port) volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux, *v.ip, *v.port, *v.portGrpc, *v.publicUrl, volumeServerId, diff --git a/weed/topology/rack.go b/weed/topology/rack.go index 2f7c091f8..9b3f2a1e9 100644 --- a/weed/topology/rack.go +++ b/weed/topology/rack.go @@ -52,11 +52,7 @@ func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl defer r.Unlock() // Determine the node ID: use provided id, or fall back to ip:port for backward compatibility - // Trim whitespace to treat " " as empty - nodeId := strings.TrimSpace(id) - if nodeId == "" { - nodeId = util.JoinHostPort(ip, port) - } + nodeId := util.GetVolumeServerId(id, ip, port) // First, try to find by node ID using O(1) map lookup (stable identity) if c, ok := r.children[NodeId(nodeId)]; ok { diff --git a/weed/util/network.go b/weed/util/network.go index 328808dbc..f7dbeebb7 100644 --- a/weed/util/network.go +++ b/weed/util/network.go @@ -64,3 +64,14 @@ func JoinHostPort(host string, port int) string { } return net.JoinHostPort(host, portStr) } + +// GetVolumeServerId returns the volume server ID. +// If id is provided (non-empty after trimming), use it as the identifier. +// Otherwise, fall back to ip:port for backward compatibility. +func GetVolumeServerId(id, ip string, port int) string { + volumeServerId := strings.TrimSpace(id) + if volumeServerId == "" { + volumeServerId = JoinHostPort(ip, port) + } + return volumeServerId +}