2013-12-09 21:34:05 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-05 08:25:23 +00:00
|
|
|
"io/ioutil"
|
2014-10-27 08:09:45 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-05-24 17:45:28 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2013-12-09 21:34:05 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-05-03 19:37:49 +00:00
|
|
|
"github.com/chrislusf/raft"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
2014-10-26 18:34:55 +00:00
|
|
|
"github.com/gorilla/mux"
|
2013-12-09 21:34:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RaftServer struct {
|
|
|
|
peers []string // initial peers to join with
|
|
|
|
raftServer raft.Server
|
|
|
|
dataDir string
|
|
|
|
httpAddr string
|
|
|
|
router *mux.Router
|
2014-03-16 06:03:49 +00:00
|
|
|
topo *topology.Topology
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 20:46:59 +00:00
|
|
|
func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
|
2013-12-09 21:34:05 +00:00
|
|
|
s := &RaftServer{
|
|
|
|
peers: peers,
|
|
|
|
httpAddr: httpAddr,
|
|
|
|
dataDir: dataDir,
|
|
|
|
router: r,
|
2014-03-16 06:03:49 +00:00
|
|
|
topo: topo,
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 09:11:28 +00:00
|
|
|
if glog.V(4) {
|
|
|
|
raft.SetLogLevel(2)
|
|
|
|
}
|
2013-12-09 21:34:05 +00:00
|
|
|
|
2014-03-16 06:03:49 +00:00
|
|
|
raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
|
|
|
|
|
2013-12-09 21:34:05 +00:00
|
|
|
var err error
|
2019-01-18 07:38:33 +00:00
|
|
|
transporter := raft.NewHTTPTransporter("/cluster", time.Second)
|
2014-03-16 20:49:49 +00:00
|
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024
|
2019-01-22 17:25:25 +00:00
|
|
|
transporter.Transport.IdleConnTimeout = time.Second
|
2019-01-26 06:15:42 +00:00
|
|
|
transporter.Transport.ResponseHeaderTimeout = time.Second
|
2018-06-12 08:54:09 +00:00
|
|
|
glog.V(0).Infof("Starting RaftServer with %v", httpAddr)
|
2014-03-16 20:49:49 +00:00
|
|
|
|
2016-05-24 17:45:28 +00:00
|
|
|
// 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)
|
2014-10-27 08:09:45 +00:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "conf"))
|
2017-12-06 08:14:14 +00:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "log"))
|
2014-10-27 08:09:45 +00:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot"))
|
|
|
|
}
|
|
|
|
|
2014-03-16 06:03:49 +00:00
|
|
|
s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
|
2013-12-09 21:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
transporter.Install(s.raftServer, s)
|
2017-01-18 17:54:43 +00:00
|
|
|
s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
|
|
|
|
s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
|
2013-12-09 21:34:05 +00:00
|
|
|
s.raftServer.Start()
|
|
|
|
|
2014-02-04 09:11:28 +00:00
|
|
|
s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
|
2013-12-09 21:34:05 +00:00
|
|
|
|
2018-06-12 08:54:09 +00:00
|
|
|
for _, peer := range s.peers {
|
|
|
|
s.raftServer.AddPeer(peer, "http://"+peer)
|
|
|
|
}
|
2019-02-09 20:52:09 +00:00
|
|
|
|
|
|
|
if s.raftServer.IsLogEmpty() && isTheFirstOne(httpAddr, s.peers) {
|
2014-10-27 08:09:45 +00:00
|
|
|
// Initialize the server by joining itself.
|
2013-12-09 21:34:05 +00:00
|
|
|
glog.V(0).Infoln("Initializing 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 08:54:09 +00:00
|
|
|
glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
|
|
|
|
|
2013-12-09 21:34:05 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-02-05 08:25:23 +00:00
|
|
|
func (s *RaftServer) Peers() (members []string) {
|
2013-12-09 21:34:05 +00:00
|
|
|
peers := s.raftServer.Peers()
|
|
|
|
|
|
|
|
for _, p := range peers {
|
|
|
|
members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-24 17:45:28 +00:00
|
|
|
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)
|
|
|
|
|
2017-12-06 08:14:14 +00:00
|
|
|
if len(peers) == 0 && len(oldPeers) <= 1 {
|
|
|
|
return oldPeers, false
|
|
|
|
}
|
|
|
|
|
2016-05-24 17:45:28 +00:00
|
|
|
sort.Strings(peers)
|
|
|
|
sort.Strings(oldPeers)
|
|
|
|
|
2016-11-03 07:46:59 +00:00
|
|
|
return oldPeers, !reflect.DeepEqual(peers, oldPeers)
|
2016-05-24 17:45:28 +00:00
|
|
|
|
|
|
|
}
|
2019-02-09 20:52:09 +00:00
|
|
|
|
|
|
|
func isTheFirstOne(self string, peers []string) bool {
|
|
|
|
sort.Strings(peers)
|
2019-02-15 08:09:48 +00:00
|
|
|
if len(peers) <= 0 {
|
2019-02-09 20:52:09 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return self == peers[0]
|
|
|
|
}
|