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.

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