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.

114 lines
3.4 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(adminMux, publicMux *http.ServeMux, ip string,
  22. port 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, ip, publicUrl, folders, maxCounts)
  36. vs.guard = security.NewGuard(whiteList, "")
  37. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  38. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  39. adminMux.HandleFunc("/admin/assign_volume", vs.guard.WhiteList(vs.assignVolumeHandler))
  40. adminMux.HandleFunc("/admin/vacuum_volume_check", vs.guard.WhiteList(vs.vacuumVolumeCheckHandler))
  41. adminMux.HandleFunc("/admin/vacuum_volume_compact", vs.guard.WhiteList(vs.vacuumVolumeCompactHandler))
  42. adminMux.HandleFunc("/admin/vacuum_volume_commit", vs.guard.WhiteList(vs.vacuumVolumeCommitHandler))
  43. adminMux.HandleFunc("/admin/freeze_volume", vs.guard.WhiteList(vs.freezeVolumeHandler))
  44. adminMux.HandleFunc("/admin/delete_collection", vs.guard.WhiteList(vs.deleteCollectionHandler))
  45. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  46. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  47. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  48. adminMux.HandleFunc("/delete", vs.guard.WhiteList(vs.batchDeleteHandler))
  49. adminMux.HandleFunc("/", vs.privateStoreHandler)
  50. if publicMux != adminMux {
  51. // separated admin and public port
  52. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  53. }
  54. go func() {
  55. connected := true
  56. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  57. vs.store.SetDataCenter(vs.dataCenter)
  58. vs.store.SetRack(vs.rack)
  59. for {
  60. master, secretKey, err := vs.store.Join()
  61. if err == nil {
  62. if !connected {
  63. connected = true
  64. vs.SetMasterNode(master)
  65. vs.guard.SecretKey = secretKey
  66. glog.V(0).Infoln("Volume Server Connected with master at", master)
  67. }
  68. } else {
  69. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  70. if connected {
  71. connected = false
  72. }
  73. }
  74. if connected {
  75. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  76. } else {
  77. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  78. }
  79. }
  80. }()
  81. return vs
  82. }
  83. func (vs *VolumeServer) GetMasterNode() string {
  84. vs.mnLock.RLock()
  85. defer vs.mnLock.RUnlock()
  86. return vs.masterNode
  87. }
  88. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  89. vs.mnLock.Lock()
  90. defer vs.mnLock.Unlock()
  91. vs.masterNode = masterNode
  92. }
  93. func (vs *VolumeServer) Shutdown() {
  94. glog.V(0).Infoln("Shutting down volume server...")
  95. vs.store.Close()
  96. glog.V(0).Infoln("Shut down successfully!")
  97. }
  98. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  99. return security.GenJwt(vs.guard.SecretKey, fileId)
  100. }