use the first entry to bootstrap master cluster

fix https://github.com/chrislusf/seaweedfs/issues/851
This commit is contained in:
Chris Lu 2019-01-28 10:35:28 -08:00
parent adcfe66034
commit 40c8725ffa

View file

@ -3,7 +3,6 @@ package weed_server
import (
"encoding/json"
"io/ioutil"
"math/rand"
"os"
"path"
"reflect"
@ -71,8 +70,8 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin
for _, peer := range s.peers {
s.raftServer.AddPeer(peer, "http://"+peer)
}
time.Sleep(time.Duration(1000+rand.Int31n(3000)) * time.Millisecond)
if s.raftServer.IsLogEmpty() {
if s.raftServer.IsLogEmpty() && isTheFirstOne(httpAddr, s.peers) {
// Initialize the server by joining itself.
glog.V(0).Infoln("Initializing new cluster")
@ -129,3 +128,11 @@ func isPeersChanged(dir string, self string, peers []string) (oldPeers []string,
return oldPeers, !reflect.DeepEqual(peers, oldPeers)
}
func isTheFirstOne(self string, peers []string) bool {
sort.Strings(peers)
if len(peers)<=0{
return true
}
return self == peers[0]
}