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.

127 lines
4.3 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "runtime/pprof"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. "github.com/gorilla/mux"
  15. "github.com/soheilhy/cmux"
  16. "google.golang.org/grpc"
  17. "google.golang.org/grpc/reflection"
  18. )
  19. func init() {
  20. cmdMaster.Run = runMaster // break init cycle
  21. }
  22. var cmdMaster = &Command{
  23. UsageLine: "master -port=9333",
  24. Short: "start a master server",
  25. Long: `start a master server to provide volume=>location mapping service
  26. and sequence number of file ids
  27. `,
  28. }
  29. var (
  30. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  31. masterIp = cmdMaster.Flag.String("ip", "localhost", "master <ip>|<server> address")
  32. masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  33. metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  34. masterPeers = cmdMaster.Flag.String("peers", "", "other master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094")
  35. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  36. volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  37. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  38. confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "Deprecating! xml configuration file")
  39. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  40. // mTimeout = cmdMaster.Flag.Int("idleTimeout", 30, "connection idle seconds")
  41. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  42. garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  43. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  44. masterSecureKey = cmdMaster.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  45. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  46. masterWhiteList []string
  47. )
  48. func runMaster(cmd *Command, args []string) bool {
  49. if *mMaxCpu < 1 {
  50. *mMaxCpu = runtime.NumCPU()
  51. }
  52. runtime.GOMAXPROCS(*mMaxCpu)
  53. if *masterCpuProfile != "" {
  54. f, err := os.Create(*masterCpuProfile)
  55. if err != nil {
  56. glog.Fatal(err)
  57. }
  58. pprof.StartCPUProfile(f)
  59. defer pprof.StopCPUProfile()
  60. OnInterrupt(func() {
  61. pprof.StopCPUProfile()
  62. })
  63. }
  64. if err := util.TestFolderWritable(*metaFolder); err != nil {
  65. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  66. }
  67. if *masterWhiteListOption != "" {
  68. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  69. }
  70. r := mux.NewRouter()
  71. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  72. *volumeSizeLimitMB, *volumePreallocate,
  73. *mpulse, *confFile, *defaultReplicaPlacement, *garbageThreshold,
  74. masterWhiteList, *masterSecureKey,
  75. )
  76. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  77. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  78. listener, e := util.NewListener(listeningAddress, 0)
  79. if e != nil {
  80. glog.Fatalf("Master startup error: %v", e)
  81. }
  82. go func() {
  83. time.Sleep(100 * time.Millisecond)
  84. myMasterAddress := *masterIp + ":" + strconv.Itoa(*mport)
  85. var peers []string
  86. if *masterPeers != "" {
  87. peers = strings.Split(*masterPeers, ",")
  88. }
  89. raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  90. ms.SetRaftServer(raftServer)
  91. }()
  92. // start grpc and http server
  93. m := cmux.New(listener)
  94. grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
  95. httpL := m.Match(cmux.Any())
  96. // Create your protocol servers.
  97. grpcS := grpc.NewServer()
  98. pb.RegisterSeaweedServer(grpcS, ms)
  99. reflection.Register(grpcS)
  100. httpS := &http.Server{Handler: r}
  101. go grpcS.Serve(grpcL)
  102. go httpS.Serve(httpL)
  103. if err := m.Serve(); err != nil {
  104. glog.Fatalf("master server failed to serve: %v", err)
  105. }
  106. return true
  107. }