From eca4b928d2b14fa940233eba3458df52077a9ede Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 12 Aug 2018 14:25:31 -0700 Subject: [PATCH] ensure master server count is odd --- weed/command/master.go | 32 ++++++++++++++++++++++++++------ weed/command/server.go | 8 ++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/weed/command/master.go b/weed/command/master.go index c1b9cf5ae..ac5902b60 100644 --- a/weed/command/master.go +++ b/weed/command/master.go @@ -35,7 +35,7 @@ var ( masterIp = cmdMaster.Flag.String("ip", "localhost", "master | address") masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to") metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data") - masterPeers = cmdMaster.Flag.String("peers", "", "other master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094") + masterPeers = cmdMaster.Flag.String("peers", "", "all master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094") volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.") volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.") mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats") @@ -86,11 +86,7 @@ func runMaster(cmd *Command, args []string) bool { go func() { time.Sleep(100 * time.Millisecond) - myMasterAddress := *masterIp + ":" + strconv.Itoa(*mport) - var peers []string - if *masterPeers != "" { - peers = strings.Split(*masterPeers, ",") - } + myMasterAddress, peers := checkPeers(*masterIp, *mport, *masterPeers) raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse) ms.SetRaftServer(raftServer) }() @@ -117,3 +113,27 @@ func runMaster(cmd *Command, args []string) bool { return true } + +func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) { + masterAddress = masterIp + ":" + strconv.Itoa(masterPort) + if peers != "" { + cleanedPeers = strings.Split(peers, ",") + } + + hasSelf := false + for _, peer := range cleanedPeers { + if peer == masterAddress { + hasSelf = true + break + } + } + + peerCount := len(cleanedPeers) + if !hasSelf { + peerCount += 1 + } + if peerCount %2 == 0 { + glog.Fatalf("Only odd number of masters are supported!") + } + return +} diff --git a/weed/command/server.go b/weed/command/server.go index 983b3075f..b0c6a1ed5 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -58,7 +58,7 @@ var ( serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name") serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name") serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.") - serverPeers = cmdServer.Flag.String("master.peers", "", "other master nodes in comma separated ip:masterPort list") + serverPeers = cmdServer.Flag.String("master.peers", "", "all master nodes in comma separated ip:masterPort list") serverSecureKey = cmdServer.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)") serverGarbageThreshold = cmdServer.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces") masterPort = cmdServer.Flag.Int("master.port", 9333, "master server http listen port") @@ -191,11 +191,7 @@ func runServer(cmd *Command, args []string) bool { go func() { raftWaitForMaster.Wait() time.Sleep(100 * time.Millisecond) - myAddress := *serverIp + ":" + strconv.Itoa(*masterPort) - var peers []string - if *serverPeers != "" { - peers = strings.Split(*serverPeers, ",") - } + myAddress, peers := checkPeers(*serverIp, *masterPort, *serverPeers) raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse) ms.SetRaftServer(raftServer) volumeWait.Done()