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.

142 lines
5.0 KiB

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