server.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package command
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "runtime/pprof"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type ServerOptions struct {
  13. cpuprofile *string
  14. v VolumeServerOptions
  15. }
  16. var (
  17. serverOptions ServerOptions
  18. masterOptions MasterOptions
  19. filerOptions FilerOptions
  20. s3Options S3Options
  21. )
  22. func init() {
  23. cmdServer.Run = runServer // break init cycle
  24. }
  25. var cmdServer = &Command{
  26. UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
  27. Short: "start a master server, a volume server, and optionally a filer and a S3 gateway",
  28. Long: `start both a volume server to provide storage spaces
  29. and a master server to provide volume=>location mapping service and sequence number of file ids
  30. This is provided as a convenient way to start both volume server and master server.
  31. The servers acts exactly the same as starting them separately.
  32. So other volume servers can connect to this master server also.
  33. Optionally, a filer server can be started.
  34. Also optionally, a S3 gateway can be started.
  35. `,
  36. }
  37. var (
  38. serverIp = cmdServer.Flag.String("ip", "localhost", "ip or server name")
  39. serverBindIp = cmdServer.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  40. serverTimeout = cmdServer.Flag.Int("idleTimeout", 30, "connection idle seconds")
  41. serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
  42. serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name")
  43. serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  44. serverDisableHttp = cmdServer.Flag.Bool("disableHttp", false, "disable http requests, only gRPC operations are allowed.")
  45. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  46. volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
  47. pulseSeconds = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  48. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  49. isStartingS3 = cmdServer.Flag.Bool("s3", false, "whether to start S3 gateway")
  50. serverWhiteList []string
  51. )
  52. func init() {
  53. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
  54. masterOptions.port = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
  55. masterOptions.metaFolder = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
  56. masterOptions.peers = cmdServer.Flag.String("master.peers", "", "all master nodes in comma separated ip:masterPort list")
  57. masterOptions.volumeSizeLimitMB = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  58. masterOptions.volumePreallocate = cmdServer.Flag.Bool("master.volumePreallocate", false, "Preallocate disk space for volumes.")
  59. masterOptions.defaultReplication = cmdServer.Flag.String("master.defaultReplication", "000", "Default replication type if not specified.")
  60. masterOptions.garbageThreshold = cmdServer.Flag.Float64("garbageThreshold", 0.3, "threshold to vacuum and reclaim spaces")
  61. masterOptions.metricsAddress = cmdServer.Flag.String("metrics.address", "", "Prometheus gateway address")
  62. masterOptions.metricsIntervalSec = cmdServer.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  63. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  64. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  65. filerOptions.publicPort = cmdServer.Flag.Int("filer.port.public", 0, "filer server public http listen port")
  66. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  67. filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  68. filerOptions.disableDirListing = cmdServer.Flag.Bool("filer.disableDirListing", false, "turn off directory listing")
  69. filerOptions.maxMB = cmdServer.Flag.Int("filer.maxMB", 32, "split files larger than the limit")
  70. filerOptions.dirListingLimit = cmdServer.Flag.Int("filer.dirListLimit", 1000, "limit sub dir listing size")
  71. serverOptions.v.port = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
  72. serverOptions.v.publicPort = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
  73. serverOptions.v.indexType = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  74. serverOptions.v.fixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  75. serverOptions.v.readRedirect = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.")
  76. serverOptions.v.compactionMBPerSecond = cmdServer.Flag.Int("volume.compactionMBps", 0, "limit compaction speed in mega bytes per second")
  77. serverOptions.v.fileSizeLimitMB = cmdServer.Flag.Int("volume.fileSizeLimitMB", 256, "limit file size to avoid out of memory")
  78. serverOptions.v.publicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
  79. serverOptions.v.enableTcp = cmdServer.Flag.Bool("volume.enableTcp", false, "[experimental] toggle tcp port, running on 20000 + port")
  80. s3Options.filerBucketsPath = cmdServer.Flag.String("s3.filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  81. s3Options.port = cmdServer.Flag.Int("s3.port", 8333, "s3 server http listen port")
  82. s3Options.domainName = cmdServer.Flag.String("s3.domainName", "", "suffix of the host name, {bucket}.{domainName}")
  83. s3Options.tlsPrivateKey = cmdServer.Flag.String("s3.key.file", "", "path to the TLS private key file")
  84. s3Options.tlsCertificate = cmdServer.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  85. }
  86. func runServer(cmd *Command, args []string) bool {
  87. util.LoadConfiguration("security", false)
  88. util.LoadConfiguration("master", false)
  89. if *serverOptions.cpuprofile != "" {
  90. f, err := os.Create(*serverOptions.cpuprofile)
  91. if err != nil {
  92. glog.Fatal(err)
  93. }
  94. pprof.StartCPUProfile(f)
  95. defer pprof.StopCPUProfile()
  96. }
  97. if *filerOptions.redirectOnRead {
  98. *isStartingFiler = true
  99. }
  100. if *isStartingS3 {
  101. *isStartingFiler = true
  102. }
  103. _, peerList := checkPeers(*serverIp, *masterOptions.port, *masterOptions.peers)
  104. peers := strings.Join(peerList, ",")
  105. masterOptions.peers = &peers
  106. masterOptions.ip = serverIp
  107. masterOptions.ipBind = serverBindIp
  108. filerOptions.masters = &peers
  109. filerOptions.ip = serverBindIp
  110. serverOptions.v.ip = serverIp
  111. serverOptions.v.bindIp = serverBindIp
  112. serverOptions.v.masters = &peers
  113. serverOptions.v.idleConnectionTimeout = serverTimeout
  114. serverOptions.v.dataCenter = serverDataCenter
  115. serverOptions.v.rack = serverRack
  116. serverOptions.v.pulseSeconds = pulseSeconds
  117. masterOptions.pulseSeconds = pulseSeconds
  118. masterOptions.whiteList = serverWhiteListOption
  119. filerOptions.dataCenter = serverDataCenter
  120. filerOptions.disableHttp = serverDisableHttp
  121. masterOptions.disableHttp = serverDisableHttp
  122. filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port)
  123. s3Options.filer = &filerAddress
  124. if *filerOptions.defaultReplicaPlacement == "" {
  125. *filerOptions.defaultReplicaPlacement = *masterOptions.defaultReplication
  126. }
  127. runtime.GOMAXPROCS(runtime.NumCPU())
  128. folders := strings.Split(*volumeDataFolders, ",")
  129. if *masterOptions.volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  130. glog.Fatalf("masterVolumeSizeLimitMB should be less than 30000")
  131. }
  132. if *masterOptions.metaFolder == "" {
  133. *masterOptions.metaFolder = folders[0]
  134. }
  135. if err := util.TestFolderWritable(*masterOptions.metaFolder); err != nil {
  136. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterOptions.metaFolder, err)
  137. }
  138. filerOptions.defaultLevelDbDirectory = masterOptions.metaFolder
  139. if *serverWhiteListOption != "" {
  140. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  141. }
  142. if *isStartingFiler {
  143. go func() {
  144. time.Sleep(1 * time.Second)
  145. filerOptions.startFiler()
  146. }()
  147. }
  148. if *isStartingS3 {
  149. go func() {
  150. time.Sleep(2 * time.Second)
  151. s3Options.startS3Server()
  152. }()
  153. }
  154. // start volume server
  155. {
  156. go serverOptions.v.startVolumeServer(*volumeDataFolders, *volumeMaxDataVolumeCounts, *serverWhiteListOption)
  157. }
  158. startMaster(masterOptions, serverWhiteList)
  159. return true
  160. }