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.

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