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.

114 lines
4.1 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. 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.NewFileSequencer(path.Join(metaFolder, "weed.seq"))
  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/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("/{filekey}", ms.redirectHandler)
  63. r.HandleFunc("/stats/counter", secure(ms.whiteList, statsCounterHandler))
  64. r.HandleFunc("/stats/memory", secure(ms.whiteList, statsMemoryHandler))
  65. ms.Topo.StartRefreshWritableVolumes(garbageThreshold)
  66. return ms
  67. }
  68. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  69. ms.Topo.RaftServer = raftServer.raftServer
  70. ms.Topo.RaftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  71. if ms.Topo.RaftServer.Leader() != "" {
  72. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  73. }
  74. })
  75. if ms.Topo.IsLeader() {
  76. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  77. } else {
  78. if ms.Topo.RaftServer.Leader() != "" {
  79. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  80. }
  81. }
  82. }
  83. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  84. return func(w http.ResponseWriter, r *http.Request) {
  85. if ms.Topo.IsLeader() {
  86. f(w, r)
  87. } else if ms.Topo.RaftServer.Leader() != "" {
  88. ms.bounedLeaderChan <- 1
  89. defer func() { <-ms.bounedLeaderChan }()
  90. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  91. if err != nil {
  92. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL http://" + ms.Topo.RaftServer.Leader() + " Parse Error " + err.Error()})
  93. return
  94. }
  95. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  96. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  97. proxy.Transport = util.Transport
  98. proxy.ServeHTTP(w, r)
  99. } else {
  100. //drop it to the floor
  101. writeJsonError(w, r, errors.New(ms.Topo.RaftServer.Name()+"does not know Leader yet:"+ms.Topo.RaftServer.Leader()))
  102. }
  103. }
  104. }