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.

135 lines
4.5 KiB

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