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
4 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.NeedleMapKind
  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, diskTypes []storage.DiskType,
  34. idxFolder string,
  35. needleMapKind storage.NeedleMapKind,
  36. masterNodes []string, pulseSeconds int,
  37. dataCenter string, rack string,
  38. whiteList []string,
  39. fixJpgOrientation bool,
  40. readRedirect bool,
  41. compactionMBPerSecond int,
  42. fileSizeLimitMB int,
  43. ) *VolumeServer {
  44. v := util.GetViper()
  45. signingKey := v.GetString("jwt.signing.key")
  46. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  47. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  48. enableUiAccess := v.GetBool("access.ui")
  49. readSigningKey := v.GetString("jwt.signing.read.key")
  50. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  51. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  52. vs := &VolumeServer{
  53. pulseSeconds: pulseSeconds,
  54. dataCenter: dataCenter,
  55. rack: rack,
  56. needleMapKind: needleMapKind,
  57. FixJpgOrientation: fixJpgOrientation,
  58. ReadRedirect: readRedirect,
  59. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.volume"),
  60. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  61. fileSizeLimitBytes: int64(fileSizeLimitMB) * 1024 * 1024,
  62. isHeartbeating: true,
  63. stopChan: make(chan bool),
  64. }
  65. vs.SeedMasterNodes = masterNodes
  66. vs.checkWithMaster()
  67. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, minFreeSpacePercents, idxFolder, vs.needleMapKind, diskTypes)
  68. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  69. handleStaticResources(adminMux)
  70. adminMux.HandleFunc("/status", vs.statusHandler)
  71. if signingKey == "" || enableUiAccess {
  72. // only expose the volume server details for safe environments
  73. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  74. /*
  75. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  76. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  77. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  78. */
  79. }
  80. adminMux.HandleFunc("/", vs.privateStoreHandler)
  81. if publicMux != adminMux {
  82. // separated admin and public port
  83. handleStaticResources(publicMux)
  84. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  85. }
  86. go vs.heartbeat()
  87. go stats.LoopPushingMetric("volumeServer", fmt.Sprintf("%s:%d", ip, port), 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. }