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.

126 lines
4.2 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 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. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  39. // mTimeout = cmdMaster.Flag.Int("idleTimeout", 30, "connection idle seconds")
  40. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  41. garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  42. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  43. masterSecureKey = cmdMaster.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  44. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  45. masterWhiteList []string
  46. )
  47. func runMaster(cmd *Command, args []string) bool {
  48. if *mMaxCpu < 1 {
  49. *mMaxCpu = runtime.NumCPU()
  50. }
  51. runtime.GOMAXPROCS(*mMaxCpu)
  52. if *masterCpuProfile != "" {
  53. f, err := os.Create(*masterCpuProfile)
  54. if err != nil {
  55. glog.Fatal(err)
  56. }
  57. pprof.StartCPUProfile(f)
  58. defer pprof.StopCPUProfile()
  59. OnInterrupt(func() {
  60. pprof.StopCPUProfile()
  61. })
  62. }
  63. if err := util.TestFolderWritable(*metaFolder); err != nil {
  64. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  65. }
  66. if *masterWhiteListOption != "" {
  67. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  68. }
  69. r := mux.NewRouter()
  70. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  71. *volumeSizeLimitMB, *volumePreallocate,
  72. *mpulse, *defaultReplicaPlacement, *garbageThreshold,
  73. masterWhiteList, *masterSecureKey,
  74. )
  75. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  76. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  77. listener, e := util.NewListener(listeningAddress, 0)
  78. if e != nil {
  79. glog.Fatalf("Master startup error: %v", e)
  80. }
  81. go func() {
  82. time.Sleep(100 * time.Millisecond)
  83. myMasterAddress := *masterIp + ":" + strconv.Itoa(*mport)
  84. var peers []string
  85. if *masterPeers != "" {
  86. peers = strings.Split(*masterPeers, ",")
  87. }
  88. raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  89. ms.SetRaftServer(raftServer)
  90. }()
  91. // start grpc and http server
  92. m := cmux.New(listener)
  93. grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
  94. httpL := m.Match(cmux.Any())
  95. // Create your protocol servers.
  96. grpcS := grpc.NewServer()
  97. pb.RegisterSeaweedServer(grpcS, ms)
  98. reflection.Register(grpcS)
  99. httpS := &http.Server{Handler: r}
  100. go grpcS.Serve(grpcL)
  101. go httpS.Serve(httpL)
  102. if err := m.Serve(); err != nil {
  103. glog.Fatalf("master server failed to serve: %v", err)
  104. }
  105. return true
  106. }