|
@ -11,6 +11,8 @@ import ( |
|
|
"net/url" |
|
|
"net/url" |
|
|
"os" |
|
|
"os" |
|
|
"path" |
|
|
"path" |
|
|
|
|
|
"reflect" |
|
|
|
|
|
"sort" |
|
|
"strings" |
|
|
"strings" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
@ -49,8 +51,9 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024 |
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024 |
|
|
glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr) |
|
|
glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr) |
|
|
|
|
|
|
|
|
// Clear old cluster configurations if peers are set
|
|
|
|
|
|
if len(s.peers) > 0 { |
|
|
|
|
|
|
|
|
// Clear old cluster configurations if peers are changed
|
|
|
|
|
|
if oldPeers, changed := isPeersChanged(s.dataDir, httpAddr, s.peers); changed { |
|
|
|
|
|
glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers) |
|
|
os.RemoveAll(path.Join(s.dataDir, "conf")) |
|
|
os.RemoveAll(path.Join(s.dataDir, "conf")) |
|
|
os.RemoveAll(path.Join(s.dataDir, "log")) |
|
|
os.RemoveAll(path.Join(s.dataDir, "log")) |
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot")) |
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot")) |
|
@ -71,6 +74,7 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
|
|
|
|
|
|
if len(s.peers) > 0 { |
|
|
if len(s.peers) > 0 { |
|
|
// Join to leader if specified.
|
|
|
// Join to leader if specified.
|
|
|
|
|
|
for { |
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ",")) |
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ",")) |
|
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) |
|
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) |
|
|
firstJoinError := s.Join(s.peers) |
|
|
firstJoinError := s.Join(s.peers) |
|
@ -82,7 +86,11 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
}) |
|
|
}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
glog.V(0).Infoln(err) |
|
|
glog.V(0).Infoln(err) |
|
|
return nil |
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else if s.raftServer.IsLogEmpty() { |
|
|
} else if s.raftServer.IsLogEmpty() { |
|
@ -116,6 +124,30 @@ func (s *RaftServer) Peers() (members []string) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) { |
|
|
|
|
|
confPath := path.Join(dir, "conf") |
|
|
|
|
|
// open conf file
|
|
|
|
|
|
b, err := ioutil.ReadFile(confPath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return oldPeers, true |
|
|
|
|
|
} |
|
|
|
|
|
conf := &raft.Config{} |
|
|
|
|
|
if err = json.Unmarshal(b, conf); err != nil { |
|
|
|
|
|
return oldPeers, true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, p := range conf.Peers { |
|
|
|
|
|
oldPeers = append(oldPeers, strings.TrimPrefix(p.ConnectionString, "http://")) |
|
|
|
|
|
} |
|
|
|
|
|
oldPeers = append(oldPeers, self) |
|
|
|
|
|
|
|
|
|
|
|
sort.Strings(peers) |
|
|
|
|
|
sort.Strings(oldPeers) |
|
|
|
|
|
|
|
|
|
|
|
return oldPeers, reflect.DeepEqual(peers, oldPeers) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Join joins an existing cluster.
|
|
|
// Join joins an existing cluster.
|
|
|
func (s *RaftServer) Join(peers []string) error { |
|
|
func (s *RaftServer) Join(peers []string) error { |
|
|
command := &raft.DefaultJoinCommand{ |
|
|
command := &raft.DefaultJoinCommand{ |
|
|