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.

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