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.

129 lines
3.1 KiB

11 years ago
11 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "math/rand"
  6. "os"
  7. "path"
  8. "reflect"
  9. "sort"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/raft"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. "github.com/gorilla/mux"
  16. )
  17. type RaftServer struct {
  18. peers []string // initial peers to join with
  19. raftServer raft.Server
  20. dataDir string
  21. httpAddr string
  22. router *mux.Router
  23. topo *topology.Topology
  24. }
  25. func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
  26. s := &RaftServer{
  27. peers: peers,
  28. httpAddr: httpAddr,
  29. dataDir: dataDir,
  30. router: r,
  31. topo: topo,
  32. }
  33. if glog.V(4) {
  34. raft.SetLogLevel(2)
  35. }
  36. raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
  37. var err error
  38. transporter := raft.NewHTTPTransporter("/cluster", 0)
  39. transporter.Transport.MaxIdleConnsPerHost = 1024
  40. glog.V(0).Infof("Starting RaftServer with %v", httpAddr)
  41. // Clear old cluster configurations if peers are changed
  42. if oldPeers, changed := isPeersChanged(s.dataDir, httpAddr, s.peers); changed {
  43. glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
  44. os.RemoveAll(path.Join(s.dataDir, "conf"))
  45. os.RemoveAll(path.Join(s.dataDir, "log"))
  46. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  47. }
  48. s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
  49. if err != nil {
  50. glog.V(0).Infoln(err)
  51. return nil
  52. }
  53. transporter.Install(s.raftServer, s)
  54. s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
  55. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
  56. s.raftServer.Start()
  57. s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
  58. for _, peer := range s.peers {
  59. s.raftServer.AddPeer(peer, "http://"+peer)
  60. }
  61. time.Sleep(time.Duration(1000+rand.Int31n(3000)) * time.Millisecond)
  62. if s.raftServer.IsLogEmpty() {
  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. }