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.

140 lines
4.9 KiB

6 years ago
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/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/sequence"
  13. "github.com/chrislusf/seaweedfs/weed/topology"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. "github.com/gorilla/mux"
  16. )
  17. type MasterServer struct {
  18. port int
  19. metaFolder string
  20. volumeSizeLimitMB uint
  21. preallocate int64
  22. pulseSeconds int
  23. defaultReplicaPlacement string
  24. garbageThreshold float64
  25. guard *security.Guard
  26. Topo *topology.Topology
  27. vg *topology.VolumeGrowth
  28. vgLock sync.Mutex
  29. bounedLeaderChan chan int
  30. // notifying clients
  31. clientChansLock sync.RWMutex
  32. clientChans map[string]chan *master_pb.VolumeLocation
  33. }
  34. func NewMasterServer(r *mux.Router, port int, metaFolder string,
  35. volumeSizeLimitMB uint,
  36. preallocate bool,
  37. pulseSeconds int,
  38. defaultReplicaPlacement string,
  39. garbageThreshold float64,
  40. whiteList []string,
  41. secureKey string,
  42. ) *MasterServer {
  43. var preallocateSize int64
  44. if preallocate {
  45. preallocateSize = int64(volumeSizeLimitMB) * (1 << 20)
  46. }
  47. ms := &MasterServer{
  48. port: port,
  49. volumeSizeLimitMB: volumeSizeLimitMB,
  50. preallocate: preallocateSize,
  51. pulseSeconds: pulseSeconds,
  52. defaultReplicaPlacement: defaultReplicaPlacement,
  53. garbageThreshold: garbageThreshold,
  54. clientChans: make(map[string]chan *master_pb.VolumeLocation),
  55. }
  56. ms.bounedLeaderChan = make(chan int, 16)
  57. seq := sequence.NewMemorySequencer()
  58. ms.Topo = topology.NewTopology("topo", seq, uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds)
  59. ms.vg = topology.NewDefaultVolumeGrowth()
  60. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  61. ms.guard = security.NewGuard(whiteList, secureKey)
  62. handleStaticResources2(r)
  63. r.HandleFunc("/", ms.uiStatusHandler)
  64. r.HandleFunc("/ui/index.html", ms.uiStatusHandler)
  65. r.HandleFunc("/dir/assign", ms.proxyToLeader(ms.guard.WhiteList(ms.dirAssignHandler)))
  66. r.HandleFunc("/dir/lookup", ms.proxyToLeader(ms.guard.WhiteList(ms.dirLookupHandler)))
  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/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("/stats/health", ms.guard.WhiteList(statsHealthHandler))
  74. r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler))
  75. r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler))
  76. r.HandleFunc("/{fileId}", ms.proxyToLeader(ms.redirectHandler))
  77. ms.Topo.StartRefreshWritableVolumes(garbageThreshold, ms.preallocate)
  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. glog.V(0).Infof("event: %+v", e)
  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. }