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.

184 lines
4.6 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. // Clear old cluster configurations if peers are set
  44. if len(s.peers) > 0 {
  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(1 * time.Second)
  56. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 3450 * time.Millisecond)
  57. s.raftServer.Start()
  58. s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
  59. s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
  60. if len(s.peers) > 0 {
  61. // Join to leader if specified.
  62. glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
  63. time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
  64. firstJoinError := s.Join(s.peers)
  65. if firstJoinError != nil {
  66. glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.")
  67. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  68. Name: s.raftServer.Name(),
  69. ConnectionString: "http://" + s.httpAddr,
  70. })
  71. if err != nil {
  72. glog.V(0).Infoln(err)
  73. return nil
  74. }
  75. }
  76. } else if s.raftServer.IsLogEmpty() {
  77. // Initialize the server by joining itself.
  78. glog.V(0).Infoln("Initializing new cluster")
  79. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  80. Name: s.raftServer.Name(),
  81. ConnectionString: "http://" + s.httpAddr,
  82. })
  83. if err != nil {
  84. glog.V(0).Infoln(err)
  85. return nil
  86. }
  87. } else {
  88. glog.V(0).Infoln("Old conf,log,snapshot should have been removed.")
  89. }
  90. return s
  91. }
  92. func (s *RaftServer) Peers() (members []string) {
  93. peers := s.raftServer.Peers()
  94. for _, p := range peers {
  95. members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
  96. }
  97. return
  98. }
  99. // Join joins an existing cluster.
  100. func (s *RaftServer) Join(peers []string) error {
  101. command := &raft.DefaultJoinCommand{
  102. Name: s.raftServer.Name(),
  103. ConnectionString: "http://" + s.httpAddr,
  104. }
  105. var err error
  106. var b bytes.Buffer
  107. json.NewEncoder(&b).Encode(command)
  108. for _, m := range peers {
  109. if m == s.httpAddr {
  110. continue
  111. }
  112. target := fmt.Sprintf("http://%s/cluster/join", strings.TrimSpace(m))
  113. glog.V(0).Infoln("Attempting to connect to:", target)
  114. err = postFollowingOneRedirect(target, "application/json", &b)
  115. if err != nil {
  116. glog.V(0).Infoln("Post returned error: ", err.Error())
  117. if _, ok := err.(*url.Error); ok {
  118. // If we receive a network error try the next member
  119. continue
  120. }
  121. } else {
  122. return nil
  123. }
  124. }
  125. return errors.New("Could not connect to any cluster peers")
  126. }
  127. // a workaround because http POST following redirection misses request body
  128. func postFollowingOneRedirect(target string, contentType string, b *bytes.Buffer) error {
  129. backupReader := bytes.NewReader(b.Bytes())
  130. resp, err := http.Post(target, contentType, b)
  131. if err != nil {
  132. return err
  133. }
  134. defer resp.Body.Close()
  135. reply, _ := ioutil.ReadAll(resp.Body)
  136. statusCode := resp.StatusCode
  137. if statusCode == http.StatusMovedPermanently {
  138. var urlStr string
  139. if urlStr = resp.Header.Get("Location"); urlStr == "" {
  140. return errors.New(fmt.Sprintf("%d response missing Location header", resp.StatusCode))
  141. }
  142. glog.V(0).Infoln("Post redirected to ", urlStr)
  143. resp2, err2 := http.Post(urlStr, contentType, backupReader)
  144. if err2 != nil {
  145. return err2
  146. }
  147. defer resp2.Body.Close()
  148. reply, _ = ioutil.ReadAll(resp2.Body)
  149. statusCode = resp2.StatusCode
  150. }
  151. glog.V(0).Infoln("Post returned status: ", statusCode, string(reply))
  152. if statusCode != http.StatusOK {
  153. return errors.New(string(reply))
  154. }
  155. return nil
  156. }