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.

89 lines
2.8 KiB

  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "time"
  6. "github.com/chrislusf/weed-fs/go/glog"
  7. "github.com/chrislusf/weed-fs/go/security"
  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. store *storage.Store
  16. guard *security.Guard
  17. FixJpgOrientation bool
  18. }
  19. func NewVolumeServer(publicMux, adminMux *http.ServeMux, ip string,
  20. port, adminPort int, publicUrl string,
  21. folders []string, maxCounts []int,
  22. masterNode string, pulseSeconds int,
  23. dataCenter string, rack string,
  24. whiteList []string,
  25. fixJpgOrientation bool) *VolumeServer {
  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, adminPort, ip, publicUrl, folders, maxCounts)
  34. vs.guard = security.NewGuard(whiteList, "")
  35. adminMux.HandleFunc("/status", vs.guard.Secure(vs.statusHandler))
  36. adminMux.HandleFunc("/admin/assign_volume", vs.guard.Secure(vs.assignVolumeHandler))
  37. adminMux.HandleFunc("/admin/vacuum_volume_check", vs.guard.Secure(vs.vacuumVolumeCheckHandler))
  38. adminMux.HandleFunc("/admin/vacuum_volume_compact", vs.guard.Secure(vs.vacuumVolumeCompactHandler))
  39. adminMux.HandleFunc("/admin/vacuum_volume_commit", vs.guard.Secure(vs.vacuumVolumeCommitHandler))
  40. adminMux.HandleFunc("/admin/freeze_volume", vs.guard.Secure(vs.freezeVolumeHandler))
  41. adminMux.HandleFunc("/admin/delete_collection", vs.guard.Secure(vs.deleteCollectionHandler))
  42. adminMux.HandleFunc("/stats/counter", vs.guard.Secure(statsCounterHandler))
  43. adminMux.HandleFunc("/stats/memory", vs.guard.Secure(statsMemoryHandler))
  44. adminMux.HandleFunc("/stats/disk", vs.guard.Secure(vs.statsDiskHandler))
  45. publicMux.HandleFunc("/delete", vs.guard.Secure(vs.batchDeleteHandler))
  46. publicMux.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. vs.masterNode = master
  58. glog.V(0).Infoln("Volume Server Connected with master at", master)
  59. }
  60. } else {
  61. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  62. if connected {
  63. connected = false
  64. }
  65. }
  66. if connected {
  67. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  68. } else {
  69. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  70. }
  71. }
  72. }()
  73. return vs
  74. }
  75. func (vs *VolumeServer) Shutdown() {
  76. glog.V(0).Infoln("Shutting down volume server...")
  77. vs.store.Close()
  78. glog.V(0).Infoln("Shut down successfully!")
  79. }