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.

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