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.

152 lines
5.5 KiB

12 years ago
12 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/raft/protobuf"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/chrislusf/seaweedfs/weed/server"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/gorilla/mux"
  10. "github.com/spf13/viper"
  11. "google.golang.org/grpc/reflection"
  12. "net/http"
  13. "os"
  14. "runtime"
  15. "strconv"
  16. "strings"
  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. 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", "", "all 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.Float64("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. disableHttp = cmdMaster.Flag.Bool("disableHttp", false, "disable http requests, only gRPC operations are allowed.")
  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 > util.VolumeSizeLimitGB*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. *disableHttp,
  70. )
  71. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  72. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  73. masterListener, e := util.NewListener(listeningAddress, 0)
  74. if e != nil {
  75. glog.Fatalf("Master startup error: %v", e)
  76. }
  77. go func() {
  78. // start raftServer
  79. myMasterAddress, peers := checkPeers(*masterIp, *mport, *masterPeers)
  80. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  81. peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  82. if raftServer == nil {
  83. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717", *metaFolder)
  84. }
  85. ms.SetRaftServer(raftServer)
  86. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  87. // starting grpc server
  88. grpcPort := *mport + 10000
  89. grpcL, err := util.NewListener(*masterBindIp+":"+strconv.Itoa(grpcPort), 0)
  90. if err != nil {
  91. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  92. }
  93. // Create your protocol servers.
  94. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  95. master_pb.RegisterSeaweedServer(grpcS, ms)
  96. protobuf.RegisterRaftServer(grpcS, raftServer)
  97. reflection.Register(grpcS)
  98. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterBindIp, grpcPort)
  99. grpcS.Serve(grpcL)
  100. }()
  101. // start http server
  102. httpS := &http.Server{Handler: r}
  103. if err := httpS.Serve(masterListener); err != nil {
  104. glog.Fatalf("master server failed to serve: %v", err)
  105. }
  106. return true
  107. }
  108. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  109. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  110. if peers != "" {
  111. cleanedPeers = strings.Split(peers, ",")
  112. }
  113. hasSelf := false
  114. for _, peer := range cleanedPeers {
  115. if peer == masterAddress {
  116. hasSelf = true
  117. break
  118. }
  119. }
  120. peerCount := len(cleanedPeers)
  121. if !hasSelf {
  122. peerCount += 1
  123. }
  124. if peerCount%2 == 0 {
  125. glog.Fatalf("Only odd number of masters are supported!")
  126. }
  127. return
  128. }