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.

129 lines
4.3 KiB

6 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/storage/types"
  4. "net/http"
  5. "sync"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/storage"
  12. )
  13. type VolumeServer struct {
  14. inFlightUploadDataSize int64
  15. inFlightDownloadDataSize int64
  16. concurrentUploadLimit int64
  17. concurrentDownloadLimit int64
  18. inFlightUploadDataLimitCond *sync.Cond
  19. inFlightDownloadDataLimitCond *sync.Cond
  20. SeedMasterNodes []string
  21. currentMaster string
  22. pulseSeconds int
  23. dataCenter string
  24. rack string
  25. store *storage.Store
  26. guard *security.Guard
  27. grpcDialOption grpc.DialOption
  28. needleMapKind storage.NeedleMapKind
  29. FixJpgOrientation bool
  30. ReadMode string
  31. compactionBytePerSecond int64
  32. metricsAddress string
  33. metricsIntervalSec int
  34. fileSizeLimitBytes int64
  35. isHeartbeating bool
  36. stopChan chan bool
  37. }
  38. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  39. port int, publicUrl string,
  40. folders []string, maxCounts []int, minFreeSpaces []util.MinFreeSpace, diskTypes []types.DiskType,
  41. idxFolder string,
  42. needleMapKind storage.NeedleMapKind,
  43. masterNodes []string, pulseSeconds int,
  44. dataCenter string, rack string,
  45. whiteList []string,
  46. fixJpgOrientation bool,
  47. readMode string,
  48. compactionMBPerSecond int,
  49. fileSizeLimitMB int,
  50. concurrentUploadLimit int64,
  51. concurrentDownloadLimit int64,
  52. ) *VolumeServer {
  53. v := util.GetViper()
  54. signingKey := v.GetString("jwt.signing.key")
  55. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  56. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  57. enableUiAccess := v.GetBool("access.ui")
  58. readSigningKey := v.GetString("jwt.signing.read.key")
  59. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  60. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  61. vs := &VolumeServer{
  62. pulseSeconds: pulseSeconds,
  63. dataCenter: dataCenter,
  64. rack: rack,
  65. needleMapKind: needleMapKind,
  66. FixJpgOrientation: fixJpgOrientation,
  67. ReadMode: readMode,
  68. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.volume"),
  69. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  70. fileSizeLimitBytes: int64(fileSizeLimitMB) * 1024 * 1024,
  71. isHeartbeating: true,
  72. stopChan: make(chan bool),
  73. inFlightUploadDataLimitCond: sync.NewCond(new(sync.Mutex)),
  74. inFlightDownloadDataLimitCond: sync.NewCond(new(sync.Mutex)),
  75. concurrentUploadLimit: concurrentUploadLimit,
  76. concurrentDownloadLimit: concurrentDownloadLimit,
  77. }
  78. vs.SeedMasterNodes = masterNodes
  79. vs.checkWithMaster()
  80. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, minFreeSpaces, idxFolder, vs.needleMapKind, diskTypes)
  81. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  82. handleStaticResources(adminMux)
  83. adminMux.HandleFunc("/status", vs.statusHandler)
  84. if signingKey == "" || enableUiAccess {
  85. // only expose the volume server details for safe environments
  86. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  87. /*
  88. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  89. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  90. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  91. */
  92. }
  93. adminMux.HandleFunc("/", vs.privateStoreHandler)
  94. if publicMux != adminMux {
  95. // separated admin and public port
  96. handleStaticResources(publicMux)
  97. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  98. }
  99. go vs.heartbeat()
  100. go stats.LoopPushingMetric("volumeServer", util.JoinHostPort(ip, port), vs.metricsAddress, vs.metricsIntervalSec)
  101. return vs
  102. }
  103. func (vs *VolumeServer) SetStopping() {
  104. glog.V(0).Infoln("Stopping volume server...")
  105. vs.store.SetStopping()
  106. }
  107. func (vs *VolumeServer) Shutdown() {
  108. glog.V(0).Infoln("Shutting down volume server...")
  109. vs.store.Close()
  110. glog.V(0).Infoln("Shut down successfully!")
  111. }