|
|
@ -9,6 +9,8 @@ import ( |
|
|
|
"math/rand" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
@ -46,6 +48,13 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
|
transporter := raft.NewHTTPTransporter("/cluster", 0) |
|
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024 |
|
|
|
|
|
|
|
// Clear old cluster configurations if peers are set
|
|
|
|
if len(s.peers) > 0 { |
|
|
|
os.RemoveAll(path.Join(s.dataDir, "conf")) |
|
|
|
os.RemoveAll(path.Join(s.dataDir, "log")) |
|
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot")) |
|
|
|
} |
|
|
|
|
|
|
|
s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "") |
|
|
|
if err != nil { |
|
|
|
glog.V(0).Infoln(err) |
|
|
@ -53,35 +62,30 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
|
} |
|
|
|
transporter.Install(s.raftServer, s) |
|
|
|
s.raftServer.SetHeartbeatInterval(1 * time.Second) |
|
|
|
s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 1150 * time.Millisecond) |
|
|
|
s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 3450 * time.Millisecond) |
|
|
|
s.raftServer.Start() |
|
|
|
|
|
|
|
s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST") |
|
|
|
s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET") |
|
|
|
|
|
|
|
// Join to leader if specified.
|
|
|
|
if len(s.peers) > 0 { |
|
|
|
if !s.raftServer.IsLogEmpty() { |
|
|
|
glog.V(0).Infoln("Starting cluster with existing logs.") |
|
|
|
} else { |
|
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ",")) |
|
|
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) |
|
|
|
firstJoinError := s.Join(s.peers) |
|
|
|
if firstJoinError != nil { |
|
|
|
glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.") |
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{ |
|
|
|
Name: s.raftServer.Name(), |
|
|
|
ConnectionString: "http://" + s.httpAddr, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
glog.V(0).Infoln(err) |
|
|
|
return nil |
|
|
|
} |
|
|
|
// Join to leader if specified.
|
|
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ",")) |
|
|
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) |
|
|
|
firstJoinError := s.Join(s.peers) |
|
|
|
if firstJoinError != nil { |
|
|
|
glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.") |
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{ |
|
|
|
Name: s.raftServer.Name(), |
|
|
|
ConnectionString: "http://" + s.httpAddr, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
glog.V(0).Infoln(err) |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Initialize the server by joining itself.
|
|
|
|
} else if s.raftServer.IsLogEmpty() { |
|
|
|
// Initialize the server by joining itself.
|
|
|
|
glog.V(0).Infoln("Initializing new cluster") |
|
|
|
|
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{ |
|
|
@ -95,7 +99,7 @@ func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir strin |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
glog.V(0).Infoln("Recovered from log") |
|
|
|
glog.V(0).Infoln("Old conf,log,snapshot should have been removed.") |
|
|
|
} |
|
|
|
|
|
|
|
return s |
|
|
|