2013-12-09 21:34:05 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-05 08:25:23 +00:00
|
|
|
"io/ioutil"
|
2018-08-12 21:27:14 +00:00
|
|
|
"math/rand"
|
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
|
2014-04-16 00:56:47 +00:00
|
|
|
transporter := raft.NewHTTPTransporter("/cluster", 0)
|
2014-03-16 20:49:49 +00:00
|
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024
|
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)
|
|
|
|
}
|
2018-08-12 21:27:14 +00:00
|
|
|
time.Sleep(time.Duration(1000+rand.Int31n(3000)) * time.Millisecond)
|
2018-06-12 08:54:09 +00:00
|
|
|
if s.raftServer.IsLogEmpty() {
|
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
|
|
|
|
|
|
|
}
|