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.

108 lines
3.7 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("/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. ms.topo.StartRefreshWritableVolumes(garbageThreshold)
  64. return ms
  65. }
  66. func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) {
  67. ms.raftServer = raftServer
  68. ms.raftServer.raftServer.AddEventListener(raft.LeaderChangeEventType, func(e raft.Event) {
  69. ms.topo.IsLeader = ms.IsLeader()
  70. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "becomes leader.")
  71. })
  72. ms.topo.IsLeader = ms.IsLeader()
  73. if ms.topo.IsLeader {
  74. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", "I am the leader!")
  75. } else {
  76. glog.V(0).Infoln("[", ms.raftServer.Name(), "]", ms.raftServer.Leader(), "is the leader.")
  77. }
  78. }
  79. func (ms *MasterServer) IsLeader() bool {
  80. return ms.raftServer == nil || ms.raftServer.IsLeader()
  81. }
  82. func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  83. return func(w http.ResponseWriter, r *http.Request) {
  84. if ms.IsLeader() {
  85. f(w, r)
  86. } else {
  87. targetUrl, err := url.Parse("http://" + ms.raftServer.Leader())
  88. if err != nil {
  89. writeJsonQuiet(w, r, map[string]interface{}{"error": "Leader URL Parse Error " + err.Error()})
  90. return
  91. }
  92. glog.V(4).Infoln("proxying to leader", ms.raftServer.Leader())
  93. proxy := httputil.NewSingleHostReverseProxy(targetUrl)
  94. proxy.ServeHTTP(w, r)
  95. }
  96. }
  97. }