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.

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