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.

411 lines
15 KiB

5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. httppprof "net/http/pprof"
  6. "os"
  7. "runtime/pprof"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. "github.com/spf13/viper"
  13. "google.golang.org/grpc"
  14. "github.com/seaweedfs/seaweedfs/weed/util/grace"
  15. "github.com/seaweedfs/seaweedfs/weed/pb"
  16. "github.com/seaweedfs/seaweedfs/weed/security"
  17. "github.com/seaweedfs/seaweedfs/weed/util/httpdown"
  18. "google.golang.org/grpc/reflection"
  19. "github.com/seaweedfs/seaweedfs/weed/glog"
  20. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  21. weed_server "github.com/seaweedfs/seaweedfs/weed/server"
  22. stats_collect "github.com/seaweedfs/seaweedfs/weed/stats"
  23. "github.com/seaweedfs/seaweedfs/weed/storage"
  24. "github.com/seaweedfs/seaweedfs/weed/util"
  25. )
  26. var (
  27. v VolumeServerOptions
  28. )
  29. type VolumeServerOptions struct {
  30. port *int
  31. portGrpc *int
  32. publicPort *int
  33. folders []string
  34. folderMaxLimits []int
  35. idxFolder *string
  36. ip *string
  37. publicUrl *string
  38. bindIp *string
  39. mastersString *string
  40. masters []pb.ServerAddress
  41. idleConnectionTimeout *int
  42. dataCenter *string
  43. rack *string
  44. whiteList []string
  45. indexType *string
  46. diskType *string
  47. fixJpgOrientation *bool
  48. readMode *string
  49. cpuProfile *string
  50. memProfile *string
  51. compactionMBPerSecond *int
  52. fileSizeLimitMB *int
  53. concurrentUploadLimitMB *int
  54. concurrentDownloadLimitMB *int
  55. pprof *bool
  56. preStopSeconds *int
  57. metricsHttpPort *int
  58. // pulseSeconds *int
  59. enableTcp *bool
  60. inflightUploadDataTimeout *time.Duration
  61. }
  62. func init() {
  63. cmdVolume.Run = runVolume // break init cycle
  64. v.port = cmdVolume.Flag.Int("port", 8080, "http listen port")
  65. v.portGrpc = cmdVolume.Flag.Int("port.grpc", 0, "grpc listen port")
  66. v.publicPort = cmdVolume.Flag.Int("port.public", 0, "port opened to public")
  67. v.ip = cmdVolume.Flag.String("ip", util.DetectedHostAddress(), "ip or server name, also used as identifier")
  68. v.publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible address")
  69. v.bindIp = cmdVolume.Flag.String("ip.bind", "", "ip address to bind to. If empty, default to same as -ip option.")
  70. v.mastersString = cmdVolume.Flag.String("mserver", "localhost:9333", "comma-separated master servers")
  71. v.preStopSeconds = cmdVolume.Flag.Int("preStopSeconds", 10, "number of seconds between stop send heartbeats and stop volume server")
  72. // v.pulseSeconds = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  73. v.idleConnectionTimeout = cmdVolume.Flag.Int("idleTimeout", 30, "connection idle seconds")
  74. v.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  75. v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  76. v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  77. v.diskType = cmdVolume.Flag.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  78. v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  79. v.readMode = cmdVolume.Flag.String("readMode", "proxy", "[local|proxy|redirect] how to deal with non-local volume: 'not found|proxy to remote node|redirect volume location'.")
  80. v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
  81. v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
  82. v.compactionMBPerSecond = cmdVolume.Flag.Int("compactionMBps", 0, "limit background compaction or copying speed in mega bytes per second")
  83. v.fileSizeLimitMB = cmdVolume.Flag.Int("fileSizeLimitMB", 256, "limit file size to avoid out of memory")
  84. v.concurrentUploadLimitMB = cmdVolume.Flag.Int("concurrentUploadLimitMB", 256, "limit total concurrent upload size")
  85. v.concurrentDownloadLimitMB = cmdVolume.Flag.Int("concurrentDownloadLimitMB", 256, "limit total concurrent download size")
  86. v.pprof = cmdVolume.Flag.Bool("pprof", false, "enable pprof http handlers. precludes --memprofile and --cpuprofile")
  87. v.metricsHttpPort = cmdVolume.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  88. v.idxFolder = cmdVolume.Flag.String("dir.idx", "", "directory to store .idx files")
  89. v.enableTcp = cmdVolume.Flag.Bool("tcp", false, "<experimental> enable tcp port")
  90. v.inflightUploadDataTimeout = cmdVolume.Flag.Duration("inflightUploadDataTimeout", 60*time.Second, "inflight upload data wait timeout of volume servers")
  91. }
  92. var cmdVolume = &Command{
  93. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  94. Short: "start a volume server",
  95. Long: `start a volume server to provide storage spaces
  96. `,
  97. }
  98. var (
  99. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  100. maxVolumeCounts = cmdVolume.Flag.String("max", "8", "maximum numbers of volumes, count[,count]... If set to zero, the limit will be auto configured as free disk space divided by volume size.")
  101. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  102. minFreeSpacePercent = cmdVolume.Flag.String("minFreeSpacePercent", "1", "minimum free disk space (default to 1%). Low disk space will mark all volumes as ReadOnly (deprecated, use minFreeSpace instead).")
  103. minFreeSpace = cmdVolume.Flag.String("minFreeSpace", "", "min free disk space (value<=100 as percentage like 1, other as human readable bytes, like 10GiB). Low disk space will mark all volumes as ReadOnly.")
  104. )
  105. func runVolume(cmd *Command, args []string) bool {
  106. util.LoadConfiguration("security", false)
  107. // If --pprof is set we assume the caller wants to be able to collect
  108. // cpu and memory profiles via go tool pprof
  109. if !*v.pprof {
  110. grace.SetupProfiling(*v.cpuProfile, *v.memProfile)
  111. }
  112. go stats_collect.StartMetricsServer(*v.metricsHttpPort)
  113. minFreeSpaces := util.MustParseMinFreeSpace(*minFreeSpace, *minFreeSpacePercent)
  114. v.masters = pb.ServerAddresses(*v.mastersString).ToAddresses()
  115. v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption, minFreeSpaces)
  116. return true
  117. }
  118. func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption string, minFreeSpaces []util.MinFreeSpace) {
  119. // Set multiple folders and each folder's max volume count limit'
  120. v.folders = strings.Split(volumeFolders, ",")
  121. for _, folder := range v.folders {
  122. if err := util.TestFolderWritable(util.ResolvePath(folder)); err != nil {
  123. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  124. }
  125. }
  126. // set max
  127. maxCountStrings := strings.Split(maxVolumeCounts, ",")
  128. for _, maxString := range maxCountStrings {
  129. if max, e := strconv.Atoi(maxString); e == nil {
  130. v.folderMaxLimits = append(v.folderMaxLimits, max)
  131. } else {
  132. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  133. }
  134. }
  135. if len(v.folderMaxLimits) == 1 && len(v.folders) > 1 {
  136. for i := 0; i < len(v.folders)-1; i++ {
  137. v.folderMaxLimits = append(v.folderMaxLimits, v.folderMaxLimits[0])
  138. }
  139. }
  140. if len(v.folders) != len(v.folderMaxLimits) {
  141. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(v.folders), len(v.folderMaxLimits))
  142. }
  143. if len(minFreeSpaces) == 1 && len(v.folders) > 1 {
  144. for i := 0; i < len(v.folders)-1; i++ {
  145. minFreeSpaces = append(minFreeSpaces, minFreeSpaces[0])
  146. }
  147. }
  148. if len(v.folders) != len(minFreeSpaces) {
  149. glog.Fatalf("%d directories by -dir, but only %d minFreeSpacePercent is set by -minFreeSpacePercent", len(v.folders), len(minFreeSpaces))
  150. }
  151. // set disk types
  152. var diskTypes []types.DiskType
  153. diskTypeStrings := strings.Split(*v.diskType, ",")
  154. for _, diskTypeString := range diskTypeStrings {
  155. diskTypes = append(diskTypes, types.ToDiskType(diskTypeString))
  156. }
  157. if len(diskTypes) == 1 && len(v.folders) > 1 {
  158. for i := 0; i < len(v.folders)-1; i++ {
  159. diskTypes = append(diskTypes, diskTypes[0])
  160. }
  161. }
  162. if len(v.folders) != len(diskTypes) {
  163. glog.Fatalf("%d directories by -dir, but only %d disk types is set by -disk", len(v.folders), len(diskTypes))
  164. }
  165. // security related white list configuration
  166. if volumeWhiteListOption != "" {
  167. v.whiteList = strings.Split(volumeWhiteListOption, ",")
  168. }
  169. if *v.ip == "" {
  170. *v.ip = util.DetectedHostAddress()
  171. glog.V(0).Infof("detected volume server ip address: %v", *v.ip)
  172. }
  173. if *v.bindIp == "" {
  174. *v.bindIp = *v.ip
  175. }
  176. if *v.publicPort == 0 {
  177. *v.publicPort = *v.port
  178. }
  179. if *v.portGrpc == 0 {
  180. *v.portGrpc = 10000 + *v.port
  181. }
  182. if *v.publicUrl == "" {
  183. *v.publicUrl = util.JoinHostPort(*v.ip, *v.publicPort)
  184. }
  185. volumeMux := http.NewServeMux()
  186. publicVolumeMux := volumeMux
  187. if v.isSeparatedPublicPort() {
  188. publicVolumeMux = http.NewServeMux()
  189. }
  190. if *v.pprof {
  191. volumeMux.HandleFunc("/debug/pprof/", httppprof.Index)
  192. volumeMux.HandleFunc("/debug/pprof/cmdline", httppprof.Cmdline)
  193. volumeMux.HandleFunc("/debug/pprof/profile", httppprof.Profile)
  194. volumeMux.HandleFunc("/debug/pprof/symbol", httppprof.Symbol)
  195. volumeMux.HandleFunc("/debug/pprof/trace", httppprof.Trace)
  196. }
  197. volumeNeedleMapKind := storage.NeedleMapInMemory
  198. switch *v.indexType {
  199. case "leveldb":
  200. volumeNeedleMapKind = storage.NeedleMapLevelDb
  201. case "leveldbMedium":
  202. volumeNeedleMapKind = storage.NeedleMapLevelDbMedium
  203. case "leveldbLarge":
  204. volumeNeedleMapKind = storage.NeedleMapLevelDbLarge
  205. }
  206. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  207. *v.ip, *v.port, *v.portGrpc, *v.publicUrl,
  208. v.folders, v.folderMaxLimits, minFreeSpaces, diskTypes,
  209. *v.idxFolder,
  210. volumeNeedleMapKind,
  211. v.masters, 5, *v.dataCenter, *v.rack,
  212. v.whiteList,
  213. *v.fixJpgOrientation, *v.readMode,
  214. *v.compactionMBPerSecond,
  215. *v.fileSizeLimitMB,
  216. int64(*v.concurrentUploadLimitMB)*1024*1024,
  217. int64(*v.concurrentDownloadLimitMB)*1024*1024,
  218. *v.inflightUploadDataTimeout,
  219. )
  220. // starting grpc server
  221. grpcS := v.startGrpcService(volumeServer)
  222. // starting public http server
  223. var publicHttpDown httpdown.Server
  224. if v.isSeparatedPublicPort() {
  225. publicHttpDown = v.startPublicHttpService(publicVolumeMux)
  226. if nil == publicHttpDown {
  227. glog.Fatalf("start public http service failed")
  228. }
  229. }
  230. // starting tcp server
  231. if *v.enableTcp {
  232. go v.startTcpService(volumeServer)
  233. }
  234. // starting the cluster http server
  235. clusterHttpServer := v.startClusterHttpService(volumeMux)
  236. stopChan := make(chan bool)
  237. grace.OnInterrupt(func() {
  238. fmt.Println("volume server has been killed")
  239. // Stop heartbeats
  240. if !volumeServer.StopHeartbeat() {
  241. volumeServer.SetStopping()
  242. glog.V(0).Infof("stop send heartbeat and wait %d seconds until shutdown ...", *v.preStopSeconds)
  243. time.Sleep(time.Duration(*v.preStopSeconds) * time.Second)
  244. }
  245. shutdown(publicHttpDown, clusterHttpServer, grpcS, volumeServer)
  246. stopChan <- true
  247. })
  248. select {
  249. case <-stopChan:
  250. }
  251. }
  252. func shutdown(publicHttpDown httpdown.Server, clusterHttpServer httpdown.Server, grpcS *grpc.Server, volumeServer *weed_server.VolumeServer) {
  253. // firstly, stop the public http service to prevent from receiving new user request
  254. if nil != publicHttpDown {
  255. glog.V(0).Infof("stop public http server ... ")
  256. if err := publicHttpDown.Stop(); err != nil {
  257. glog.Warningf("stop the public http server failed, %v", err)
  258. }
  259. }
  260. glog.V(0).Infof("graceful stop cluster http server ... ")
  261. if err := clusterHttpServer.Stop(); err != nil {
  262. glog.Warningf("stop the cluster http server failed, %v", err)
  263. }
  264. glog.V(0).Infof("graceful stop gRPC ...")
  265. grpcS.GracefulStop()
  266. volumeServer.Shutdown()
  267. pprof.StopCPUProfile()
  268. }
  269. // check whether configure the public port
  270. func (v VolumeServerOptions) isSeparatedPublicPort() bool {
  271. return *v.publicPort != *v.port
  272. }
  273. func (v VolumeServerOptions) startGrpcService(vs volume_server_pb.VolumeServerServer) *grpc.Server {
  274. grpcPort := *v.portGrpc
  275. grpcL, err := util.NewListener(util.JoinHostPort(*v.bindIp, grpcPort), 0)
  276. if err != nil {
  277. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  278. }
  279. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.volume"))
  280. volume_server_pb.RegisterVolumeServerServer(grpcS, vs)
  281. reflection.Register(grpcS)
  282. go func() {
  283. if err := grpcS.Serve(grpcL); err != nil {
  284. glog.Fatalf("start gRPC service failed, %s", err)
  285. }
  286. }()
  287. return grpcS
  288. }
  289. func (v VolumeServerOptions) startPublicHttpService(handler http.Handler) httpdown.Server {
  290. publicListeningAddress := util.JoinHostPort(*v.bindIp, *v.publicPort)
  291. glog.V(0).Infoln("Start Seaweed volume server", util.Version(), "public at", publicListeningAddress)
  292. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  293. if e != nil {
  294. glog.Fatalf("Volume server listener error:%v", e)
  295. }
  296. pubHttp := httpdown.HTTP{StopTimeout: 5 * time.Minute, KillTimeout: 5 * time.Minute}
  297. publicHttpDown := pubHttp.Serve(&http.Server{Handler: handler}, publicListener)
  298. go func() {
  299. if err := publicHttpDown.Wait(); err != nil {
  300. glog.Errorf("public http down wait failed, %v", err)
  301. }
  302. }()
  303. return publicHttpDown
  304. }
  305. func (v VolumeServerOptions) startClusterHttpService(handler http.Handler) httpdown.Server {
  306. var (
  307. certFile, keyFile string
  308. )
  309. if viper.GetString("https.volume.key") != "" {
  310. certFile = viper.GetString("https.volume.cert")
  311. keyFile = viper.GetString("https.volume.key")
  312. }
  313. listeningAddress := util.JoinHostPort(*v.bindIp, *v.port)
  314. glog.V(0).Infof("Start Seaweed volume server %s at %s", util.Version(), listeningAddress)
  315. listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  316. if e != nil {
  317. glog.Fatalf("Volume server listener error:%v", e)
  318. }
  319. httpDown := httpdown.HTTP{
  320. KillTimeout: time.Minute,
  321. StopTimeout: 30 * time.Second,
  322. CertFile: certFile,
  323. KeyFile: keyFile}
  324. httpS := &http.Server{Handler: handler}
  325. if viper.GetString("https.volume.ca") != "" {
  326. clientCertFile := viper.GetString("https.volume.ca")
  327. httpS.TLSConfig = security.LoadClientTLSHTTP(clientCertFile)
  328. }
  329. clusterHttpServer := httpDown.Serve(httpS, listener)
  330. go func() {
  331. if e := clusterHttpServer.Wait(); e != nil {
  332. glog.Fatalf("Volume server fail to serve: %v", e)
  333. }
  334. }()
  335. return clusterHttpServer
  336. }
  337. func (v VolumeServerOptions) startTcpService(volumeServer *weed_server.VolumeServer) {
  338. listeningAddress := util.JoinHostPort(*v.bindIp, *v.port+20000)
  339. glog.V(0).Infoln("Start Seaweed volume server", util.Version(), "tcp at", listeningAddress)
  340. listener, e := util.NewListener(listeningAddress, 0)
  341. if e != nil {
  342. glog.Fatalf("Volume server listener error on %s:%v", listeningAddress, e)
  343. }
  344. defer listener.Close()
  345. for {
  346. c, err := listener.Accept()
  347. if err != nil {
  348. fmt.Println(err)
  349. return
  350. }
  351. go volumeServer.HandleTcpConnection(c)
  352. }
  353. }