raft_server.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "reflect"
  8. "sort"
  9. "time"
  10. "google.golang.org/grpc"
  11. "github.com/chrislusf/seaweedfs/weed/pb"
  12. "github.com/chrislusf/raft"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. )
  16. type RaftServer struct {
  17. peers []string // initial peers to join with
  18. raftServer raft.Server
  19. dataDir string
  20. serverAddr string
  21. topo *topology.Topology
  22. *raft.GrpcServer
  23. }
  24. func NewRaftServer(grpcDialOption grpc.DialOption, peers []string, serverAddr, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
  25. s := &RaftServer{
  26. peers: peers,
  27. serverAddr: serverAddr,
  28. dataDir: dataDir,
  29. topo: topo,
  30. }
  31. if glog.V(4) {
  32. raft.SetLogLevel(2)
  33. }
  34. raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
  35. var err error
  36. transporter := raft.NewGrpcTransporter(grpcDialOption)
  37. glog.V(0).Infof("Starting RaftServer with %v", serverAddr)
  38. // always clear previous metadata
  39. os.RemoveAll(path.Join(s.dataDir, "conf"))
  40. os.RemoveAll(path.Join(s.dataDir, "log"))
  41. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  42. // Clear old cluster configurations if peers are changed
  43. if oldPeers, changed := isPeersChanged(s.dataDir, serverAddr, s.peers); changed {
  44. glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
  45. }
  46. s.raftServer, err = raft.NewServer(s.serverAddr, s.dataDir, transporter, nil, topo, "")
  47. if err != nil {
  48. glog.V(0).Infoln(err)
  49. return nil
  50. }
  51. s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
  52. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
  53. s.raftServer.Start()
  54. for _, peer := range s.peers {
  55. s.raftServer.AddPeer(peer, pb.ServerToGrpcAddress(peer))
  56. }
  57. s.GrpcServer = raft.NewGrpcServer(s.raftServer)
  58. if s.raftServer.IsLogEmpty() && isTheFirstOne(serverAddr, s.peers) {
  59. // Initialize the server by joining itself.
  60. glog.V(0).Infoln("Initializing new cluster")
  61. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  62. Name: s.raftServer.Name(),
  63. ConnectionString: pb.ServerToGrpcAddress(s.serverAddr),
  64. })
  65. if err != nil {
  66. glog.V(0).Infoln(err)
  67. return nil
  68. }
  69. }
  70. glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
  71. return s
  72. }
  73. func (s *RaftServer) Peers() (members []string) {
  74. peers := s.raftServer.Peers()
  75. for _, p := range peers {
  76. members = append(members, p.Name)
  77. }
  78. return
  79. }
  80. func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
  81. confPath := path.Join(dir, "conf")
  82. // open conf file
  83. b, err := ioutil.ReadFile(confPath)
  84. if err != nil {
  85. return oldPeers, true
  86. }
  87. conf := &raft.Config{}
  88. if err = json.Unmarshal(b, conf); err != nil {
  89. return oldPeers, true
  90. }
  91. for _, p := range conf.Peers {
  92. oldPeers = append(oldPeers, p.Name)
  93. }
  94. oldPeers = append(oldPeers, self)
  95. if len(peers) == 0 && len(oldPeers) <= 1 {
  96. return oldPeers, false
  97. }
  98. sort.Strings(peers)
  99. sort.Strings(oldPeers)
  100. return oldPeers, !reflect.DeepEqual(peers, oldPeers)
  101. }
  102. func isTheFirstOne(self string, peers []string) bool {
  103. sort.Strings(peers)
  104. if len(peers) <= 0 {
  105. return true
  106. }
  107. return self == peers[0]
  108. }