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.

107 lines
3.8 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/replication"
  5. "code.google.com/p/weed-fs/go/sequence"
  6. "code.google.com/p/weed-fs/go/topology"
  7. "code.google.com/p/weed-fs/go/util"
  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. version string
  25. Topo *topology.Topology
  26. vg *replication.VolumeGrowth
  27. vgLock sync.Mutex
  28. bounedLeaderChan chan int
  29. }
  30. func NewMasterServer(r *mux.Router, version string, port int, metaFolder string,
  31. volumeSizeLimitMB uint,
  32. pulseSeconds int,
  33. confFile string,
  34. defaultReplicaPlacement string,
  35. garbageThreshold string,
  36. whiteList []string,
  37. ) *MasterServer {
  38. ms := &MasterServer{
  39. version: version,
  40. volumeSizeLimitMB: volumeSizeLimitMB,
  41. pulseSeconds: pulseSeconds,
  42. defaultReplicaPlacement: defaultReplicaPlacement,
  43. garbageThreshold: garbageThreshold,
  44. whiteList: whiteList,
  45. }
  46. ms.bounedLeaderChan = make(chan int, 16)
  47. seq := sequence.NewFileSequencer(path.Join(metaFolder, "weed.seq"))
  48. var e error
  49. if ms.Topo, e = topology.NewTopology("topo", confFile, seq,
  50. uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds); e != nil {
  51. glog.Fatalf("cannot create topology:%s", e)
  52. }
  53. ms.vg = replication.NewDefaultVolumeGrowth()
  54. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  55. r.HandleFunc("/dir/assign", ms.proxyToLeader(secure(ms.whiteList, ms.dirAssignHandler)))
  56. r.HandleFunc("/dir/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.dirLookupHandler)))
  57. r.HandleFunc("/dir/join", ms.proxyToLeader(secure(ms.whiteList, ms.dirJoinHandler)))
  58. r.HandleFunc("/dir/status", ms.proxyToLeader(secure(ms.whiteList, ms.dirStatusHandler)))
  59. r.HandleFunc("/col/delete", ms.proxyToLeader(secure(ms.whiteList, ms.collectionDeleteHandler)))
  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("/{filekey}", ms.redirectHandler)
  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. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "becomes leader.")
  72. })
  73. if ms.Topo.IsLeader() {
  74. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", "I am the leader!")
  75. } else {
  76. glog.V(0).Infoln("[", ms.Topo.RaftServer.Name(), "]", ms.Topo.RaftServer.Leader(), "is the leader.")
  77. }
  78. }
  79. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  80. return func(w http.ResponseWriter, r *http.Request) {
  81. if ms.Topo.IsLeader() {
  82. f(w, r)
  83. } else {
  84. ms.bounedLeaderChan <- 1
  85. defer func() { <-ms.bounedLeaderChan }()
  86. targetUrl, err := url.Parse("http://" + ms.Topo.RaftServer.Leader())
  87. if err != nil || ms.Topo.RaftServer.Leader() == "" {
  88. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL http://" + ms.Topo.RaftServer.Leader() + " Parse Error " + err.Error()})
  89. return
  90. }
  91. glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
  92. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  93. proxy.Transport = util.Transport
  94. proxy.ServeHTTP(w, r)
  95. }
  96. }
  97. }