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.

109 lines
3.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "sync"
  6. "time"
  7. "github.com/chrislusf/weed-fs/go/glog"
  8. "github.com/chrislusf/weed-fs/go/security"
  9. "github.com/chrislusf/weed-fs/go/storage"
  10. )
  11. type VolumeServer struct {
  12. masterNode string
  13. mnLock sync.RWMutex
  14. pulseSeconds int
  15. dataCenter string
  16. rack string
  17. store *storage.Store
  18. guard *security.Guard
  19. FixJpgOrientation bool
  20. }
  21. func NewVolumeServer(publicMux, adminMux *http.ServeMux, ip string,
  22. port, adminPort int, publicUrl string,
  23. folders []string, maxCounts []int,
  24. masterNode string, pulseSeconds int,
  25. dataCenter string, rack string,
  26. whiteList []string,
  27. fixJpgOrientation bool) *VolumeServer {
  28. vs := &VolumeServer{
  29. pulseSeconds: pulseSeconds,
  30. dataCenter: dataCenter,
  31. rack: rack,
  32. FixJpgOrientation: fixJpgOrientation,
  33. }
  34. vs.SetMasterNode(masterNode)
  35. vs.store = storage.NewStore(port, adminPort, ip, publicUrl, folders, maxCounts)
  36. vs.guard = security.NewGuard(whiteList, "")
  37. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  38. adminMux.HandleFunc("/admin/assign_volume", vs.guard.WhiteList(vs.assignVolumeHandler))
  39. adminMux.HandleFunc("/admin/vacuum_volume_check", vs.guard.WhiteList(vs.vacuumVolumeCheckHandler))
  40. adminMux.HandleFunc("/admin/vacuum_volume_compact", vs.guard.WhiteList(vs.vacuumVolumeCompactHandler))
  41. adminMux.HandleFunc("/admin/vacuum_volume_commit", vs.guard.WhiteList(vs.vacuumVolumeCommitHandler))
  42. adminMux.HandleFunc("/admin/freeze_volume", vs.guard.WhiteList(vs.freezeVolumeHandler))
  43. adminMux.HandleFunc("/admin/delete_collection", vs.guard.WhiteList(vs.deleteCollectionHandler))
  44. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  45. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  46. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  47. publicMux.HandleFunc("/delete", vs.guard.Secure(vs.batchDeleteHandler))
  48. publicMux.HandleFunc("/", vs.storeHandler)
  49. go func() {
  50. connected := true
  51. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  52. vs.store.SetDataCenter(vs.dataCenter)
  53. vs.store.SetRack(vs.rack)
  54. for {
  55. master, secretKey, err := vs.store.Join()
  56. if err == nil {
  57. if !connected {
  58. connected = true
  59. vs.SetMasterNode(master)
  60. vs.guard.SecretKey = secretKey
  61. glog.V(0).Infoln("Volume Server Connected with master at", master)
  62. }
  63. } else {
  64. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  65. if connected {
  66. connected = false
  67. }
  68. }
  69. if connected {
  70. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  71. } else {
  72. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  73. }
  74. }
  75. }()
  76. return vs
  77. }
  78. func (vs *VolumeServer) GetMasterNode() string {
  79. vs.mnLock.RLock()
  80. defer vs.mnLock.RUnlock()
  81. return vs.masterNode
  82. }
  83. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  84. vs.mnLock.Lock()
  85. defer vs.mnLock.Unlock()
  86. vs.masterNode = masterNode
  87. }
  88. func (vs *VolumeServer) Shutdown() {
  89. glog.V(0).Infoln("Shutting down volume server...")
  90. vs.store.Close()
  91. glog.V(0).Infoln("Shut down successfully!")
  92. }
  93. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  94. return security.GenJwt(vs.guard.SecretKey, fileId)
  95. }