volume.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "runtime/pprof"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/spf13/viper"
  12. "github.com/valyala/fasthttp"
  13. "google.golang.org/grpc"
  14. "github.com/chrislusf/seaweedfs/weed/security"
  15. "github.com/chrislusf/seaweedfs/weed/util/httpdown"
  16. "google.golang.org/grpc/reflection"
  17. "github.com/chrislusf/seaweedfs/weed/glog"
  18. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  19. "github.com/chrislusf/seaweedfs/weed/server"
  20. "github.com/chrislusf/seaweedfs/weed/storage"
  21. "github.com/chrislusf/seaweedfs/weed/util"
  22. )
  23. var (
  24. v VolumeServerOptions
  25. )
  26. type VolumeServerOptions struct {
  27. port *int
  28. publicPort *int
  29. folders []string
  30. folderMaxLimits []int
  31. ip *string
  32. publicUrl *string
  33. bindIp *string
  34. masters *string
  35. pulseSeconds *int
  36. idleConnectionTimeout *int
  37. dataCenter *string
  38. rack *string
  39. whiteList []string
  40. indexType *string
  41. fixJpgOrientation *bool
  42. readRedirect *bool
  43. cpuProfile *string
  44. memProfile *string
  45. compactionMBPerSecond *int
  46. fileSizeLimitMB *int
  47. }
  48. func init() {
  49. cmdVolume.Run = runVolume // break init cycle
  50. v.port = cmdVolume.Flag.Int("port", 8080, "http listen port")
  51. v.publicPort = cmdVolume.Flag.Int("port.public", 0, "port opened to public")
  52. v.ip = cmdVolume.Flag.String("ip", "", "ip or server name")
  53. v.publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible address")
  54. v.bindIp = cmdVolume.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  55. v.masters = cmdVolume.Flag.String("mserver", "localhost:9333", "comma-separated master servers")
  56. v.pulseSeconds = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  57. v.idleConnectionTimeout = cmdVolume.Flag.Int("idleTimeout", 30, "connection idle seconds")
  58. v.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  59. v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  60. v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  61. v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  62. v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.")
  63. v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
  64. v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
  65. v.compactionMBPerSecond = cmdVolume.Flag.Int("compactionMBps", 0, "limit background compaction or copying speed in mega bytes per second")
  66. v.fileSizeLimitMB = cmdVolume.Flag.Int("fileSizeLimitMB", 256, "limit file size to avoid out of memory")
  67. }
  68. var cmdVolume = &Command{
  69. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  70. Short: "start a volume server",
  71. Long: `start a volume server to provide storage spaces
  72. `,
  73. }
  74. var (
  75. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  76. maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  77. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  78. )
  79. func runVolume(cmd *Command, args []string) bool {
  80. util.LoadConfiguration("security", false)
  81. runtime.GOMAXPROCS(runtime.NumCPU())
  82. util.SetupProfiling(*v.cpuProfile, *v.memProfile)
  83. v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption)
  84. return true
  85. }
  86. func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption string) {
  87. // Set multiple folders and each folder's max volume count limit'
  88. v.folders = strings.Split(volumeFolders, ",")
  89. maxCountStrings := strings.Split(maxVolumeCounts, ",")
  90. for _, maxString := range maxCountStrings {
  91. if max, e := strconv.Atoi(maxString); e == nil {
  92. v.folderMaxLimits = append(v.folderMaxLimits, max)
  93. } else {
  94. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  95. }
  96. }
  97. if len(v.folders) != len(v.folderMaxLimits) {
  98. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(v.folders), len(v.folderMaxLimits))
  99. }
  100. for _, folder := range v.folders {
  101. if err := util.TestFolderWritable(folder); err != nil {
  102. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  103. }
  104. }
  105. // security related white list configuration
  106. if volumeWhiteListOption != "" {
  107. v.whiteList = strings.Split(volumeWhiteListOption, ",")
  108. }
  109. if *v.ip == "" {
  110. *v.ip = "127.0.0.1"
  111. }
  112. if *v.publicPort == 0 {
  113. *v.publicPort = *v.port
  114. }
  115. if *v.publicUrl == "" {
  116. *v.publicUrl = *v.ip + ":" + strconv.Itoa(*v.publicPort)
  117. }
  118. volumeMux := http.NewServeMux()
  119. publicVolumeMux := volumeMux
  120. if v.isSeparatedPublicPort() {
  121. publicVolumeMux = http.NewServeMux()
  122. }
  123. volumeNeedleMapKind := storage.NeedleMapInMemory
  124. switch *v.indexType {
  125. case "leveldb":
  126. volumeNeedleMapKind = storage.NeedleMapLevelDb
  127. case "leveldbMedium":
  128. volumeNeedleMapKind = storage.NeedleMapLevelDbMedium
  129. case "leveldbLarge":
  130. volumeNeedleMapKind = storage.NeedleMapLevelDbLarge
  131. }
  132. masters := *v.masters
  133. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  134. *v.ip, *v.port, *v.publicUrl,
  135. v.folders, v.folderMaxLimits,
  136. volumeNeedleMapKind,
  137. strings.Split(masters, ","), *v.pulseSeconds, *v.dataCenter, *v.rack,
  138. v.whiteList,
  139. *v.fixJpgOrientation, *v.readRedirect,
  140. *v.compactionMBPerSecond,
  141. *v.fileSizeLimitMB,
  142. )
  143. go fasthttp.ListenAndServe(":8081", volumeServer.HandleFastHTTP)
  144. // starting grpc server
  145. grpcS := v.startGrpcService(volumeServer)
  146. // starting public http server
  147. var publicHttpDown httpdown.Server
  148. if v.isSeparatedPublicPort() {
  149. publicHttpDown = v.startPublicHttpService(publicVolumeMux)
  150. if nil == publicHttpDown {
  151. glog.Fatalf("start public http service failed")
  152. }
  153. }
  154. // starting the cluster http server
  155. clusterHttpServer := v.startClusterHttpService(volumeMux)
  156. stopChain := make(chan struct{})
  157. util.OnInterrupt(func() {
  158. fmt.Println("volume server has be killed")
  159. var startTime time.Time
  160. // firstly, stop the public http service to prevent from receiving new user request
  161. if nil != publicHttpDown {
  162. startTime = time.Now()
  163. if err := publicHttpDown.Stop(); err != nil {
  164. glog.Warningf("stop the public http server failed, %v", err)
  165. }
  166. delta := time.Now().Sub(startTime).Nanoseconds() / 1e6
  167. glog.V(0).Infof("stop public http server, elapsed %dms", delta)
  168. }
  169. startTime = time.Now()
  170. if err := clusterHttpServer.Stop(); err != nil {
  171. glog.Warningf("stop the cluster http server failed, %v", err)
  172. }
  173. delta := time.Now().Sub(startTime).Nanoseconds() / 1e6
  174. glog.V(0).Infof("graceful stop cluster http server, elapsed [%d]", delta)
  175. startTime = time.Now()
  176. grpcS.GracefulStop()
  177. delta = time.Now().Sub(startTime).Nanoseconds() / 1e6
  178. glog.V(0).Infof("graceful stop gRPC, elapsed [%d]", delta)
  179. startTime = time.Now()
  180. volumeServer.Shutdown()
  181. delta = time.Now().Sub(startTime).Nanoseconds() / 1e6
  182. glog.V(0).Infof("stop volume server, elapsed [%d]", delta)
  183. pprof.StopCPUProfile()
  184. close(stopChain) // notify exit
  185. })
  186. select {
  187. case <-stopChain:
  188. }
  189. glog.Warningf("the volume server exit.")
  190. }
  191. // check whether configure the public port
  192. func (v VolumeServerOptions) isSeparatedPublicPort() bool {
  193. return *v.publicPort != *v.port
  194. }
  195. func (v VolumeServerOptions) startGrpcService(vs volume_server_pb.VolumeServerServer) *grpc.Server {
  196. grpcPort := *v.port + 10000
  197. grpcL, err := util.NewListener(*v.bindIp+":"+strconv.Itoa(grpcPort), 0)
  198. if err != nil {
  199. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  200. }
  201. grpcS := util.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.volume"))
  202. volume_server_pb.RegisterVolumeServerServer(grpcS, vs)
  203. reflection.Register(grpcS)
  204. go func() {
  205. if err := grpcS.Serve(grpcL); err != nil {
  206. glog.Fatalf("start gRPC service failed, %s", err)
  207. }
  208. }()
  209. return grpcS
  210. }
  211. func (v VolumeServerOptions) startPublicHttpService(handler http.Handler) httpdown.Server {
  212. publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
  213. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  214. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  215. if e != nil {
  216. glog.Fatalf("Volume server listener error:%v", e)
  217. }
  218. pubHttp := httpdown.HTTP{StopTimeout: 5 * time.Minute, KillTimeout: 5 * time.Minute}
  219. publicHttpDown := pubHttp.Serve(&http.Server{Handler: handler}, publicListener)
  220. go func() {
  221. if err := publicHttpDown.Wait(); err != nil {
  222. glog.Errorf("public http down wait failed, %v", err)
  223. }
  224. }()
  225. return publicHttpDown
  226. }
  227. func (v VolumeServerOptions) startClusterHttpService(handler http.Handler) httpdown.Server {
  228. var (
  229. certFile, keyFile string
  230. )
  231. if viper.GetString("https.volume.key") != "" {
  232. certFile = viper.GetString("https.volume.cert")
  233. keyFile = viper.GetString("https.volume.key")
  234. }
  235. listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
  236. glog.V(0).Infof("Start Seaweed volume server %s at %s", util.VERSION, listeningAddress)
  237. listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  238. if e != nil {
  239. glog.Fatalf("Volume server listener error:%v", e)
  240. }
  241. httpDown := httpdown.HTTP{
  242. KillTimeout: 5 * time.Minute,
  243. StopTimeout: 5 * time.Minute,
  244. CertFile: certFile,
  245. KeyFile: keyFile}
  246. clusterHttpServer := httpDown.Serve(&http.Server{Handler: handler}, listener)
  247. go func() {
  248. if e := clusterHttpServer.Wait(); e != nil {
  249. glog.Fatalf("Volume server fail to serve: %v", e)
  250. }
  251. }()
  252. return clusterHttpServer
  253. }