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.

179 lines
4.5 KiB

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