volume_server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. FixJpgOrientation bool
  23. ReadRedirect bool
  24. compactionBytePerSecond int64
  25. MetricsAddress string
  26. MetricsIntervalSec int
  27. fileSizeLimitBytes int64
  28. }
  29. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  30. port int, publicUrl string,
  31. folders []string, maxCounts []int,
  32. needleMapKind storage.NeedleMapType,
  33. masterNodes []string, pulseSeconds int,
  34. dataCenter string, rack string,
  35. whiteList []string,
  36. fixJpgOrientation bool,
  37. readRedirect bool,
  38. compactionMBPerSecond int,
  39. fileSizeLimitMB int,
  40. ) *VolumeServer {
  41. v := util.GetViper()
  42. signingKey := v.GetString("jwt.signing.key")
  43. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  44. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  45. enableUiAccess := v.GetBool("access.ui")
  46. readSigningKey := v.GetString("jwt.signing.read.key")
  47. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  48. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  49. vs := &VolumeServer{
  50. pulseSeconds: pulseSeconds,
  51. dataCenter: dataCenter,
  52. rack: rack,
  53. needleMapKind: needleMapKind,
  54. FixJpgOrientation: fixJpgOrientation,
  55. ReadRedirect: readRedirect,
  56. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.volume"),
  57. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  58. fileSizeLimitBytes: int64(fileSizeLimitMB) * 1024 * 1024,
  59. }
  60. vs.SeedMasterNodes = masterNodes
  61. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, vs.needleMapKind)
  62. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  63. handleStaticResources(adminMux)
  64. if signingKey == "" || enableUiAccess {
  65. // only expose the volume server details for safe environments
  66. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  67. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  68. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  69. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  70. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  71. }
  72. adminMux.HandleFunc("/", vs.privateStoreHandler)
  73. if publicMux != adminMux {
  74. // separated admin and public port
  75. handleStaticResources(publicMux)
  76. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  77. }
  78. go vs.heartbeat()
  79. hostAddress := fmt.Sprintf("%s:%d", ip, port)
  80. go stats.LoopPushingMetric("volumeServer", hostAddress, stats.VolumeServerGather,
  81. func() (addr string, intervalSeconds int) {
  82. return vs.MetricsAddress, vs.MetricsIntervalSec
  83. })
  84. return vs
  85. }
  86. func (vs *VolumeServer) Shutdown() {
  87. glog.V(0).Infoln("Shutting down volume server...")
  88. vs.store.Close()
  89. glog.V(0).Infoln("Shut down successfully!")
  90. }