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.

141 lines
5.1 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. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  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 string
  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 string,
  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. 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("/stats/health", ms.guard.WhiteList(statsHealthHandler))
  75. r.HandleFunc("/stats/counter", ms.guard.WhiteList(statsCounterHandler))
  76. r.HandleFunc("/stats/memory", ms.guard.WhiteList(statsMemoryHandler))
  77. r.HandleFunc("/{fileId}", ms.proxyToLeader(ms.redirectHandler))
  78. ms.Topo.StartRefreshWritableVolumes(garbageThreshold, ms.preallocate)
  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. glog.V(0).Infof("event: %+v", e)
  85. if ms.Topo.RaftServer.Leader() != "" {
  86. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  87. }
  88. })
  89. if ms.Topo.IsLeader() {
  90. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  91. } else {
  92. if ms.Topo.RaftServer.Leader() != "" {
  93. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  94. }
  95. }
  96. }
  97. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  98. return func(w http.ResponseWriter, r *http.Request) {
  99. if ms.Topo.IsLeader() {
  100. f(w, r)
  101. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  102. ms.bounedLeaderChan <- 1
  103. defer func() { <-ms.bounedLeaderChan }()
  104. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  105. if err != nil {
  106. writeJsonError(w, r, http.StatusInternalServerError,
  107. fmt.Errorf("Leader URL http://%s Parse Error: %v", ms.Topo.RaftServer.Leader(), err))
  108. return
  109. }
  110. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  111. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  112. director := proxy.Director
  113. proxy.Director = func(req *http.Request) {
  114. actualHost, err := security.GetActualRemoteHost(req)
  115. if err == nil {
  116. req.Header.Set("HTTP_X_FORWARDED_FOR", actualHost)
  117. }
  118. director(req)
  119. }
  120. proxy.Transport = util.Transport
  121. proxy.ServeHTTP(w, r)
  122. } else {
  123. //drop it to the floor
  124. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  125. }
  126. }
  127. }