master.go 4.2 KB

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