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.

185 lines
4.7 KiB

11 years ago
11 years ago
11 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "path"
  13. "strings"
  14. "time"
  15. "github.com/chrislusf/weed-fs/go/glog"
  16. "github.com/chrislusf/weed-fs/go/topology"
  17. "github.com/goraft/raft"
  18. "github.com/gorilla/mux"
  19. )
  20. type RaftServer struct {
  21. peers []string // initial peers to join with
  22. raftServer raft.Server
  23. dataDir string
  24. httpAddr string
  25. router *mux.Router
  26. topo *topology.Topology
  27. }
  28. func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
  29. s := &RaftServer{
  30. peers: peers,
  31. httpAddr: httpAddr,
  32. dataDir: dataDir,
  33. router: r,
  34. topo: topo,
  35. }
  36. if glog.V(4) {
  37. raft.SetLogLevel(2)
  38. }
  39. raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
  40. var err error
  41. transporter := raft.NewHTTPTransporter("/cluster", 0)
  42. transporter.Transport.MaxIdleConnsPerHost = 1024
  43. glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr)
  44. // Clear old cluster configurations if peers are set
  45. if len(s.peers) > 0 {
  46. os.RemoveAll(path.Join(s.dataDir, "conf"))
  47. os.RemoveAll(path.Join(s.dataDir, "log"))
  48. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  49. }
  50. s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
  51. if err != nil {
  52. glog.V(0).Infoln(err)
  53. return nil
  54. }
  55. transporter.Install(s.raftServer, s)
  56. s.raftServer.SetHeartbeatInterval(1 * time.Second)
  57. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 3450 * time.Millisecond)
  58. s.raftServer.Start()
  59. s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
  60. s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
  61. if len(s.peers) > 0 {
  62. // Join to leader if specified.
  63. glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
  64. time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
  65. firstJoinError := s.Join(s.peers)
  66. if firstJoinError != nil {
  67. glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.")
  68. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  69. Name: s.raftServer.Name(),
  70. ConnectionString: "http://" + s.httpAddr,
  71. })
  72. if err != nil {
  73. glog.V(0).Infoln(err)
  74. return nil
  75. }
  76. }
  77. } else if s.raftServer.IsLogEmpty() {
  78. // Initialize the server by joining itself.
  79. glog.V(0).Infoln("Initializing new cluster")
  80. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  81. Name: s.raftServer.Name(),
  82. ConnectionString: "http://" + s.httpAddr,
  83. })
  84. if err != nil {
  85. glog.V(0).Infoln(err)
  86. return nil
  87. }
  88. } else {
  89. glog.V(0).Infoln("Old conf,log,snapshot should have been removed.")
  90. }
  91. return s
  92. }
  93. func (s *RaftServer) Peers() (members []string) {
  94. peers := s.raftServer.Peers()
  95. for _, p := range peers {
  96. members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
  97. }
  98. return
  99. }
  100. // Join joins an existing cluster.
  101. func (s *RaftServer) Join(peers []string) error {
  102. command := &raft.DefaultJoinCommand{
  103. Name: s.raftServer.Name(),
  104. ConnectionString: "http://" + s.httpAddr,
  105. }
  106. var err error
  107. var b bytes.Buffer
  108. json.NewEncoder(&b).Encode(command)
  109. for _, m := range peers {
  110. if m == s.httpAddr {
  111. continue
  112. }
  113. target := fmt.Sprintf("http://%s/cluster/join", strings.TrimSpace(m))
  114. glog.V(0).Infoln("Attempting to connect to:", target)
  115. err = postFollowingOneRedirect(target, "application/json", &b)
  116. if err != nil {
  117. glog.V(0).Infoln("Post returned error: ", err.Error())
  118. if _, ok := err.(*url.Error); ok {
  119. // If we receive a network error try the next member
  120. continue
  121. }
  122. } else {
  123. return nil
  124. }
  125. }
  126. return errors.New("Could not connect to any cluster peers")
  127. }
  128. // a workaround because http POST following redirection misses request body
  129. func postFollowingOneRedirect(target string, contentType string, b *bytes.Buffer) error {
  130. backupReader := bytes.NewReader(b.Bytes())
  131. resp, err := http.Post(target, contentType, b)
  132. if err != nil {
  133. return err
  134. }
  135. defer resp.Body.Close()
  136. reply, _ := ioutil.ReadAll(resp.Body)
  137. statusCode := resp.StatusCode
  138. if statusCode == http.StatusMovedPermanently {
  139. var urlStr string
  140. if urlStr = resp.Header.Get("Location"); urlStr == "" {
  141. return fmt.Errorf("%d response missing Location header", resp.StatusCode)
  142. }
  143. glog.V(0).Infoln("Post redirected to ", urlStr)
  144. resp2, err2 := http.Post(urlStr, contentType, backupReader)
  145. if err2 != nil {
  146. return err2
  147. }
  148. defer resp2.Body.Close()
  149. reply, _ = ioutil.ReadAll(resp2.Body)
  150. statusCode = resp2.StatusCode
  151. }
  152. glog.V(0).Infoln("Post returned status: ", statusCode, string(reply))
  153. if statusCode != http.StatusOK {
  154. return errors.New(string(reply))
  155. }
  156. return nil
  157. }