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.

88 lines
2.7 KiB

  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "strconv"
  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. pulseSeconds int
  14. dataCenter string
  15. rack string
  16. store *storage.Store
  17. guard *security.Guard
  18. FixJpgOrientation bool
  19. }
  20. func NewVolumeServer(r *http.ServeMux, ip string, port int, publicIp string, folders []string, maxCounts []int,
  21. masterNode string, pulseSeconds int,
  22. dataCenter string, rack string,
  23. whiteList []string,
  24. fixJpgOrientation bool) *VolumeServer {
  25. publicUrl := publicIp + ":" + strconv.Itoa(port)
  26. vs := &VolumeServer{
  27. masterNode: masterNode,
  28. pulseSeconds: pulseSeconds,
  29. dataCenter: dataCenter,
  30. rack: rack,
  31. FixJpgOrientation: fixJpgOrientation,
  32. }
  33. vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts)
  34. vs.guard = security.NewGuard(whiteList, "")
  35. r.HandleFunc("/status", vs.guard.Secure(vs.statusHandler))
  36. r.HandleFunc("/admin/assign_volume", vs.guard.Secure(vs.assignVolumeHandler))
  37. r.HandleFunc("/admin/vacuum_volume_check", vs.guard.Secure(vs.vacuumVolumeCheckHandler))
  38. r.HandleFunc("/admin/vacuum_volume_compact", vs.guard.Secure(vs.vacuumVolumeCompactHandler))
  39. r.HandleFunc("/admin/vacuum_volume_commit", vs.guard.Secure(vs.vacuumVolumeCommitHandler))
  40. r.HandleFunc("/admin/freeze_volume", vs.guard.Secure(vs.freezeVolumeHandler))
  41. r.HandleFunc("/admin/delete_collection", vs.guard.Secure(vs.deleteCollectionHandler))
  42. r.HandleFunc("/stats/counter", vs.guard.Secure(statsCounterHandler))
  43. r.HandleFunc("/stats/memory", vs.guard.Secure(statsMemoryHandler))
  44. r.HandleFunc("/stats/disk", vs.guard.Secure(vs.statsDiskHandler))
  45. r.HandleFunc("/delete", vs.guard.Secure(vs.batchDeleteHandler))
  46. r.HandleFunc("/", vs.storeHandler)
  47. go func() {
  48. connected := true
  49. vs.store.SetBootstrapMaster(vs.masterNode)
  50. vs.store.SetDataCenter(vs.dataCenter)
  51. vs.store.SetRack(vs.rack)
  52. for {
  53. master, err := vs.store.Join()
  54. if err == nil {
  55. if !connected {
  56. connected = true
  57. glog.V(0).Infoln("Volume Server Connected with master at", master)
  58. }
  59. } else {
  60. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  61. if connected {
  62. connected = false
  63. }
  64. }
  65. if connected {
  66. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  67. } else {
  68. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  69. }
  70. }
  71. }()
  72. return vs
  73. }
  74. func (vs *VolumeServer) Shutdown() {
  75. glog.V(0).Infoln("Shutting down volume server...")
  76. vs.store.Close()
  77. glog.V(0).Infoln("Shut down successfully!")
  78. }