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.

116 lines
3.5 KiB

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_volume_check", vs.guard.WhiteList(vs.vacuumVolumeCheckHandler))
  44. adminMux.HandleFunc("/admin/vacuum_volume_compact", vs.guard.WhiteList(vs.vacuumVolumeCompactHandler))
  45. adminMux.HandleFunc("/admin/vacuum_volume_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. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  59. vs.store.SetDataCenter(vs.dataCenter)
  60. vs.store.SetRack(vs.rack)
  61. for {
  62. master, secretKey, err := vs.store.SendHeartbeatToMaster()
  63. if err == nil {
  64. if !connected {
  65. connected = true
  66. vs.SetMasterNode(master)
  67. vs.guard.SecretKey = secretKey
  68. glog.V(0).Infoln("Volume Server Connected with master at", master)
  69. }
  70. } else {
  71. glog.V(1).Infof("Volume Server Failed to talk with master %+v: %v", vs, err)
  72. if connected {
  73. connected = false
  74. }
  75. }
  76. if connected {
  77. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  78. } else {
  79. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  80. }
  81. }
  82. }()
  83. return vs
  84. }
  85. func (vs *VolumeServer) GetMasterNode() string {
  86. vs.mnLock.RLock()
  87. defer vs.mnLock.RUnlock()
  88. return vs.masterNode
  89. }
  90. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  91. vs.mnLock.Lock()
  92. defer vs.mnLock.Unlock()
  93. vs.masterNode = masterNode
  94. }
  95. func (vs *VolumeServer) Shutdown() {
  96. glog.V(0).Infoln("Shutting down volume server...")
  97. vs.store.Close()
  98. glog.V(0).Infoln("Shut down successfully!")
  99. }
  100. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  101. return security.GenJwt(vs.guard.SecretKey, fileId)
  102. }