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.

154 lines
5.6 KiB

12 years ago
12 years ago
6 years ago
12 years ago
12 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "github.com/chrislusf/raft/protobuf"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. "github.com/gorilla/mux"
  15. "github.com/spf13/viper"
  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. 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. util.LoadConfiguration("security", false)
  50. util.LoadConfiguration("master", false)
  51. if *mMaxCpu < 1 {
  52. *mMaxCpu = runtime.NumCPU()
  53. }
  54. runtime.GOMAXPROCS(*mMaxCpu)
  55. util.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  56. if err := util.TestFolderWritable(*metaFolder); err != nil {
  57. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  58. }
  59. if *masterWhiteListOption != "" {
  60. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  61. }
  62. if *volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  63. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  64. }
  65. r := mux.NewRouter()
  66. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  67. *volumeSizeLimitMB, *volumePreallocate,
  68. *mpulse, *defaultReplicaPlacement, *garbageThreshold,
  69. masterWhiteList,
  70. *disableHttp,
  71. )
  72. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  73. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  74. masterListener, e := util.NewListener(listeningAddress, 0)
  75. if e != nil {
  76. glog.Fatalf("Master startup error: %v", e)
  77. }
  78. go func() {
  79. // start raftServer
  80. myMasterAddress, peers := checkPeers(*masterIp, *mport, *masterPeers)
  81. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  82. peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  83. if raftServer == nil {
  84. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717", *metaFolder)
  85. }
  86. ms.SetRaftServer(raftServer)
  87. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  88. // starting grpc server
  89. grpcPort := *mport + 10000
  90. grpcL, err := util.NewListener(*masterBindIp+":"+strconv.Itoa(grpcPort), 0)
  91. if err != nil {
  92. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  93. }
  94. // Create your protocol servers.
  95. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  96. master_pb.RegisterSeaweedServer(grpcS, ms)
  97. protobuf.RegisterRaftServer(grpcS, raftServer)
  98. reflection.Register(grpcS)
  99. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterBindIp, grpcPort)
  100. grpcS.Serve(grpcL)
  101. }()
  102. // start http server
  103. httpS := &http.Server{Handler: r}
  104. if err := httpS.Serve(masterListener); err != nil {
  105. glog.Fatalf("master server failed to serve: %v", err)
  106. }
  107. return true
  108. }
  109. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  110. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  111. if peers != "" {
  112. cleanedPeers = strings.Split(peers, ",")
  113. }
  114. hasSelf := false
  115. for _, peer := range cleanedPeers {
  116. if peer == masterAddress {
  117. hasSelf = true
  118. break
  119. }
  120. }
  121. peerCount := len(cleanedPeers)
  122. if !hasSelf {
  123. peerCount += 1
  124. }
  125. if peerCount%2 == 0 {
  126. glog.Fatalf("Only odd number of masters are supported!")
  127. }
  128. return
  129. }