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.

72 lines
2.2 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. version string
  17. }
  18. func NewVolumeServer(r *http.ServeMux, version string, ip string, port int, publicUrl string, folders []string, maxCounts []int,
  19. masterNode string, pulseSeconds int,
  20. dataCenter string, rack string,
  21. whiteList []string) *VolumeServer {
  22. vs := &VolumeServer{
  23. version: version,
  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("/", vs.storeHandler)
  40. go func() {
  41. connected := true
  42. vs.store.SetBootstrapMaster(vs.masterNode)
  43. vs.store.SetDataCenter(vs.dataCenter)
  44. vs.store.SetRack(vs.rack)
  45. for {
  46. err := vs.store.Join()
  47. if err == nil {
  48. if !connected {
  49. connected = true
  50. glog.V(0).Infoln("Reconnected with master")
  51. }
  52. } else {
  53. if connected {
  54. connected = false
  55. }
  56. }
  57. if connected {
  58. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  59. } else {
  60. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  61. }
  62. }
  63. }()
  64. glog.V(0).Infoln("store joined at", vs.masterNode)
  65. return vs
  66. }