raft_server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "os"
  6. "path"
  7. "sort"
  8. "strings"
  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 []pb.ServerAddress // initial peers to join with
  18. raftServer raft.Server
  19. dataDir string
  20. serverAddr pb.ServerAddress
  21. topo *topology.Topology
  22. *raft.GrpcServer
  23. }
  24. type StateMachine struct {
  25. raft.StateMachine
  26. topo *topology.Topology
  27. }
  28. func (s StateMachine) Save() ([]byte, error) {
  29. state := topology.MaxVolumeIdCommand{
  30. MaxVolumeId: s.topo.GetMaxVolumeId(),
  31. }
  32. glog.V(1).Infof("Save raft state %+v", state)
  33. return json.Marshal(state)
  34. }
  35. func (s StateMachine) Recovery(data []byte) error {
  36. state := topology.MaxVolumeIdCommand{}
  37. err := json.Unmarshal(data, &state)
  38. if err != nil {
  39. return err
  40. }
  41. glog.V(1).Infof("Recovery raft state %+v", state)
  42. s.topo.UpAdjustMaxVolumeId(state.MaxVolumeId)
  43. return nil
  44. }
  45. func NewRaftServer(grpcDialOption grpc.DialOption, peers []pb.ServerAddress, serverAddr pb.ServerAddress, dataDir string, topo *topology.Topology, raftResumeState bool) (*RaftServer, error) {
  46. s := &RaftServer{
  47. peers: peers,
  48. serverAddr: serverAddr,
  49. dataDir: dataDir,
  50. topo: topo,
  51. }
  52. if glog.V(4) {
  53. raft.SetLogLevel(2)
  54. }
  55. raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
  56. var err error
  57. transporter := raft.NewGrpcTransporter(grpcDialOption)
  58. glog.V(0).Infof("Starting RaftServer with %v", serverAddr)
  59. if !raftResumeState {
  60. // always clear previous metadata
  61. os.RemoveAll(path.Join(s.dataDir, "conf"))
  62. os.RemoveAll(path.Join(s.dataDir, "log"))
  63. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  64. }
  65. if err := os.MkdirAll(path.Join(s.dataDir, "snapshot"), 0600); err != nil {
  66. return nil, err
  67. }
  68. stateMachine := StateMachine{topo: topo}
  69. s.raftServer, err = raft.NewServer(string(s.serverAddr), s.dataDir, transporter, stateMachine, topo, "")
  70. if err != nil {
  71. glog.V(0).Infoln(err)
  72. return nil, err
  73. }
  74. s.raftServer.SetHeartbeatInterval(time.Duration(300+rand.Intn(150)) * time.Millisecond)
  75. s.raftServer.SetElectionTimeout(10 * time.Second)
  76. if err := s.raftServer.LoadSnapshot(); err != nil {
  77. return nil, err
  78. }
  79. if err := s.raftServer.Start(); err != nil {
  80. return nil, err
  81. }
  82. for _, peer := range s.peers {
  83. if err := s.raftServer.AddPeer(string(peer), peer.ToGrpcAddress()); err != nil {
  84. return nil, err
  85. }
  86. }
  87. // Remove deleted peers
  88. for existsPeerName := range s.raftServer.Peers() {
  89. exists := false
  90. var existingPeer pb.ServerAddress
  91. for _, peer := range s.peers {
  92. if peer.ToGrpcAddress() == existsPeerName {
  93. exists, existingPeer = true, peer
  94. break
  95. }
  96. }
  97. if exists {
  98. if err := s.raftServer.RemovePeer(existsPeerName); err != nil {
  99. glog.V(0).Infoln(err)
  100. return nil, err
  101. } else {
  102. glog.V(0).Infof("removing old peer %s", existingPeer)
  103. }
  104. }
  105. }
  106. s.GrpcServer = raft.NewGrpcServer(s.raftServer)
  107. if s.raftServer.IsLogEmpty() && isTheFirstOne(serverAddr, s.peers) {
  108. // Initialize the server by joining itself.
  109. // s.DoJoinCommand()
  110. }
  111. glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
  112. return s, nil
  113. }
  114. func (s *RaftServer) Peers() (members []string) {
  115. peers := s.raftServer.Peers()
  116. for _, p := range peers {
  117. members = append(members, p.Name)
  118. }
  119. return
  120. }
  121. func isTheFirstOne(self pb.ServerAddress, peers []pb.ServerAddress) bool {
  122. sort.Slice(peers, func(i, j int) bool {
  123. return strings.Compare(string(peers[i]), string(peers[j])) < 0
  124. })
  125. if len(peers) <= 0 {
  126. return true
  127. }
  128. return self == peers[0]
  129. }
  130. func (s *RaftServer) DoJoinCommand() {
  131. glog.V(0).Infoln("Initializing new cluster")
  132. if _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  133. Name: s.raftServer.Name(),
  134. ConnectionString: s.serverAddr.ToGrpcAddress(),
  135. }); err != nil {
  136. glog.Errorf("fail to send join command: %v", err)
  137. }
  138. }