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.

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