From 42dd66431f74ec14ec67b84485bb8ccc811ba878 Mon Sep 17 00:00:00 2001 From: chrislu Date: Fri, 31 Oct 2025 17:35:52 -0700 Subject: [PATCH] refactoring --- weed/command/master.go | 19 ++++++++++++++----- weed/command/server.go | 29 ++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/weed/command/master.go b/weed/command/master.go index e58d60ac3..db36e8dc9 100644 --- a/weed/command/master.go +++ b/weed/command/master.go @@ -38,6 +38,10 @@ var ( m MasterOptions ) +const ( + raftJoinCheckDelay = 1500 * time.Millisecond // delay before checking if we should join a raft cluster +) + type MasterOptions struct { port *int portGrpc *int @@ -185,8 +189,7 @@ func startMaster(masterOption MasterOptions, masterWhiteList []string) { // start raftServer metaDir := path.Join(*masterOption.metaFolder, fmt.Sprintf("m%d", *masterOption.port)) - peersString := strings.TrimSpace(*masterOption.peers) - isSingleMaster := peersString == "none" + isSingleMaster := isSingleMasterMode(*masterOption.peers) raftServerOption := &weed_server.RaftServerOption{ GrpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.master"), @@ -245,7 +248,7 @@ func startMaster(masterOption MasterOptions, masterWhiteList []string) { // For multi-master mode with non-Hashicorp raft, wait and check if we should join if !*masterOption.raftHashicorp && !isSingleMaster { go func() { - time.Sleep(1500 * time.Millisecond) + time.Sleep(raftJoinCheckDelay) ms.Topo.RaftServerAccessLock.RLock() isEmptyMaster := ms.Topo.RaftServer.Leader() == "" && ms.Topo.RaftServer.IsLogEmpty() @@ -304,18 +307,24 @@ func startMaster(masterOption MasterOptions, masterWhiteList []string) { select {} } +func isSingleMasterMode(peers string) bool { + p := strings.TrimSpace(peers) + return p == "none" +} + func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers string) (masterAddress pb.ServerAddress, cleanedPeers []pb.ServerAddress) { glog.V(0).Infof("current: %s:%d peers:%s", masterIp, masterPort, peers) masterAddress = pb.NewServerAddress(masterIp, masterPort, masterGrpcPort) // Handle special case: -peers=none for single-master setup - peers = strings.TrimSpace(peers) - if peers == "none" { + if isSingleMasterMode(peers) { glog.V(0).Infof("Running in single-master mode (peers=none), no quorum required") cleanedPeers = []pb.ServerAddress{masterAddress} return } + peers = strings.TrimSpace(peers) + cleanedPeers = pb.ServerAddresses(peers).ToAddresses() hasSelf := false diff --git a/weed/command/server.go b/weed/command/server.go index 8b47fa4f4..d9e69ec30 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -234,16 +234,31 @@ func runServer(cmd *Command, args []string) bool { var actualPeersForComponents string if *isStartingMasterServer { - // Determine actual master addresses for other components without calling checkPeers - // (checkPeers will be called later in startMaster) - peersString := strings.TrimSpace(*masterOptions.peers) - if peersString == "none" || peersString == "" { - // Single-master mode or no peers specified: use the current server address + // Determine master addresses for other components + // checkPeers will be called later in startMaster for validation + if isSingleMasterMode(*masterOptions.peers) || *masterOptions.peers == "" { + // Single-master mode or no peers: use current server address actualPeersForComponents = util.JoinHostPort(*serverIp, *masterOptions.port) } else { - // Multi-master mode: use the provided peers string - actualPeersForComponents = peersString + // Multi-master mode: parse and potentially add self to peer list + peerList := pb.ServerAddresses(*masterOptions.peers).ToAddresses() + myAddress := pb.NewServerAddress(*serverIp, *masterOptions.port, *masterOptions.portGrpc) + // Check if current server is in the list + hasSelf := false + for _, peer := range peerList { + if peer.ToHttpAddress() == myAddress.ToHttpAddress() { + hasSelf = true + break + } + } + if !hasSelf { + peerList = append(peerList, myAddress) + } + actualPeersForComponents = strings.Join(pb.ToAddressStrings(peerList), ",") } + } else if *masterOptions.peers != "" { + // If not starting a master, just use the provided peers + actualPeersForComponents = *masterOptions.peers } if *serverBindIp == "" {