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.

125 lines
4.1 KiB

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