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.

67 lines
2.0 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. "github.com/gorilla/mux"
  6. "math/rand"
  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 *mux.Router, 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("/", vs.storeHandler)
  39. go func() {
  40. connected := true
  41. vs.store.SetMaster(vs.masterNode)
  42. vs.store.SetDataCenter(vs.dataCenter)
  43. vs.store.SetRack(vs.rack)
  44. for {
  45. err := vs.store.Join()
  46. if err == nil {
  47. if !connected {
  48. connected = true
  49. glog.V(0).Infoln("Reconnected with master")
  50. }
  51. } else {
  52. if connected {
  53. connected = false
  54. }
  55. }
  56. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  57. }
  58. }()
  59. glog.V(0).Infoln("store joined at", vs.masterNode)
  60. return vs
  61. }