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.

149 lines
5.4 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. ms.SetRaftServer(raftServer)
  83. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  84. // starting grpc server
  85. grpcPort := *mport + 10000
  86. grpcL, err := util.NewListener(*masterBindIp+":"+strconv.Itoa(grpcPort), 0)
  87. if err != nil {
  88. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  89. }
  90. // Create your protocol servers.
  91. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  92. master_pb.RegisterSeaweedServer(grpcS, ms)
  93. protobuf.RegisterRaftServer(grpcS, raftServer)
  94. reflection.Register(grpcS)
  95. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterBindIp, grpcPort)
  96. grpcS.Serve(grpcL)
  97. }()
  98. // start http server
  99. httpS := &http.Server{Handler: r}
  100. if err := httpS.Serve(masterListener); err != nil {
  101. glog.Fatalf("master server failed to serve: %v", err)
  102. }
  103. return true
  104. }
  105. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  106. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  107. if peers != "" {
  108. cleanedPeers = strings.Split(peers, ",")
  109. }
  110. hasSelf := false
  111. for _, peer := range cleanedPeers {
  112. if peer == masterAddress {
  113. hasSelf = true
  114. break
  115. }
  116. }
  117. peerCount := len(cleanedPeers)
  118. if !hasSelf {
  119. peerCount += 1
  120. }
  121. if peerCount%2 == 0 {
  122. glog.Fatalf("Only odd number of masters are supported!")
  123. }
  124. return
  125. }