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.

259 lines
11 KiB

6 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "runtime/pprof"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/chrislusf/raft/protobuf"
  13. "github.com/chrislusf/seaweedfs/weed/security"
  14. "github.com/spf13/viper"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  17. "github.com/chrislusf/seaweedfs/weed/server"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  19. "github.com/gorilla/mux"
  20. "google.golang.org/grpc/reflection"
  21. )
  22. type ServerOptions struct {
  23. cpuprofile *string
  24. v VolumeServerOptions
  25. }
  26. var (
  27. serverOptions ServerOptions
  28. filerOptions FilerOptions
  29. s3Options S3Options
  30. )
  31. func init() {
  32. cmdServer.Run = runServer // break init cycle
  33. }
  34. var cmdServer = &Command{
  35. UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
  36. Short: "start a master server, a volume server, and optionally a filer and a S3 gateway",
  37. Long: `start both a volume server to provide storage spaces
  38. and a master server to provide volume=>location mapping service and sequence number of file ids
  39. This is provided as a convenient way to start both volume server and master server.
  40. The servers acts exactly the same as starting them separately.
  41. So other volume servers can connect to this master server also.
  42. Optionally, a filer server can be started.
  43. Also optionally, a S3 gateway can be started.
  44. `,
  45. }
  46. var (
  47. serverIp = cmdServer.Flag.String("ip", "localhost", "ip or server name")
  48. serverBindIp = cmdServer.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  49. serverMaxCpu = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  50. serverTimeout = cmdServer.Flag.Int("idleTimeout", 30, "connection idle seconds")
  51. serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
  52. serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name")
  53. serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  54. serverDisableHttp = cmdServer.Flag.Bool("disableHttp", false, "disable http requests, only gRPC operations are allowed.")
  55. serverPeers = cmdServer.Flag.String("master.peers", "", "all master nodes in comma separated ip:masterPort list")
  56. serverGarbageThreshold = cmdServer.Flag.Float64("garbageThreshold", 0.3, "threshold to vacuum and reclaim spaces")
  57. serverMetricsAddress = cmdServer.Flag.String("metrics.address", "", "Prometheus gateway address")
  58. serverMetricsIntervalSec = cmdServer.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  59. masterPort = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
  60. masterMetaFolder = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
  61. masterVolumeSizeLimitMB = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  62. masterVolumePreallocate = cmdServer.Flag.Bool("master.volumePreallocate", false, "Preallocate disk space for volumes.")
  63. masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
  64. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  65. volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
  66. pulseSeconds = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  67. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  68. isStartingS3 = cmdServer.Flag.Bool("s3", false, "whether to start S3 gateway")
  69. serverWhiteList []string
  70. )
  71. func init() {
  72. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
  73. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  74. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  75. filerOptions.publicPort = cmdServer.Flag.Int("filer.port.public", 0, "filer server public http listen port")
  76. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  77. filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  78. filerOptions.disableDirListing = cmdServer.Flag.Bool("filer.disableDirListing", false, "turn off directory listing")
  79. filerOptions.maxMB = cmdServer.Flag.Int("filer.maxMB", 32, "split files larger than the limit")
  80. filerOptions.dirListingLimit = cmdServer.Flag.Int("filer.dirListLimit", 1000, "limit sub dir listing size")
  81. serverOptions.v.port = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
  82. serverOptions.v.publicPort = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
  83. serverOptions.v.indexType = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  84. serverOptions.v.fixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  85. serverOptions.v.readRedirect = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.")
  86. serverOptions.v.compactionMBPerSecond = cmdServer.Flag.Int("volume.compactionMBps", 0, "limit compaction speed in mega bytes per second")
  87. serverOptions.v.publicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
  88. s3Options.filerBucketsPath = cmdServer.Flag.String("s3.filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  89. s3Options.port = cmdServer.Flag.Int("s3.port", 8333, "s3 server http listen port")
  90. s3Options.domainName = cmdServer.Flag.String("s3.domainName", "", "suffix of the host name, {bucket}.{domainName}")
  91. s3Options.tlsPrivateKey = cmdServer.Flag.String("s3.key.file", "", "path to the TLS private key file")
  92. s3Options.tlsCertificate = cmdServer.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  93. }
  94. func runServer(cmd *Command, args []string) bool {
  95. util.LoadConfiguration("security", false)
  96. util.LoadConfiguration("master", false)
  97. if *serverOptions.cpuprofile != "" {
  98. f, err := os.Create(*serverOptions.cpuprofile)
  99. if err != nil {
  100. glog.Fatal(err)
  101. }
  102. pprof.StartCPUProfile(f)
  103. defer pprof.StopCPUProfile()
  104. }
  105. if *filerOptions.redirectOnRead {
  106. *isStartingFiler = true
  107. }
  108. if *isStartingS3 {
  109. *isStartingFiler = true
  110. }
  111. master := *serverIp + ":" + strconv.Itoa(*masterPort)
  112. filerOptions.masters = &master
  113. filerOptions.ip = serverBindIp
  114. serverOptions.v.ip = serverIp
  115. serverOptions.v.bindIp = serverBindIp
  116. serverOptions.v.masters = &master
  117. serverOptions.v.idleConnectionTimeout = serverTimeout
  118. serverOptions.v.maxCpu = serverMaxCpu
  119. serverOptions.v.dataCenter = serverDataCenter
  120. serverOptions.v.rack = serverRack
  121. serverOptions.v.pulseSeconds = pulseSeconds
  122. filerOptions.dataCenter = serverDataCenter
  123. filerOptions.disableHttp = serverDisableHttp
  124. filerOptions.metricsAddress = serverMetricsAddress
  125. filerOptions.metricsIntervalSec = serverMetricsIntervalSec
  126. filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port)
  127. s3Options.filer = &filerAddress
  128. if *filerOptions.defaultReplicaPlacement == "" {
  129. *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
  130. }
  131. if *serverMaxCpu < 1 {
  132. *serverMaxCpu = runtime.NumCPU()
  133. }
  134. runtime.GOMAXPROCS(*serverMaxCpu)
  135. folders := strings.Split(*volumeDataFolders, ",")
  136. if *masterVolumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  137. glog.Fatalf("masterVolumeSizeLimitMB should be less than 30000")
  138. }
  139. if *masterMetaFolder == "" {
  140. *masterMetaFolder = folders[0]
  141. }
  142. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  143. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
  144. }
  145. filerOptions.defaultLevelDbDirectory = masterMetaFolder
  146. if *serverWhiteListOption != "" {
  147. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  148. }
  149. if *isStartingFiler {
  150. go func() {
  151. time.Sleep(1 * time.Second)
  152. filerOptions.startFiler()
  153. }()
  154. }
  155. if *isStartingS3 {
  156. go func() {
  157. time.Sleep(2 * time.Second)
  158. s3Options.startS3Server()
  159. }()
  160. }
  161. var volumeWait sync.WaitGroup
  162. volumeWait.Add(1)
  163. go func() {
  164. r := mux.NewRouter()
  165. ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
  166. *masterVolumeSizeLimitMB, *masterVolumePreallocate,
  167. *pulseSeconds, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
  168. serverWhiteList, *serverDisableHttp,
  169. *serverMetricsAddress, *serverMetricsIntervalSec,
  170. )
  171. glog.V(0).Infof("Start Seaweed Master %s at %s:%d", util.VERSION, *serverIp, *masterPort)
  172. masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), 0)
  173. if e != nil {
  174. glog.Fatalf("Master startup error: %v", e)
  175. }
  176. go func() {
  177. // start raftServer
  178. myMasterAddress, peers := checkPeers(*serverIp, *masterPort, *serverPeers)
  179. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  180. peers, myMasterAddress, *masterMetaFolder, ms.Topo, *pulseSeconds)
  181. ms.SetRaftServer(raftServer)
  182. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  183. // starting grpc server
  184. grpcPort := *masterPort + 10000
  185. grpcL, err := util.NewListener(*serverBindIp+":"+strconv.Itoa(grpcPort), 0)
  186. if err != nil {
  187. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  188. }
  189. // Create your protocol servers.
  190. glog.V(1).Infof("grpc config %+v", viper.Sub("grpc"))
  191. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  192. master_pb.RegisterSeaweedServer(grpcS, ms)
  193. protobuf.RegisterRaftServer(grpcS, raftServer)
  194. reflection.Register(grpcS)
  195. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *serverIp, grpcPort)
  196. grpcS.Serve(grpcL)
  197. }()
  198. volumeWait.Done()
  199. // start http server
  200. httpS := &http.Server{Handler: r}
  201. if err := httpS.Serve(masterListener); err != nil {
  202. glog.Fatalf("master server failed to serve: %v", err)
  203. }
  204. }()
  205. volumeWait.Wait()
  206. time.Sleep(100 * time.Millisecond)
  207. serverOptions.v.startVolumeServer(*volumeDataFolders, *volumeMaxDataVolumeCounts, *serverWhiteListOption)
  208. return true
  209. }