volume_server.go 4.9 KB

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