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.

135 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. glog.V(0).Infof("event: %+v", e)
  80. if ms.Topo.RaftServer.Leader() != "" {
  81. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  82. }
  83. })
  84. if ms.Topo.IsLeader() {
  85. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  86. } else {
  87. if ms.Topo.RaftServer.Leader() != "" {
  88. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  89. }
  90. }
  91. }
  92. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  93. return func(w http.ResponseWriter, r *http.Request) {
  94. if ms.Topo.IsLeader() {
  95. f(w, r)
  96. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  97. ms.bounedLeaderChan <- 1
  98. defer func() { <-ms.bounedLeaderChan }()
  99. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  100. if err != nil {
  101. writeJsonError(w, r, http.StatusInternalServerError,
  102. fmt.Errorf("Leader URL http://%s Parse Error: %v", ms.Topo.RaftServer.Leader(), err))
  103. return
  104. }
  105. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  106. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  107. director := proxy.Director
  108. proxy.Director = func(req *http.Request) {
  109. actualHost, err := security.GetActualRemoteHost(req)
  110. if err == nil {
  111. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  112. }
  113. director(req)
  114. }
  115. proxy.Transport = util.Transport
  116. proxy.ServeHTTP(w, r)
  117. } else {
  118. //drop it to the floor
  119. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  120. }
  121. }
  122. }