2013-12-10 05:34:05 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-05 16:25:23 +08:00
|
|
|
"io/ioutil"
|
2014-10-27 16:09:45 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-05-25 01:45:28 +08:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2013-12-10 05:34:05 +08:00
|
|
|
"time"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2020-03-04 16:39:47 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
|
|
|
|
2015-05-04 03:37:49 +08:00
|
|
|
"github.com/chrislusf/raft"
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
2013-12-10 05:34:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RaftServer struct {
|
|
|
|
peers []string // initial peers to join with
|
|
|
|
raftServer raft.Server
|
|
|
|
dataDir string
|
2019-02-19 14:38:14 +08:00
|
|
|
serverAddr string
|
2014-03-16 14:03:49 +08:00
|
|
|
topo *topology.Topology
|
2019-02-19 14:38:14 +08:00
|
|
|
*raft.GrpcServer
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-29 21:28:28 +08:00
|
|
|
func NewRaftServer(grpcDialOption grpc.DialOption, peers []string, serverAddr, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
|
2013-12-10 05:34:05 +08:00
|
|
|
s := &RaftServer{
|
2019-02-19 14:38:14 +08:00
|
|
|
peers: peers,
|
|
|
|
serverAddr: serverAddr,
|
|
|
|
dataDir: dataDir,
|
|
|
|
topo: topo,
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 17:11:28 +08:00
|
|
|
if glog.V(4) {
|
|
|
|
raft.SetLogLevel(2)
|
|
|
|
}
|
2013-12-10 05:34:05 +08:00
|
|
|
|
2014-03-16 14:03:49 +08:00
|
|
|
raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
|
|
|
|
|
2013-12-10 05:34:05 +08:00
|
|
|
var err error
|
2019-02-19 14:38:14 +08:00
|
|
|
transporter := raft.NewGrpcTransporter(grpcDialOption)
|
|
|
|
glog.V(0).Infof("Starting RaftServer with %v", serverAddr)
|
2014-03-17 04:49:49 +08:00
|
|
|
|
2016-05-25 01:45:28 +08:00
|
|
|
// Clear old cluster configurations if peers are changed
|
2019-02-19 14:38:14 +08:00
|
|
|
if oldPeers, changed := isPeersChanged(s.dataDir, serverAddr, s.peers); changed {
|
2016-05-25 01:45:28 +08:00
|
|
|
glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
|
2014-10-27 16:09:45 +08:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "conf"))
|
2017-12-06 16:14:14 +08:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "log"))
|
2014-10-27 16:09:45 +08:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot"))
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:38:14 +08:00
|
|
|
s.raftServer, err = raft.NewServer(s.serverAddr, s.dataDir, transporter, nil, topo, "")
|
2013-12-10 05:34:05 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-19 01:54:43 +08:00
|
|
|
s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
|
|
|
|
s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
|
2013-12-10 05:34:05 +08:00
|
|
|
s.raftServer.Start()
|
|
|
|
|
2018-06-12 16:54:09 +08:00
|
|
|
for _, peer := range s.peers {
|
2020-03-04 16:39:47 +08:00
|
|
|
s.raftServer.AddPeer(peer, pb.ServerToGrpcAddress(peer))
|
2018-06-12 16:54:09 +08:00
|
|
|
}
|
2019-02-10 04:52:09 +08:00
|
|
|
|
2019-02-19 14:38:14 +08:00
|
|
|
s.GrpcServer = raft.NewGrpcServer(s.raftServer)
|
|
|
|
|
|
|
|
if s.raftServer.IsLogEmpty() && isTheFirstOne(serverAddr, s.peers) {
|
2014-10-27 16:09:45 +08:00
|
|
|
// Initialize the server by joining itself.
|
2013-12-10 05:34:05 +08:00
|
|
|
glog.V(0).Infoln("Initializing new cluster")
|
|
|
|
|
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
2020-03-04 16:39:47 +08:00
|
|
|
ConnectionString: pb.ServerToGrpcAddress(s.serverAddr),
|
2013-12-10 05:34:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:54:09 +08:00
|
|
|
glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
|
|
|
|
|
2013-12-10 05:34:05 +08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:25:23 +08:00
|
|
|
func (s *RaftServer) Peers() (members []string) {
|
2013-12-10 05:34:05 +08:00
|
|
|
peers := s.raftServer.Peers()
|
|
|
|
|
|
|
|
for _, p := range peers {
|
2019-02-19 14:38:14 +08:00
|
|
|
members = append(members, p.Name)
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:45:28 +08: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 {
|
2019-02-19 14:38:14 +08:00
|
|
|
oldPeers = append(oldPeers, p.Name)
|
2016-05-25 01:45:28 +08:00
|
|
|
}
|
|
|
|
oldPeers = append(oldPeers, self)
|
|
|
|
|
2017-12-06 16:14:14 +08:00
|
|
|
if len(peers) == 0 && len(oldPeers) <= 1 {
|
|
|
|
return oldPeers, false
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:45:28 +08:00
|
|
|
sort.Strings(peers)
|
|
|
|
sort.Strings(oldPeers)
|
|
|
|
|
2016-11-03 15:46:59 +08:00
|
|
|
return oldPeers, !reflect.DeepEqual(peers, oldPeers)
|
2016-05-25 01:45:28 +08:00
|
|
|
|
|
|
|
}
|
2019-02-10 04:52:09 +08:00
|
|
|
|
|
|
|
func isTheFirstOne(self string, peers []string) bool {
|
|
|
|
sort.Strings(peers)
|
2019-02-15 16:09:48 +08:00
|
|
|
if len(peers) <= 0 {
|
2019-02-10 04:52:09 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return self == peers[0]
|
|
|
|
}
|