Files
seaweedFS/weed/pb/server_address.go
Chris Lu 995dfc4d5d chore: remove ~50k lines of unreachable dead code (#8913)
* chore: remove unreachable dead code across the codebase

Remove ~50,000 lines of unreachable code identified by static analysis.

Major removals:
- weed/filer/redis_lua: entire unused Redis Lua filer store implementation
- weed/wdclient/net2, resource_pool: unused connection/resource pool packages
- weed/plugin/worker/lifecycle: unused lifecycle plugin worker
- weed/s3api: unused S3 policy templates, presigned URL IAM, streaming copy,
  multipart IAM, key rotation, and various SSE helper functions
- weed/mq/kafka: unused partition mapping, compression, schema, and protocol functions
- weed/mq/offset: unused SQL storage and migration code
- weed/worker: unused registry, task, and monitoring functions
- weed/query: unused SQL engine, parquet scanner, and type functions
- weed/shell: unused EC proportional rebalance functions
- weed/storage/erasure_coding/distribution: unused distribution analysis functions
- Individual unreachable functions removed from 150+ files across admin,
  credential, filer, iam, kms, mount, mq, operation, pb, s3api, server,
  shell, storage, topology, and util packages

* fix(s3): reset shared memory store in IAM test to prevent flaky failure

TestLoadIAMManagerFromConfig_EmptyConfigWithFallbackKey was flaky because
the MemoryStore credential backend is a singleton registered via init().
Earlier tests that create anonymous identities pollute the shared store,
causing LookupAnonymous() to unexpectedly return true.

Fix by calling Reset() on the memory store before the test runs.

* style: run gofmt on changed files

* fix: restore KMS functions used by integration tests

* fix(plugin): prevent panic on send to closed worker session channel

The Plugin.sendToWorker method could panic with "send on closed channel"
when a worker disconnected while a message was being sent. The race was
between streamSession.close() closing the outgoing channel and sendToWorker
writing to it concurrently.

Add a done channel to streamSession that is closed before the outgoing
channel, and check it in sendToWorker's select to safely detect closed
sessions without panicking.
2026-04-03 16:04:27 -07:00

187 lines
5.0 KiB
Go

package pb
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
type ServerAddress string
type ServerAddresses string
type ServerSrvAddress string
func NewServerAddress(host string, port int, grpcPort int) ServerAddress {
if grpcPort == 0 {
return ServerAddress(util.JoinHostPort(host, port))
}
return ServerAddress(util.JoinHostPort(host, port) + "." + strconv.Itoa(grpcPort))
}
func NewServerAddressWithGrpcPort(address string, grpcPort int) ServerAddress {
if grpcPort == 0 {
return ServerAddress(address)
}
return ServerAddress(address + "." + strconv.Itoa(grpcPort))
}
func NewServerAddressFromDataNode(dn *master_pb.DataNodeInfo) ServerAddress {
// Use Address field if available (new behavior), fall back to Id for backward compatibility
addr := dn.Address
if addr == "" {
addr = dn.Id // backward compatibility: old nodes use ip:port as id
}
return NewServerAddressWithGrpcPort(addr, int(dn.GrpcPort))
}
func NewServerAddressFromLocation(dn *master_pb.Location) ServerAddress {
return NewServerAddressWithGrpcPort(dn.Url, int(dn.GrpcPort))
}
func (sa ServerAddress) String() string {
return sa.ToHttpAddress()
}
func (sa ServerAddress) ToHttpAddress() string {
portsSepIndex := strings.LastIndex(string(sa), ":")
if portsSepIndex < 0 {
return string(sa)
}
if portsSepIndex+1 >= len(sa) {
return string(sa)
}
ports := string(sa[portsSepIndex+1:])
sepIndex := strings.LastIndex(string(ports), ".")
if sepIndex >= 0 {
host := string(sa[0:portsSepIndex])
return util.JoinHostPortStr(host, ports[0:sepIndex])
}
return string(sa)
}
func (sa ServerAddress) Equals(other ServerAddress) bool {
if sa == other {
return true
}
return sa.ToHttpAddress() == other.ToHttpAddress()
}
func (sa ServerAddress) ToGrpcAddress() string {
portsSepIndex := strings.LastIndex(string(sa), ":")
if portsSepIndex < 0 {
return string(sa)
}
if portsSepIndex+1 >= len(sa) {
return string(sa)
}
ports := string(sa[portsSepIndex+1:])
sepIndex := strings.LastIndex(ports, ".")
if sepIndex >= 0 {
host := string(sa[0:portsSepIndex])
return util.JoinHostPortStr(host, ports[sepIndex+1:])
}
return ServerToGrpcAddress(string(sa))
}
// ToHost returns the host part only, without any port information.
func (sa ServerAddress) ToHost() string {
httpAddr := sa.ToHttpAddress()
host, _, err := net.SplitHostPort(httpAddr)
if err == nil {
return host
}
// Fallback: if parsing fails, it's likely a host without a port.
// Handle bracketed IPv6 (e.g., "[::1]" without port) by trimming brackets.
if strings.HasPrefix(httpAddr, "[") && strings.HasSuffix(httpAddr, "]") {
return httpAddr[1 : len(httpAddr)-1]
}
return httpAddr
}
// LookUp may return an error for some records along with successful lookups - make sure you do not
// discard `addresses` even if `err == nil`
func (r ServerSrvAddress) LookUp() (addresses []ServerAddress, err error) {
_, records, lookupErr := net.LookupSRV("", "", string(r))
if lookupErr != nil {
err = fmt.Errorf("lookup SRV address %s: %v", r, lookupErr)
}
for _, srv := range records {
address := fmt.Sprintf("%s:%d", srv.Target, srv.Port)
addresses = append(addresses, ServerAddress(address))
}
return
}
// ToServiceDiscovery expects one of: a comma-separated list of ip:port, like
//
// 10.0.0.1:9999,10.0.0.2:24:9999
//
// OR an SRV Record prepended with 'dnssrv+', like:
//
// dnssrv+_grpc._tcp.master.consul
// dnssrv+_grpc._tcp.headless.default.svc.cluster.local
// dnssrv+seaweed-master.master.consul
func (sa ServerAddresses) ToServiceDiscovery() (sd *ServerDiscovery) {
sd = &ServerDiscovery{}
prefix := "dnssrv+"
if strings.HasPrefix(string(sa), prefix) {
trimmed := strings.TrimPrefix(string(sa), prefix)
srv := ServerSrvAddress(trimmed)
sd.srvRecord = &srv
} else {
sd.list = sa.ToAddresses()
}
return
}
func (sa ServerAddresses) ToAddresses() (addresses []ServerAddress) {
parts := strings.Split(string(sa), ",")
for _, address := range parts {
if address != "" {
addresses = append(addresses, ServerAddress(address))
}
}
return
}
func (sa ServerAddresses) ToAddressMap() (addresses map[string]ServerAddress) {
addresses = make(map[string]ServerAddress)
for _, address := range sa.ToAddresses() {
addresses[string(address)] = address
}
return
}
func ToAddressStrings(addresses []ServerAddress) []string {
var strings []string
for _, addr := range addresses {
strings = append(strings, string(addr))
}
return strings
}
func ParseUrl(input string) (address ServerAddress, path string, err error) {
if !strings.HasPrefix(input, "http://") {
return "", "", fmt.Errorf("url %s needs prefix 'http://'", input)
}
input = input[7:]
pathSeparatorIndex := strings.Index(input, "/")
hostAndPorts := input
if pathSeparatorIndex > 0 {
path = input[pathSeparatorIndex:]
hostAndPorts = input[0:pathSeparatorIndex]
}
commaSeparatorIndex := strings.Index(input, ":")
if commaSeparatorIndex < 0 {
err = fmt.Errorf("port should be specified in %s", input)
return
}
address = ServerAddress(hostAndPorts)
return
}