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.

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