You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.1 KiB

11 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "google.golang.org/grpc"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "reflect"
  10. "sort"
  11. "time"
  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. // Clear old cluster configurations if peers are changed
  39. if oldPeers, changed := isPeersChanged(s.dataDir, serverAddr, s.peers); changed {
  40. glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
  41. os.RemoveAll(path.Join(s.dataDir, "conf"))
  42. os.RemoveAll(path.Join(s.dataDir, "log"))
  43. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  44. }
  45. s.raftServer, err = raft.NewServer(s.serverAddr, s.dataDir, transporter, nil, topo, "")
  46. if err != nil {
  47. glog.V(0).Infoln(err)
  48. return nil
  49. }
  50. s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
  51. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
  52. s.raftServer.Start()
  53. for _, peer := range s.peers {
  54. s.raftServer.AddPeer(peer, util.ServerToGrpcAddress(peer))
  55. }
  56. s.GrpcServer = raft.NewGrpcServer(s.raftServer)
  57. if s.raftServer.IsLogEmpty() && isTheFirstOne(serverAddr, s.peers) {
  58. // Initialize the server by joining itself.
  59. glog.V(0).Infoln("Initializing new cluster")
  60. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  61. Name: s.raftServer.Name(),
  62. ConnectionString: util.ServerToGrpcAddress(s.serverAddr),
  63. })
  64. if err != nil {
  65. glog.V(0).Infoln(err)
  66. return nil
  67. }
  68. }
  69. glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
  70. return s
  71. }
  72. func (s *RaftServer) Peers() (members []string) {
  73. peers := s.raftServer.Peers()
  74. for _, p := range peers {
  75. members = append(members, p.Name)
  76. }
  77. return
  78. }
  79. func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
  80. confPath := path.Join(dir, "conf")
  81. // open conf file
  82. b, err := ioutil.ReadFile(confPath)
  83. if err != nil {
  84. return oldPeers, true
  85. }
  86. conf := &raft.Config{}
  87. if err = json.Unmarshal(b, conf); err != nil {
  88. return oldPeers, true
  89. }
  90. for _, p := range conf.Peers {
  91. oldPeers = append(oldPeers, p.Name)
  92. }
  93. oldPeers = append(oldPeers, self)
  94. if len(peers) == 0 && len(oldPeers) <= 1 {
  95. return oldPeers, false
  96. }
  97. sort.Strings(peers)
  98. sort.Strings(oldPeers)
  99. return oldPeers, !reflect.DeepEqual(peers, oldPeers)
  100. }
  101. func isTheFirstOne(self string, peers []string) bool {
  102. sort.Strings(peers)
  103. if len(peers) <= 0 {
  104. return true
  105. }
  106. return self == peers[0]
  107. }