From 7761bfe02ea0cd2c558f921ec32956a3bf9d0e95 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 18 Oct 2026 20:59:44 -0700 Subject: [PATCH] master: close etcd volume id allocator client on shutdown --- weed/server/master_server.go | 10 ++++++++-- weed/server/master_volume_id_allocator.go | 18 +++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/weed/server/master_server.go b/weed/server/master_server.go index e08fab5b7e..b7f1d5a719 100644 --- a/weed/server/master_server.go +++ b/weed/server/master_server.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "net/http/httputil" "net/url" @@ -74,7 +75,8 @@ type MasterServer struct { option *MasterOption guard *security.Guard - preallocateSize int64 + preallocateSize int64 + volumeIdAllocatorCloser io.Closer Topo *topology.Topology vg *topology.VolumeGrowth @@ -187,10 +189,11 @@ func NewMasterServer(r *mux.Router, option *MasterOption, peers map[string]pb.Se switch v.GetString("master.volume_id_allocator") { case "": case "etcd": - allocator, err := newVolumeIdAllocator(v, ms.Topo) + allocator, closer, err := newVolumeIdAllocator(v, ms.Topo) if err != nil { glog.Fatalf("volume id allocator: %v", err) } + ms.volumeIdAllocatorCloser = closer ms.Topo.SetVolumeIdAllocator(allocator) default: glog.Fatalf("unknown master.volume_id_allocator %q", v.GetString("master.volume_id_allocator")) @@ -686,6 +689,9 @@ func (ms *MasterServer) Shutdown() { if raftServer != nil { raftServer.Stop() } + if ms.volumeIdAllocatorCloser != nil { + ms.volumeIdAllocatorCloser.Close() + } } func (ms *MasterServer) Reload() { diff --git a/weed/server/master_volume_id_allocator.go b/weed/server/master_volume_id_allocator.go index 8a4987d677..2c40ad5471 100644 --- a/weed/server/master_volume_id_allocator.go +++ b/weed/server/master_volume_id_allocator.go @@ -3,6 +3,7 @@ package weed_server import ( "crypto/tls" "fmt" + "io" "strings" "time" @@ -14,11 +15,13 @@ import ( "github.com/seaweedfs/seaweedfs/weed/util" ) -func newVolumeIdAllocator(v *util.ViperProxy, topo *topology.Topology) (topology.VolumeIdAllocator, error) { +// newVolumeIdAllocator returns the allocator and an io.Closer to release the +// etcd client on shutdown; the closer is nil for built-in allocation. +func newVolumeIdAllocator(v *util.ViperProxy, topo *topology.Topology) (topology.VolumeIdAllocator, io.Closer, error) { servers := v.GetString("master.volume_id_etcd.servers") timeout, err := time.ParseDuration(v.GetString("master.volume_id_etcd.timeout")) if err != nil { - return nil, fmt.Errorf("parse master.volume_id_etcd.timeout: %w", err) + return nil, nil, fmt.Errorf("parse master.volume_id_etcd.timeout: %w", err) } var tlsConfig *tls.Config @@ -29,7 +32,7 @@ func newVolumeIdAllocator(v *util.ViperProxy, topo *topology.Topology) (topology TrustedCAFile: caFile, } if tlsConfig, err = tlsInfo.ClientConfig(); err != nil { - return nil, fmt.Errorf("etcd TLS client configuration error: %w", err) + return nil, nil, fmt.Errorf("etcd TLS client configuration error: %w", err) } } @@ -41,13 +44,14 @@ func newVolumeIdAllocator(v *util.ViperProxy, topo *topology.Topology) (topology TLS: tlsConfig, }) if err != nil { - return nil, fmt.Errorf("connect volume id etcd: %w", err) + return nil, nil, fmt.Errorf("connect volume id etcd: %w", err) } glog.Infof("volume id allocator: etcd %s", servers) - return topology.NewEtcdVolumeIdAllocator( - client.KV, + allocator := topology.NewEtcdVolumeIdAllocator( + client, v.GetString("master.volume_id_etcd.key_prefix"), timeout, - topo.GetMaxVolumeId), nil + topo.GetMaxVolumeId) + return allocator, allocator, nil }