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.6 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. "github.com/goraft/raft"
  8. "github.com/gorilla/mux"
  9. "net/http"
  10. "net/http/httputil"
  11. "net/url"
  12. "path"
  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. version string
  24. topo *topology.Topology
  25. vg *replication.VolumeGrowth
  26. vgLock sync.Mutex
  27. raftServer *RaftServer
  28. }
  29. func NewMasterServer(r *mux.Router, version string, 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. version: version,
  39. volumeSizeLimitMB: volumeSizeLimitMB,
  40. pulseSeconds: pulseSeconds,
  41. defaultReplicaPlacement: defaultReplicaPlacement,
  42. garbageThreshold: garbageThreshold,
  43. whiteList: whiteList,
  44. }
  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 = replication.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("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler)))
  58. r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler)))
  59. r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler)))
  60. r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler))
  61. r.HandleFunc("/{filekey}", ms.redirectHandler)
  62. ms.topo.StartRefreshWritableVolumes(garbageThreshold)
  63. return ms
  64. }
  65. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  66. ms.raftServer = raftServer
  67. ms.raftServer.raftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  68. ms.topo.IsLeader = ms.IsLeader()
  69. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "becomes leader.")
  70. })
  71. ms.topo.IsLeader = ms.IsLeader()
  72. if ms.topo.IsLeader {
  73. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", "I am the leader!")
  74. } else {
  75. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "is the leader.")
  76. }
  77. }
  78. func (ms *MasterServer) IsLeader() bool {
  79. return ms.raftServer == nil || ms.raftServer.IsLeader()
  80. }
  81. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  82. return func(w http.ResponseWriter, r *http.Request) {
  83. if ms.IsLeader() {
  84. f(w, r)
  85. } else {
  86. targetUrl, err := url.Parse("http://" + ms.raftServer.Leader())
  87. if err != nil {
  88. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL Parse Error " + err.Error()})
  89. return
  90. }
  91. glog.V(4).Infoln("proxying to leader", ms.raftServer.Leader())
  92. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  93. proxy.ServeHTTP(w, r)
  94. }
  95. }
  96. }