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.

103 lines
3.3 KiB

6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "google.golang.org/grpc"
  6. "github.com/chrislusf/seaweedfs/weed/stats"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. )
  12. type VolumeServer struct {
  13. SeedMasterNodes []string
  14. currentMaster string
  15. pulseSeconds int
  16. dataCenter string
  17. rack string
  18. store *storage.Store
  19. guard *security.Guard
  20. grpcDialOption grpc.DialOption
  21. needleMapKind storage.NeedleMapType
  22. ReadRedirect bool
  23. compactionBytePerSecond int64
  24. MetricsAddress string
  25. MetricsIntervalSec int
  26. fileSizeLimitBytes int64
  27. }
  28. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  29. port int, publicUrl string,
  30. folders []string, maxCounts []int, minFreeSpacePercent []float32,
  31. needleMapKind storage.NeedleMapType,
  32. masterNodes []string, pulseSeconds int,
  33. dataCenter string, rack string,
  34. whiteList []string,
  35. readRedirect bool,
  36. compactionMBPerSecond int,
  37. fileSizeLimitMB int,
  38. ) *VolumeServer {
  39. v := util.GetViper()
  40. signingKey := v.GetString("jwt.signing.key")
  41. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  42. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  43. enableUiAccess := v.GetBool("access.ui")
  44. readSigningKey := v.GetString("jwt.signing.read.key")
  45. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  46. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  47. vs := &VolumeServer{
  48. pulseSeconds: pulseSeconds,
  49. dataCenter: dataCenter,
  50. rack: rack,
  51. needleMapKind: needleMapKind,
  52. ReadRedirect: readRedirect,
  53. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.volume"),
  54. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  55. fileSizeLimitBytes: int64(fileSizeLimitMB) * 1024 * 1024,
  56. }
  57. vs.SeedMasterNodes = masterNodes
  58. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, minFreeSpacePercent, vs.needleMapKind)
  59. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  60. handleStaticResources(adminMux)
  61. if signingKey == "" || enableUiAccess {
  62. // only expose the volume server details for safe environments
  63. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  64. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  65. /*
  66. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  67. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  68. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  69. */
  70. }
  71. adminMux.HandleFunc("/", vs.privateStoreHandler)
  72. if publicMux != adminMux {
  73. // separated admin and public port
  74. handleStaticResources(publicMux)
  75. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  76. }
  77. go vs.heartbeat()
  78. hostAddress := fmt.Sprintf("%s:%d", ip, port)
  79. go stats.LoopPushingMetric("volumeServer", hostAddress, stats.VolumeServerGather,
  80. func() (addr string, intervalSeconds int) {
  81. return vs.MetricsAddress, vs.MetricsIntervalSec
  82. })
  83. return vs
  84. }
  85. func (vs *VolumeServer) Shutdown() {
  86. glog.V(0).Infoln("Shutting down volume server...")
  87. vs.store.Close()
  88. glog.V(0).Infoln("Shut down successfully!")
  89. }