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.

222 lines
9.4 KiB

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