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.

138 lines
4.7 KiB

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