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.

133 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. 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("/{fileId}", ms.proxyToLeader(ms.redirectHandler))
  70. r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler))
  71. r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler))
  72. ms.Topo.StartRefreshWritableVolumes(garbageThreshold, ms.preallocate)
  73. return ms
  74. }
  75. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  76. ms.Topo.RaftServer = raftServer.raftServer
  77. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  78. if ms.Topo.RaftServer.Leader() != "" {
  79. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  80. }
  81. })
  82. if ms.Topo.IsLeader() {
  83. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  84. } else {
  85. if ms.Topo.RaftServer.Leader() != "" {
  86. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  87. }
  88. }
  89. }
  90. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  91. return func(w http.ResponseWriter, r *http.Request) {
  92. if ms.Topo.IsLeader() {
  93. f(w, r)
  94. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  95. ms.bounedLeaderChan <- 1
  96. defer func() { <-ms.bounedLeaderChan }()
  97. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  98. if err != nil {
  99. writeJsonError(w, r, http.StatusInternalServerError,
  100. fmt.Errorf("Leader URL http://%s Parse Error: %v", ms.Topo.RaftServer.Leader(), err))
  101. return
  102. }
  103. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  104. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  105. director := proxy.Director
  106. proxy.Director = func(req *http.Request) {
  107. actualHost, err := security.GetActualRemoteHost(req)
  108. if err == nil {
  109. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  110. }
  111. director(req)
  112. }
  113. proxy.Transport = util.Transport
  114. proxy.ServeHTTP(w, r)
  115. } else {
  116. //drop it to the floor
  117. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  118. }
  119. }
  120. }