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.

118 lines
4.2 KiB

  1. package weed_server
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "sync"
  7. "github.com/chrislusf/weed-fs/go/glog"
  8. "github.com/chrislusf/weed-fs/go/security"
  9. "github.com/chrislusf/weed-fs/go/sequence"
  10. "github.com/chrislusf/weed-fs/go/topology"
  11. "github.com/chrislusf/weed-fs/go/util"
  12. "github.com/goraft/raft"
  13. "github.com/gorilla/mux"
  14. )
  15. type MasterServer struct {
  16. port int
  17. metaFolder string
  18. volumeSizeLimitMB uint
  19. pulseSeconds int
  20. defaultReplicaPlacement string
  21. garbageThreshold string
  22. Topo *topology.Topology
  23. vg *topology.VolumeGrowth
  24. vgLock sync.Mutex
  25. bounedLeaderChan chan int
  26. }
  27. func NewMasterServer(r *mux.Router, port int, metaFolder string,
  28. volumeSizeLimitMB uint,
  29. pulseSeconds int,
  30. confFile string,
  31. defaultReplicaPlacement string,
  32. garbageThreshold string,
  33. whiteList []string,
  34. secureKey string,
  35. ) *MasterServer {
  36. ms := &MasterServer{
  37. port: port,
  38. volumeSizeLimitMB: volumeSizeLimitMB,
  39. pulseSeconds: pulseSeconds,
  40. defaultReplicaPlacement: defaultReplicaPlacement,
  41. garbageThreshold: garbageThreshold,
  42. }
  43. ms.bounedLeaderChan = make(chan int, 16)
  44. seq := sequence.NewMemorySequencer()
  45. var e error
  46. if ms.Topo, e = topology.NewTopology("topo", confFile, seq,
  47. uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds); e != nil {
  48. glog.Fatalf("cannot create topology:%s", e)
  49. }
  50. ms.vg = topology.NewDefaultVolumeGrowth()
  51. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  52. guard := security.NewGuard(whiteList, secureKey)
  53. r.HandleFunc("/dir/assign", ms.proxyToLeader(guard.Secure(ms.dirAssignHandler)))
  54. r.HandleFunc("/dir/lookup", ms.proxyToLeader(guard.Secure(ms.dirLookupHandler)))
  55. r.HandleFunc("/dir/join", ms.proxyToLeader(guard.Secure(ms.dirJoinHandler)))
  56. r.HandleFunc("/dir/status", ms.proxyToLeader(guard.Secure(ms.dirStatusHandler)))
  57. r.HandleFunc("/col/delete", ms.proxyToLeader(guard.Secure(ms.collectionDeleteHandler)))
  58. r.HandleFunc("/vol/lookup", ms.proxyToLeader(guard.Secure(ms.volumeLookupHandler)))
  59. r.HandleFunc("/vol/grow", ms.proxyToLeader(guard.Secure(ms.volumeGrowHandler)))
  60. r.HandleFunc("/vol/status", ms.proxyToLeader(guard.Secure(ms.volumeStatusHandler)))
  61. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(guard.Secure(ms.volumeVacuumHandler)))
  62. r.HandleFunc("/submit", guard.Secure(ms.submitFromMasterServerHandler))
  63. r.HandleFunc("/delete", guard.Secure(ms.deleteFromMasterServerHandler))
  64. r.HandleFunc("/{fileId}", ms.redirectHandler)
  65. r.HandleFunc("/stats/counter", guard.Secure(statsCounterHandler))
  66. r.HandleFunc("/stats/memory", guard.Secure(statsMemoryHandler))
  67. ms.Topo.StartRefreshWritableVolumes(garbageThreshold)
  68. return ms
  69. }
  70. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  71. ms.Topo.RaftServer = raftServer.raftServer
  72. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  73. if ms.Topo.RaftServer.Leader() != "" {
  74. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  75. }
  76. })
  77. if ms.Topo.IsLeader() {
  78. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  79. } else {
  80. if ms.Topo.RaftServer.Leader() != "" {
  81. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  82. }
  83. }
  84. }
  85. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  86. return func(w http.ResponseWriter, r *http.Request) {
  87. if ms.Topo.IsLeader() {
  88. f(w, r)
  89. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  90. ms.bounedLeaderChan <- 1
  91. defer func() { <-ms.bounedLeaderChan }()
  92. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  93. if err != nil {
  94. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL http://" + ms.Topo.RaftServer.Leader() + " Parse Error " + err.Error()})
  95. return
  96. }
  97. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  98. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  99. proxy.Transport = util.Transport
  100. proxy.ServeHTTP(w, r)
  101. } else {
  102. //drop it to the floor
  103. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  104. }
  105. }
  106. }