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.

131 lines
4.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httputil"
  6. "net/url"
  7. "sync"
  8. "github.com/chrislusf/raft"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/sequence"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. "github.com/gorilla/mux"
  15. )
  16. type MasterServer struct {
  17. port int
  18. metaFolder string
  19. volumeSizeLimitMB uint
  20. pulseSeconds int
  21. defaultReplicaPlacement string
  22. garbageThreshold string
  23. guard *security.Guard
  24. Topo *topology.Topology
  25. vg *topology.VolumeGrowth
  26. vgLock sync.Mutex
  27. bounedLeaderChan chan int
  28. }
  29. func NewMasterServer(r *mux.Router, port int, metaFolder string,
  30. volumeSizeLimitMB uint,
  31. pulseSeconds int,
  32. confFile string,
  33. defaultReplicaPlacement string,
  34. garbageThreshold string,
  35. whiteList []string,
  36. secureKey string,
  37. ) *MasterServer {
  38. ms := &MasterServer{
  39. port: port,
  40. volumeSizeLimitMB: volumeSizeLimitMB,
  41. pulseSeconds: pulseSeconds,
  42. defaultReplicaPlacement: defaultReplicaPlacement,
  43. garbageThreshold: garbageThreshold,
  44. }
  45. ms.bounedLeaderChan = make(chan int, 16)
  46. seq := sequence.NewMemorySequencer()
  47. var e error
  48. if ms.Topo, e = topology.NewTopology("topo", confFile, seq,
  49. uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds); e != nil {
  50. glog.Fatalf("cannot create topology:%s", e)
  51. }
  52. ms.vg = topology.NewDefaultVolumeGrowth()
  53. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  54. ms.guard = security.NewGuard(whiteList, secureKey)
  55. r.HandleFunc("/", ms.uiStatusHandler)
  56. r.HandleFunc("/ui/index.html", ms.uiStatusHandler)
  57. r.HandleFunc("/dir/assign", ms.proxyToLeader(ms.guard.WhiteList(ms.dirAssignHandler)))
  58. r.HandleFunc("/dir/lookup", ms.proxyToLeader(ms.guard.WhiteList(ms.dirLookupHandler)))
  59. r.HandleFunc("/dir/join", ms.proxyToLeader(ms.guard.WhiteList(ms.dirJoinHandler)))
  60. r.HandleFunc("/dir/status", ms.proxyToLeader(ms.guard.WhiteList(ms.dirStatusHandler)))
  61. r.HandleFunc("/col/delete", ms.proxyToLeader(ms.guard.WhiteList(ms.collectionDeleteHandler)))
  62. r.HandleFunc("/vol/lookup", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeLookupHandler)))
  63. r.HandleFunc("/vol/grow", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeGrowHandler)))
  64. r.HandleFunc("/vol/status", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeStatusHandler)))
  65. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(ms.guard.WhiteList(ms.volumeVacuumHandler)))
  66. r.HandleFunc("/submit", ms.guard.WhiteList(ms.submitFromMasterServerHandler))
  67. r.HandleFunc("/delete", ms.guard.WhiteList(ms.deleteFromMasterServerHandler))
  68. r.HandleFunc("/{fileId}", ms.proxyToLeader(ms.redirectHandler))
  69. r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler))
  70. r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler))
  71. ms.Topo.StartRefreshWritableVolumes(garbageThreshold)
  72. return ms
  73. }
  74. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  75. ms.Topo.RaftServer = raftServer.raftServer
  76. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  77. if ms.Topo.RaftServer.Leader() != "" {
  78. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  79. }
  80. })
  81. if ms.Topo.IsLeader() {
  82. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  83. } else {
  84. if ms.Topo.RaftServer.Leader() != "" {
  85. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  86. }
  87. }
  88. }
  89. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  90. return func(w http.ResponseWriter, r *http.Request) {
  91. if ms.Topo.IsLeader() {
  92. f(w, r)
  93. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  94. ms.bounedLeaderChan <- 1
  95. defer func() { <-ms.bounedLeaderChan }()
  96. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  97. if err != nil {
  98. writeJsonError(w, r, http.StatusInternalServerError,
  99. fmt.Errorf("Leader URL http://%s Parse Error: %v", ms.Topo.RaftServer.Leader(), err))
  100. return
  101. }
  102. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  103. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  104. director := proxy.Director
  105. proxy.Director = func(req *http.Request) {
  106. actualHost, err := security.GetActualRemoteHost(req)
  107. if err == nil {
  108. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  109. }
  110. director(req)
  111. }
  112. proxy.Transport = util.Transport
  113. proxy.ServeHTTP(w, r)
  114. } else {
  115. //drop it to the floor
  116. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  117. }
  118. }
  119. }