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.

112 lines
3.6 KiB

6 years ago
6 years ago
5 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. DiskType storage.DiskType
  19. store *storage.Store
  20. guard *security.Guard
  21. grpcDialOption grpc.DialOption
  22. needleMapKind storage.NeedleMapType
  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,
  35. idxFolder string, diskType storage.DiskType,
  36. needleMapKind storage.NeedleMapType,
  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. DiskType: diskType,
  58. needleMapKind: needleMapKind,
  59. FixJpgOrientation: fixJpgOrientation,
  60. ReadRedirect: readRedirect,
  61. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.volume"),
  62. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  63. fileSizeLimitBytes: int64(fileSizeLimitMB) * 1024 * 1024,
  64. isHeartbeating: true,
  65. stopChan: make(chan bool),
  66. }
  67. vs.SeedMasterNodes = masterNodes
  68. vs.checkWithMaster()
  69. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, minFreeSpacePercents, idxFolder, vs.needleMapKind, vs.DiskType)
  70. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  71. handleStaticResources(adminMux)
  72. adminMux.HandleFunc("/status", vs.statusHandler)
  73. if signingKey == "" || enableUiAccess {
  74. // only expose the volume server details for safe environments
  75. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  76. /*
  77. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  78. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  79. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  80. */
  81. }
  82. adminMux.HandleFunc("/", vs.privateStoreHandler)
  83. if publicMux != adminMux {
  84. // separated admin and public port
  85. handleStaticResources(publicMux)
  86. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  87. }
  88. go vs.heartbeat()
  89. go stats.LoopPushingMetric("volumeServer", fmt.Sprintf("%s:%d", ip, port), vs.metricsAddress, vs.metricsIntervalSec)
  90. return vs
  91. }
  92. func (vs *VolumeServer) Shutdown() {
  93. glog.V(0).Infoln("Shutting down volume server...")
  94. vs.store.Close()
  95. glog.V(0).Infoln("Shut down successfully!")
  96. }