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.

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