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.

96 lines
3.1 KiB

6 years ago
  1. package weed_server
  2. import (
  3. "google.golang.org/grpc"
  4. "net/http"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. "github.com/spf13/viper"
  9. )
  10. type VolumeServer struct {
  11. SeedMasterNodes []string
  12. currentMaster string
  13. pulseSeconds int
  14. dataCenter string
  15. rack string
  16. store *storage.Store
  17. guard *security.Guard
  18. grpcDialOption grpc.DialOption
  19. needleMapKind storage.NeedleMapType
  20. FixJpgOrientation bool
  21. ReadRedirect bool
  22. compactionBytePerSecond int64
  23. MetricsAddress string
  24. MetricsIntervalSec int
  25. }
  26. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  27. port int, publicUrl string,
  28. folders []string, maxCounts []int,
  29. needleMapKind storage.NeedleMapType,
  30. masterNodes []string, pulseSeconds int,
  31. dataCenter string, rack string,
  32. whiteList []string,
  33. fixJpgOrientation bool,
  34. readRedirect bool,
  35. compactionMBPerSecond int,
  36. metricsAddress string,
  37. metricsIntervalSec int,
  38. ) *VolumeServer {
  39. v := viper.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. FixJpgOrientation: fixJpgOrientation,
  53. ReadRedirect: readRedirect,
  54. grpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "volume"),
  55. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  56. }
  57. vs.SeedMasterNodes = masterNodes
  58. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, 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. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  66. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  67. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  68. }
  69. adminMux.HandleFunc("/", vs.privateStoreHandler)
  70. if publicMux != adminMux {
  71. // separated admin and public port
  72. handleStaticResources(publicMux)
  73. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  74. }
  75. go vs.heartbeat()
  76. startPushingMetric("volumeServer", volumeServerGather, metricsAddress, metricsIntervalSec)
  77. return vs
  78. }
  79. func (vs *VolumeServer) Shutdown() {
  80. glog.V(0).Infoln("Shutting down volume server...")
  81. vs.store.Close()
  82. glog.V(0).Infoln("Shut down successfully!")
  83. }