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.

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