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. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/sequence"
  5. "code.google.com/p/weed-fs/go/topology"
  6. "code.google.com/p/weed-fs/go/util"
  7. "errors"
  8. "github.com/goraft/raft"
  9. "github.com/gorilla/mux"
  10. "net/http"
  11. "net/http/httputil"
  12. "net/url"
  13. "sync"
  14. )
  15. type MasterServer struct {
  16. port int
  17. metaFolder string
  18. volumeSizeLimitMB uint
  19. pulseSeconds int
  20. defaultReplicaPlacement string
  21. garbageThreshold string
  22. whiteList []string
  23. Topo *topology.Topology
  24. vg *topology.VolumeGrowth
  25. vgLock sync.Mutex
  26. bounedLeaderChan chan int
  27. }
  28. func NewMasterServer(r *mux.Router, port int, metaFolder string,
  29. volumeSizeLimitMB uint,
  30. pulseSeconds int,
  31. confFile string,
  32. defaultReplicaPlacement string,
  33. garbageThreshold string,
  34. whiteList []string,
  35. ) *MasterServer {
  36. ms := &MasterServer{
  37. port: port,
  38. volumeSizeLimitMB: volumeSizeLimitMB,
  39. pulseSeconds: pulseSeconds,
  40. defaultReplicaPlacement: defaultReplicaPlacement,
  41. garbageThreshold: garbageThreshold,
  42. whiteList: whiteList,
  43. }
  44. ms.bounedLeaderChan = make(chan int, 16)
  45. seq := sequence.NewMemorySequencer()
  46. var e error
  47. if ms.Topo, e = topology.NewTopology("topo", confFile, seq,
  48. uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds); e != nil {
  49. glog.Fatalf("cannot create topology:%s", e)
  50. }
  51. ms.vg = topology.NewDefaultVolumeGrowth()
  52. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  53. r.HandleFunc("/dir/assign", ms.proxyToLeader(secure(ms.whiteList, ms.dirAssignHandler)))
  54. r.HandleFunc("/dir/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.dirLookupHandler)))
  55. r.HandleFunc("/dir/join", ms.proxyToLeader(secure(ms.whiteList, ms.dirJoinHandler)))
  56. r.HandleFunc("/dir/status", ms.proxyToLeader(secure(ms.whiteList, ms.dirStatusHandler)))
  57. r.HandleFunc("/col/delete", ms.proxyToLeader(secure(ms.whiteList, ms.collectionDeleteHandler)))
  58. r.HandleFunc("/vol/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.volumeLookupHandler)))
  59. r.HandleFunc("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler)))
  60. r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler)))
  61. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler)))
  62. r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler))
  63. r.HandleFunc("/delete", secure(ms.whiteList, ms.deleteFromMasterServerHandler))
  64. r.HandleFunc("/{fileId}", ms.redirectHandler)
  65. r.HandleFunc("/stats/counter", secure(ms.whiteList, statsCounterHandler))
  66. r.HandleFunc("/stats/memory", secure(ms.whiteList, 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. }