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.

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