Browse Source

refactor: extract GetVolumeServerId to util package

Move the volume server ID determination logic to a shared utility function
to avoid code duplication between volume.go and rack.go.

Ref: https://github.com/seaweedfs/seaweedfs/issues/7487
pull/7609/head
chrislu 1 day ago
parent
commit
ec63b41d26
  1. 5
      weed/command/volume.go
  2. 6
      weed/topology/rack.go
  3. 11
      weed/util/network.go

5
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 // 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, volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
*v.ip, *v.port, *v.portGrpc, *v.publicUrl, volumeServerId, *v.ip, *v.port, *v.portGrpc, *v.publicUrl, volumeServerId,

6
weed/topology/rack.go

@ -52,11 +52,7 @@ func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl
defer r.Unlock() defer r.Unlock()
// Determine the node ID: use provided id, or fall back to ip:port for backward compatibility // 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) // First, try to find by node ID using O(1) map lookup (stable identity)
if c, ok := r.children[NodeId(nodeId)]; ok { if c, ok := r.children[NodeId(nodeId)]; ok {

11
weed/util/network.go

@ -64,3 +64,14 @@ func JoinHostPort(host string, port int) string {
} }
return net.JoinHostPort(host, portStr) 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
}
Loading…
Cancel
Save