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.

130 lines
4.4 KiB

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