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.

226 lines
9.1 KiB

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