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.8 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "math/rand"
  6. "net/http"
  7. "strconv"
  8. "time"
  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("/submit", secure(vs.whiteList, vs.submitFromVolumeServerHandler))
  34. r.HandleFunc("/status", secure(vs.whiteList, vs.statusHandler))
  35. r.HandleFunc("/admin/assign_volume", secure(vs.whiteList, vs.assignVolumeHandler))
  36. r.HandleFunc("/admin/vacuum_volume_check", secure(vs.whiteList, vs.vacuumVolumeCheckHandler))
  37. r.HandleFunc("/admin/vacuum_volume_compact", secure(vs.whiteList, vs.vacuumVolumeCompactHandler))
  38. r.HandleFunc("/admin/vacuum_volume_commit", secure(vs.whiteList, vs.vacuumVolumeCommitHandler))
  39. r.HandleFunc("/admin/freeze_volume", secure(vs.whiteList, vs.freezeVolumeHandler))
  40. r.HandleFunc("/admin/delete_collection", secure(vs.whiteList, vs.deleteCollectionHandler))
  41. r.HandleFunc("/stats/counter", secure(vs.whiteList, statsCounterHandler))
  42. r.HandleFunc("/stats/memory", secure(vs.whiteList, statsMemoryHandler))
  43. r.HandleFunc("/stats/disk", secure(vs.whiteList, vs.statsDiskHandler))
  44. r.HandleFunc("/delete", secure(vs.whiteList, vs.batchDeleteHandler))
  45. r.HandleFunc("/", vs.storeHandler)
  46. go func() {
  47. connected := true
  48. vs.store.SetBootstrapMaster(vs.masterNode)
  49. vs.store.SetDataCenter(vs.dataCenter)
  50. vs.store.SetRack(vs.rack)
  51. for {
  52. master, err := vs.store.Join()
  53. if err == nil {
  54. if !connected {
  55. connected = true
  56. glog.V(0).Infoln("Volume Server Connected with master at", master)
  57. }
  58. } else {
  59. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  60. if connected {
  61. connected = false
  62. }
  63. }
  64. if connected {
  65. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  66. } else {
  67. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  68. }
  69. }
  70. }()
  71. return vs
  72. }
  73. func (vs *VolumeServer) Shutdown() {
  74. glog.V(0).Infoln("Shutting down volume server...")
  75. vs.store.Close()
  76. glog.V(0).Infoln("Shut down successfully!")
  77. }