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.

121 lines
3.9 KiB

10 years ago
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/seaweedfs/go/glog"
  8. "github.com/chrislusf/seaweedfs/go/security"
  9. "github.com/chrislusf/seaweedfs/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. needleMapKind storage.NeedleMapType
  20. FixJpgOrientation bool
  21. }
  22. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  23. port int, publicUrl string,
  24. folders []string, maxCounts []int,
  25. needleMapKind storage.NeedleMapType,
  26. masterNode string, pulseSeconds int,
  27. dataCenter string, rack string,
  28. whiteList []string,
  29. fixJpgOrientation bool) *VolumeServer {
  30. vs := &VolumeServer{
  31. pulseSeconds: pulseSeconds,
  32. dataCenter: dataCenter,
  33. rack: rack,
  34. needleMapKind: needleMapKind,
  35. FixJpgOrientation: fixJpgOrientation,
  36. }
  37. vs.SetMasterNode(masterNode)
  38. vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts, vs.needleMapKind)
  39. vs.guard = security.NewGuard(whiteList, "")
  40. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  41. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  42. adminMux.HandleFunc("/admin/assign_volume", vs.guard.WhiteList(vs.assignVolumeHandler))
  43. adminMux.HandleFunc("/admin/vacuum/check", vs.guard.WhiteList(vs.vacuumVolumeCheckHandler))
  44. adminMux.HandleFunc("/admin/vacuum/compact", vs.guard.WhiteList(vs.vacuumVolumeCompactHandler))
  45. adminMux.HandleFunc("/admin/vacuum/commit", vs.guard.WhiteList(vs.vacuumVolumeCommitHandler))
  46. adminMux.HandleFunc("/admin/delete_collection", vs.guard.WhiteList(vs.deleteCollectionHandler))
  47. adminMux.HandleFunc("/admin/sync/status", vs.guard.WhiteList(vs.getVolumeSyncStatusHandler))
  48. adminMux.HandleFunc("/admin/sync/index", vs.guard.WhiteList(vs.getVolumeIndexContentHandler))
  49. adminMux.HandleFunc("/admin/sync/data", vs.guard.WhiteList(vs.getVolumeDataContentHandler))
  50. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  51. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  52. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  53. adminMux.HandleFunc("/delete", vs.guard.WhiteList(vs.batchDeleteHandler))
  54. adminMux.HandleFunc("/", vs.privateStoreHandler)
  55. if publicMux != adminMux {
  56. // separated admin and public port
  57. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  58. }
  59. go func() {
  60. connected := true
  61. glog.V(0).Infof("Volume server bootstraps with master %s", vs.GetMasterNode())
  62. vs.store.SetBootstrapMaster(vs.GetMasterNode())
  63. vs.store.SetDataCenter(vs.dataCenter)
  64. vs.store.SetRack(vs.rack)
  65. for {
  66. glog.V(4).Infof("Volume server sending to master %s", vs.GetMasterNode())
  67. master, secretKey, err := vs.store.SendHeartbeatToMaster()
  68. if err == nil {
  69. if !connected {
  70. connected = true
  71. vs.SetMasterNode(master)
  72. vs.guard.SecretKey = secretKey
  73. glog.V(0).Infoln("Volume Server Connected with master at", master)
  74. }
  75. } else {
  76. glog.V(1).Infof("Volume Server Failed to talk with master %s: %v", vs.masterNode, err)
  77. if connected {
  78. connected = false
  79. }
  80. }
  81. if connected {
  82. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*(1+rand.Float32())) * time.Millisecond)
  83. } else {
  84. time.Sleep(time.Duration(float32(vs.pulseSeconds*1e3)*0.25) * time.Millisecond)
  85. }
  86. }
  87. }()
  88. return vs
  89. }
  90. func (vs *VolumeServer) GetMasterNode() string {
  91. vs.mnLock.RLock()
  92. defer vs.mnLock.RUnlock()
  93. return vs.masterNode
  94. }
  95. func (vs *VolumeServer) SetMasterNode(masterNode string) {
  96. vs.mnLock.Lock()
  97. defer vs.mnLock.Unlock()
  98. vs.masterNode = masterNode
  99. }
  100. func (vs *VolumeServer) Shutdown() {
  101. glog.V(0).Infoln("Shutting down volume server...")
  102. vs.store.Close()
  103. glog.V(0).Infoln("Shut down successfully!")
  104. }
  105. func (vs *VolumeServer) jwt(fileId string) security.EncodedJwt {
  106. return security.GenJwt(vs.guard.SecretKey, fileId)
  107. }