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.

138 lines
3.3 KiB

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