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.

215 lines
5.3 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. "reflect"
  14. "sort"
  15. "strings"
  16. "time"
  17. "github.com/chrislusf/raft"
  18. "github.com/chrislusf/seaweedfs/weed/glog"
  19. "github.com/chrislusf/seaweedfs/weed/topology"
  20. "github.com/gorilla/mux"
  21. )
  22. type RaftServer struct {
  23. peers []string // initial peers to join with
  24. raftServer raft.Server
  25. dataDir string
  26. httpAddr string
  27. router *mux.Router
  28. topo *topology.Topology
  29. }
  30. func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
  31. s := &RaftServer{
  32. peers: peers,
  33. httpAddr: httpAddr,
  34. dataDir: dataDir,
  35. router: r,
  36. topo: topo,
  37. }
  38. if glog.V(4) {
  39. raft.SetLogLevel(2)
  40. }
  41. raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
  42. var err error
  43. transporter := raft.NewHTTPTransporter("/cluster", 0)
  44. transporter.Transport.MaxIdleConnsPerHost = 1024
  45. glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr)
  46. // Clear old cluster configurations if peers are changed
  47. if oldPeers, changed := isPeersChanged(s.dataDir, httpAddr, s.peers); changed {
  48. glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
  49. os.RemoveAll(path.Join(s.dataDir, "conf"))
  50. os.RemoveAll(path.Join(s.dataDir, "log"))
  51. os.RemoveAll(path.Join(s.dataDir, "snapshot"))
  52. }
  53. s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
  54. if err != nil {
  55. glog.V(0).Infoln(err)
  56. return nil
  57. }
  58. transporter.Install(s.raftServer, s)
  59. s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
  60. s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
  61. s.raftServer.Start()
  62. s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
  63. s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
  64. if len(s.peers) > 0 {
  65. // Join to leader if specified.
  66. for {
  67. glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
  68. time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
  69. firstJoinError := s.Join(s.peers)
  70. if firstJoinError != nil {
  71. glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.")
  72. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  73. Name: s.raftServer.Name(),
  74. ConnectionString: "http://" + s.httpAddr,
  75. })
  76. if err != nil {
  77. glog.V(0).Infoln(err)
  78. } else {
  79. break
  80. }
  81. } else {
  82. break
  83. }
  84. }
  85. } else if s.raftServer.IsLogEmpty() {
  86. // Initialize the server by joining itself.
  87. glog.V(0).Infoln("Initializing new cluster")
  88. _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
  89. Name: s.raftServer.Name(),
  90. ConnectionString: "http://" + s.httpAddr,
  91. })
  92. if err != nil {
  93. glog.V(0).Infoln(err)
  94. return nil
  95. }
  96. } else {
  97. glog.V(0).Infoln("Old conf,log,snapshot should have been removed.")
  98. }
  99. return s
  100. }
  101. func (s *RaftServer) Peers() (members []string) {
  102. peers := s.raftServer.Peers()
  103. for _, p := range peers {
  104. members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
  105. }
  106. return
  107. }
  108. func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
  109. confPath := path.Join(dir, "conf")
  110. // open conf file
  111. b, err := ioutil.ReadFile(confPath)
  112. if err != nil {
  113. return oldPeers, true
  114. }
  115. conf := &raft.Config{}
  116. if err = json.Unmarshal(b, conf); err != nil {
  117. return oldPeers, true
  118. }
  119. for _, p := range conf.Peers {
  120. oldPeers = append(oldPeers, strings.TrimPrefix(p.ConnectionString, "http://"))
  121. }
  122. oldPeers = append(oldPeers, self)
  123. sort.Strings(peers)
  124. sort.Strings(oldPeers)
  125. return oldPeers, !reflect.DeepEqual(peers, oldPeers)
  126. }
  127. // Join joins an existing cluster.
  128. func (s *RaftServer) Join(peers []string) error {
  129. command := &raft.DefaultJoinCommand{
  130. Name: s.raftServer.Name(),
  131. ConnectionString: "http://" + s.httpAddr,
  132. }
  133. var err error
  134. var b bytes.Buffer
  135. json.NewEncoder(&b).Encode(command)
  136. for _, m := range peers {
  137. if m == s.httpAddr {
  138. continue
  139. }
  140. target := fmt.Sprintf("http://%s/cluster/join", strings.TrimSpace(m))
  141. glog.V(0).Infoln("Attempting to connect to:", target)
  142. err = postFollowingOneRedirect(target, "application/json", b)
  143. if err != nil {
  144. glog.V(0).Infoln("Post returned error: ", err.Error())
  145. if _, ok := err.(*url.Error); ok {
  146. // If we receive a network error try the next member
  147. continue
  148. }
  149. } else {
  150. return nil
  151. }
  152. }
  153. return errors.New("Could not connect to any cluster peers")
  154. }
  155. // a workaround because http POST following redirection misses request body
  156. func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer) error {
  157. backupReader := bytes.NewReader(b.Bytes())
  158. resp, err := http.Post(target, contentType, &b)
  159. if err != nil {
  160. return err
  161. }
  162. defer resp.Body.Close()
  163. statusCode := resp.StatusCode
  164. data, _ := ioutil.ReadAll(resp.Body)
  165. reply := string(data)
  166. if strings.HasPrefix(reply, "\"http") {
  167. urlStr := reply[1 : len(reply)-1]
  168. glog.V(0).Infoln("Post redirected to ", urlStr)
  169. resp2, err2 := http.Post(urlStr, contentType, backupReader)
  170. if err2 != nil {
  171. return err2
  172. }
  173. defer resp2.Body.Close()
  174. data, _ = ioutil.ReadAll(resp2.Body)
  175. statusCode = resp2.StatusCode
  176. }
  177. glog.V(0).Infoln("Post returned status: ", statusCode, string(data))
  178. if statusCode != http.StatusOK {
  179. return errors.New(string(data))
  180. }
  181. return nil
  182. }