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.

104 lines
3.1 KiB

  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "sync"
  6. "time"
  7. "github.com/chrislusf/weed-fs/go/glog"
  8. "github.com/chrislusf/weed-fs/go/security"
  9. "github.com/chrislusf/weed-fs/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. FixJpgOrientation bool
  20. }
  21. func NewVolumeServer(publicMux, adminMux *http.ServeMux, ip string,
  22. port, adminPort int, publicUrl string,
  23. folders []string, maxCounts []int,
  24. masterNode string, pulseSeconds int,
  25. dataCenter string, rack string,
  26. whiteList []string,
  27. fixJpgOrientation bool) *VolumeServer {
  28. vs := &VolumeServer{
  29. pulseSeconds: pulseSeconds,
  30. dataCenter: dataCenter,
  31. rack: rack,
  32. FixJpgOrientation: fixJpgOrientation,
  33. }
  34. vs.SetMasterNode(masterNode)
  35. vs.store = storage.NewStore(port, adminPort, ip, publicUrl, folders, maxCounts)
  36. vs.guard = security.NewGuard(whiteList, "")
  37. adminMux.HandleFunc("/status", vs.guard.Secure(vs.statusHandler))
  38. adminMux.HandleFunc("/admin/assign_volume", vs.guard.Secure(vs.assignVolumeHandler))
  39. adminMux.HandleFunc("/admin/vacuum_volume_check", vs.guard.Secure(vs.vacuumVolumeCheckHandler))
  40. adminMux.HandleFunc("/admin/vacuum_volume_compact", vs.guard.Secure(vs.vacuumVolumeCompactHandler))
  41. adminMux.HandleFunc("/admin/vacuum_volume_commit", vs.guard.Secure(vs.vacuumVolumeCommitHandler))
  42. adminMux.HandleFunc("/admin/freeze_volume", vs.guard.Secure(vs.freezeVolumeHandler))
  43. adminMux.HandleFunc("/admin/delete_collection", vs.guard.Secure(vs.deleteCollectionHandler))
  44. adminMux.HandleFunc("/stats/counter", vs.guard.Secure(statsCounterHandler))
  45. adminMux.HandleFunc("/stats/memory", vs.guard.Secure(statsMemoryHandler))
  46. adminMux.HandleFunc("/stats/disk", vs.guard.Secure(vs.statsDiskHandler))
  47. publicMux.HandleFunc("/delete", vs.guard.Secure(vs.batchDeleteHandler))
  48. publicMux.HandleFunc("/", vs.storeHandler)
  49. go func() {
  50. connected := true
  51. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  52. vs.store.SetDataCenter(vs.dataCenter)
  53. vs.store.SetRack(vs.rack)
  54. for {
  55. master, err := vs.store.Join()
  56. if err == nil {
  57. if !connected {
  58. connected = true
  59. vs.SetMasterNode(master)
  60. glog.V(0).Infoln("Volume Server Connected with master at", master, "and set it as masterNode")
  61. }
  62. } else {
  63. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  64. if connected {
  65. connected = false
  66. }
  67. }
  68. if connected {
  69. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  70. } else {
  71. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  72. }
  73. }
  74. }()
  75. return vs
  76. }
  77. func (vs *VolumeServer) GetMasterNode() string {
  78. vs.mnLock.RLock()
  79. defer vs.mnLock.RUnlock()
  80. return vs.masterNode
  81. }
  82. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  83. vs.mnLock.Lock()
  84. defer vs.mnLock.Unlock()
  85. vs.masterNode = masterNode
  86. }
  87. func (vs *VolumeServer) Shutdown() {
  88. glog.V(0).Infoln("Shutting down volume server...")
  89. vs.store.Close()
  90. glog.V(0).Infoln("Shut down successfully!")
  91. }