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.

121 lines
4.4 KiB

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