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.

130 lines
3.2 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", time.Second)
  39. transporter.Transport.MaxIdleConnsPerHost = 1024
  40. transporter.Transport.IdleConnTimeout = 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. time.Sleep(time.Duration(1000+rand.Int31n(3000)) * time.Millisecond)
  63. if s.raftServer.IsLogEmpty() {
  64. // Initialize the server by joining itself.
  65. glog.V(0).Infoln("Initializing new cluster")
  66. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  67. Name: s.raftServer.Name(),
  68. ConnectionString: "http://" + s.httpAddr,
  69. })
  70. if err != nil {
  71. glog.V(0).Infoln(err)
  72. return nil
  73. }
  74. }
  75. glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
  76. return s
  77. }
  78. func (s *RaftServer) Peers() (members []string) {
  79. peers := s.raftServer.Peers()
  80. for _, p := range peers {
  81. members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
  82. }
  83. return
  84. }
  85. func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
  86. confPath := path.Join(dir, "conf")
  87. // open conf file
  88. b, err := ioutil.ReadFile(confPath)
  89. if err != nil {
  90. return oldPeers, true
  91. }
  92. conf := &raft.Config{}
  93. if err = json.Unmarshal(b, conf); err != nil {
  94. return oldPeers, true
  95. }
  96. for _, p := range conf.Peers {
  97. oldPeers = append(oldPeers, strings.TrimPrefix(p.ConnectionString, "http://"))
  98. }
  99. oldPeers = append(oldPeers, self)
  100. if len(peers) == 0 && len(oldPeers) <= 1 {
  101. return oldPeers, false
  102. }
  103. sort.Strings(peers)
  104. sort.Strings(oldPeers)
  105. return oldPeers, !reflect.DeepEqual(peers, oldPeers)
  106. }