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.

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