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.

118 lines
3.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "sync"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/go/glog"
  8. "github.com/chrislusf/seaweedfs/go/security"
  9. "github.com/chrislusf/seaweedfs/go/storage"
  10. )
  11. type VolumeServer struct {
  12. masterNode string
  13. mnLock sync.RWMutex
  14. pulseSeconds int
  15. dataCenter string
  16. rack string
  17. store *storage.Store
  18. guard *security.Guard
  19. needleMapKind storage.NeedleMapType
  20. FixJpgOrientation bool
  21. }
  22. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  23. port int, publicUrl string,
  24. folders []string, maxCounts []int,
  25. needleMapKind storage.NeedleMapType,
  26. masterNode string, pulseSeconds int,
  27. dataCenter string, rack string,
  28. whiteList []string,
  29. fixJpgOrientation bool) *VolumeServer {
  30. vs := &VolumeServer{
  31. pulseSeconds: pulseSeconds,
  32. dataCenter: dataCenter,
  33. rack: rack,
  34. needleMapKind: needleMapKind,
  35. FixJpgOrientation: fixJpgOrientation,
  36. }
  37. vs.SetMasterNode(masterNode)
  38. vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts, vs.needleMapKind)
  39. vs.guard = security.NewGuard(whiteList, "")
  40. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  41. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  42. adminMux.HandleFunc("/admin/assign_volume", vs.guard.WhiteList(vs.assignVolumeHandler))
  43. adminMux.HandleFunc("/admin/vacuum/check", vs.guard.WhiteList(vs.vacuumVolumeCheckHandler))
  44. adminMux.HandleFunc("/admin/vacuum/compact", vs.guard.WhiteList(vs.vacuumVolumeCompactHandler))
  45. adminMux.HandleFunc("/admin/vacuum/commit", vs.guard.WhiteList(vs.vacuumVolumeCommitHandler))
  46. adminMux.HandleFunc("/admin/delete_collection", vs.guard.WhiteList(vs.deleteCollectionHandler))
  47. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  48. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  49. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  50. adminMux.HandleFunc("/delete", vs.guard.WhiteList(vs.batchDeleteHandler))
  51. adminMux.HandleFunc("/", vs.privateStoreHandler)
  52. if publicMux != adminMux {
  53. // separated admin and public port
  54. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  55. }
  56. go func() {
  57. connected := true
  58. glog.V(0).Infof("Volume server bootstraps with master %s", vs.GetMasterNode())
  59. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  60. vs.store.SetDataCenter(vs.dataCenter)
  61. vs.store.SetRack(vs.rack)
  62. for {
  63. glog.V(4).Infof("Volume server sending to master %s", vs.GetMasterNode())
  64. master, secretKey, err := vs.store.SendHeartbeatToMaster()
  65. if err == nil {
  66. if !connected {
  67. connected = true
  68. vs.SetMasterNode(master)
  69. vs.guard.SecretKey = secretKey
  70. glog.V(0).Infoln("Volume Server Connected with master at", master)
  71. }
  72. } else {
  73. glog.V(1).Infof("Volume Server Failed to talk with master %+v: %v", vs, err)
  74. if connected {
  75. connected = false
  76. }
  77. }
  78. if connected {
  79. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  80. } else {
  81. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  82. }
  83. }
  84. }()
  85. return vs
  86. }
  87. func (vs *VolumeServer) GetMasterNode() string {
  88. vs.mnLock.RLock()
  89. defer vs.mnLock.RUnlock()
  90. return vs.masterNode
  91. }
  92. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  93. vs.mnLock.Lock()
  94. defer vs.mnLock.Unlock()
  95. vs.masterNode = masterNode
  96. }
  97. func (vs *VolumeServer) Shutdown() {
  98. glog.V(0).Infoln("Shutting down volume server...")
  99. vs.store.Close()
  100. glog.V(0).Infoln("Shut down successfully!")
  101. }
  102. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  103. return security.GenJwt(vs.guard.SecretKey, fileId)
  104. }