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
3.9 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. raftServer *RaftServer
  29. bounedLeaderChan chan int
  30. }
  31. func NewMasterServer(r *mux.Router, version string, port int, metaFolder string,
  32. volumeSizeLimitMB uint,
  33. pulseSeconds int,
  34. confFile string,
  35. defaultReplicaPlacement string,
  36. garbageThreshold string,
  37. whiteList []string,
  38. ) *MasterServer {
  39. ms := &MasterServer{
  40. version: version,
  41. volumeSizeLimitMB: volumeSizeLimitMB,
  42. pulseSeconds: pulseSeconds,
  43. defaultReplicaPlacement: defaultReplicaPlacement,
  44. garbageThreshold: garbageThreshold,
  45. whiteList: whiteList,
  46. }
  47. ms.bounedLeaderChan = make(chan int, 16)
  48. seq := sequence.NewFileSequencer(path.Join(metaFolder, "weed.seq"))
  49. var e error
  50. if ms.topo, e = topology.NewTopology("topo", confFile, seq,
  51. uint64(volumeSizeLimitMB)*1024*1024, pulseSeconds); e != nil {
  52. glog.Fatalf("cannot create topology:%s", e)
  53. }
  54. ms.vg = replication.NewDefaultVolumeGrowth()
  55. glog.V(0).Infoln("Volume Size Limit is", volumeSizeLimitMB, "MB")
  56. r.HandleFunc("/dir/assign", ms.proxyToLeader(secure(ms.whiteList, ms.dirAssignHandler)))
  57. r.HandleFunc("/dir/lookup", ms.proxyToLeader(secure(ms.whiteList, ms.dirLookupHandler)))
  58. r.HandleFunc("/dir/join", ms.proxyToLeader(secure(ms.whiteList, ms.dirJoinHandler)))
  59. r.HandleFunc("/dir/status", ms.proxyToLeader(secure(ms.whiteList, ms.dirStatusHandler)))
  60. r.HandleFunc("/col/delete", ms.proxyToLeader(secure(ms.whiteList, ms.collectionDeleteHandler)))
  61. r.HandleFunc("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler)))
  62. r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler)))
  63. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler)))
  64. r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler))
  65. r.HandleFunc("/{filekey}", ms.redirectHandler)
  66. ms.topo.StartRefreshWritableVolumes(garbageThreshold)
  67. return ms
  68. }
  69. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  70. ms.raftServer = raftServer
  71. ms.raftServer.raftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  72. ms.topo.IsLeader = ms.IsLeader()
  73. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "becomes leader.")
  74. })
  75. ms.topo.IsLeader = ms.IsLeader()
  76. if ms.topo.IsLeader {
  77. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", "I am the leader!")
  78. } else {
  79. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "is the leader.")
  80. }
  81. }
  82. func (ms *MasterServer) IsLeader() bool {
  83. return ms.raftServer == nil || ms.raftServer.IsLeader()
  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.IsLeader() {
  88. f(w, r)
  89. } else {
  90. ms.bounedLeaderChan <- 1
  91. defer func() { <-ms.bounedLeaderChan }()
  92. targetUrl, err := url.Parse("http://" + ms.raftServer.Leader())
  93. if err != nil {
  94. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL Parse Error " + err.Error()})
  95. return
  96. }
  97. glog.V(4).Infoln("proxying to leader", ms.raftServer.Leader())
  98. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  99. proxy.Transport = util.Transport
  100. proxy.ServeHTTP(w, r)
  101. }
  102. }
  103. }