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.

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