server.go 11 KB

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