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.

113 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("/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. if publicMux != adminMux {
  48. // separated admin and public port
  49. adminMux.HandleFunc("/delete", vs.guard.WhiteList(vs.batchDeleteHandler))
  50. adminMux.HandleFunc("/", vs.privateStoreHandler)
  51. }
  52. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  53. go func() {
  54. connected := true
  55. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  56. vs.store.SetDataCenter(vs.dataCenter)
  57. vs.store.SetRack(vs.rack)
  58. for {
  59. master, secretKey, err := vs.store.Join()
  60. if err == nil {
  61. if !connected {
  62. connected = true
  63. vs.SetMasterNode(master)
  64. vs.guard.SecretKey = secretKey
  65. glog.V(0).Infoln("Volume Server Connected with master at", master)
  66. }
  67. } else {
  68. glog.V(4).Infoln("Volume Server Failed to talk with master:", err.Error())
  69. if connected {
  70. connected = false
  71. }
  72. }
  73. if connected {
  74. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  75. } else {
  76. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  77. }
  78. }
  79. }()
  80. return vs
  81. }
  82. func (vs *VolumeServer) GetMasterNode() string {
  83. vs.mnLock.RLock()
  84. defer vs.mnLock.RUnlock()
  85. return vs.masterNode
  86. }
  87. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  88. vs.mnLock.Lock()
  89. defer vs.mnLock.Unlock()
  90. vs.masterNode = masterNode
  91. }
  92. func (vs *VolumeServer) Shutdown() {
  93. glog.V(0).Infoln("Shutting down volume server...")
  94. vs.store.Close()
  95. glog.V(0).Infoln("Shut down successfully!")
  96. }
  97. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  98. return security.GenJwt(vs.guard.SecretKey, fileId)
  99. }