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.

111 lines
3.6 KiB

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