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.

128 lines
3.1 KiB

11 years ago
11 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", 0)
  38. transporter.Transport.MaxIdleConnsPerHost = 1024
  39. glog.V(0).Infof("Starting RaftServer with %v", httpAddr)
  40. // Clear old cluster configurations if peers are changed
  41. if oldPeers, changed := isPeersChanged(s.dataDir, httpAddr, s.peers); changed {
  42. glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
  43. os.RemoveAll(path.Join(s.dataDir, "conf"))
  44. os.RemoveAll(path.Join(s.dataDir, "log"))
  45. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  46. }
  47. s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
  48. if err != nil {
  49. glog.V(0).Infoln(err)
  50. return nil
  51. }
  52. transporter.Install(s.raftServer, s)
  53. s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
  54. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
  55. s.raftServer.Start()
  56. s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
  57. for _, peer := range s.peers {
  58. s.raftServer.AddPeer(peer, "http://"+peer)
  59. }
  60. time.Sleep(2 * time.Second)
  61. if s.raftServer.IsLogEmpty() {
  62. // Initialize the server by joining itself.
  63. glog.V(0).Infoln("Initializing new cluster")
  64. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  65. Name: s.raftServer.Name(),
  66. ConnectionString: "http://" + s.httpAddr,
  67. })
  68. if err != nil {
  69. glog.V(0).Infoln(err)
  70. return nil
  71. }
  72. }
  73. glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
  74. return s
  75. }
  76. func (s *RaftServer) Peers() (members []string) {
  77. peers := s.raftServer.Peers()
  78. for _, p := range peers {
  79. members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
  80. }
  81. return
  82. }
  83. func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
  84. confPath := path.Join(dir, "conf")
  85. // open conf file
  86. b, err := ioutil.ReadFile(confPath)
  87. if err != nil {
  88. return oldPeers, true
  89. }
  90. conf := &raft.Config{}
  91. if err = json.Unmarshal(b, conf); err != nil {
  92. return oldPeers, true
  93. }
  94. for _, p := range conf.Peers {
  95. oldPeers = append(oldPeers, strings.TrimPrefix(p.ConnectionString, "http://"))
  96. }
  97. oldPeers = append(oldPeers, self)
  98. if len(peers) == 0 && len(oldPeers) <= 1 {
  99. return oldPeers, false
  100. }
  101. sort.Strings(peers)
  102. sort.Strings(oldPeers)
  103. return oldPeers, !reflect.DeepEqual(peers, oldPeers)
  104. }