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.

116 lines
4.3 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/sequence"
  9. "github.com/chrislusf/weed-fs/go/topology"
  10. "github.com/chrislusf/weed-fs/go/util"
  11. "github.com/goraft/raft"
  12. "github.com/gorilla/mux"
  13. )
  14. type MasterServer struct {
  15. port int
  16. metaFolder string
  17. volumeSizeLimitMB uint
  18. pulseSeconds int
  19. defaultReplicaPlacement string
  20. garbageThreshold string
  21. whiteList []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. ) *MasterServer {
  35. ms := &MasterServer{
  36. port: port,
  37. volumeSizeLimitMB: volumeSizeLimitMB,
  38. pulseSeconds: pulseSeconds,
  39. defaultReplicaPlacement: defaultReplicaPlacement,
  40. garbageThreshold: garbageThreshold,
  41. whiteList: whiteList,
  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. r.HandleFunc("/dir/assign", ms.proxyToLeader(secure(ms.whiteList, ms.dirAssignHandler)))
  53. r.HandleFunc("/dir/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.dirLookupHandler)))
  54. r.HandleFunc("/dir/join", ms.proxyToLeader(secure(ms.whiteList, ms.dirJoinHandler)))
  55. r.HandleFunc("/dir/status", ms.proxyToLeader(secure(ms.whiteList, ms.dirStatusHandler)))
  56. r.HandleFunc("/col/delete", ms.proxyToLeader(secure(ms.whiteList, ms.collectionDeleteHandler)))
  57. r.HandleFunc("/vol/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.volumeLookupHandler)))
  58. r.HandleFunc("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler)))
  59. r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler)))
  60. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler)))
  61. r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler))
  62. r.HandleFunc("/delete", secure(ms.whiteList, ms.deleteFromMasterServerHandler))
  63. r.HandleFunc("/{fileId}", ms.redirectHandler)
  64. r.HandleFunc("/stats/counter", secure(ms.whiteList, statsCounterHandler))
  65. r.HandleFunc("/stats/memory", secure(ms.whiteList, statsMemoryHandler))
  66. ms.Topo.StartRefreshWritableVolumes(garbageThreshold)
  67. return ms
  68. }
  69. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  70. ms.Topo.RaftServer = raftServer.raftServer
  71. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  72. if ms.Topo.RaftServer.Leader() != "" {
  73. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  74. }
  75. })
  76. if ms.Topo.IsLeader() {
  77. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  78. } else {
  79. if ms.Topo.RaftServer.Leader() != "" {
  80. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  81. }
  82. }
  83. }
  84. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  85. return func(w http.ResponseWriter, r *http.Request) {
  86. if ms.Topo.IsLeader() {
  87. f(w, r)
  88. } else if ms.Topo.RaftServer != nil && ms.Topo.RaftServer.Leader() != "" {
  89. ms.bounedLeaderChan <- 1
  90. defer func() { <-ms.bounedLeaderChan }()
  91. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  92. if err != nil {
  93. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL http://" + ms.Topo.RaftServer.Leader() + " Parse Error " + err.Error()})
  94. return
  95. }
  96. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  97. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  98. proxy.Transport = util.Transport
  99. proxy.ServeHTTP(w, r)
  100. } else {
  101. //drop it to the floor
  102. //writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+" does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  103. }
  104. }
  105. }