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.

151 lines
5.2 KiB

12 years ago
12 years ago
6 years ago
12 years ago
12 years ago
6 years ago
12 years ago
12 years ago
  1. package command
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/security"
  4. "github.com/spf13/viper"
  5. "net/http"
  6. "os"
  7. "runtime"
  8. "strconv"
  9. "strings"
  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. 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 and sequence number of file ids
  25. The configuration file "security.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  26. The example security.toml configuration file can be generated by "weed scaffold -config=security"
  27. `,
  28. }
  29. var (
  30. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  31. mGrpcPort = cmdMaster.Flag.Int("port.grpc", 0, "grpc server listen port, default to http port + 10000")
  32. masterIp = cmdMaster.Flag.String("ip", "localhost", "master <ip>|<server> address")
  33. masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  34. metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  35. masterPeers = cmdMaster.Flag.String("peers", "", "all master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094")
  36. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  37. volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  38. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  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.Float64("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. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  45. masterMemProfile = cmdMaster.Flag.String("memprofile", "", "memory profile output file")
  46. masterWhiteList []string
  47. )
  48. func runMaster(cmd *Command, args []string) bool {
  49. weed_server.LoadConfiguration("security", false)
  50. if *mMaxCpu < 1 {
  51. *mMaxCpu = runtime.NumCPU()
  52. }
  53. runtime.GOMAXPROCS(*mMaxCpu)
  54. util.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  55. if err := util.TestFolderWritable(*metaFolder); err != nil {
  56. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  57. }
  58. if *masterWhiteListOption != "" {
  59. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  60. }
  61. if *volumeSizeLimitMB > 30*1000 {
  62. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  63. }
  64. r := mux.NewRouter()
  65. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  66. *volumeSizeLimitMB, *volumePreallocate,
  67. *mpulse, *defaultReplicaPlacement, *garbageThreshold,
  68. masterWhiteList,
  69. )
  70. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  71. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  72. masterListener, e := util.NewListener(listeningAddress, 0)
  73. if e != nil {
  74. glog.Fatalf("Master startup error: %v", e)
  75. }
  76. go func() {
  77. time.Sleep(100 * time.Millisecond)
  78. myMasterAddress, peers := checkPeers(*masterIp, *mport, *masterPeers)
  79. raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  80. ms.SetRaftServer(raftServer)
  81. }()
  82. go func() {
  83. // starting grpc server
  84. grpcPort := *mGrpcPort
  85. if grpcPort == 0 {
  86. grpcPort = *mport + 10000
  87. }
  88. grpcL, err := util.NewListener(*masterBindIp+":"+strconv.Itoa(grpcPort), 0)
  89. if err != nil {
  90. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  91. }
  92. // Create your protocol servers.
  93. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  94. master_pb.RegisterSeaweedServer(grpcS, ms)
  95. reflection.Register(grpcS)
  96. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterBindIp, grpcPort)
  97. grpcS.Serve(grpcL)
  98. }()
  99. // start http server
  100. httpS := &http.Server{Handler: r}
  101. if err := httpS.Serve(masterListener); err != nil {
  102. glog.Fatalf("master server failed to serve: %v", err)
  103. }
  104. return true
  105. }
  106. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  107. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  108. if peers != "" {
  109. cleanedPeers = strings.Split(peers, ",")
  110. }
  111. hasSelf := false
  112. for _, peer := range cleanedPeers {
  113. if peer == masterAddress {
  114. hasSelf = true
  115. break
  116. }
  117. }
  118. peerCount := len(cleanedPeers)
  119. if !hasSelf {
  120. peerCount += 1
  121. }
  122. if peerCount%2 == 0 {
  123. glog.Fatalf("Only odd number of masters are supported!")
  124. }
  125. return
  126. }