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.

251 lines
10 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. "github.com/chrislusf/raft/protobuf"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/spf13/viper"
  7. "net/http"
  8. "os"
  9. "runtime"
  10. "runtime/pprof"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  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. masterPort = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
  58. masterMetaFolder = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
  59. masterVolumeSizeLimitMB = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  60. masterVolumePreallocate = cmdServer.Flag.Bool("master.volumePreallocate", false, "Preallocate disk space for volumes.")
  61. masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
  62. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  63. volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
  64. pulseSeconds = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  65. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  66. isStartingS3 = cmdServer.Flag.Bool("s3", false, "whether to start S3 gateway")
  67. serverWhiteList []string
  68. )
  69. func init() {
  70. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
  71. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  72. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  73. filerOptions.publicPort = cmdServer.Flag.Int("filer.port.public", 0, "filer server public http listen port")
  74. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  75. filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  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. serverOptions.v.port = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
  80. serverOptions.v.publicPort = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
  81. serverOptions.v.indexType = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  82. serverOptions.v.fixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  83. serverOptions.v.readRedirect = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.")
  84. serverOptions.v.compactionMBPerSecond = cmdServer.Flag.Int("volume.compactionMBps", 0, "limit compaction speed in mega bytes per second")
  85. serverOptions.v.publicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
  86. s3Options.filerBucketsPath = cmdServer.Flag.String("s3.filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  87. s3Options.port = cmdServer.Flag.Int("s3.port", 8333, "s3 server http listen port")
  88. s3Options.domainName = cmdServer.Flag.String("s3.domainName", "", "suffix of the host name, {bucket}.{domainName}")
  89. s3Options.tlsPrivateKey = cmdServer.Flag.String("s3.key.file", "", "path to the TLS private key file")
  90. s3Options.tlsCertificate = cmdServer.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  91. }
  92. func runServer(cmd *Command, args []string) bool {
  93. weed_server.LoadConfiguration("security", false)
  94. if *serverOptions.cpuprofile != "" {
  95. f, err := os.Create(*serverOptions.cpuprofile)
  96. if err != nil {
  97. glog.Fatal(err)
  98. }
  99. pprof.StartCPUProfile(f)
  100. defer pprof.StopCPUProfile()
  101. }
  102. if *filerOptions.redirectOnRead {
  103. *isStartingFiler = true
  104. }
  105. if *isStartingS3 {
  106. *isStartingFiler = true
  107. }
  108. master := *serverIp + ":" + strconv.Itoa(*masterPort)
  109. filerOptions.masters = &master
  110. filerOptions.ip = serverBindIp
  111. serverOptions.v.ip = serverIp
  112. serverOptions.v.bindIp = serverBindIp
  113. serverOptions.v.masters = &master
  114. serverOptions.v.idleConnectionTimeout = serverTimeout
  115. serverOptions.v.maxCpu = serverMaxCpu
  116. serverOptions.v.dataCenter = serverDataCenter
  117. serverOptions.v.rack = serverRack
  118. serverOptions.v.pulseSeconds = pulseSeconds
  119. filerOptions.dataCenter = serverDataCenter
  120. filerOptions.disableHttp = serverDisableHttp
  121. filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port)
  122. s3Options.filer = &filerAddress
  123. if *filerOptions.defaultReplicaPlacement == "" {
  124. *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
  125. }
  126. if *serverMaxCpu < 1 {
  127. *serverMaxCpu = runtime.NumCPU()
  128. }
  129. runtime.GOMAXPROCS(*serverMaxCpu)
  130. folders := strings.Split(*volumeDataFolders, ",")
  131. if *masterVolumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  132. glog.Fatalf("masterVolumeSizeLimitMB should be less than 30000")
  133. }
  134. if *masterMetaFolder == "" {
  135. *masterMetaFolder = folders[0]
  136. }
  137. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  138. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
  139. }
  140. filerOptions.defaultLevelDbDirectory = masterMetaFolder
  141. if *serverWhiteListOption != "" {
  142. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  143. }
  144. if *isStartingFiler {
  145. go func() {
  146. time.Sleep(1 * time.Second)
  147. filerOptions.startFiler()
  148. }()
  149. }
  150. if *isStartingS3 {
  151. go func() {
  152. time.Sleep(2 * time.Second)
  153. s3Options.startS3Server()
  154. }()
  155. }
  156. var volumeWait sync.WaitGroup
  157. volumeWait.Add(1)
  158. go func() {
  159. r := mux.NewRouter()
  160. ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
  161. *masterVolumeSizeLimitMB, *masterVolumePreallocate,
  162. *pulseSeconds, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
  163. serverWhiteList, *serverDisableHttp,
  164. )
  165. glog.V(0).Infof("Start Seaweed Master %s at %s:%d", util.VERSION, *serverIp, *masterPort)
  166. masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), 0)
  167. if e != nil {
  168. glog.Fatalf("Master startup error: %v", e)
  169. }
  170. go func() {
  171. // start raftServer
  172. myMasterAddress, peers := checkPeers(*serverIp, *masterPort, *serverPeers)
  173. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  174. peers, myMasterAddress, *masterMetaFolder, ms.Topo, *pulseSeconds)
  175. ms.SetRaftServer(raftServer)
  176. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  177. // starting grpc server
  178. grpcPort := *masterPort + 10000
  179. grpcL, err := util.NewListener(*serverBindIp+":"+strconv.Itoa(grpcPort), 0)
  180. if err != nil {
  181. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  182. }
  183. // Create your protocol servers.
  184. glog.V(1).Infof("grpc config %+v", viper.Sub("grpc"))
  185. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  186. master_pb.RegisterSeaweedServer(grpcS, ms)
  187. protobuf.RegisterRaftServer(grpcS, raftServer)
  188. reflection.Register(grpcS)
  189. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *serverIp, grpcPort)
  190. grpcS.Serve(grpcL)
  191. }()
  192. volumeWait.Done()
  193. // start http server
  194. httpS := &http.Server{Handler: r}
  195. if err := httpS.Serve(masterListener); err != nil {
  196. glog.Fatalf("master server failed to serve: %v", err)
  197. }
  198. }()
  199. volumeWait.Wait()
  200. time.Sleep(100 * time.Millisecond)
  201. serverOptions.v.startVolumeServer(*volumeDataFolders, *volumeMaxDataVolumeCounts, *serverWhiteListOption)
  202. return true
  203. }